GithubHelp home page GithubHelp logo

mikeengland / django-setty Goto Github PK

View Code? Open in Web Editor NEW
11.0 11.0 2.0 55 KB

Configure settings dynamically in the Django Admin console

License: MIT License

Python 100.00%
admin cache configuration database django django-setty dynamic live settings setty

django-setty's People

Contributors

dependabot[bot] avatar jayvdb avatar kocunop avatar mikeengland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

kocunop jayvdb

django-setty's Issues

Remove update value in getter

Hello!

backend.py

def _retrieve_and_cache_setting(self, name):
        value = super().get(name)
        return self.set(name, value)

No need to update the value in getter.

return self.set(name, value) -> return self.set_in_cache(name, value)???

Fix travis build

The build is failing due to not being able to find the correct Python interpreter.

pip install doesn't provide migration..

$ pip install django-setty
Successfully installed django-picklefield-2.0 django-setty-2.0.0 python-memcached-1.59

$ python manage.py migrate setty
CommandError: App 'setty' does not have migrations. -- why -- I see it on github?

I had to create it myself, which might be problem on production

$ python manage.py makemigrations setty

Migrations for 'setty':
  /home/vagrant/tableair-cloud/ta_env/lib/python3.5/site-packages/setty/migrations/0001_initial.py
    - Create model SettySettings

Py 3.5
Django 1.11.7

pip install error

pip install setty
ERROR: Could not find a version that satisfies the requirement setty (from versions: none)
ERROR: No matching distribution found for setty

add new feature

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).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.