Skip to content

Commit

Permalink
✨ [open-zaak/open-notificaties#156] Pass request as kwarg to Kanaal.g…
Browse files Browse the repository at this point in the history
…et_kenmerken

and add hook for `_get_field` to support nested attributes in Open Zaak
  • Loading branch information
stevenbal committed Sep 20, 2024
1 parent 7abd32e commit 81bacce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
29 changes: 22 additions & 7 deletions notifications_api_common/kanalen.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
"""
Provide notifications kanaal/exchange classes.
"""

from collections import defaultdict
from typing import Dict, Tuple
from typing import Dict, Union

from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
from django.db.models import Model
from django.db.models.base import ModelBase
from django.db.models import Field, Model

from rest_framework.request import Request

KANAAL_REGISTRY = set()


class Kanaal:
def __init__(self, label: str, main_resource: ModelBase, kenmerken: Tuple = None):
def __init__(
self, label: str, main_resource: Model, kenmerken: Union[tuple, None] = None
):
self.label = label
self.main_resource = main_resource

Expand All @@ -22,7 +26,7 @@ def __init__(self, label: str, main_resource: ModelBase, kenmerken: Tuple = None
self.kenmerken = kenmerken or ()
for kenmerk in self.kenmerken:
try:
self.main_resource._meta.get_field(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 @@ -38,7 +42,18 @@ def __repr__(self):
self.main_resource,
)

def get_kenmerken(self, obj: Model, data: Dict = None) -> Dict:
def _get_field(self, model: Model, field: str) -> Field:
"""
Function to retrieve a field from a Model
"""
return model._meta.get_field(field)

def get_kenmerken(
self,
obj: Model,
data: dict | None = None,
request: Request | None = None, # noqa
) -> Dict:
data = data or {}
return {
kenmerk: data.get(kenmerk, getattr(obj, kenmerk))
Expand All @@ -54,7 +69,7 @@ def description(self):
kenmerken = [
kenmerk_template.format(
kenmerk=kenmerk,
help_text=self.main_resource._meta.get_field(kenmerk).help_text,
help_text=self._get_field(self.main_resource, kenmerk).help_text,
)
for kenmerk in self.kenmerken
]
Expand Down
4 changes: 3 additions & 1 deletion notifications_api_common/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def construct_message(self, data: dict, instance: models.Model = None) -> dict:
"actie": self.action,
"aanmaakdatum": timezone.now(),
# each channel knows which kenmerken it has, so delegate this
"kenmerken": kanaal.get_kenmerken(main_object, main_object_data),
"kenmerken": kanaal.get_kenmerken(
main_object, main_object_data, request=self.request
),
}

# let the serializer & render machinery shape the data the way it
Expand Down

0 comments on commit 81bacce

Please sign in to comment.