mirror of
https://github.com/jazzband/django-constance.git
synced 2026-03-16 22:40:24 +00:00
Merge pull request #155 from farooqaaa/master
Added support for fieldsets and fixed inheritance issue.
This commit is contained in:
commit
f3d68a2496
6 changed files with 104 additions and 48 deletions
|
|
@ -85,9 +85,9 @@ class ConstanceForm(forms.Form):
|
|||
for name, options in settings.CONFIG.items():
|
||||
default, help_text = options[0], options[1]
|
||||
if len(options) == 3:
|
||||
config_type = options[2]
|
||||
config_type = options[2]
|
||||
else:
|
||||
config_type = type(default)
|
||||
config_type = type(default)
|
||||
|
||||
if config_type not in FIELDS:
|
||||
raise ImproperlyConfigured(_("Constance doesn't support "
|
||||
|
|
@ -134,6 +134,24 @@ class ConstanceAdmin(admin.ModelAdmin):
|
|||
name='%s_%s_add' % info),
|
||||
]
|
||||
|
||||
def get_config_value(self, name, options, form, initial):
|
||||
default, help_text = options[0], options[1]
|
||||
# First try to load the value from the actual backend
|
||||
value = initial.get(name)
|
||||
# Then if the returned value is None, get the default
|
||||
if value is None:
|
||||
value = getattr(config, name)
|
||||
config_value = {
|
||||
'name': name,
|
||||
'default': localize(default),
|
||||
'help_text': _(help_text),
|
||||
'value': localize(value),
|
||||
'modified': value != default,
|
||||
'form_field': form[name],
|
||||
}
|
||||
|
||||
return config_value
|
||||
|
||||
@csrf_protect_m
|
||||
def changelist_view(self, request, extra_context=None):
|
||||
# First load a mapping between config name and default value
|
||||
|
|
@ -160,26 +178,30 @@ class ConstanceAdmin(admin.ModelAdmin):
|
|||
'config_values': [],
|
||||
'title': _('Constance config'),
|
||||
'app_label': 'constance',
|
||||
'opts': Config._meta,
|
||||
'opts': self.model._meta,
|
||||
'form': form,
|
||||
'media': self.media + form.media,
|
||||
'icon_type': 'gif' if VERSION < (1, 9) else 'svg',
|
||||
}
|
||||
for name, options in settings.CONFIG.items():
|
||||
default, help_text = options[0], options[1]
|
||||
# First try to load the value from the actual backend
|
||||
value = initial.get(name)
|
||||
# Then if the returned value is None, get the default
|
||||
if value is None:
|
||||
value = getattr(config, name)
|
||||
context['config_values'].append({
|
||||
'name': name,
|
||||
'default': localize(default),
|
||||
'help_text': _(help_text),
|
||||
'value': localize(value),
|
||||
'modified': value != default,
|
||||
'form_field': form[name],
|
||||
})
|
||||
context['config_values'].append(self.get_config_value(name, options, form, initial))
|
||||
|
||||
if settings.CONFIG_FIELDSETS:
|
||||
context['fieldsets'] = []
|
||||
for fieldset_title, fields_list in settings.CONFIG_FIELDSETS.items():
|
||||
fields_exist = all(field in settings.CONFIG.keys() for field in fields_list)
|
||||
assert fields_exist, "CONSTANCE_CONFIG_FIELDSETS contains fields that does not exist"
|
||||
config_values = []
|
||||
|
||||
for name, options in settings.CONFIG.items():
|
||||
if name in fields_list:
|
||||
config_values.append(self.get_config_value(name, options, form, initial))
|
||||
|
||||
context['fieldsets'].append({
|
||||
'title': fieldset_title,
|
||||
'config_values': config_values
|
||||
})
|
||||
|
||||
context['config_values'].sort(key=itemgetter('name'))
|
||||
request.current_app = self.admin_site.name
|
||||
# compatibility to be removed when 1.7 is deprecated
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ BACKEND = getattr(settings, 'CONSTANCE_BACKEND',
|
|||
|
||||
CONFIG = getattr(settings, 'CONSTANCE_CONFIG', {})
|
||||
|
||||
CONFIG_FIELDSETS = getattr(settings, 'CONSTANCE_CONFIG_FIELDSETS', {})
|
||||
|
||||
ADDITIONAL_FIELDS = getattr(settings, 'CONSTANCE_ADDITIONAL_FIELDS', {})
|
||||
|
||||
DATABASE_CACHE_BACKEND = getattr(settings, 'CONSTANCE_DATABASE_CACHE_BACKEND',
|
||||
|
|
|
|||
|
|
@ -51,37 +51,18 @@
|
|||
{% if form.errors %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
<table cellspacing="0" id="result_list">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><div class="text">{% trans "Name" %}</div></th>
|
||||
<th><div class="text">{% trans "Default" %}</div></th>
|
||||
<th><div class="text">{% trans "Value" %}</div></th>
|
||||
<th><div class="text">{% trans "Is modified" %}</div></th>
|
||||
</tr>
|
||||
</thead>
|
||||
{% for item in config_values %}
|
||||
<tr class="{% cycle 'row1' 'row2' %}">
|
||||
<th>{{ item.name }}
|
||||
<div class="help">{{ item.help_text|linebreaksbr }}</div>
|
||||
</th>
|
||||
<td>
|
||||
{{ item.default }}
|
||||
</td>
|
||||
<td>
|
||||
{{ item.form_field.errors }}
|
||||
{{ item.form_field }}
|
||||
</td>
|
||||
<td>
|
||||
{% if item.modified %}
|
||||
<img src="{% static 'admin/img/icon-yes.'|add:icon_type %}" alt="{{ item.modified }}" />
|
||||
{% else %}
|
||||
<img src="{% static 'admin/img/icon-no.'|add:icon_type %}" alt="{{ item.modified }}" />
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
||||
{% if fieldsets %}
|
||||
{% for fieldset in fieldsets %}
|
||||
<h3>{{ fieldset.title }}</h3>
|
||||
{% with config_values=fieldset.config_values %}
|
||||
{% include "admin/constance/includes/results_list.html" %}
|
||||
{% endwith %}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
{% include "admin/constance/includes/results_list.html" %}
|
||||
{% endif %}
|
||||
|
||||
<p class="paginator">
|
||||
<input type="submit" name="_save" class="default" value="{% trans 'Save' %}"/>
|
||||
</p>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
{% load admin_static admin_list i18n %}
|
||||
<table cellspacing="0" id="result_list">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><div class="text">{% trans "Name" %}</div></th>
|
||||
<th><div class="text">{% trans "Default" %}</div></th>
|
||||
<th><div class="text">{% trans "Value" %}</div></th>
|
||||
<th><div class="text">{% trans "Is modified" %}</div></th>
|
||||
</tr>
|
||||
</thead>
|
||||
{% for item in config_values %}
|
||||
<tr class="{% cycle 'row1' 'row2' %}">
|
||||
<th>{{ item.name }}
|
||||
<div class="help">{{ item.help_text|linebreaksbr }}</div>
|
||||
</th>
|
||||
<td>
|
||||
{{ item.default }}
|
||||
</td>
|
||||
<td>
|
||||
{{ item.form_field.errors }}
|
||||
{{ item.form_field }}
|
||||
</td>
|
||||
<td>
|
||||
{% if item.modified %}
|
||||
<img src="{% static 'admin/img/icon-yes.'|add:icon_type %}" alt="{{ item.modified }}" />
|
||||
{% else %}
|
||||
<img src="{% static 'admin/img/icon-no.'|add:icon_type %}" alt="{{ item.modified }}" />
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
|
@ -118,6 +118,25 @@ Note: Use later evaluated strings instead of direct classes for the field and wi
|
|||
'MY_SELECT_KEY': ('yes', 'select yes or no', 'yes_no_null_select'),
|
||||
}
|
||||
|
||||
Fieldsets
|
||||
-------------
|
||||
|
||||
To group settings together you can define fieldsets. Here's an example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
CONSTANCE_CONFIG = {
|
||||
'SITE_NAME': ('My Title', 'Website title'),
|
||||
'SITE_DESCRIPTION': ('', 'Website description'),
|
||||
'THEME': ('light-blue', 'Website theme'),
|
||||
}
|
||||
|
||||
CONSTANCE_CONFIG_FIELDSETS = {
|
||||
'General Options': ('SITE_NAME', 'SITE_DESCRIPTION'),
|
||||
'Theme Options': ('THEME',),
|
||||
}
|
||||
.. image:: screenshot3.png
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
|
|
|
|||
BIN
docs/screenshot3.png
Normal file
BIN
docs/screenshot3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
Loading…
Reference in a new issue