mikeengland / django-setty Goto Github PK
View Code? Open in Web Editor NEWConfigure settings dynamically in the Django Admin console
License: MIT License
Configure settings dynamically in the Django Admin console
License: MIT License
I use DB backend
I create setting:
name="test"
type="integer"
value=42
it displays in admin list ok: "test=42"
I open edit/change: and I see "value" field empty -- not a tragedy (as representation shows "test=42") but still weirdness...
screenshot: http://pix.toile-libre.org/upload/original/1553088164.png
Py 3.5
Django 1.11.7
Re-opening a setting that is a list of strings converts strings to single quotes. This is invalid JSON and causes the field to fail to validate. The list of strings should open up in the input dialog with double quotes.
pip install setty
ERROR: Could not find a version that satisfies the requirement setty (from versions: none)
ERROR: No matching distribution found for setty
$ 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
The build is failing due to not being able to find the correct Python interpreter.
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).
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)
???
We would have to infer the type from the value that is passed, and add validation to ensure that only certain types can be added as settings.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.