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 %}