Add a new app settings via config_panel.toml

I tried to add a new app settings here: Add config to change settings.DEBUG by jedie · Pull Request #18 · YunoHost-Apps/django-fritzconnection_ynh · GitHub

I would like to set Django’s settings.DEBUG flag.

First Question: I need the string “True” / “False” in the python script file. How to define this in config_panel.toml ?

I tried:

1.:

        [djfritz_config.django_config.django_debug]
        ask = "debug mode"
        bind = "DJANGO_DEBUG:/opt/yunohost/__APP__/local_settings.py"
        type = "boolean"
        default = false

2.:

            [djfritz_config.django_config.django_debug]
            ask = "debug mode"
            bind = "DJANGO_DEBUG:/opt/yunohost/__APP__/local_settings.py"
            type = "boolean"
            yes = "True"
            no = "False"
            default = false

3.:

            [djfritz_config.django_config.django_debug]
            ask = "debug mode"
            bind = "DJANGO_DEBUG:/opt/yunohost/__APP__/local_settings.py"
            type = "select"
            choices = ["True", "False"]
            default = "False"

Think 2 is the best choice, isn’t it?
But currently it doesn’t work at all. Because i get:

root@testyunohost:/home/jens# yunohost app config get django-fritzconnection --debug
66   DEBUG initializing base actions map parser for cli
67   DEBUG loading actions map namespace 'yunohost'
69   DEBUG building parser...
76   DEBUG building parser took 0.007s
77   DEBUG acquiring lock...
85   DEBUG lock has been acquired
91   DEBUG loading python module yunohost.app took 0.006s
91   DEBUG processing action [9808.1]: yunohost.app.config.get with args={'app': 'django-fritzconnection', 'key': None, 'full': False, 'export': False}
92   DEBUG action [9808.1] executed in 0.001s
92   DEBUG lock has been released
93   ERROR Der Filterschlüssel '' ist inkorrekt.

I found ERROR Der Filterschlüssel '' ist inkorrekt. is config_unknown_filter_key here: yunohost/config.py at dddc92949c86bbd2d0077a517130e81e86953148 · YunoHost/yunohost · GitHub

The sections was wrong, e.g.:

[main]
...

[djfritz_config]
...

    [djfritz_config.django_config]
    ...

        [djfritz_config.django_config.django_debug]
        ...

I cleanup them to:

[main]
name = "Settings"

    [main.djfritz_config]
    name = "django-fritzconnection configuration"

    services = ["__APP__"]

        [main.djfritz_config.django_debug]
        ask = "debug mode"
        help = "Important: Never activate this settings.DEBUG on production!"
        type = "boolean"
        yes = "True"
        no = "False"
        bind = "DJANGO_DEBUG:/opt/yunohost/__APP__/local_settings.py"

Now i get this:

root@testyunohost:/home/jens# yunohost app config get django-fritzconnection 
Fehler: Config panel question 'django_debug' should be initialized with a value during install or upgrade.

The generated local_settings.py looks fine:

root@testyunohost:/home/jens# cat /opt/yunohost/django-fritzconnection/local_settings.py 
"""
    Here it's possible to overwrite everything in settings.py

    Note:
        Used for YunoHost config and will be **overwritten** on every config change!
"""

DEBUG = False  # Don't turn DEBUG on in production!

You should initialized django_debug variable in install script
ynh_app_setting_set --app=$app --key=django_debug --value="1"

I have already this:

ynh_app_setting_set --app=$app --key=django_debug --value="False"

But it must be “0” ?!?

EDIT: I test it with “0” and it’s the same:

root@testyunohost:/home/jens# yunohost app config get django-fritzconnection 
Fehler: Config panel question 'django_debug' should be initialized with a value during install or upgrade.

But ynh_add_config --template="local_settings.py" --destination="$final_path/local_settings.py" creates a correct file by replacing DEBUG = __DJANGO_DEBUG__ with DEBUG = False

I’m a little confused about the values for a boolean…
Internally YunoHost stores “0” and “1” so ynh_app_setting_set must be feeded with “0” / “1” but ynh_add_config will convert them to yes/no values if defined in config_panel.toml ?!?
And if not?

Is there some documentation about boolean handling, somewhere?!?

btw. i seems that Configuration panel | Yunohost Documentation is outdated.

What is your working branch?
is it this one: GitHub - YunoHost-Apps/django-fritzconnection_ynh at config

yes, see open PR here: Add config to change settings.DEBUG by jedie · Pull Request #18 · YunoHost-Apps/django-fritzconnection_ynh · GitHub

Can you try if see if this solves the issue? Config panel by ericgaspar · Pull Request #19 · YunoHost-Apps/django-fritzconnection_ynh · GitHub
(not really sure we need the caps for False and True)

Think this can’t work: I need “False” and not “false” in generated local_settings.py

But i will try this.

EDIT: I created “config2” branch: Comparing master...config2 · YunoHost-Apps/django-fritzconnection_ynh · GitHub

Interesting: Now i get this:

root@testyunohost:/home/jens# cat /opt/yunohost/django-fritzconnection/local_settings.py 
...
DEBUG = 0

So the “yes/no” values in config_panel.toml are ignored?!?

But i still get the same error:

root@testyunohost:/home/jens# yunohost app config get django-fritzconnection 
Fehler: Config panel question 'django_debug' should be initialized with a value during install or upgrade.

EDIT2: I now try this:

-        bind = "DJANGO_DEBUG:/opt/yunohost/__APP__/local_settings.py"
+        bind = "DJANGO_DEBUG:__FINALPATH__/local_settings.py"

EDIT3: Oh interesting:

Seems that the path change has an effect… I now try to change yes/no from “true/false” to “True/False”…

EDIT4: No, still the same error: Config panel question 'django_debug' should be initialized...

I have no idea.

I checked the generated settings.yml:

root@testyunohost:/home/jens# cat /etc/yunohost/apps/django-fritzconnection/settings.yml 
...
django_debug: 'False'
...

So the value is set and the error seems to be wrong:

root@testyunohost:/home/jens# yunohost app config get django-fritzconnection
Fehler: Config panel question 'django_debug' should be initialized with a value during install or upgrade.

There the complete output with --debughastebin

1 Like

Nobody any idea what happens here?
Should i open a github issues?

I create: Config panel question 'foo_bar' should be initialized with a value during install or upgrade. · Issue #2015 · YunoHost/issues · GitHub

Copying and pasting from Github:

(disclaimer: I have little knowledge about the config panels, I’m only tweaking here)

Understanding bind

bind = "DJANGO_DEBUG:/opt/yunohost/__APP__/local_settings.py"

As far as I understood the config panels, the bind function will parse local_settings.py looking for the DJANGO_DEBUG variable declaration line. However, the current file only contains DEBUG = False # Don't turn DEBUG on in production!

If you replace the config panel line by bind = "DEBUG:/opt/yunohost/__APP__/local_settings.py", it will find the right line but output ERROR Pick a valid value for argument 'django_debug': 'false # don't turn debug on in production!' is not among the available choices (yes/no).

Do not put comments on the same line

To avoid parsing issues, put # don't turn debug on in production!) on another line in local_settings.py.

Profit.

N.B. It seems it sets the True and False values as strings. I do not know if Django accepts that. If needed, you will need to write your own getter/setter or propose a core change to fix that.

Looks like a small PEBCAK and a strong “we really need proper documentation on that”.
Reference: YunoHost/yunohost#987

Very strange, i think!

I reply on github: Config panel question 'foo_bar' should be initialized with a value during install or upgrade. · Issue #2015 · YunoHost/issues · GitHub

I started a second one with success in GitHub - YunoHost-Apps/django_example_ynh: Demo YunoHost Application to demonstrate the integration of a Django project under YunoHost. project:

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"

and conf/settings.py looks like:

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

Now I understand that it is important to name the variable DEBUG_ENABLED exactly like the name in the placeholder '__DEBUG_ENABLED__'

This DEBUG = bool(int(DEBUG_ENABLED)) looks a little bit strange, but the goals are:

  • The script is has no syntax error, even if the replacement was not done
  • int() will crash (“fail fast”) if replacement was not done :wink:
  • "1" and "0" are a better choices as the strings "true" and "false", because second ones looks so similar to the real True and False objects :slight_smile:

Now it looks like: