feat: Send mail when new report comes in

This commit is contained in:
moanos [he/him] 2024-03-25 10:49:56 +01:00
parent e02967957d
commit 8b06a2045d
4 changed files with 41 additions and 2 deletions

View File

@ -3,6 +3,7 @@
[notfellchen] [notfellchen]
instance_name=Notfellchen DEBUG instance_name=Notfellchen DEBUG
host=localhost
[django] [django]
secret=CHANGE-ME secret=CHANGE-ME
@ -15,4 +16,7 @@ name=notfellchen
[locations] [locations]
media=./media media=./media
[mail]
console-only=true

View File

@ -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()

View File

@ -221,6 +221,10 @@ class Report(models.Model):
def __str__(self): def __str__(self):
return f"[{self.status}]: {self.adoption_notice.name}" 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): def get_reported_rules(self):
return self.reported_broken_rules.all() return self.reported_broken_rules.all()

View File

@ -1,7 +1,6 @@
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.urls import reverse 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, \ from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule, Image, Report, ModerationAction, \
Member Member
@ -96,6 +95,7 @@ def report_adoption(request, adoption_notice_id):
report_instance.adoption_notice_id = adoption_notice_id report_instance.adoption_notice_id = adoption_notice_id
report_instance.status = Report.WAITING report_instance.status = Report.WAITING
report_instance.save() report_instance.save()
mail_admins_new_report(report_instance)
return redirect(reverse("report-detail-success", args=[report_instance.pk], )) return redirect(reverse("report-detail-success", args=[report_instance.pk], ))
else: else:
form = ReportForm() form = ReportForm()