Skip to main content

Glue code to package django projects as yunohost apps.

Project description

django_yunohost_integration

pytest Coverage Status on codecov.io

django_yunohost_integration @ PyPi Python Versions License GPL

Python package django_yunohost_integration with helpers for integrate a Django project as YunoHost package.

A example usage of this package is here:

Pull requests welcome ;)

These projects used django_yunohost_integration:

Features

  • SSOwat integration (see below)
  • Helper to create first super user for scripts/install
  • Run Django development server with a local generated YunoHost package installation (called local_test)
  • Helper to run pytest against local_test "installation"

SSO authentication

SSOwat is fully supported:

  • First user ($YNH_APP_ARG_ADMIN) will be created as Django's super user
  • All new users will be created as normal users
  • Login via SSO is fully supported
  • User Email, First / Last name will be updated from SSO data

usage

To create/update the first user in install/upgrade, e.g.:

./manage.py create_superuser --username="$admin" --email="$admin_mail"

This Create/update Django superuser and set a unusable password. A password is not needed, because auth done via SSOwat ;)

Main parts in settings.py:

from pathlib import Path as __Path

from django_yunohost_integration.base_settings import *  # noqa
from django_yunohost_integration.secret_key import get_or_create_secret as __get_or_create_secret


DEBUG = bool(int('__DEBUG_ENABLED__'))  # Set via config_panel.toml

# -----------------------------------------------------------------------------

FINALPATH = __Path('__FINALPATH__')  # /opt/yunohost/$app
assert FINALPATH.is_dir(), f'Directory not exists: {FINALPATH}'

PUBLIC_PATH = __Path('__PUBLIC_PATH__')  # /var/www/$app
assert PUBLIC_PATH.is_dir(), f'Directory not exists: {PUBLIC_PATH}'

LOG_FILE = __Path('__LOG_FILE__')  # /var/log/$app/$app.log
assert LOG_FILE.is_file(), f'File not exists: {LOG_FILE}'

PATH_URL = '__PATH_URL__'  # $YNH_APP_ARG_PATH
PATH_URL = PATH_URL.strip('/')

# -----------------------------------------------------------------------------

# Function that will be called to finalize a user profile:
YNH_SETUP_USER = 'setup_user.setup_project_user'

SECRET_KEY = __get_or_create_secret(FINALPATH / 'secret.txt')  # /opt/yunohost/$app/secret.txt

# INSTALLED_APPS.append('<insert-your-app-here>')

# -----------------------------------------------------------------------------

At least you have to set these settings:

from django_yunohost_integration.base_settings import *  # noqa

INSTALLED_APPS.append('django_yunohost_integration')

MIDDLEWARE.insert(
    MIDDLEWARE.index('django.contrib.auth.middleware.AuthenticationMiddleware') + 1,
    # login a user via HTTP_REMOTE_USER header from SSOwat:
    'django_yunohost_integration.sso_auth.auth_middleware.SSOwatRemoteUserMiddleware',
)

# Keep ModelBackend around for per-user permissions and superuser
AUTHENTICATION_BACKENDS = (
    'axes.backends.AxesBackend',  # AxesBackend should be the first backend!
    #
    # Authenticate via SSO and nginx 'HTTP_REMOTE_USER' header:
    'django_yunohost_integration.sso_auth.auth_backend.SSOwatUserBackend',
    #
    # Fallback to normal Django model backend:
    'django.contrib.auth.backends.ModelBackend',
)

LOGIN_REDIRECT_URL = None
LOGIN_URL = '/yunohost/sso/'
LOGOUT_REDIRECT_URL = '/yunohost/sso/'
# /yunohost/sso/?action=logout

ROOT_URLCONF = 'urls'  # .../conf/urls.py

local test

Build prerequisites

We install psycopg2 (a PostgreSQL adapter for the Python) that needs some build prerequisites, e.g.:

~$ sudo apt install build-essential python3-dev libpq-dev

For quicker developing of django_yunohost_integration in the context of YunoHost app, it's possible to run the Django developer server with the settings and urls made for YunoHost installation.

e.g.:

~$ git clone https://github.com/YunoHost-Apps/django_yunohost_integration.git
~$ cd django_yunohost_integration/
~/django_yunohost_integration$ ./devshell.py

+ .venv/bin/python .venv/bin/devshell



Developer shell - django_yunohost_integration - v0.2.0


Documented commands (use 'help -v' for verbose/'help <topic>' for details):

dev-shell commands
==================
fix_code_style  linting  list_venv_packages  publish  pytest  update


Django-YunoHost-Integration commands
====================================
local_test

Uncategorized
=============
alias  help     macro  run_pyscript  set    shortcuts
edit   history  quit   run_script    shell


(django_yunohost_integration)

For quicker developing of django_yunohost_integration in the context of YunoHost app, it's possible to run the Django developer server with the settings and urls made for YunoHost installation.

e.g.:

~/django_yunohost_integration$ ./devshell.py
(django_yunohost_integration) local_test
  • SQlite database will be used
  • A super user with username test and password test is created
  • The page is available under http://127.0.0.1:8000/app_path/

history

  • dev
    • tbc
  • v0.5.0rc1 - 05.10.2022
    • Set min. Python version from 3.7 to 3.9 (Needs YunoHost 11!)
    • Display logs in local tests and use colorlog.StreamHandler
    • Bugfix example url pattern
    • Code cleanup: Remove request_media_debug_view
  • v0.4.1 - 04.10.2022
    • Add assert_project_version and get_github_version_tag in test_utils
  • v0.4.0 - 15.09.2022
    • Add SyslogHandler to logging settings and enhance logging example settings.
    • rename replacements (but still support the old ones):
      • __FINAL_HOME_PATH__ -> __FINALPATH__
      • __FINAL_WWW_PATH__ -> __PUBLIC_PATH__
    • Add system checks to verify all "EMAIL" in settings and settings.LOG_LEVEL
    • Bugfix dev shell and exit if it's called as CLI
    • Run "saftey" check in CI
  • v0.3.0 - 14.08.2022
    • Add extra_replacements:dict argument to create_local_test() to pass own __YNH_VARIABLE__ replacements
    • Remove pytest_helper.run_pytest() because every project should used a own conftest.py with create_local_test() usage.
  • v0.2.5 - 12.08.2022
    • Support new variable names, for ynh_add_config usage in local test helper.
    • Run tests with Python v3.10, too.
    • Update project setup.
  • v0.2.4 - 30.01.2022
    • Rename git "master" branch to "main"
    • Use darker and pytest-darker as code formatter + update requirements
  • v0.2.3 - 07.01.2022
    • Bugfix Fix local test by set "SECURE_SSL_REDIRECT = False"
  • v0.2.2 - 10.10.2021
    • Read YunoHost App Id from "manifest.json" and check root directory name
  • v0.2.1 - 16.09.2021
    • Bugfix endless redirect loop, by adding SECURE_PROXY_SSL_HEADER to settings
  • v0.2.0 - 15.09.2021
    • rename/split django_ynh into:
    • Replace psycopg2-binary with psycopg2 (needs some prerequisites packages, see above)
  • v0.1.5 - 19.01.2021
    • Make some deps gunicorn, psycopg2-binary, django-redis, django-axes optional
  • v0.1.4 - 08.01.2021
    • Bugfix: CSRF verification failed on POST requests
  • v0.1.3 - 08.01.2021
    • set "DEBUG = True" in local_test (so static files are served and auth works)
    • Bugfixes and cleanups
  • v0.1.2 - 29.12.2020
    • Bugfixes
  • v0.1.1 - 29.12.2020
    • Refactor "create_superuser" to a manage command, useable via "django_yunohost_integration" in INSTALLED_APPS
    • Generate "conf/requirements.txt" and use this file for install
    • rename own settings and urls (in /conf/)
  • v0.1.0 - 28.12.2020
    • first working state
  • 23.12.2020
    • init the project

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_yunohost_integration-0.5.0rc1.tar.gz (31.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django_yunohost_integration-0.5.0rc1.tar.gz.

File metadata

File hashes

Hashes for django_yunohost_integration-0.5.0rc1.tar.gz
Algorithm Hash digest
SHA256 5d8823acb83a668a5126e1324d2c3c7b239595a77a03d8c9bdaa8446154c64e4
MD5 11ee2676ccaf809c3bde96ab761c5eb6
BLAKE2b-256 41a4043b3d34aa1d8baada106d191d814ea4186fc676d9cb237119b84ebda22d

See more details on using hashes here.

Provenance

File details

Details for the file django_yunohost_integration-0.5.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_yunohost_integration-0.5.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e6d8ec12d48d9897c9dc02fc60c35bb3c7cc5f9446961fc0be61bb6f5586197
MD5 e513c1943e5b7c5702a2f0adf5cb1d23
BLAKE2b-256 ea592aa4d4fe135a3b7f4e0b5216a6afd76e32e381907ec21b0197b631811790

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page