Skip to content

Commit

Permalink
[#180] Update admin.py and models.py
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmursa-dev committed Dec 24, 2024
1 parent a8bcb73 commit 00a1af3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/nrc/datamodel/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from requests.exceptions import RequestException
from rest_framework.exceptions import ValidationError
from rest_framework.fields import DateTimeField

from nrc.api.utils import send_notification
from nrc.api.validators import CallbackURLValidator
Expand Down Expand Up @@ -196,15 +195,24 @@ class FilterGroup(admin.ModelAdmin):

@admin.register(NotificatieResponse)
class NotificatieResponseAdmin(admin.ModelAdmin):
list_display = ("notificatie", "abonnement", "get_result_display")

list_display = (
"notificatie",
"get_notificatie_created_date",
"abonnement",
"get_result_display",
)
raw_id_fields = ["notificatie", "abonnement"]
list_filter = ("abonnement", "response_status")
search_fields = ("abonnement",)

@admin.display(description=_("result"))
def get_result_display(self, obj):
return obj.response_status or obj.exception

@admin.display(description=_("Notificatie created date"))
def get_notificatie_created_date(self, obj):
return obj.notificatie.created_date


class NotificatieResponseInline(admin.TabularInline):
model = NotificatieResponse
Expand Down Expand Up @@ -284,14 +292,6 @@ def action(self, obj):
def resource(self, obj):
return obj.forwarded_msg.get("resource")

@admin.display(description=_("Created date"))
def created_date(self, obj):
aanmaakdatum = obj.forwarded_msg.get("aanmaakdatum")
if not aanmaakdatum:
return None

return DateTimeField().to_internal_value(aanmaakdatum)

def get_inline_instances(self, request, obj=None):
# Hide the NotificatieResponseInline when creating a Notification
if obj is None:
Expand Down
9 changes: 9 additions & 0 deletions src/nrc/datamodel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.utils.translation import gettext_lazy as _

from djangorestframework_camel_case.util import camelize
from rest_framework.fields import DateTimeField


class Kanaal(models.Model):
Expand Down Expand Up @@ -153,6 +154,14 @@ def last_attempt(self):
self.notificatieresponse_set.aggregate(Max("attempt"))["attempt__max"] or 0
)

@property
def created_date(self):
aanmaakdatum = self.forwarded_msg.get("aanmaakdatum")
if not aanmaakdatum:
return None

return DateTimeField().to_internal_value(aanmaakdatum)

def __str__(self) -> str:
return f"Notificatie ({self.kanaal})"

Expand Down

0 comments on commit 00a1af3

Please sign in to comment.