From 9086e2e75b7c50d99e00606c07e72595679e5c44 Mon Sep 17 00:00:00 2001 From: moanos Date: Mon, 10 Mar 2025 22:03:47 +0100 Subject: [PATCH] fix: Show name of reported content --- src/fellchensammlung/models.py | 18 ++++++++++++++++++ .../partials/partial-report.html | 4 +--- src/fellchensammlung/views.py | 5 +++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index ac18e2a..cb3ce55 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -680,6 +680,21 @@ class Report(models.Model): def get_moderation_actions(self): return ModerationAction.objects.filter(report=self) + @property + def reported_content(self): + """ + Dynamically fetch the reported content based on subclass. + The alternative would be to use the ContentType framework: + https://docs.djangoproject.com/en/5.1/ref/contrib/contenttypes/ + """ + print("dodo") + if hasattr(self, "reportadoptionnotice"): + return self.reportadoptionnotice.adoption_notice + elif hasattr(self, "reportcomment"): + return self.reportcomment.reported_comment + print("dada") + return "didi" + class ReportAdoptionNotice(Report): adoption_notice = models.ForeignKey("AdoptionNotice", on_delete=models.CASCADE) @@ -688,6 +703,9 @@ class ReportAdoptionNotice(Report): def reported_content(self): return self.adoption_notice + def __str__(self): + return f"Report der Vermittlung {self.adoption_notice}" + class ReportComment(Report): reported_comment = models.ForeignKey("Comment", on_delete=models.CASCADE) diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/partial-report.html b/src/fellchensammlung/templates/fellchensammlung/partials/partial-report.html index 0a0a47e..1e883c1 100644 --- a/src/fellchensammlung/templates/fellchensammlung/partials/partial-report.html +++ b/src/fellchensammlung/templates/fellchensammlung/partials/partial-report.html @@ -1,9 +1,7 @@ {% load i18n %}

- {% blocktranslate %} - Meldung von {{ report.reported_content }} - {% endblocktranslate %} + {% translate 'Meldung von ' %}{{ report.reported_content }}

{% if report.reported_broken_rules %} {% translate "Regeln gegen die Verstoßen wurde" %} diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index cd7b87d..ff958ad 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -429,7 +429,8 @@ def report_detail(request, report_id, form_complete=False): """ Detailed view of a report, including moderation actions """ - report = Report.objects.get(pk=report_id) + # Prefetching reduces the number of queries to the database that are needed (see reported_content) + report = Report.objects.select_related("reportadoptionnotice", "reportcomment").get(pk=report_id) moderation_actions = ModerationAction.objects.filter(report_id=report_id) is_mod_or_above = user_is_trust_level_or_above(request.user, TrustLevel.MODERATOR) @@ -505,7 +506,7 @@ def my_profile(request): @user_passes_test(user_is_trust_level_or_above) def modqueue(request): - open_reports = Report.objects.filter(status=Report.WAITING) + open_reports = Report.objects.select_related("reportadoptionnotice", "reportcomment").filter(status=Report.WAITING) context = {"reports": open_reports} return render(request, 'fellchensammlung/modqueue.html', context=context)