Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify report views #1872

Merged
merged 2 commits into from Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/catalog/api/models/media.py
Expand Up @@ -241,7 +241,10 @@ def save(self, *args, **kwargs):
)
if self.status != DEINDEXED:
same_reports = same_reports.filter(reason=self.reason)
same_reports.update(status=self.status)

# Prevent redundant update statement when creating the report
if self.status != PENDING:
same_reports.update(status=self.status)
Comment on lines +244 to +247
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be clearer if the status check was before the query on line 238 and caused an early return? Something like:

if self.status == PENDING:
    return

Only because then it is quite clear that the intention is that pending reports should have no effect on other reports.



class AbstractDeletedMedia(OpenLedgerModel):
Expand Down
4 changes: 2 additions & 2 deletions api/catalog/api/views/audio_views.py
Expand Up @@ -93,5 +93,5 @@ def waveform(self, *_, **__):
methods=["post"],
serializer_class=AudioReportRequestSerializer,
)
def report(self, *args, **kwargs):
return super().report(*args, **kwargs)
def report(self, request, identifier):
return super().report(request, identifier)
4 changes: 2 additions & 2 deletions api/catalog/api/views/image_views.py
Expand Up @@ -179,8 +179,8 @@ def watermark(self, request, *_, **__):
methods=["post"],
serializer_class=ImageReportRequestSerializer,
)
def report(self, *args, **kwargs):
return super().report(*args, **kwargs)
def report(self, request, identifier):
return super().report(request, identifier)

# Helper functions

Expand Down
7 changes: 2 additions & 5 deletions api/catalog/api/views/media_views.py
Expand Up @@ -148,14 +148,11 @@ def related(self, request, identifier=None, *_, **__):
serializer = self.get_serializer(results, many=True)
return self.get_paginated_response(serializer.data)

def report(self, request, *_, **__):
media = self.get_object()
identifier = media.identifier
def report(self, request, identifier):
serializer = self.get_serializer(data=request.data | {"identifier": identifier})
serializer.is_valid(raise_exception=True)
report = serializer.save()
serializer.save()

serializer = self.get_serializer(report)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what was going on in the original code here 🤔 But the changes look good to me!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be the original author assumed that the serializer's data wouldn't be updated if the underlying object changed during save? The only reason I could think of for this is if one of the fields on the response was something that could be changed by saving, but none are:

fields = ["identifier", "reason", "description"]

Anyway, that's just a guess. I'm not sure what Django's behaviour is when saving the serializer changes the underlying data (due to the model's save method making changes), but in this case it definitely does not appear to matter.

return Response(data=serializer.data, status=status.HTTP_201_CREATED)

def thumbnail(self, image_url, request, *_, **__):
Expand Down