Skip to content

Commit

Permalink
Create MediaDecisionThrough rows too
Browse files Browse the repository at this point in the history
  • Loading branch information
krysal committed Jun 5, 2024
1 parent 65daed2 commit df449e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
17 changes: 16 additions & 1 deletion api/api/management/commands/backfillmoderationdecision.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
from django_tqdm import BaseCommand

from api.constants.moderation import DecisionAction
from api.models import AudioDecision, AudioReport, ImageDecision, ImageReport
from api.models import (
AudioDecision,
AudioDecisionThrough,
AudioReport,
ImageDecision,
ImageDecisionThrough,
ImageReport,
)
from api.models.media import DMCA, MATURE_FILTERED, NO_ACTION, PENDING


Expand Down Expand Up @@ -43,9 +50,11 @@ def handle(self, *args, **options):

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

non_pending_reports = MediaReport.objects.filter(decision=None).exclude(
status=PENDING
Expand Down Expand Up @@ -83,6 +92,12 @@ def handle(self, *args, **options):
for report, decision in zip(reports_chunk, decisions):
report.decision = decision
MediaReport.objects.bulk_update(reports_chunk, ["decision"])
MediaDecisionThrough.objects.bulk_create(
[
MediaDecisionThrough(media_obj=report.media_obj, decision=decision)
for report, decision in zip(reports_chunk, decisions)
]
)
t.update(1)

t.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
NO_ACTION,
OTHER,
AudioDecision,
AudioDecisionThrough,
ImageDecision,
ImageDecisionThrough,
)
from test.factory.models.audio import AudioReportFactory
from test.factory.models.image import ImageReportFactory
Expand All @@ -37,9 +39,9 @@ def call_cmd(**options):

def make_reports(media_type, reason: str, status: str, count: int = 1):
if media_type == "audio":
AudioReportFactory.create_batch(count, status=status, reason=reason)
return AudioReportFactory.create_batch(count, status=status, reason=reason)
else:
ImageReportFactory.create_batch(count, status=status, reason=reason)
return ImageReportFactory.create_batch(count, status=status, reason=reason)


@pytest.mark.parametrize(
Expand All @@ -64,18 +66,26 @@ def test_create_moderation_decision_for_reports(
username = "opener"
UserFactory.create(username=username)

make_reports(media_type=media_type, reason=reason, status=status)
report = make_reports(media_type=media_type, reason=reason, status=status)[0]

out, err = call_cmd(dry_run=False, media_type=media_type, moderator=username)

MediaDecision = ImageDecision if media_type == "image" else AudioDecision
MediaDecisionThrough = (
ImageDecisionThrough if media_type == "image" else AudioDecisionThrough
)
assert MediaDecision.objects.count() == 1
assert f"Created 1 {media_type} moderation decisions from existing reports." in out

decision = MediaDecision.objects.first()
assert decision.media_objs.count() == 1
assert decision.action == expected_action
assert decision.moderator.username == username

decision_through = MediaDecisionThrough.objects.first()
assert decision_through.media_obj == report.media_obj
assert decision_through.decision == decision


@pytest.mark.django_db
def test_catch_user_exception():
Expand Down

0 comments on commit df449e4

Please sign in to comment.