Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Merge 1483875 into 61fb810
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch authored Jan 29, 2022
2 parents 61fb810 + 1483875 commit e106253
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 11 deletions.
18 changes: 18 additions & 0 deletions api/migrations/0019_submission_approved.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.10 on 2022-01-29 16:05

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("api", "0018_auto_20220116_2114"),
]

operations = [
migrations.AddField(
model_name="submission",
name="approved",
field=models.BooleanField(default=False),
),
]
4 changes: 4 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ class Meta:
# specifically.
removed_from_queue = models.BooleanField(default=False)

# Whether the submission has been approved by the moderators.
# If this is set to True, no new reports should be generated for this submission.
approved = models.BooleanField(default=False)

# If the submission has been reported, this contains the report reason
report_reason = models.CharField(max_length=300, null=True, blank=True)
# If the submission has been reported, this contains the info to get
Expand Down
2 changes: 2 additions & 0 deletions api/slack/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def update_submission_report(
approve_post(submission)
# Make sure the submission isn't marked as removed
submission.removed_from_queue = False
submission.approved = True
submission.save(skip_extras=True)

blocks = _construct_report_message_blocks(
Expand All @@ -197,6 +198,7 @@ def update_submission_report(
# If reported on the app side this already happened, but not for
# reports from Reddit
submission.removed_from_queue = True
submission.approved = False
submission.save(skip_extras=True)

blocks = _construct_report_message_blocks(
Expand Down
118 changes: 118 additions & 0 deletions api/tests/submissions/test_submission_approve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import json

from django.test import Client
from django.urls import reverse
from rest_framework import status

from utils.test_helpers import create_submission, setup_user_client


class TestSubmissionApprove:
"""Tests validating the behavior of the Submission approval process."""

def test_approve_no_params(self, client: Client) -> None:
"""Verify that approving a submission works without parameters."""
client, headers, user = setup_user_client(client)

submission = create_submission(id=3)
assert not submission.approved

data = {}

result = client.patch(
reverse("submission-approve", args=[submission.id]),
json.dumps(data),
content_type="application/json",
**headers
)

submission.refresh_from_db()

assert result.status_code == status.HTTP_200_OK
assert submission.approved

def test_approve_no_change(self, client: Client) -> None:
"""Verify that approving a submission works without parameters."""
client, headers, user = setup_user_client(client)

submission = create_submission(id=3, approved=True)
assert submission.approved

data = {}

result = client.patch(
reverse("submission-approve", args=[submission.id]),
json.dumps(data),
content_type="application/json",
**headers
)

submission.refresh_from_db()

assert result.status_code == status.HTTP_200_OK
assert submission.approved

def test_approve_param_false(self, client: Client) -> None:
"""Verify that reverting the approval works correctly."""
client, headers, user = setup_user_client(client)

submission = create_submission(id=3, approved=True)
assert submission.approved

data = {"approved": False}

result = client.patch(
reverse("submission-approve", args=[submission.id]),
json.dumps(data),
content_type="application/json",
**headers
)

submission.refresh_from_db()

assert result.status_code == status.HTTP_200_OK
assert not submission.approved

def test_approve_param_true(self, client: Client) -> None:
"""Verify that approving a submission works with parameters."""
client, headers, user = setup_user_client(client)

submission = create_submission(id=3)
assert not submission.removed_from_queue

data = {"approved": True}

result = client.patch(
reverse("submission-approve", args=[submission.id]),
json.dumps(data),
content_type="application/json",
**headers
)

submission.refresh_from_db()

assert result.status_code == status.HTTP_200_OK
assert submission.approved

def test_approve_reverting_removal(self, client: Client) -> None:
"""Verify that approving a submission reverts its removal."""
client, headers, user = setup_user_client(client)

submission = create_submission(id=3, removed_from_queue=True)
assert not submission.approved
assert submission.removed_from_queue

data = {}

result = client.patch(
reverse("submission-approve", args=[submission.id]),
json.dumps(data),
content_type="application/json",
**headers
)

submission.refresh_from_db()

assert result.status_code == status.HTTP_200_OK
assert submission.approved
assert not submission.removed_from_queue
31 changes: 27 additions & 4 deletions api/tests/submissions/test_submission_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_remove_no_params(self, client: Client) -> None:

submission.refresh_from_db()

assert result.status_code == status.HTTP_201_CREATED
assert result.status_code == status.HTTP_200_OK
assert submission.removed_from_queue

def test_remove_no_change(self, client: Client) -> None:
Expand All @@ -49,7 +49,7 @@ def test_remove_no_change(self, client: Client) -> None:

submission.refresh_from_db()

assert result.status_code == status.HTTP_201_CREATED
assert result.status_code == status.HTTP_200_OK
assert submission.removed_from_queue

def test_remove_param_false(self, client: Client) -> None:
Expand All @@ -70,7 +70,7 @@ def test_remove_param_false(self, client: Client) -> None:

submission.refresh_from_db()

assert result.status_code == status.HTTP_201_CREATED
assert result.status_code == status.HTTP_200_OK
assert not submission.removed_from_queue

def test_remove_param_true(self, client: Client) -> None:
Expand All @@ -91,5 +91,28 @@ def test_remove_param_true(self, client: Client) -> None:

submission.refresh_from_db()

assert result.status_code == status.HTTP_201_CREATED
assert result.status_code == status.HTTP_200_OK
assert submission.removed_from_queue

def test_remove_reverting_approval(self, client: Client) -> None:
"""Verify that removing the submission reverts an approval."""
client, headers, user = setup_user_client(client)

submission = create_submission(id=3, approved=True)
assert not submission.removed_from_queue
assert submission.approved

data = {}

result = client.patch(
reverse("submission-remove", args=[submission.id]),
json.dumps(data),
content_type="application/json",
**headers
)

submission.refresh_from_db()

assert result.status_code == status.HTTP_200_OK
assert submission.removed_from_queue
assert not submission.approved
49 changes: 42 additions & 7 deletions api/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def leaderboard(self, request: Request,) -> Response:
type="object", properties={"removed_from_queue": Schema(type="bool")}
),
responses={
201: DocResponse("Successful removal", schema=serializer_class),
200: DocResponse("Successful removal", schema=serializer_class),
404: "Submission not found.",
},
)
Expand All @@ -862,9 +862,12 @@ def remove(self, request: Request, pk: int) -> Response:
removed_from_queue = request.data.get("removed_from_queue", True)

submission.removed_from_queue = removed_from_queue
if removed_from_queue:
# Revert the approval
submission.approved = False
submission.save()
return Response(
status=status.HTTP_201_CREATED,
status=status.HTTP_200_OK,
data=self.serializer_class(submission, context={"request": request}).data,
)

Expand All @@ -885,30 +888,62 @@ def report(self, request: Request, pk: int, reason: str) -> Response:
"""
submission = get_object_or_404(Submission, id=pk)

if submission.removed_from_queue or submission.report_reason is not None:
# The submission is already removed or reported-- ignore the report
print("Already reported!")
if (
submission.removed_from_queue
or submission.report_reason is not None
or submission.approved
):
# The submission is already removed, reported or approved-- ignore the report
return Response(
status=status.HTTP_201_CREATED,
data=self.serializer_class(
submission, context={"request": request}
).data,
)

print("Setting report reason")
# Save the report reason
submission.report_reason = reason
submission.save(skip_extras=True)

# Send the report to mod chat
ask_about_removing_post(submission, reason)
print("Asked for post removal")

return Response(
status=status.HTTP_201_CREATED,
data=self.serializer_class(submission, context={"request": request}).data,
)

@csrf_exempt
@swagger_auto_schema(
request_body=Schema(
type="object", properties={"approved": Schema(type="bool")}
),
responses={
200: DocResponse("Successful approval", schema=serializer_class),
404: "Submission not found.",
},
)
@action(detail=True, methods=["patch"])
def approve(self, request: Request, pk: int) -> Response:
"""
Approve the submission.
This will prevent future reports from being generated for this submission.
"""
submission = get_object_or_404(Submission, id=pk)

approved = request.data.get("approved", True)

submission.approved = approved
if approved:
# Revert the removal
submission.removed_from_queue = False
submission.save()
return Response(
status=status.HTTP_200_OK,
data=self.serializer_class(submission, context={"request": request}).data,
)


def _is_returning_transcriber(volunteer: BlossomUser) -> bool:
"""Determine if the transcriber is returning.
Expand Down

0 comments on commit e106253

Please sign in to comment.