extensions-website/abuse/signals.py
Anna Sirota 8124bc62ae Log deleted extension data (#84)
When an extension or a version is deleted, create a `LogEntry` for each record linked to that extension or version.
Each `LogEntry` has action flag `DELETION` and contains a blob with the last known field values of the records.
These `LogEntry` can be viewed by admins in `/admin/admin/logentry/`, same as any other `LogEntry` created by Django admin, so this should be sufficient for archival purposes.

Part of #82

Reviewed-on: #84
2024-04-19 16:25:49 +02:00

53 lines
1.2 KiB
Python

import logging
from actstream import action
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from abuse.models import AbuseReport
from constants.activity import 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,
)
@receiver(pre_delete, sender=AbuseReport)
def _log_deletion(sender: object, instance: AbuseReport, **kwargs: object) -> None:
instance.record_deletion()