moviewyrm/bookwyrm/models/report.py

56 lines
1.8 KiB
Python
Raw Permalink Normal View History

2021-03-09 01:16:59 +00:00
""" flagged for moderation """
2021-03-16 19:57:23 +00:00
from django.apps import apps
2021-03-09 01:16:59 +00:00
from django.db import models
2021-03-09 02:36:34 +00:00
from django.db.models import F, Q
2021-03-09 01:16:59 +00:00
from .base_model import BookWyrmModel
class Report(BookWyrmModel):
""" reported status or user """
reporter = models.ForeignKey(
"User", related_name="reporter", on_delete=models.PROTECT
)
note = models.TextField(null=True, blank=True)
user = models.ForeignKey("User", on_delete=models.PROTECT)
statuses = models.ManyToManyField("Status", blank=True)
2021-03-09 01:16:59 +00:00
resolved = models.BooleanField(default=False)
2021-03-16 19:57:23 +00:00
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(
2021-03-16 19:59:25 +00:00
Q(user_permissions__name__in=["moderate_user", "moderate_post"])
| Q(is_superuser=True)
2021-03-16 19:57:23 +00:00
).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",
)
2021-03-09 02:36:34 +00:00
class Meta:
""" don't let users report themselves """
2021-03-09 20:57:38 +00:00
2021-03-09 02:36:34 +00:00
constraints = [
2021-03-09 20:57:38 +00:00
models.CheckConstraint(check=~Q(reporter=F("user")), name="self_report")
2021-03-09 02:36:34 +00:00
]
2021-03-12 01:38:21 +00:00
ordering = ("-created_date",)
2021-03-09 01:16:59 +00:00
2021-03-09 20:57:38 +00:00
2021-03-09 01:16:59 +00:00
class ReportComment(BookWyrmModel):
""" updates on a report """
user = models.ForeignKey("User", on_delete=models.PROTECT)
note = models.TextField()
report = models.ForeignKey(Report, on_delete=models.PROTECT)
2021-03-12 23:56:54 +00:00
class Meta:
""" sort comments """
ordering = ("-created_date",)