From 8b06a2045da300a1716d024bb0e6ce9a28444ec2 Mon Sep 17 00:00:00 2001 From: moanos Date: Mon, 25 Mar 2024 10:49:56 +0100 Subject: [PATCH] feat: Send mail when new report comes in --- notfellchen.cfg | 4 ++++ src/fellchensammlung/mail.py | 31 +++++++++++++++++++++++++++++++ src/fellchensammlung/models.py | 4 ++++ src/fellchensammlung/views.py | 4 ++-- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/fellchensammlung/mail.py diff --git a/notfellchen.cfg b/notfellchen.cfg index 9fe9e69..4abc78b 100644 --- a/notfellchen.cfg +++ b/notfellchen.cfg @@ -3,6 +3,7 @@ [notfellchen] instance_name=Notfellchen DEBUG +host=localhost [django] secret=CHANGE-ME @@ -15,4 +16,7 @@ name=notfellchen [locations] media=./media +[mail] +console-only=true + diff --git a/src/fellchensammlung/mail.py b/src/fellchensammlung/mail.py new file mode 100644 index 0000000..3f35747 --- /dev/null +++ b/src/fellchensammlung/mail.py @@ -0,0 +1,31 @@ +import django.conf.global_settings + +from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext +from django.conf import settings +from django.core import mail +from django.db.models import Q +from fellchensammlung.models import Member +from notfellchen.settings import host + + +def mail_admins_new_report(report): + subject = _("New report") + for moderator in Member.objects.filter(Q(trust_level=Member.MODERATOR) | Q(trust_level=Member.ADMIN)): + greeting = _("Moin,\r\n") + new_report_text = "es wurde eine Vermittlung gemeldet.\r\n" + if len(report.reported_broken_rules.all()) > 0: + reported_rules_text = f"Ein Verstoß gegen die folgenden Regeln wurde gemeldet [{', '.join(report.reported_broken_rules.all())}]\r\n" + else: + reported_rules_text = f"Es wurden keine Regeln angegeben gegen die Verstoßen wurde.\r\n" + if report.comment: + comment_text = f'Kommentar zum Report: "{report.comment}"\r\n' + else: + comment_text = f"Es wurde kein Kommentar hinzugefügt.\r\n" + + report_url = "https://" + host + report.get_absolute_url() + link_text = f"Um alle Details zu sehen, geh bitte auf: {report_url}" + body_text = greeting + new_report_text + reported_rules_text + comment_text + link_text + message = mail.EmailMessage(subject, body_text, settings.DEFAULT_FROM_EMAIL, [moderator.user.email]) + print("Sending email to ", moderator.user.email) + message.send() diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index dab900a..6bdc095 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -221,6 +221,10 @@ class Report(models.Model): def __str__(self): return f"[{self.status}]: {self.adoption_notice.name}" + def get_absolute_url(self): + """Returns the url to access a detailed page for the report.""" + return reverse('report-detail', args=[str(self.id)]) + def get_reported_rules(self): return self.reported_broken_rules.all() diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 8f73239..6c06bb4 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -1,7 +1,6 @@ from django.shortcuts import render, redirect -from django.http import HttpResponse from django.urls import reverse -import markdown +from .mail import mail_admins_new_report from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule, Image, Report, ModerationAction, \ Member @@ -96,6 +95,7 @@ def report_adoption(request, adoption_notice_id): report_instance.adoption_notice_id = adoption_notice_id report_instance.status = Report.WAITING report_instance.save() + mail_admins_new_report(report_instance) return redirect(reverse("report-detail-success", args=[report_instance.pk], )) else: form = ReportForm()