This commit is contained in:
JensDiemer 2021-09-15 19:16:35 +02:00
parent c448b138b1
commit 131346524e
12 changed files with 459 additions and 521 deletions

166
README.md
View file

@ -9,84 +9,16 @@ Demo [YunoHost Application](https://install-app.yunohost.org/?app=django_example
Pull requests welcome ;)
## 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`)
* Run `pytest` against `local_test` "installation"
### SSO authentication
[SSOwat](https://github.com/YunoHost/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.:
```bash
./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`:
```python
from django_ynh.secret_key import get_or_create_secret as __get_or_create_secret
# Function that will be called to finalize a user profile:
YNH_SETUP_USER = 'setup_user.setup_project_user'
SECRET_KEY = __get_or_create_secret(FINAL_HOME_PATH / 'secret.txt') # /opt/yunohost/$app/secret.txt
INSTALLED_APPS = [
#...
'django_ynh',
#...
]
MIDDLEWARE = [
#... after AuthenticationMiddleware ...
#
# login a user via HTTP_REMOTE_USER header from SSOwat:
'django_ynh.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_ynh.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/'
```
## local test
For quicker developing of django_ynh in the context of YunoHost app,
For quicker developing of django_example_ynh 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.:
```bash
~$ git clone https://github.com/YunoHost-Apps/django_example_ynh.git
~$ cd django_ynh/
~$ cd django_example_ynh/
~/django_example_ynh$ make
install-poetry install or update poetry
install install project via poetry
@ -114,38 +46,38 @@ Notes:
## history
* [compare v0.1.5...master](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.5...master) **dev**
* [compare v0.1.5...master](https://github.com/YunoHost-Apps/django_example_ynh/compare/v0.2.0...master) **dev**
* tbc
* v0.2.0.alpha0 **dev**
* rename/split `django_ynh` into:
* `django_yunohost_integration` - Python package with the glue code to integrate a Django project with YunoHost
* `django_example_ynh` - Demo YunoHost App to demonstrate the integration of a Django project under YunoHost
* [v0.1.5 - 19.01.2021](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.4...v0.1.5)
* [v0.2.0 - 15.09.2021](https://github.com/YunoHost-Apps/django_example_ynh/compare/v0.1.5...v0.2.0)
* rename/split `django_example_ynh` into:
* [django_yunohost_integration](https://github.com/jedie/django_yunohost_integration) - Python package with the glue code to integrate a Django project with YunoHost
* [django_example_ynh](https://github.com/YunoHost-Apps/django_example_ynh) - Demo YunoHost App to demonstrate the integration of a Django project under YunoHost
* [v0.1.5 - 19.01.2021](https://github.com/YunoHost-Apps/django_example_ynh/compare/v0.1.4...v0.1.5)
* Make some deps `gunicorn`, `psycopg2-binary`, `django-redis`, `django-axes` optional
* [v0.1.4 - 08.01.2021](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.3...v0.1.4)
* Bugfix [CSRF verification failed on POST requests #7](https://github.com/YunoHost-Apps/django_ynh/issues/7)
* [v0.1.3 - 08.01.2021](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.2...v0.1.3)
* [v0.1.4 - 08.01.2021](https://github.com/YunoHost-Apps/django_example_ynh/compare/v0.1.3...v0.1.4)
* Bugfix [CSRF verification failed on POST requests #7](https://github.com/YunoHost-Apps/django_example_ynh/issues/7)
* [v0.1.3 - 08.01.2021](https://github.com/YunoHost-Apps/django_example_ynh/compare/v0.1.2...v0.1.3)
* set "DEBUG = True" in local_test (so static files are served and auth works)
* Bugfixes and cleanups
* [v0.1.2 - 29.12.2020](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.1...v0.1.2)
* [v0.1.2 - 29.12.2020](https://github.com/YunoHost-Apps/django_example_ynh/compare/v0.1.1...v0.1.2)
* Bugfixes
* [v0.1.1 - 29.12.2020](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.0...v0.1.1)
* Refactor "create_superuser" to a manage command, useable via "django_ynh" in `INSTALLED_APPS`
* [v0.1.1 - 29.12.2020](https://github.com/YunoHost-Apps/django_example_ynh/compare/v0.1.0...v0.1.1)
* Refactor "create_superuser" to a manage command, useable via "django_example_ynh" 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](https://github.com/YunoHost-Apps/django_ynh/compare/f578f14...v0.1.0)
* [v0.1.0 - 28.12.2020](https://github.com/YunoHost-Apps/django_example_ynh/compare/f578f14...v0.1.0)
* first working state
* [23.12.2020](https://github.com/YunoHost-Apps/django_ynh/commit/f578f144a3a6d11d7044597c37d550d29c247773)
* [23.12.2020](https://github.com/YunoHost-Apps/django_example_ynh/commit/f578f144a3a6d11d7044597c37d550d29c247773)
* init the project
## Links
* Report a bug about this package: https://github.com/YunoHost-Apps/django_ynh
* Report a bug about this package: https://github.com/YunoHost-Apps/django_example_ynh
* YunoHost website: https://yunohost.org/
* PyPi package: https://pypi.org/project/django-ynh/
These projects used `django_ynh`:
These projects used `django_example_ynh`:
* https://github.com/YunoHost-Apps/django_example_ynh
* https://github.com/YunoHost-Apps/django-for-runners_ynh
@ -156,73 +88,73 @@ These projects used `django_ynh`:
## package installation / debugging
Please send your pull request to https://github.com/YunoHost-Apps/django_ynh
Please send your pull request to https://github.com/YunoHost-Apps/django_example_ynh
Try 'main' branch, e.g.:
```bash
sudo yunohost app install https://github.com/YunoHost-Apps/django_ynh/tree/master --debug
sudo yunohost app install https://github.com/YunoHost-Apps/django_example_ynh/tree/master --debug
or
sudo yunohost app upgrade django_ynh -u https://github.com/YunoHost-Apps/django_ynh/tree/master --debug
sudo yunohost app upgrade django_example_ynh -u https://github.com/YunoHost-Apps/django_example_ynh/tree/master --debug
```
Try 'testing' branch, e.g.:
```bash
sudo yunohost app install https://github.com/YunoHost-Apps/django_ynh/tree/testing --debug
sudo yunohost app install https://github.com/YunoHost-Apps/django_example_ynh/tree/testing --debug
or
sudo yunohost app upgrade django_ynh -u https://github.com/YunoHost-Apps/django_ynh/tree/testing --debug
sudo yunohost app upgrade django_example_ynh -u https://github.com/YunoHost-Apps/django_example_ynh/tree/testing --debug
```
To remove call e.g.:
```bash
sudo yunohost app remove django_ynh
sudo yunohost app remove django_example_ynh
```
Backup / remove / restore cycle, e.g.:
```bash
yunohost backup create --apps django_ynh
yunohost backup create --apps django_example_ynh
yunohost backup list
archives:
- django_ynh-pre-upgrade1
- django_example_ynh-pre-upgrade1
- 20201223-163434
yunohost app remove django_ynh
yunohost backup restore 20201223-163434 --apps django_ynh
yunohost app remove django_example_ynh
yunohost backup restore 20201223-163434 --apps django_example_ynh
```
Debug installation, e.g.:
```bash
root@yunohost:~# ls -la /var/www/django_ynh/
root@yunohost:~# ls -la /var/www/django_example_ynh/
total 18
drwxr-xr-x 4 root root 4 Dec 8 08:36 .
drwxr-xr-x 6 root root 6 Dec 8 08:36 ..
drwxr-xr-x 2 root root 2 Dec 8 08:36 media
drwxr-xr-x 7 root root 8 Dec 8 08:40 static
root@yunohost:~# ls -la /opt/yunohost/django_ynh/
root@yunohost:~# ls -la /opt/yunohost/django_example_ynh/
total 58
drwxr-xr-x 5 django_ynh django_ynh 11 Dec 8 08:39 .
drwxr-xr-x 5 django_example_ynh django_example_ynh 11 Dec 8 08:39 .
drwxr-xr-x 3 root root 3 Dec 8 08:36 ..
-rw-r--r-- 1 django_ynh django_ynh 460 Dec 8 08:39 gunicorn.conf.py
-rw-r--r-- 1 django_ynh django_ynh 0 Dec 8 08:39 local_settings.py
-rwxr-xr-x 1 django_ynh django_ynh 274 Dec 8 08:39 manage.py
-rw-r--r-- 1 django_ynh django_ynh 171 Dec 8 08:39 secret.txt
drwxr-xr-x 6 django_ynh django_ynh 6 Dec 8 08:37 venv
-rw-r--r-- 1 django_ynh django_ynh 115 Dec 8 08:39 wsgi.py
-rw-r--r-- 1 django_ynh django_ynh 4737 Dec 8 08:39 django_ynh_demo_settings.py
-rw-r--r-- 1 django_example_ynh django_example_ynh 460 Dec 8 08:39 gunicorn.conf.py
-rw-r--r-- 1 django_example_ynh django_example_ynh 0 Dec 8 08:39 local_settings.py
-rwxr-xr-x 1 django_example_ynh django_example_ynh 274 Dec 8 08:39 manage.py
-rw-r--r-- 1 django_example_ynh django_example_ynh 171 Dec 8 08:39 secret.txt
drwxr-xr-x 6 django_example_ynh django_example_ynh 6 Dec 8 08:37 venv
-rw-r--r-- 1 django_example_ynh django_example_ynh 115 Dec 8 08:39 wsgi.py
-rw-r--r-- 1 django_example_ynh django_example_ynh 4737 Dec 8 08:39 django_example_ynh_demo_settings.py
root@yunohost:~# cd /opt/yunohost/django_ynh/
root@yunohost:/opt/yunohost/django_ynh# source venv/bin/activate
(venv) root@yunohost:/opt/yunohost/django_ynh# ./manage.py check
django_ynh v0.8.2 (Django v2.2.17)
DJANGO_SETTINGS_MODULE='django_ynh_demo_settings'
PROJECT_PATH:/opt/yunohost/django_ynh/venv/lib/python3.7/site-packages
BASE_PATH:/opt/yunohost/django_ynh
root@yunohost:~# cd /opt/yunohost/django_example_ynh/
root@yunohost:/opt/yunohost/django_example_ynh# source venv/bin/activate
(venv) root@yunohost:/opt/yunohost/django_example_ynh# ./manage.py check
django_example_ynh v0.8.2 (Django v2.2.17)
DJANGO_SETTINGS_MODULE='django_example_ynh_demo_settings'
PROJECT_PATH:/opt/yunohost/django_example_ynh/venv/lib/python3.7/site-packages
BASE_PATH:/opt/yunohost/django_example_ynh
System check identified no issues (0 silenced).
root@yunohost:~# tail -f /var/log/django_ynh/django_ynh.log
root@yunohost:~# cat /etc/systemd/system/django_ynh.service
root@yunohost:~# tail -f /var/log/django_example_ynh/django_example_ynh.log
root@yunohost:~# cat /etc/systemd/system/django_example_ynh.service
root@yunohost:~# systemctl reload-or-restart django_ynh
root@yunohost:~# journalctl --unit=django_ynh --follow
root@yunohost:~# systemctl reload-or-restart django_example_ynh
root@yunohost:~# journalctl --unit=django_example_ynh --follow
```

View file

@ -1,5 +1,5 @@
[Unit]
Description=PyInventory application server
Description=__APP__ server
After=redis.service postgresql.service
[Service]

View file

@ -5,13 +5,6 @@ location __PATH__/static/ {
expires 30d;
}
# TODO: django-sendfile2:
#location __PATH__/media/ {
# # DATA_DIR/media/
# alias __PUBLIC_PATH__/media/;
# expires 30d;
#}
location __PATH__/ {
# https://github.com/benoitc/gunicorn/blob/master/examples/nginx.conf

View file

@ -11,8 +11,8 @@
from pathlib import Path as __Path
from django_ynh.secret_key import get_or_create_secret as __get_or_create_secret
from inventory_project.settings.base import * # noqa
from django_yunohost_integration.secret_key import get_or_create_secret as __get_or_create_secret
from django_yunohost_integration.base_settings import * # noqa
DEBUG = False # Don't turn DEBUG on in production!
@ -33,36 +33,12 @@ PATH_URL = PATH_URL.strip('/')
# -----------------------------------------------------------------------------
ROOT_URLCONF = 'urls' # /opt/yunohost/django_example_ynh/ynh_urls.py
# Function that will be called to finalize a user profile:
YNH_SETUP_USER = 'setup_user.setup_project_user'
SECRET_KEY = __get_or_create_secret(FINAL_HOME_PATH / 'secret.txt') # /opt/yunohost/$app/secret.txt
INSTALLED_APPS.append('django_ynh')
MIDDLEWARE.insert(
MIDDLEWARE.index('django.contrib.auth.middleware.AuthenticationMiddleware') + 1,
# login a user via HTTP_REMOTE_USER header from SSOwat:
'django_ynh.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_ynh.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
# INSTALLED_APPS.append('<insert-your-app-here>')
# -----------------------------------------------------------------------------
@ -133,15 +109,6 @@ else:
STATIC_ROOT = str(FINAL_WWW_PATH / 'static')
MEDIA_ROOT = str(FINAL_WWW_PATH / 'media')
# _____________________________________________________________________________
# django-ckeditor
CKEDITOR_BASEPATH = STATIC_URL + 'ckeditor/ckeditor/'
# _____________________________________________________________________________
# Django-dbbackup
DBBACKUP_STORAGE_OPTIONS['location'] = str(FINAL_HOME_PATH / 'backups')
# -----------------------------------------------------------------------------
@ -173,8 +140,7 @@ LOGGING = {
'django': {'handlers': ['log_file', 'mail_admins'], 'level': 'INFO', 'propagate': False},
'axes': {'handlers': ['log_file', 'mail_admins'], 'level': 'WARNING', 'propagate': False},
'django_tools': {'handlers': ['log_file', 'mail_admins'], 'level': 'INFO', 'propagate': False},
'django_ynh': {'handlers': ['log_file', 'mail_admins'], 'level': 'INFO', 'propagate': False},
'inventory': {'handlers': ['log_file', 'mail_admins'], 'level': 'INFO', 'propagate': False},
'django_yunohost_integration': {'handlers': ['log_file', 'mail_admins'], 'level': 'INFO', 'propagate': False},
},
}

View file

@ -1,7 +1,7 @@
def setup_project_user(user):
"""
All users used the Django admin, so we need to set the "staff" user flag.
Called from django_ynh.sso_auth
Called from django_yunohost_integration.sso_auth
"""
user.is_staff = True
user.save()

View file

@ -10,7 +10,7 @@ from pathlib import Path
try:
from django_ynh.local_test import create_local_test
from django_yunohost_integration.local_test import create_local_test
except ImportError as err:
raise ImportError('Did you forget to activate a virtual environment?') from err

723
poetry.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,16 +1,18 @@
[tool.poetry]
name = "django_example_ynh"
version = "v0.2.0.alpha0"
version = "v0.2.0"
description = "Demo YunoHost Application to demonstrate the integration of a Django project under YunoHost."
authors = ["JensDiemer <git@jensdiemer.de>"]
license = "GPL"
homepage = "https://github.com/YunoHost-Apps/django_example_ynh"
[tool.poetry.dependencies]
python = ">=3.7,<4.0.0"
django_yunohost_integration = {version = "*", extras = ["ynh"]}
[tool.poetry.dev-dependencies]
bx_py_utils = "*"
bx_py_utils = "*" # https://github.com/boxine/bx_py_utils
bx_django_utils = "*" # https://github.com/boxine/bx_django_utils
tox = "*"
pytest = "*"
pytest-cov = "*"

View file

@ -6,7 +6,7 @@ from pathlib import Path
try:
from django_ynh.pytest_helper import run_pytest
from django_yunohost_integration.pytest_helper import run_pytest
except ImportError as err:
raise ImportError('Did you forget to activate a virtual environment?') from err

View file

@ -1,15 +1,13 @@
from axes.models import AccessLog
from bx_py_utils.test_utils.html_assertion import HtmlAssertionMixin
from bx_django_utils.test_utils.html_assertion import HtmlAssertionMixin
from django.conf import settings
from django.contrib.auth.models import User
from django.test import override_settings
from django.test.testcases import TestCase
from django.urls import NoReverseMatch
from django.urls.base import reverse
from django_ynh.test_utils import generate_basic_auth
from django_ynh.views import request_media_debug_view
import inventory
from django_yunohost_integration.test_utils import generate_basic_auth
from django_yunohost_integration.views import request_media_debug_view
@override_settings(DEBUG=False)
@ -32,7 +30,7 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase):
def test_urls(self):
assert reverse('admin:index') == '/app_path/'
# The django_ynh debug view should not be avaiable:
# The django_yunohost_integration debug view should not be avaiable:
with self.assertRaises(NoReverseMatch):
reverse(request_media_debug_view)
@ -62,13 +60,13 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase):
user = User.objects.first()
assert user.username == 'test'
assert user.is_active is True
assert user.is_staff is True # Set by: conf.django_ynh_demo_urls.setup_user_handler
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
assert user.is_superuser is False
self.assert_html_parts(
response,
parts=(
f'<title>Site administration | PyInventory v{inventory.__version__}</title>',
f'<title>Site administration</title>',
'<strong>test</strong>',
),
)
@ -90,7 +88,7 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase):
user = User.objects.first()
assert user.username == 'test'
assert user.is_active is True
assert user.is_staff is True # Set by: conf.django_ynh_demo_urls.setup_user_handler
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
assert user.is_superuser is False
assert AccessLog.objects.count() == 1
@ -114,7 +112,7 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase):
user = User.objects.first()
assert user.username == 'test'
assert user.is_active is True
assert user.is_staff is True # Set by: conf.django_ynh_demo_urls.setup_user_handler
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
assert user.is_superuser is False
assert AccessLog.objects.count() == 1
@ -137,7 +135,7 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase):
user = User.objects.first()
assert user.username == 'test'
assert user.is_active is True
assert user.is_staff is True # Set by: conf.django_ynh_demo_urls.setup_user_handler
assert user.is_staff is True # Set by: conf.setup_user.setup_project_user
assert user.is_superuser is False
assert AccessLog.objects.count() == 1

View file

@ -3,7 +3,7 @@ import shutil
import subprocess
from pathlib import Path
import django_exyunohost_integration
import django_yunohost_integration
PACKAGE_ROOT = Path(__file__).parent.parent
@ -18,10 +18,10 @@ def assert_file_contains_string(file_path, string):
def test_version():
version = inventory.__version__
version = django_yunohost_integration.__version__
assert_file_contains_string(file_path=Path(PACKAGE_ROOT, 'pyproject.toml'), string=f'version = "{version}~ynh')
assert_file_contains_string(file_path=Path(PACKAGE_ROOT, 'pyproject.toml'), string=f'django_example_ynh = "=={version}"')
assert_file_contains_string(file_path=Path(PACKAGE_ROOT, 'pyproject.toml'), string=f'django_yunohost_integration = "=={version}"')
assert_file_contains_string(file_path=Path(PACKAGE_ROOT, 'manifest.json'), string=f'"version": "{version}~ynh')

View file

@ -1,6 +1,6 @@
from unittest.case import TestCase
from django_ynh.test_utils import generate_basic_auth
from django_yunohost_integration.test_utils import generate_basic_auth
class TestUtilsTestCase(TestCase):