From 273bef952671f6c95f32b072eaca25f47dd839b1 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 30 May 2024 15:46:51 +0200 Subject: [PATCH] feat: Add reports for comments, add to admin --- src/fellchensammlung/admin.py | 36 ++++++++-- src/fellchensammlung/forms.py | 15 ++-- ..._remove_report_adoption_notice_and_more.py | 71 +++++++++++++++++++ ...0004_alter_report_reported_broken_rules.py | 18 +++++ src/fellchensammlung/models.py | 30 ++++++-- .../partials/partial-comment.html | 1 + .../partials/partial-report.html | 4 +- src/fellchensammlung/urls.py | 4 +- src/fellchensammlung/views.py | 27 ++++++- 9 files changed, 188 insertions(+), 18 deletions(-) create mode 100644 src/fellchensammlung/migrations/0003_remove_report_adoption_notice_and_more.py create mode 100644 src/fellchensammlung/migrations/0004_alter_report_reported_broken_rules.py diff --git a/src/fellchensammlung/admin.py b/src/fellchensammlung/admin.py index 3d0380a..97e9cd6 100644 --- a/src/fellchensammlung/admin.py +++ b/src/fellchensammlung/admin.py @@ -1,9 +1,11 @@ from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib import admin -from .models import User, Language, Text +from django.utils.html import format_html + +from .models import User, Language, Text, ReportComment, ReportAdoptionNotice from .models import Animal, Species, RescueOrganization, AdoptionNotice, Location, Rule, Image, ModerationAction, \ - Report, Member, Comment + Member, Comment, Report # Define an inline admin descriptor for Employee model @@ -22,6 +24,34 @@ class UserAdmin(BaseUserAdmin): # Re-register UserAdmin admin.site.register(User, UserAdmin) + +def _reported_content_link(obj): + reported_content = obj.reported_content + return format_html(f'{reported_content}') + + +@admin.register(ReportComment) +class ReportCommentAdmin(admin.ModelAdmin): + list_display = ["user_comment", "reported_content_link"] + date_hierarchy = "created_at" + + def reported_content_link(self, obj): + return _reported_content_link(obj) + + reported_content_link.short_description = "Reported Content" + + +@admin.register(ReportAdoptionNotice) +class ReportAdoptionNoticeAdmin(admin.ModelAdmin): + list_display = ["user_comment", "reported_content_link"] + date_hierarchy = "created_at" + + def reported_content_link(self, obj): + return _reported_content_link(obj) + + reported_content_link.short_description = "Reported Content" + + admin.site.register(Animal) admin.site.register(Species) admin.site.register(RescueOrganization) @@ -29,8 +59,6 @@ admin.site.register(Location) admin.site.register(AdoptionNotice) admin.site.register(Rule) admin.site.register(Image) -admin.site.register(Report) admin.site.register(ModerationAction) admin.site.register(Language) admin.site.register(Text) -admin.site.register(Comment) diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index 9901fb2..56b8cbc 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -2,7 +2,8 @@ import datetime from django import forms -from .models import AdoptionNotice, Animal, Image, Report, ModerationAction, User, Species, Comment +from .models import AdoptionNotice, Animal, Image, ReportAdoptionNotice, ReportComment, ModerationAction, User, Species, \ + Comment from django_registration.forms import RegistrationForm from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit, Layout, Fieldset, HTML, Row, Column, Field @@ -53,10 +54,16 @@ class ImageForm(forms.ModelForm): fields = ('title', 'image', 'alt_text') -class ReportForm(forms.ModelForm): +class ReportAdoptionNoticeForm(forms.ModelForm): class Meta: - model = Report - fields = ('reported_broken_rules', 'comment') + model = ReportAdoptionNotice + fields = ('reported_broken_rules', 'user_comment') + + +class ReportCommentForm(forms.ModelForm): + class Meta: + model = ReportComment + fields = ('reported_broken_rules', 'user_comment') class CommentForm(forms.ModelForm): diff --git a/src/fellchensammlung/migrations/0003_remove_report_adoption_notice_and_more.py b/src/fellchensammlung/migrations/0003_remove_report_adoption_notice_and_more.py new file mode 100644 index 0000000..2c3c23a --- /dev/null +++ b/src/fellchensammlung/migrations/0003_remove_report_adoption_notice_and_more.py @@ -0,0 +1,71 @@ +# Generated by Django 5.0.6 on 2024-06-04 06:22 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("fellchensammlung", "0002_comment"), + ] + + operations = [ + migrations.RemoveField( + model_name="report", + name="adoption_notice", + ), + migrations.RenameField( + model_name="report", + old_name="comment", + new_name="user_comment", + ), + migrations.CreateModel( + name="ReportComment", + fields=[ + ( + "report_ptr", + models.OneToOneField( + auto_created=True, + on_delete=django.db.models.deletion.CASCADE, + parent_link=True, + primary_key=True, + serialize=False, + to="fellchensammlung.report", + ), + ), + ( + "reported_comment", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="fellchensammlung.comment", + ), + ), + ], + bases=("fellchensammlung.report",), + ), + migrations.CreateModel( + name="ReportAdoptionNotice", + fields=[ + ( + "report_ptr", + models.OneToOneField( + auto_created=True, + on_delete=django.db.models.deletion.CASCADE, + parent_link=True, + primary_key=True, + serialize=False, + to="fellchensammlung.report", + ), + ), + ( + "adoption_notice", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="fellchensammlung.adoptionnotice", + ), + ), + ], + bases=("fellchensammlung.report",), + ), + ] diff --git a/src/fellchensammlung/migrations/0004_alter_report_reported_broken_rules.py b/src/fellchensammlung/migrations/0004_alter_report_reported_broken_rules.py new file mode 100644 index 0000000..f4406fa --- /dev/null +++ b/src/fellchensammlung/migrations/0004_alter_report_reported_broken_rules.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.6 on 2024-06-04 08:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("fellchensammlung", "0003_remove_report_adoption_notice_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="report", + name="reported_broken_rules", + field=models.ManyToManyField(to="fellchensammlung.rule"), + ), + ] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index d8fb334..3b9de56 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -241,13 +241,12 @@ class Report(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text=_('ID dieses reports'), verbose_name=_('ID')) status = models.CharField(max_length=30, choices=STATES) - reported_broken_rules = models.ManyToManyField(Rule, blank=True) - adoption_notice = models.ForeignKey("AdoptionNotice", on_delete=models.CASCADE) - comment = models.TextField(blank=True) + reported_broken_rules = models.ManyToManyField(Rule) + user_comment = models.TextField(blank=True) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): - return f"[{self.status}]: {self.adoption_notice.name}" + return f"[{self.status}]: {self.user_comment:.20}" def get_absolute_url(self): """Returns the url to access a detailed page for the report.""" @@ -260,6 +259,22 @@ class Report(models.Model): return ModerationAction.objects.filter(report=self) +class ReportAdoptionNotice(Report): + adoption_notice = models.ForeignKey("AdoptionNotice", on_delete=models.CASCADE) + + @property + def reported_content(self): + return self.adoption_notice + + +class ReportComment(Report): + reported_comment = models.ForeignKey("Comment", on_delete=models.CASCADE) + + @property + def reported_content(self): + return self.reported_comment + + class ModerationAction(models.Model): BAN = "user_banned" DELETE = "content_deleted" @@ -368,3 +383,10 @@ class Comment(models.Model): def __str__(self): return f"{self.user} at {self.created_at.strftime('%H:%M %d.%m.%y')}: {self.text:.10}" + + def get_report_url(self): + return reverse('report-comment', args=[str(self.id)]) + + @property + def get_absolute_url(self): + return self.adoption_notice.get_absolute_url() diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/partial-comment.html b/src/fellchensammlung/templates/fellchensammlung/partials/partial-comment.html index 54af454..7868231 100644 --- a/src/fellchensammlung/templates/fellchensammlung/partials/partial-comment.html +++ b/src/fellchensammlung/templates/fellchensammlung/partials/partial-comment.html @@ -4,6 +4,7 @@
{{ comment.user }} {{ comment.created_at }} +

{{ comment.text | render_markdown }} diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/partial-report.html b/src/fellchensammlung/templates/fellchensammlung/partials/partial-report.html index 7db36e8..ebf45b2 100644 --- a/src/fellchensammlung/templates/fellchensammlung/partials/partial-report.html +++ b/src/fellchensammlung/templates/fellchensammlung/partials/partial-report.html @@ -2,7 +2,7 @@

{% blocktranslate %} - Meldung von {{ report.adoption_notice.name }} + Meldung von {{ report.reported_content }} {% endblocktranslate %}

{% if report.reported_broken_rules %} @@ -14,6 +14,6 @@ {% endif %}

{% translate "Kommentar zur Meldung" %}: - {{ report.comment }} + {{ report.user_comment }}

\ No newline at end of file diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index 87093d3..a36277f 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -28,7 +28,9 @@ urlpatterns = [ ############# ## Reports ## ############# - path("melden//", views.report_adoption, name="report-adoption-notice"), + path("vermittlung//report", views.report_adoption, name="report-adoption-notice"), + + path("kommentar//report", views.report_comment, name="report-comment"), path("meldung//", views.report_detail, name="report-detail"), path("meldung//sucess", views.report_detail_success, name="report-detail-success"), path("modqueue/", views.modqueue, name="modqueue"), diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index f981a02..872da5f 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -13,7 +13,7 @@ from notfellchen import settings from fellchensammlung import logger from fellchensammlung.models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ Member -from .forms import AdoptionNoticeForm, ImageForm, ReportForm, CommentForm +from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, CommentForm, ReportCommentForm from .models import Language @@ -223,17 +223,38 @@ def report_adoption(request, adoption_notice_id): Form to report adoption notices """ if request.method == 'POST': - form = ReportForm(request.POST) + form = ReportAdoptionNoticeForm(request.POST) if form.is_valid(): report_instance = form.save(commit=False) report_instance.adoption_notice_id = adoption_notice_id report_instance.status = Report.WAITING report_instance.save() + form.save_m2m() mail_admins_new_report(report_instance) return redirect(reverse("report-detail-success", args=[report_instance.pk], )) else: - form = ReportForm() + form = ReportAdoptionNoticeForm() + return render(request, 'fellchensammlung/forms/form-report.html', {'form': form}) + + +def report_comment(request, comment_id): + """ + Form to report comments + """ + if request.method == 'POST': + form = ReportCommentForm(request.POST) + + if form.is_valid(): + report_instance = form.save(commit=False) + report_instance.reported_comment_id = comment_id + report_instance.status = Report.WAITING + report_instance.save() + form.save_m2m() + mail_admins_new_report(report_instance) + return redirect(reverse("report-detail-success", args=[report_instance.pk], )) + else: + form = ReportCommentForm() return render(request, 'fellchensammlung/forms/form-report.html', {'form': form})