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 support for using gettext in fieldset headers #489

Merged
merged 4 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions constance/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,13 @@ def changelist_view(self, request, extra_context=None):
)

if settings.CONFIG_FIELDSETS:
if isinstance(settings.CONFIG_FIELDSETS, dict):
fieldset_items = settings.CONFIG_FIELDSETS.items()
else:
fieldset_items = settings.CONFIG_FIELDSETS

context['fieldsets'] = []
for fieldset_title, fieldset_data in settings.CONFIG_FIELDSETS.items():
for fieldset_title, fieldset_data in fieldset_items:
if type(fieldset_data) == dict:
fields_list = fieldset_data['fields']
collapse = fieldset_data.get('collapse', False)
Expand Down Expand Up @@ -307,7 +312,7 @@ def changelist_view(self, request, extra_context=None):
if collapse:
fieldset_context['collapse'] = True
context['fieldsets'].append(fieldset_context)
if not isinstance(settings.CONFIG_FIELDSETS, OrderedDict):
if not isinstance(settings.CONFIG_FIELDSETS, (OrderedDict, tuple)):
context['fieldsets'].sort(key=itemgetter('title'))

if not isinstance(settings.CONFIG, OrderedDict):
Expand Down
7 changes: 6 additions & 1 deletion constance/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ def get_inconsistent_fieldnames():
"""
from . import settings

if isinstance(settings.CONFIG_FIELDSETS, dict):
fieldset_items = settings.CONFIG_FIELDSETS.items()
else:
fieldset_items = settings.CONFIG_FIELDSETS

field_name_list = []
for fieldset_title, fields_list in settings.CONFIG_FIELDSETS.items():
for fieldset_title, fields_list in fieldset_items:
# fields_list can be a dictionary, when a fieldset is defined as collapsible
# https://django-constance.readthedocs.io/en/latest/#fieldsets-collapsing
if isinstance(fields_list, dict) and 'fields' in fields_list:
Expand Down
30 changes: 30 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,36 @@ To make some fieldsets collapsing you can use new format in CONSTANCE_CONFIG_FIE
'Theme Options': ('THEME',),
}

Field internationalization
--------------------------

Field description and fieldset headers can be integrated into Django's
internationalization using the ``gettext_lazy`` function. Note that the
``CONSTANCE_CONFIG_FIELDSETS`` must be converted to a tuple instead of dict
as it is not possible to have lazy proxy objects as dictionary keys in the
settings file. Example:

.. code-block:: python

from django.utils.translation import gettext_lazy as _

CONSTANCE_CONFIG = {
'SITE_NAME': ('My Title', _('Website title')),
'SITE_DESCRIPTION': ('', _('Website description')),
'THEME': ('light-blue', _('Website theme')),
}

CONSTANCE_CONFIG_FIELDSETS = (
(
_('General Options'),
{
'fields': ('SITE_NAME', 'SITE_DESCRIPTION'),
'collapse': True,
},
),
(_('Theme Options'), ('THEME',)),
)

Usage
-----

Expand Down
12 changes: 12 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def test_fieldset_headers(self):
self.assertContains(response, '<h2>Numbers</h2>')
self.assertContains(response, '<h2>Text</h2>')

@mock.patch('constance.settings.CONFIG_FIELDSETS', (
('Numbers', ('INT_VALUE',)),
('Text', ('STRING_VALUE',)),
))
def test_fieldset_tuple(self):
self.client.login(username='admin', password='nimda')
request = self.rf.get('/admin/constance/config/')
request.user = self.superuser
response = self.options.changelist_view(request, {})
self.assertContains(response, '<h2>Numbers</h2>')
self.assertContains(response, '<h2>Text</h2>')

@mock.patch('constance.settings.CONFIG_FIELDSETS', {
'Numbers': {
'fields': ('INT_VALUE', 'DECIMAL_VALUE',),
Expand Down