Skip to content

Commit

Permalink
fix: Grid Container get_short_description for Nested Tuples Setting (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleyboar authored Sep 1, 2022
1 parent 4b5d248 commit 7543e87
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
9 changes: 7 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ for now only the following can be changed::

DJANGOCMS_BOOTSTRAP4_GRID_SIZE = 12
DJANGOCMS_BOOTSTRAP4_GRID_CONTAINERS = (
('container', _('Container')),
('container-fluid', _('Fluid container')),
(_('Default'), (
('container', _('Container')),
('container-fluid', _('Fluid container')),
)),
(_('Custom'), (
('container-yours', _('Your container')),
)),
)
DJANGOCMS_BOOTSTRAP4_GRID_COLUMN_CHOICES = (
('col', _('Column')),
Expand Down
9 changes: 3 additions & 6 deletions djangocms_bootstrap4/contrib/bootstrap4_grid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Bootstrap4GridContainer(CMSPlugin):
container_type = models.CharField(
verbose_name=_('Container type'),
choices=GRID_CONTAINER_CHOICES,
default=GRID_CONTAINER_CHOICES[0][0],
default=get_first_choice(GRID_CONTAINER_CHOICES),
max_length=255,
help_text=mark_safe_lazy(_(
'Defines if the grid should use fixed width (<code>.container</code>) '
Expand All @@ -38,11 +38,8 @@ def __str__(self):
return str(self.pk)

def get_short_description(self):
text = ''
for item in GRID_CONTAINER_CHOICES:
if item[0] == self.container_type:
text = item[1]
return f'({text})'
choice = get_choices_match(GRID_CONTAINER_CHOICES, self.container_type)
return f'({choice})'


class Bootstrap4GridRow(CMSPlugin):
Expand Down
25 changes: 25 additions & 0 deletions djangocms_bootstrap4/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,28 @@ def get_plugin_template(instance, prefix, name, templates):
# otherwise they will not be added to /locale/
# https://docs.djangoproject.com/en/1.11/topics/i18n/translation/#other-uses-of-lazy-in-delayed-translations
mark_safe_lazy = lazy(mark_safe, str)


# get first element of "choices" (can be nested)
def get_first_choice(choices):
for value, verbose in choices:
if not isinstance(verbose, (tuple, list)):
return value
else:
first = get_first_choice(verbose)
if first is not None:
return first
return None


# get verbose text of element matching given value in "choices" (can be nested)
def get_choices_match(choices, value_to_match):
for value, verbose in choices:
if not isinstance(verbose, (tuple, list)):
if value == value_to_match:
return verbose
else:
match = get_choices_match(verbose, value_to_match)
if match is not None:
return match
return None

0 comments on commit 7543e87

Please sign in to comment.