Skip to content

Commit

Permalink
Add backfillmoderationdecision management command
Browse files Browse the repository at this point in the history
  • Loading branch information
krysal committed May 31, 2024
1 parent 7e558ef commit cb2d606
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions api/api/management/commands/backfillmoderationdecision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import argparse

from django.contrib.auth import get_user_model

from django_tqdm import BaseCommand

from api.models import AudioDecision, AudioReport, ImageDecision, ImageReport
from api.models.media import DMCA, MATURE_FILTERED, NO_ACTION, PENDING


class Command(BaseCommand):
help = "Back-fill the moderation decision table for a given media type."

@staticmethod
def add_arguments(parser):
parser.add_argument(
"--dry-run",
help="Count reports to process, and don't do anything else.",
type=bool,
default=True,
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
"--media-type",
help="The media type to back-fill moderation decisions.",
type=str,
default="image",
choices=["image", "audio"],
)
parser.add_argument(
"--moderator",
help="The username of the moderator to attribute the decisions to.",
type=str,
default="opener",
)

def handle(self, *args, **options):
dry = options["dry_run"]
username = options["moderator"]
media_type = options["media_type"]

MediaReport = ImageReport
MediaDecision = ImageDecision
if media_type == "audio":
MediaReport = AudioReport
MediaDecision = AudioDecision

non_pending_reports = MediaReport.objects.filter(decision=None).exclude(
status=PENDING
)
count_to_process = non_pending_reports.count()

if dry:
self.info(
f"{count_to_process} {media_type} reports to back-fill. "
f"This is a dry run, exiting without making changes."
)
return

if not count_to_process:
self.info("No reports to process.")
return

t = self.tqdm(total=count_to_process)

User = get_user_model()
try:
moderator = User.objects.get(username=username)
except User.DoesNotExist:
t.error(f"User {username} not found.")
return

for report in non_pending_reports:
decision = MediaDecision.objects.create(
action=self.get_action(report),
moderator=moderator,
notes="__backfilled_from_report_status",
)
report.decision = decision
report.save()
t.update(1)

t.info(
self.style.SUCCESS(
f"Created {count_to_process} {media_type} moderation decisions from existing media reports."
)
)

@staticmethod
def get_action(report):
if report.status == MATURE_FILTERED:
return "confirmed_sensitive"

if report.status == NO_ACTION:
return "rejected_reports"

# Cases with status = DEINDEXED
if report.reason == DMCA:
return "deindexed_copyright"

return "deindexed_sensitive" # For MATURE and OTHER cases

0 comments on commit cb2d606

Please sign in to comment.