Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new feature #12

Open
kocunop opened this issue Feb 13, 2020 · 4 comments
Open

add new feature #12

kocunop opened this issue Feb 13, 2020 · 4 comments

Comments

@kocunop
Copy link
Contributor

kocunop commented Feb 13, 2020

Hello!

I wrote manage.py command handler to setty based on default_settings.py file on application dir.
If the file exist, handler import that file and invite to input variable value and save into DB.

If this functionality is needed, I can prepare pull request.

Full code:

from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from setty import models

TYPES_MAPPING = {
    'str': 'string',
    'bool': 'bool',
    'dict': 'dict',
    'float': 'float',
    'int': 'integer',
    'list': 'list',
}


class Command(BaseCommand):
    help = 'Initial site settings'

    def handle(self, *args, **options):
        if not 'setty' in settings.INSTALLED_APPS:
            print('Setty not in INSTALLED_APPS')
            return

        for app_name in settings.INSTALLED_APPS:
            try:
                _app = __import__(f'{app_name}.default_settings')
            except ModuleNotFoundError:
                continue
            try:
                _app_settings = _app.default_settings
            except AttributeError:
                continue

            default_settings = [s for s in dir(_app_settings) if not '__' in s]

            print(f'Settings for {app_name}')
            for setting in default_settings:
                default = getattr(_app_settings, setting)
                value = None
                while not value:
                    value = input(f'{setting} [{default}]: ')
                    if value == '':
                        value = default
                    if not type(value) == type(default):
                        print(f'Incorrect type of {setting}. Received {type(value)}, but expected {type(default)}')
                        value = None

                _setting = models.SettySettings.objects.filter(name=setting, app_name=app_name).first() \
                           or models.SettySettings()
                _setting.name = setting
                _setting.app_name = app_name
                _setting.type = TYPES_MAPPING[type(value).__name__]
                _setting.value = value

                _setting.save()

        print('Good job!')

It would be nice to remake choices in TypeChoices into standard python types names ('string' -> 'str' and 'integer' -> 'int').

And i have working templatetag & template to map Setty variable into JS variable.
Templatetag code:

@register.simple_tag
def setty_get_app_settings(request):
    app_name = resolve(request.path).app_name
    if app_name:
        return config.get_for_app(app_name)

Template code:

<!-- Load settings -->
<script>
    {% for s in setty_current_app %}
        {% if s.type == 'string' %}
            var {{ s.name }} = '{{ s.value }}';
        {% else %}
            var {{ s.name }} = {{ s.value }};
        {% endif %}
    {% endfor %}
</script>

P.S. In plans to add a property that will determine where this setting is needed (JS/Django).

@mikeengland
Copy link
Owner

I do like the idea of providing default values to aid with adding new settings. That is definitely a shortcoming of the library at the moment.

Django constance allows users to specify these default settings in the django settings file. I am not sure of the best approach for this at the moment - whether to expect a setty_settings.py file in an app or look for a specific setting name in the project settings file.

@kocunop
Copy link
Contributor Author

kocunop commented May 5, 2020

Django constance allows users to specify these default settings in the django settings file.

But what if some applications need to be disabled? And this approach allows you to store the settings in the application.
In my case, it should be possible to build a project with an arbitrary set of applications.

expect a setty_settings.py file in an app

app_settings or default_settings, may be setty_default_settings ... need to discuss

look for a specific setting name

its not good idea, but import * from setty_settings even worse

And its default settings only to command python manage.py init_settings.

P.S. Sorry for the long silence, I became dady =D And I'll be back soon.

P.S.S. Setty is very actually for my work.

@kocunop
Copy link
Contributor Author

kocunop commented May 5, 2020

may be setting filename and setting name specify in django settings

@mikeengland
Copy link
Owner

Hi - congratulations :)

I think having a setty_settings.py file in an app is acceptable for default settings. I also think that we should autodiscover all of these files on load to ensure that the defaults are picked up. If it is left up to a management command the database could get out of sync with the contents of the file.

I'll try and take a look at this soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants