Set settings.DEBUG via config_panel.toml

This commit is contained in:
JensDiemer 2022-08-14 19:30:06 +02:00
parent 1f0a6c7a86
commit 363a93f205
11 changed files with 84 additions and 17 deletions

View file

@ -15,7 +15,9 @@ 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 = False # Don't turn DEBUG on in production!
# Set via config_panel.toml
DEBUG_ENABLED = '__DEBUG_ENABLED__'
DEBUG = bool(int(DEBUG_ENABLED))
# -----------------------------------------------------------------------------

View file

@ -25,12 +25,12 @@ if settings.PATH_URL:
# settings.PATH_URL is the $YNH_APP_ARG_PATH
# Prefix all urls with "PATH_URL":
urlpatterns = [
path(f'{settings.PATH_URL}/', admin.site.urls),
path(f'{settings.PATH_URL}/debug/', request_media_debug_view),
path(f'{settings.PATH_URL}/', admin.site.urls),
]
else:
# Installed to domain root, without a path prefix
urlpatterns = [
path('/', admin.site.urls),
path('/debug/', request_media_debug_view),
path('/', admin.site.urls),
]

16
config_panel.toml Normal file
View file

@ -0,0 +1,16 @@
version = "1.0"
[main]
name = "django_example_ynh configuration"
services = ["__APP__"]
[main.config]
name = "Configuration Options"
[main.config.debug_enabled]
ask = "Enable DEBUG mode"
type = "boolean"
yes = "1"
no = "0"
help = "Should be never enabled in production!"
bind = "debug_enabled:__FINALPATH__/settings.py"

View file

@ -110,6 +110,9 @@ yunohost backup restore 20201223-163434 --apps django_example_ynh
Debug the installation, e.g.:
```bash
root@yunohost:~# cat /etc/yunohost/apps/django_example_ynh/settings.yml
...
root@yunohost:~# ls -la /var/www/django_example_ynh/
total 18
drwxr-xr-x 4 root root 4 Dec 8 08:36 .
@ -140,6 +143,7 @@ System check identified no issues (0 silenced).
root@yunohost:~# tail -f /var/log/django_example_ynh/django_example_ynh.log
root@yunohost:~# cat /etc/systemd/system/systemd.service
...
root@yunohost:~# systemctl reload-or-restart django_example_ynh
root@yunohost:~# journalctl --unit=django_example_ynh --follow

View file

@ -22,6 +22,9 @@ def main():
django_settings_path=BASE_PATH / 'conf' / 'settings.py',
destination=BASE_PATH / 'local_test',
runserver=True,
extra_replacements={
'__DEBUG_ENABLED__': '1',
},
)

View file

@ -11,6 +11,13 @@ admin=$YNH_APP_ARG_ADMIN
is_public=$YNH_APP_ARG_IS_PUBLIC
app=$YNH_APP_INSTANCE_NAME
#=================================================
# ARGUMENTS FROM CONFIG PANEL
#=================================================
# 'debug_enabled' -> '__DEBUG_ENABLED__' -> settings.DEBUG
debug_enabled="0"
#=================================================
# SET CONSTANTS
#=================================================
@ -22,9 +29,6 @@ log_file="${log_path}/django_example_ynh.log"
adminmail=$(ynh_user_get_info --username=$admin --key=mail)
# Default: settings.DEBUG=False
django_debug="False"
#=================================================
# COMMON VARIABLES
#=================================================

View file

@ -61,7 +61,7 @@ redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db"
# Always deactivate settings.DEBUG:
ynh_app_setting_set --app=$app --key=django_debug --value="False"
ynh_app_setting_set --app="$app" --key=debug_enabled --value="$debug_enabled"
#=================================================
# STANDARD MODIFICATIONS
@ -105,9 +105,9 @@ ynh_script_progression --message="Configuring system user..."
ynh_system_user_create --username="$app" --home_dir="$final_path" --use_shell
#=================================================
# PIP INSTALLATION
# PYTHON VIRTUALENV
#=================================================
ynh_script_progression --message="Install project via pip..." --weight=50
ynh_script_progression --message="Create Python virtualenv..." --weight=5
# Always recreate everything fresh with current python version
ynh_secure_remove "${final_path}/venv"
@ -118,6 +118,11 @@ python3 -m venv --without-pip "${final_path}/venv"
cp ../conf/requirements.txt "$final_path/requirements.txt"
chown -R "$app:" "$final_path"
#=================================================
# PIP INSTALLATION
#=================================================
ynh_script_progression --message="Install project via pip..." --weight=45
#run source in a 'sub shell'
(
set +o nounset

View file

@ -79,10 +79,10 @@ ynh_script_progression --message="Reinstalling dependencies..." --weight=20
ynh_exec_warn_less ynh_install_app_dependencies "$pkg_dependencies"
#=================================================
# REINSTALL PYTHON VIRTUALENV
# PYTHON VIRTUALENV
# Maybe the backup contains a other Python version
#=================================================
ynh_script_progression --message="Upgrade Python virtualenv..." --weight=50
ynh_script_progression --message="Recreate Python virtualenv..." --weight=5
# Always recreate everything fresh with current python version
ynh_secure_remove "${final_path}/venv"
@ -91,6 +91,10 @@ ynh_secure_remove "${final_path}/venv"
python3 -m venv --without-pip "${final_path}/venv"
chown -R "$app:" "$final_path"
#=================================================
# PIP INSTALLATION
#=================================================
ynh_script_progression --message="Install project via pip..." --weight=45
#run source in a 'sub shell'
(
set +o nounset

View file

@ -29,7 +29,11 @@ db_user=$db_name
admin_mail=$(ynh_user_get_info "$admin" mail)
redis_db=$(ynh_app_setting_get --app="$app" --key=redis_db)
django_debug=$(ynh_app_setting_get --app="$app" --key=django_debug)
debug_enabled=$(ynh_app_setting_get --app="$app" --key=debug_enabled)
if [ -z "$debug_enabled" ]; then
debug_enabled="0"
ynh_app_setting_set --app="$app" --key=debug_enabled --value="$debug_enabled"
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
@ -59,7 +63,7 @@ ynh_systemd_action --service_name="$app" --action="stop"
#=================================================
# Always deactivate settings.DEBUG:
ynh_app_setting_set --app=$app --key=django_debug --value="False"
ynh_app_setting_set --app=$app --key=debug_enabled --value="False"
#=================================================
# NGINX CONFIGURATION
@ -96,9 +100,9 @@ ynh_script_progression --message="Configuring a systemd service..."
ynh_add_systemd_config --service="$app" --template="systemd.service"
#=================================================
# UPGRADE VENV
# PYTHON VIRTUALENV
#=================================================
ynh_script_progression --message="Upgrade project via pip..." --weight=50
ynh_script_progression --message="Recreate Python virtualenv..." --weight=5
# Always recreate everything fresh with current python version
ynh_secure_remove "${final_path}/venv"
@ -109,6 +113,10 @@ python3 -m venv --without-pip "${final_path}/venv"
cp ../conf/requirements.txt "$final_path/requirements.txt"
chown -R "$app:" "$final_path"
#=================================================
# PIP INSTALLATION
#=================================================
ynh_script_progression --message="Install project via pip..." --weight=45
#run source in a 'sub shell'
(
set +o nounset

View file

@ -26,7 +26,9 @@ def pytest_configure():
django_settings_path=BASE_PATH / 'conf' / 'settings.py',
destination=BASE_PATH / 'local_test',
runserver=False,
extra_replacements={},
extra_replacements={
'__DEBUG_ENABLED__': '0',
},
)
print('Local test files created here:')
print(f'"{final_path}"')

View file

@ -30,9 +30,28 @@ class DjangoYnhTestCase(HtmlAssertionMixin, TestCase):
def test_urls(self):
assert reverse('admin:index') == '/app_path/'
# In example app, the debug view is in urls.py:
def test_request_media_debug_view(self):
assert reverse(request_media_debug_view) == '/app_path/debug/'
self.client.cookies['SSOwAuthUser'] = 'test'
kwargs = dict(
path='/app_path/debug/',
HTTP_REMOTE_USER='test',
HTTP_AUTH_USER='test',
HTTP_AUTHORIZATION='basic dGVzdDp0ZXN0MTIz',
secure=True,
)
assert settings.DEBUG is False
with self.assertRaisesMessage(AssertionError, 'Only in DEBUG mode available!'):
self.client.get(**kwargs)
with override_settings(DEBUG=True):
response = self.client.get(**kwargs)
self.assert_html_parts(
response,
parts=('request.META',),
)
def test_auth(self):
# SecurityMiddleware should redirects all non-HTTPS requests to HTTPS:
assert settings.SECURE_SSL_REDIRECT is True