From b26ec64edb68d09d7eb44a2fa8e1bad6c3be1426 Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Wed, 30 Aug 2023 13:41:06 +0200 Subject: [PATCH] Add custom django CustomNotificationAdmin --- compose/django/Dockerfile | 5 ++-- server/vbv_lernwelt/notify/admin.py | 26 +++++++++++++++++++ server/vbv_lernwelt/notify/apps.py | 14 ++++++++++ .../vbv_lernwelt/notify/tests/test_service.py | 3 ++- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 server/vbv_lernwelt/notify/admin.py diff --git a/compose/django/Dockerfile b/compose/django/Dockerfile index 0bdccfde..8c411fa4 100644 --- a/compose/django/Dockerfile +++ b/compose/django/Dockerfile @@ -120,11 +120,10 @@ RUN chown django:django ${APP_HOME} USER django +# pip install under the user django, the scripts will be in /home/django/.local/bin RUN pip install supervisor -RUN exit +COPY ./compose/django/supervisord.conf /app/supervisord.conf EXPOSE 7555 -COPY ./compose/django/supervisord.conf /app/supervisord.conf - ENTRYPOINT ["/entrypoint"] diff --git a/server/vbv_lernwelt/notify/admin.py b/server/vbv_lernwelt/notify/admin.py new file mode 100644 index 00000000..0f667d3c --- /dev/null +++ b/server/vbv_lernwelt/notify/admin.py @@ -0,0 +1,26 @@ +from vbv_lernwelt.core.admin import LogAdmin + + +# admin.register in apps.py +class CustomNotificationAdmin(LogAdmin): + date_hierarchy = "timestamp" + raw_id_fields = ("recipient",) + list_display = ( + "recipient", + "actor", + "notification_type", + "emailed", + "unread", + ) + list_filter = ( + "notification_type", + "emailed", + "unread", + "timestamp", + "level", + "public", + ) + + def get_queryset(self, request): + qs = super(CustomNotificationAdmin, self).get_queryset(request) + return qs.prefetch_related("actor") diff --git a/server/vbv_lernwelt/notify/apps.py b/server/vbv_lernwelt/notify/apps.py index 59aae618..8fd3f8d0 100644 --- a/server/vbv_lernwelt/notify/apps.py +++ b/server/vbv_lernwelt/notify/apps.py @@ -1,6 +1,20 @@ from django.apps import AppConfig +from django.contrib import admin class NotifyConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "vbv_lernwelt.notify" + + def ready(self): + # Move the admin import here to avoid early imports + from .admin import CustomNotificationAdmin + + # Unregister the default Notification admin if it exists + from vbv_lernwelt.notify.models import Notification + + if admin.site.is_registered(Notification): + admin.site.unregister(Notification) + + # Register the custom admin + admin.site.register(Notification, CustomNotificationAdmin) diff --git a/server/vbv_lernwelt/notify/tests/test_service.py b/server/vbv_lernwelt/notify/tests/test_service.py index 15dbac49..2ec5500c 100644 --- a/server/vbv_lernwelt/notify/tests/test_service.py +++ b/server/vbv_lernwelt/notify/tests/test_service.py @@ -125,7 +125,8 @@ class TestNotificationService(TestCase): self.assertEqual(target_url, notification.target_url) self.assertEqual(course, notification.course) self.assertEqual( - str(NotificationType.USER_INTERACTION), notification.notification_type + str(NotificationType.USER_INTERACTION), + notification.notification_type, ) self.assertTrue(notification.emailed)