diff --git a/conf/settings.py b/conf/settings.py index a1daedc..5d77582 100644 --- a/conf/settings.py +++ b/conf/settings.py @@ -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)) # ----------------------------------------------------------------------------- diff --git a/conf/urls.py b/conf/urls.py index e76b15e..cfd7ae6 100644 --- a/conf/urls.py +++ b/conf/urls.py @@ -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), ] diff --git a/config_panel.toml b/config_panel.toml new file mode 100644 index 0000000..c7144fc --- /dev/null +++ b/config_panel.toml @@ -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" diff --git a/doc/DISCLAIMER.md b/doc/DISCLAIMER.md index f6b962b..9d298c1 100644 --- a/doc/DISCLAIMER.md +++ b/doc/DISCLAIMER.md @@ -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 diff --git a/local_test.py b/local_test.py index e2664e5..1f857e9 100644 --- a/local_test.py +++ b/local_test.py @@ -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', + }, ) diff --git a/scripts/_common.sh b/scripts/_common.sh index ba6c851..fee5921 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -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 #================================================= diff --git a/scripts/install b/scripts/install index 727dad1..5eab770 100755 --- a/scripts/install +++ b/scripts/install @@ -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 diff --git a/scripts/restore b/scripts/restore index 34d464f..0401327 100755 --- a/scripts/restore +++ b/scripts/restore @@ -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 diff --git a/scripts/upgrade b/scripts/upgrade index f639cbb..9dfc208 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 3a798e4..d1a679f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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}"') diff --git a/tests/test_django_project.py b/tests/test_django_project.py index 91ad6ab..cb6460b 100644 --- a/tests/test_django_project.py +++ b/tests/test_django_project.py @@ -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