From a878cc4900391127b14bb30376c4c0ea5ad9c970 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Fri, 20 Sep 2024 16:39:49 +0200 Subject: [PATCH] :sparkles: [open-zaak/open-notificaties#156] Allow custom help_texts for Kanaal kenmerken --- notifications_api_common/kanalen.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/notifications_api_common/kanalen.py b/notifications_api_common/kanalen.py index 94a52fa..f61afc2 100644 --- a/notifications_api_common/kanalen.py +++ b/notifications_api_common/kanalen.py @@ -15,10 +15,15 @@ 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 @@ -26,7 +31,7 @@ def __init__( 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}" @@ -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, @@ -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 ]