feat: make use of new notification mapping

This commit is contained in:
2025-07-13 00:00:30 +02:00
parent d8bcb8ece6
commit a4b8486bd4
5 changed files with 44 additions and 58 deletions

View File

@@ -7,29 +7,27 @@ from django.utils.translation import gettext_lazy as _
from django.conf import settings from django.conf import settings
from django.core import mail from django.core import mail
from fellchensammlung.models import User, Notification, TrustLevel, NotificationTypeChoices from fellchensammlung.models import User, Notification, TrustLevel, NotificationTypeChoices
from notfellchen.settings import base_url from fellchensammlung.tools.model_helpers import ndm
NEWLINE = "\r\n"
def mail_admins_new_report(report): def notify_mods_new_report(report, notification_type):
""" """
Sends an e-mail to all users that should handle the report. Sends an e-mail to all users that should handle the report.
""" """
for moderator in User.objects.filter(trust_level__gt=TrustLevel.MODERATOR): for moderator in User.objects.filter(trust_level__gt=TrustLevel.MODERATOR):
report_url = base_url + report.get_absolute_url() if notification_type == NotificationTypeChoices.NEW_REPORT_AN:
context = {"report_url": report_url, title = _("Vermittlung gemeldet")
"user_comment": report.user_comment, } elif notification_type == NotificationTypeChoices.NEW_COMMENT:
title = _("Kommentar gemeldet")
subject = _("Neue Meldung") else:
html_message = render_to_string('fellchensammlung/mail/notifications/report.html', context) raise NotImplementedError
plain_message = render_to_string('fellchensammlung/mail/notifications/report.txt', context) notification = Notification.objects.create(
notification_type=notification_type,
mail.send_mail(subject, user_to_notify=moderator,
plain_message, report=report,
from_email="info@notfellchen.org", title=title,
recipient_list=[moderator.email], )
html_message=html_message) notification.save()
def send_notification_email(notification_pk): def send_notification_email(notification_pk):
@@ -37,33 +35,9 @@ def send_notification_email(notification_pk):
subject = f"{notification.title}" subject = f"{notification.title}"
context = {"notification": notification, } context = {"notification": notification, }
if notification.notification_type == NotificationTypeChoices.NEW_REPORT_COMMENT or notification.notification_type == NotificationTypeChoices.NEW_REPORT_AN: html_message = render_to_string(ndm[notification.notification_type].email_html_template, context)
context["user_comment"] = notification.report.user_comment plain_message = render_to_string(ndm[notification.notification_type].email_plain_template, context)
context["report_url"] = f"{base_url}{notification.report.get_absolute_url()}"
html_message = render_to_string('fellchensammlung/mail/notifications/report.html', context)
plain_message = render_to_string('fellchensammlung/mail/notifications/report.txt', context)
elif notification.notification_type == NotificationTypeChoices.NEW_USER:
html_message = render_to_string('fellchensammlung/mail/notifications/new-user.html', context)
new_user_url = notification.user_related.get_full_url()
context["new_user_url"] = new_user_url
plain_message = render_to_string('fellchensammlung/mail/notifications/new-user.txt', context)
elif notification.notification_type == NotificationTypeChoices.AN_IS_TO_BE_CHECKED:
html_message = render_to_string('fellchensammlung/mail/notifications/an-to-be-checked.html', context)
plain_message = render_to_string('fellchensammlung/mail/notifications/an-to-be-checked.txt', context)
elif notification.notification_type == NotificationTypeChoices.AN_WAS_DEACTIVATED:
html_message = render_to_string('fellchensammlung/mail/notifications/an-deactivated.html', context)
plain_message = render_to_string('fellchensammlung/mail/notifications/an-deactivated.txt', context)
elif notification.notification_type == NotificationTypeChoices.AN_FOR_SEARCH_FOUND:
html_message = render_to_string('fellchensammlung/mail/notifications/an-for-search-found.html', context)
plain_message = render_to_string('fellchensammlung/mail/notifications/an-for-search-found.txt', context)
elif notification.notification_type == NotificationTypeChoices.NEW_COMMENT:
html_message = render_to_string('fellchensammlung/mail/notifications/new-comment.html', context)
plain_message = render_to_string('fellchensammlung/mail/notifications/new-comment.txt', context)
else:
raise NotImplementedError("Unknown notification type")
if "plain_message" not in locals():
plain_message = strip_tags(html_message)
mail.send_mail(subject, plain_message, settings.DEFAULT_FROM_EMAIL, mail.send_mail(subject, plain_message, settings.DEFAULT_FROM_EMAIL,
[notification.user_to_notify.email], [notification.user_to_notify.email],
html_message=html_message) html_message=html_message)

View File

@@ -732,6 +732,9 @@ class Report(models.Model):
"""Returns the url to access a detailed page for the report.""" """Returns the url to access a detailed page for the report."""
return reverse('report-detail', args=[str(self.id)]) return reverse('report-detail', args=[str(self.id)])
def get_full_url(self):
return f"{base_url}{self.get_absolute_url()}"
def get_reported_rules(self): def get_reported_rules(self):
return self.reported_broken_rules.all() return self.reported_broken_rules.all()

View File

@@ -1,6 +1,6 @@
{% extends "fellchensammlung/mail/base.txt" %} {% extends "fellchensammlung/mail/base.txt" %}
{% load i18n %} {% load i18n %}
{% block content %}{% blocktranslate %}Moin, {% block content %}{% blocktranslate with new_user_url=notification.user_related.get_full_url %}Moin,
es wurde ein neuer Useraccount erstellt. es wurde ein neuer Useraccount erstellt.

View File

@@ -5,21 +5,31 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<p>Moin,</p> <p>{% translate 'Moin' %},</p>
<p> <p>
es gibt eine neue Meldung. Folgende Nachricht wurde zur Meldung hinzugefügt: {% blocktranslate %}
es gibt eine neue Meldung.
{% endblocktranslate %}
{% if notification.report.user_comment %}
{% blocktranslate %}
Folgende Nachricht wurde zur Meldung hinzugefügt:
{% endblocktranslate %}
{% endif %}
</p> </p>
{% if notification.report.user_comment %}
<p>
<i>
{{ notification.report.user_comment }}
</i>
</p>
{% endif %}
<p> <p>
<i> {% blocktranslate %}
{{ user_comment }} Bitte bearbeite die Meldung möglichst bald.
</i> {% endblocktranslate %}
</p> </p>
<p> <p>
<a href="{{ notification.report.get_full_url }}" class="cta-button">{% translate 'Report bearbeiten' %}</a>
Bitte bearbeite die Meldung möglichst bald.
</p>
<p>
<a href="{{ report_url }}" class="cta-button">{% translate 'Report bearbeiten' %}</a>
</p> </p>
{% endblock %} {% endblock %}

View File

@@ -15,7 +15,7 @@ from django.core.serializers import serialize
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
import json import json
from .mail import mail_admins_new_report from .mail import notify_mods_new_report
from notfellchen import settings from notfellchen import settings
from fellchensammlung import logger from fellchensammlung import logger
@@ -478,8 +478,7 @@ def report_adoption(request, adoption_notice_id):
report_instance.status = Report.WAITING report_instance.status = Report.WAITING
report_instance.save() report_instance.save()
form.save_m2m() form.save_m2m()
mail_admins_new_report(report_instance) notify_mods_new_report(report_instance, NotificationTypeChoices.NEW_REPORT_AN)
print("dada")
return redirect(reverse("report-detail-success", args=[report_instance.pk], )) return redirect(reverse("report-detail-success", args=[report_instance.pk], ))
else: else:
form = ReportAdoptionNoticeForm() form = ReportAdoptionNoticeForm()
@@ -499,7 +498,7 @@ def report_comment(request, comment_id):
report_instance.status = Report.WAITING report_instance.status = Report.WAITING
report_instance.save() report_instance.save()
form.save_m2m() form.save_m2m()
mail_admins_new_report(report_instance) notify_mods_new_report(report_instance, NotificationTypeChoices.NEW_REPORT_COMMENT)
return redirect(reverse("report-detail-success", args=[report_instance.pk], )) return redirect(reverse("report-detail-success", args=[report_instance.pk], ))
else: else:
form = ReportCommentForm() form = ReportCommentForm()