From e5750de3dd7b58cd76986f7904d53589c7c78e15 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 16 Mar 2021 12:57:23 -0700 Subject: [PATCH 1/2] Notify admins when a report is filed --- .../migrations/0051_auto_20210316_1950.py | 32 +++++++++++++++++++ bookwyrm/models/notification.py | 4 ++- bookwyrm/models/report.py | 19 +++++++++++ bookwyrm/templates/notifications.html | 7 +++- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 bookwyrm/migrations/0051_auto_20210316_1950.py diff --git a/bookwyrm/migrations/0051_auto_20210316_1950.py b/bookwyrm/migrations/0051_auto_20210316_1950.py new file mode 100644 index 00000000..d8695d5c --- /dev/null +++ b/bookwyrm/migrations/0051_auto_20210316_1950.py @@ -0,0 +1,32 @@ +# Generated by Django 3.0.7 on 2021-03-16 19:50 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0050_auto_20210313_0030'), + ] + + operations = [ + migrations.RemoveConstraint( + model_name='notification', + name='notification_type_valid', + ), + migrations.AddField( + model_name='notification', + name='related_report', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='bookwyrm.Report'), + ), + migrations.AlterField( + model_name='notification', + name='notification_type', + field=models.CharField(choices=[('FAVORITE', 'Favorite'), ('REPLY', 'Reply'), ('MENTION', 'Mention'), ('TAG', 'Tag'), ('FOLLOW', 'Follow'), ('FOLLOW_REQUEST', 'Follow Request'), ('BOOST', 'Boost'), ('IMPORT', 'Import'), ('ADD', 'Add'), ('REPORT', 'Report')], max_length=255), + ), + migrations.AddConstraint( + model_name='notification', + constraint=models.CheckConstraint(check=models.Q(notification_type__in=['FAVORITE', 'REPLY', 'MENTION', 'TAG', 'FOLLOW', 'FOLLOW_REQUEST', 'BOOST', 'IMPORT', 'ADD', 'REPORT']), name='notification_type_valid'), + ), + ] diff --git a/bookwyrm/models/notification.py b/bookwyrm/models/notification.py index 19018165..233d635b 100644 --- a/bookwyrm/models/notification.py +++ b/bookwyrm/models/notification.py @@ -5,7 +5,7 @@ from .base_model import BookWyrmModel NotificationType = models.TextChoices( "NotificationType", - "FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT ADD", + "FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT ADD REPORT", ) @@ -22,6 +22,7 @@ class Notification(BookWyrmModel): related_list_item = models.ForeignKey( "ListItem", on_delete=models.CASCADE, null=True ) + related_report = models.ForeignKey("Report", on_delete=models.CASCADE, null=True) read = models.BooleanField(default=False) notification_type = models.CharField( max_length=255, choices=NotificationType.choices @@ -37,6 +38,7 @@ class Notification(BookWyrmModel): related_status=self.related_status, related_import=self.related_import, related_list_item=self.related_list_item, + related_report=self.related_report, notification_type=self.notification_type, ).exists(): return diff --git a/bookwyrm/models/report.py b/bookwyrm/models/report.py index 6a76d9fd..bdd7765e 100644 --- a/bookwyrm/models/report.py +++ b/bookwyrm/models/report.py @@ -1,4 +1,5 @@ """ flagged for moderation """ +from django.apps import apps from django.db import models from django.db.models import F, Q from .base_model import BookWyrmModel @@ -15,6 +16,24 @@ class Report(BookWyrmModel): statuses = models.ManyToManyField("Status", blank=True) resolved = models.BooleanField(default=False) + def save(self, *args, **kwargs): + """ notify admins when a report is created """ + super().save(*args, **kwargs) + user_model = apps.get_model("bookwyrm.User", require_ready=True) + # moderators and superusers should be notified + admins = user_model.objects.filter( + Q(user_permissions__name__in=["moderate_user", "moderate_post"]) | + Q(is_superuser=True) + ).all() + notification_model = apps.get_model("bookwyrm.Notification", require_ready=True) + for admin in admins: + notification_model.objects.create( + user=admin, + related_report=self, + notification_type="REPORT", + ) + + class Meta: """ don't let users report themselves """ diff --git a/bookwyrm/templates/notifications.html b/bookwyrm/templates/notifications.html index 3f0300bd..7c694d78 100644 --- a/bookwyrm/templates/notifications.html +++ b/bookwyrm/templates/notifications.html @@ -35,6 +35,8 @@ {% elif notification.notification_type == 'ADD' %} + {% elif notification.notification_type == 'REPORT' %} + {% endif %}
@@ -105,7 +107,10 @@ {% endif %} {% endif %} {% elif notification.related_import %} - {% blocktrans with related_id=notification.related_import.id %} your import completed.{% endblocktrans %} + {% blocktrans with related_id=notification.related_import.id %}Your import completed.{% endblocktrans %} + {% elif notification.related_report %} + {% url 'settings-report' notification.related_report.id as path %} + {% blocktrans with related_id=path %}A new report needs moderation.{% endblocktrans %} {% endif %}

From 550eba1f6881d4aa8ed09e1bb239bdb4d51396bd Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 16 Mar 2021 12:59:25 -0700 Subject: [PATCH 2/2] Black formatting --- .../migrations/0051_auto_20210316_1950.py | 56 +++++++++++++++---- bookwyrm/models/report.py | 5 +- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/bookwyrm/migrations/0051_auto_20210316_1950.py b/bookwyrm/migrations/0051_auto_20210316_1950.py index d8695d5c..3caecbbe 100644 --- a/bookwyrm/migrations/0051_auto_20210316_1950.py +++ b/bookwyrm/migrations/0051_auto_20210316_1950.py @@ -7,26 +7,60 @@ import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ - ('bookwyrm', '0050_auto_20210313_0030'), + ("bookwyrm", "0050_auto_20210313_0030"), ] operations = [ migrations.RemoveConstraint( - model_name='notification', - name='notification_type_valid', + model_name="notification", + name="notification_type_valid", ), migrations.AddField( - model_name='notification', - name='related_report', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='bookwyrm.Report'), + model_name="notification", + name="related_report", + field=models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.CASCADE, + to="bookwyrm.Report", + ), ), migrations.AlterField( - model_name='notification', - name='notification_type', - field=models.CharField(choices=[('FAVORITE', 'Favorite'), ('REPLY', 'Reply'), ('MENTION', 'Mention'), ('TAG', 'Tag'), ('FOLLOW', 'Follow'), ('FOLLOW_REQUEST', 'Follow Request'), ('BOOST', 'Boost'), ('IMPORT', 'Import'), ('ADD', 'Add'), ('REPORT', 'Report')], max_length=255), + model_name="notification", + name="notification_type", + field=models.CharField( + choices=[ + ("FAVORITE", "Favorite"), + ("REPLY", "Reply"), + ("MENTION", "Mention"), + ("TAG", "Tag"), + ("FOLLOW", "Follow"), + ("FOLLOW_REQUEST", "Follow Request"), + ("BOOST", "Boost"), + ("IMPORT", "Import"), + ("ADD", "Add"), + ("REPORT", "Report"), + ], + max_length=255, + ), ), migrations.AddConstraint( - model_name='notification', - constraint=models.CheckConstraint(check=models.Q(notification_type__in=['FAVORITE', 'REPLY', 'MENTION', 'TAG', 'FOLLOW', 'FOLLOW_REQUEST', 'BOOST', 'IMPORT', 'ADD', 'REPORT']), name='notification_type_valid'), + model_name="notification", + constraint=models.CheckConstraint( + check=models.Q( + notification_type__in=[ + "FAVORITE", + "REPLY", + "MENTION", + "TAG", + "FOLLOW", + "FOLLOW_REQUEST", + "BOOST", + "IMPORT", + "ADD", + "REPORT", + ] + ), + name="notification_type_valid", + ), ), ] diff --git a/bookwyrm/models/report.py b/bookwyrm/models/report.py index bdd7765e..f9e8905b 100644 --- a/bookwyrm/models/report.py +++ b/bookwyrm/models/report.py @@ -22,8 +22,8 @@ class Report(BookWyrmModel): user_model = apps.get_model("bookwyrm.User", require_ready=True) # moderators and superusers should be notified admins = user_model.objects.filter( - Q(user_permissions__name__in=["moderate_user", "moderate_post"]) | - Q(is_superuser=True) + Q(user_permissions__name__in=["moderate_user", "moderate_post"]) + | Q(is_superuser=True) ).all() notification_model = apps.get_model("bookwyrm.Notification", require_ready=True) for admin in admins: @@ -33,7 +33,6 @@ class Report(BookWyrmModel): notification_type="REPORT", ) - class Meta: """ don't let users report themselves """