extensions-website/abuse/signals.py
Oleg Komarov 012845c712 Abuse reports: moderator form for resolving/dismissing + notification (#173)
It is possible to submit the form update the note and status multiple times,
e.g. a report that was initially dismissed, can be resolved later.
Each valid form submission will generate a notification for the reporter.

Once a note is saved, it becomes visible to the reporter.

The migration in this PR replaces the AbuseReport status Confirmed(=2)
that wasn't used in production with Dismissed(=2). Assuming that this is
a minor sin while the project is still in beta.

Reviewed-on: #173
Reviewed-by: Anna Sirota <annasirota@noreply.localhost>
2024-06-07 17:04:52 +02:00

56 lines
1.4 KiB
Python

import logging
from actstream import action
from actstream.actions import follow
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from abuse.models import AbuseReport
from constants.activity import Flag, Verb
from constants.base import (
ABUSE_TYPE_EXTENSION,
ABUSE_TYPE_RATING,
ABUSE_TYPE_USER,
)
logger = logging.getLogger(__name__)
@receiver(post_save, sender=AbuseReport)
def _create_action_from_report(
sender: object,
instance: AbuseReport,
created: bool,
raw: bool,
**kwargs: object,
) -> None:
if not created:
return
if raw:
return
if instance.type == ABUSE_TYPE_EXTENSION:
verb = Verb.REPORTED_EXTENSION
elif instance.type == ABUSE_TYPE_RATING:
verb = Verb.REPORTED_RATING
elif instance.type == ABUSE_TYPE_USER:
# TODO?
return
else:
logger.warning(f'ignoring an unexpected AbuseReport type={instance.type}')
return
action.send(
instance.reporter,
verb=verb,
target=instance.extension,
action_object=instance,
)
# subscribe to the report object to get notifications about its resolution
follow(instance.reporter, instance, send_action=False, flag=Flag.REPORTER)
@receiver(pre_delete, sender=AbuseReport)
def _log_deletion(sender: object, instance: AbuseReport, **kwargs: object) -> None:
instance.record_deletion()