Skip to content

Commit 7543e87

Browse files
authored
fix: Grid Container get_short_description for Nested Tuples Setting (#145)
1 parent 4b5d248 commit 7543e87

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

README.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,13 @@ for now only the following can be changed::
135135

136136
DJANGOCMS_BOOTSTRAP4_GRID_SIZE = 12
137137
DJANGOCMS_BOOTSTRAP4_GRID_CONTAINERS = (
138-
('container', _('Container')),
139-
('container-fluid', _('Fluid container')),
138+
(_('Default'), (
139+
('container', _('Container')),
140+
('container-fluid', _('Fluid container')),
141+
)),
142+
(_('Custom'), (
143+
('container-yours', _('Your container')),
144+
)),
140145
)
141146
DJANGOCMS_BOOTSTRAP4_GRID_COLUMN_CHOICES = (
142147
('col', _('Column')),

djangocms_bootstrap4/contrib/bootstrap4_grid/models.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Bootstrap4GridContainer(CMSPlugin):
2424
container_type = models.CharField(
2525
verbose_name=_('Container type'),
2626
choices=GRID_CONTAINER_CHOICES,
27-
default=GRID_CONTAINER_CHOICES[0][0],
27+
default=get_first_choice(GRID_CONTAINER_CHOICES),
2828
max_length=255,
2929
help_text=mark_safe_lazy(_(
3030
'Defines if the grid should use fixed width (<code>.container</code>) '
@@ -38,11 +38,8 @@ def __str__(self):
3838
return str(self.pk)
3939

4040
def get_short_description(self):
41-
text = ''
42-
for item in GRID_CONTAINER_CHOICES:
43-
if item[0] == self.container_type:
44-
text = item[1]
45-
return f'({text})'
41+
choice = get_choices_match(GRID_CONTAINER_CHOICES, self.container_type)
42+
return f'({choice})'
4643

4744

4845
class Bootstrap4GridRow(CMSPlugin):

djangocms_bootstrap4/helpers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ def get_plugin_template(instance, prefix, name, templates):
3232
# otherwise they will not be added to /locale/
3333
# https://docs.djangoproject.com/en/1.11/topics/i18n/translation/#other-uses-of-lazy-in-delayed-translations
3434
mark_safe_lazy = lazy(mark_safe, str)
35+
36+
37+
# get first element of "choices" (can be nested)
38+
def get_first_choice(choices):
39+
for value, verbose in choices:
40+
if not isinstance(verbose, (tuple, list)):
41+
return value
42+
else:
43+
first = get_first_choice(verbose)
44+
if first is not None:
45+
return first
46+
return None
47+
48+
49+
# get verbose text of element matching given value in "choices" (can be nested)
50+
def get_choices_match(choices, value_to_match):
51+
for value, verbose in choices:
52+
if not isinstance(verbose, (tuple, list)):
53+
if value == value_to_match:
54+
return verbose
55+
else:
56+
match = get_choices_match(verbose, value_to_match)
57+
if match is not None:
58+
return match
59+
return None

0 commit comments

Comments
 (0)