Skip to content

Commit

Permalink
✨ [open-zaak/open-notificaties#156] Allow custom help_texts for Kanaa…
Browse files Browse the repository at this point in the history
…l kenmerken
  • Loading branch information
stevenbal committed Sep 20, 2024
1 parent 17ff810 commit a878cc4
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions notifications_api_common/kanalen.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@

class Kanaal:
def __init__(
self, label: str, main_resource: Model, kenmerken: Union[Tuple, None] = None
self,
label: str,
main_resource: Model,
kenmerken: Union[Tuple, None] = None,
extra_kwargs: Union[Dict, None] = None,
):
self.label = label
self.main_resource = main_resource
self.extra_kwargs = extra_kwargs or {}

self.usage = defaultdict(list) # filled in by metaclass of notifications

# check that we're refering to existing fields
self.kenmerken = kenmerken or ()
for kenmerk in self.kenmerken:
try:
self._get_field(self.main_resource, kenmerk)
self.get_field(self.main_resource, kenmerk)
except FieldDoesNotExist as exc:
raise ImproperlyConfigured(
f"Kenmerk '{kenmerk}' does not exist on the model {main_resource}"
Expand All @@ -42,12 +47,22 @@ def __repr__(self):
self.main_resource,
)

def _get_field(self, model: Model, field: str) -> Field:
@staticmethod
def get_field(model: Model, field: str) -> Field:
"""
Function to retrieve a field from a Model
"""
return model._meta.get_field(field)

def get_help_text(self, field: Field, kenmerk: str) -> str:
"""
Retrieve the help_text for a kenmerk, pulled from the model field by default,
but can be overridden by setting extra_kwargs on `Kanaal.__init__`
"""
if help_text := self.extra_kwargs.get(kenmerk, {}).get("help_text"):
return help_text
return field.help_text

def get_kenmerken(
self,
obj: Model,
Expand All @@ -69,7 +84,9 @@ def description(self):
kenmerken = [
kenmerk_template.format(
kenmerk=kenmerk,
help_text=self._get_field(self.main_resource, kenmerk).help_text,
help_text=self.get_help_text(
self.get_field(self.main_resource, kenmerk), kenmerk
),
)
for kenmerk in self.kenmerken
]
Expand Down

0 comments on commit a878cc4

Please sign in to comment.