From b01ac219a3c89be01fa1bb214db326f3567f9e1e Mon Sep 17 00:00:00 2001 From: moanos Date: Sat, 12 Jul 2025 17:43:40 +0200 Subject: [PATCH] feat: Add notification partials including new mapping system for templates --- src/fellchensammlung/models.py | 18 +++--- .../notifications/body-an-deactivated.html | 5 ++ .../notifications/body-an-for-search.html | 5 ++ .../notifications/body-an-to-be-checked.html | 5 ++ .../notifications/body-new-comment.html | 13 +++++ .../notifications/body-new-report.html | 13 +++++ .../partials/notifications/body-new-user.html | 5 ++ .../partials/partial-notification.html | 2 +- src/fellchensammlung/tools/model_helpers.py | 57 +++++++++++++++++++ src/fellchensammlung/tools/notifications.py | 9 ++- 10 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-deactivated.html create mode 100644 src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-for-search.html create mode 100644 src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-to-be-checked.html create mode 100644 src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-comment.html create mode 100644 src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-report.html create mode 100644 src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-user.html create mode 100644 src/fellchensammlung/tools/model_helpers.py diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index cd01beb..c615d55 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -17,6 +17,8 @@ from .tools import misc, geo from notfellchen.settings import MEDIA_URL, base_url from .tools.geo import LocationProxy, Position from .tools.misc import age_as_hr_string, time_since_as_hr_string +from .tools.model_helpers import NotificationTypeChoices +from .tools.model_helpers import ndm as NotificationDisplayMapping class Language(models.Model): @@ -913,16 +915,6 @@ class Comment(models.Model): return self.adoption_notice.get_absolute_url() -class NotificationTypeChoices(models.TextChoices): - NEW_USER = "new_user", _("Useraccount wurde erstellt") - NEW_REPORT_AN = "new_report_an", _("Vermittlung wurde gemeldet") - NEW_REPORT_COMMENT = "new_report_comment", _("Kommentar wurde gemeldet") - AN_IS_TO_BE_CHECKED = "an_is_to_be_checked", _("Vermittlung muss überprüft werden") - AN_WAS_DEACTIVATED = "an_was_deactivated", _("Vermittlung wurde deaktiviert") - AN_FOR_SEARCH_FOUND = "an_for_search_found", _("Vermittlung für Suche gefunden") - NEW_COMMENT = "new_comment", _("Neuer Kommentar") - - class Notification(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) @@ -939,7 +931,8 @@ class Notification(models.Model): read = models.BooleanField(default=False) read_at = models.DateTimeField(blank=True, null=True, verbose_name=_("Gelesen am")) comment = models.ForeignKey(Comment, blank=True, null=True, on_delete=models.CASCADE, verbose_name=_('Antwort')) - adoption_notice = models.ForeignKey(AdoptionNotice, blank=True, null=True, on_delete=models.CASCADE, verbose_name=_('Vermittlung')) + adoption_notice = models.ForeignKey(AdoptionNotice, blank=True, null=True, on_delete=models.CASCADE, + verbose_name=_('Vermittlung')) user_related = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE, verbose_name=_('Verwandter Useraccount'), @@ -961,6 +954,9 @@ class Notification(models.Model): self.read_at = timezone.now() self.save() + def get_body_part(self): + return NotificationDisplayMapping[self.notification_type].web_partial + class Subscriptions(models.Model): owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('Nutzer*in')) diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-deactivated.html b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-deactivated.html new file mode 100644 index 0000000..a2bb21f --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-deactivated.html @@ -0,0 +1,5 @@ +{% load i18n %} +{% blocktranslate with adoption_notice_title=notification.adoption_notice.name %} + Die Vermittlung {{ adoption_notice_title }} wurde deaktiviert. +{% endblocktranslate %} +{% translate 'Vermittlung anzeigen' %} diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-for-search.html b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-for-search.html new file mode 100644 index 0000000..e0974e2 --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-for-search.html @@ -0,0 +1,5 @@ +{% load i18n %} +{% blocktranslate %} + Es wurde eine neue Vermittlung gefunden, die deinen Kriterien entspricht: +{% endblocktranslate %} +{{ notification.adoption_notice.name }} \ No newline at end of file diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-to-be-checked.html b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-to-be-checked.html new file mode 100644 index 0000000..e07f16d --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-an-to-be-checked.html @@ -0,0 +1,5 @@ +{% load i18n %} +{% blocktranslate with adoption_notice_title=notification.adoption_notice.name %} + Die Vermittlung {{ adoption_notice_title }} muss überprüft werden. +{% endblocktranslate %} +{% translate 'Vermittlung anzeigen' %} diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-comment.html b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-comment.html new file mode 100644 index 0000000..a61d40f --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-comment.html @@ -0,0 +1,13 @@ +{% load i18n %} +{% load custom_tags %} +

+ {% blocktranslate with adoption_notice_title=notification.adoption_notice.name %} + Folgender Kommentar wurde zur Vermittlung {{ adoption_notice_title }} hinzugefügt: + {% endblocktranslate %} +

+

+ {{ notification.comment.text | render_markdown }} +

+

+ {% translate 'Vermittlung anzeigen' %} +

\ No newline at end of file diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-report.html b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-report.html new file mode 100644 index 0000000..16e6646 --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-report.html @@ -0,0 +1,13 @@ +{% load i18n %} +

+{% blocktranslate %} + Es gibt eine neue Meldung. Folgende Nachricht wurde zur Meldung hinzugefügt: +{% endblocktranslate %} +

+

+ + {{ notification.report.user_comment }} + +

+ +{% translate 'Meldung anzeigen' %} \ No newline at end of file diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-user.html b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-user.html new file mode 100644 index 0000000..655a26a --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/partials/notifications/body-new-user.html @@ -0,0 +1,5 @@ +{% load i18n %} +{% blocktranslate %} + Es wurde ein neuer Useraccount erstellt: +{% endblocktranslate %} +{% translate 'User anzeigen' %} \ No newline at end of file diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/partial-notification.html b/src/fellchensammlung/templates/fellchensammlung/partials/partial-notification.html index 1179a5d..e3dd5a3 100644 --- a/src/fellchensammlung/templates/fellchensammlung/partials/partial-notification.html +++ b/src/fellchensammlung/templates/fellchensammlung/partials/partial-notification.html @@ -12,6 +12,6 @@ {{ notification.created_at|time_since_hr }}
- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + {% include notification.get_body_part %}
diff --git a/src/fellchensammlung/tools/model_helpers.py b/src/fellchensammlung/tools/model_helpers.py new file mode 100644 index 0000000..ca85485 --- /dev/null +++ b/src/fellchensammlung/tools/model_helpers.py @@ -0,0 +1,57 @@ +from django.utils.translation import gettext_lazy as _ +from django.db import models + +""" +Helpers that MUST NOT DEPEND ON MODELS to avoid circular imports +""" + + +class NotificationTypeChoices(models.TextChoices): + NEW_USER = "new_user", _("Useraccount wurde erstellt") + NEW_REPORT_AN = "new_report_an", _("Vermittlung wurde gemeldet") + NEW_REPORT_COMMENT = "new_report_comment", _("Kommentar wurde gemeldet") + AN_IS_TO_BE_CHECKED = "an_is_to_be_checked", _("Vermittlung muss überprüft werden") + AN_WAS_DEACTIVATED = "an_was_deactivated", _("Vermittlung wurde deaktiviert") + AN_FOR_SEARCH_FOUND = "an_for_search_found", _("Vermittlung für Suche gefunden") + NEW_COMMENT = "new_comment", _("Neuer Kommentar") + + +class NotificationDisplayMapping: + def __init__(self, email_html_template, email_plain_template, web_partial): + self.email_html_template = email_html_template + self.email_plain_template = email_plain_template + self.web_partial = web_partial + + +report_mapping = NotificationDisplayMapping( + email_html_template='fellchensammlung/mail/notifications/report.html', + email_plain_template='fellchensammlung/mail/notifications/report.txt', + web_partial="fellchensammlung/partials/notifications/body-new-report.html" +) +# ndm = notification display mapping +ndm = {NotificationTypeChoices.NEW_USER: NotificationDisplayMapping( + email_html_template='fellchensammlung/mail/notifications/report.html', + email_plain_template="fellchensammlung/mail/notifications/report.txt", + web_partial="fellchensammlung/partials/notifications/body-new-user.html"), + NotificationTypeChoices.NEW_COMMENT: NotificationDisplayMapping( + email_html_template='fellchensammlung/mail/notifications/new-comment.html', + email_plain_template='fellchensammlung/mail/notifications/new-comment.txt', + web_partial="fellchensammlung/partials/notifications/body-new-comment.html"), + NotificationTypeChoices.NEW_REPORT_AN: report_mapping, + NotificationTypeChoices.NEW_REPORT_COMMENT: report_mapping, + NotificationTypeChoices.AN_IS_TO_BE_CHECKED: NotificationDisplayMapping( + email_html_template='fellchensammlung/mail/notifications/an-to-be-checked.html', + email_plain_template='fellchensammlung/mail/notifications/an-to-be-checked.txt', + web_partial='fellchensammlung/partials/notifications/body-an-to-be-checked.html' + ), + NotificationTypeChoices.AN_WAS_DEACTIVATED: NotificationDisplayMapping( + email_html_template='fellchensammlung/mail/notifications/an-deactivated.html', + email_plain_template='fellchensammlung/mail/notifications/an-deactivated.txt', + web_partial='fellchensammlung/partials/notifications/body-an-deactivated.html' + ), + NotificationTypeChoices.AN_FOR_SEARCH_FOUND: NotificationDisplayMapping( + email_html_template='fellchensammlung/mail/notifications/an-for-search-found.html', + email_plain_template='fellchensammlung/mail/notifications/an-for-search-found.txt', + web_partial='fellchensammlung/partials/notifications/body-an-for-search.html' + ) +} diff --git a/src/fellchensammlung/tools/notifications.py b/src/fellchensammlung/tools/notifications.py index 3430d0d..b10c115 100644 --- a/src/fellchensammlung/tools/notifications.py +++ b/src/fellchensammlung/tools/notifications.py @@ -1,4 +1,9 @@ -from fellchensammlung.models import User, Notification, TrustLevel, NotificationTypeChoices +from types import SimpleNamespace as sn + +from celery.bin.celery import report + +from fellchensammlung.models import User, Notification, TrustLevel +from fellchensammlung.models import NotificationTypeChoices as ntc def notify_of_AN_to_be_checked(adoption_notice): @@ -8,7 +13,7 @@ def notify_of_AN_to_be_checked(adoption_notice): for user in users_to_notify: Notification.objects.create(adoption_notice=adoption_notice, user_to_notify=user, - notification_type=NotificationTypeChoices.AN_IS_TO_BE_CHECKED, + notification_type=ntc.AN_IS_TO_BE_CHECKED, title=f" Prüfe Vermittlung {adoption_notice}", text=f"{adoption_notice} muss geprüft werden bevor sie veröffentlicht wird.", )