Skip to content

Commit

Permalink
Reorder code
Browse files Browse the repository at this point in the history
Signed-off-by: Olga Bulat <obulat@gmail.com>
  • Loading branch information
obulat committed Apr 29, 2024
1 parent 17d69fb commit 4fe8a8f
Showing 1 changed file with 52 additions and 58 deletions.
110 changes: 52 additions & 58 deletions api/api/admin/media_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,51 @@ class MediaReportAdmin(admin.ModelAdmin):
actions = None
media_type = None

def get_fieldsets(self, request, obj=None):
if obj is None:
return [
(
"Report details",
{"fields": ["status", "decision", "reason", "description"]},
),
("Media details", {"fields": ["media_obj"]}),
]
return [
(
"Report details",
{
"fields": [
"created_at",
"status",
"decision",
"reason",
"description",
"has_sensitive_text",
],
},
),
]

def get_exclude(self, request, obj=None):
# ``identifier`` cannot be edited on an existing report.
if request.path.endswith("/change/"):
return ["media_obj"]

def get_readonly_fields(self, request, obj=None):
if obj is None:
return []
readonly_fields = [
"created_at",
"reason",
"description",
"has_sensitive_text",
"media_obj_id",
]
# ``status`` cannot be changed on a finalised report.
if obj.status != PENDING:
readonly_fields.append("status")
return readonly_fields

@admin.display(description="Has sensitive text")
def has_sensitive_text(self, obj):
"""
Expand Down Expand Up @@ -58,37 +103,12 @@ def get_other_reports(self, obj):
)
return reports

def get_exclude(self, request, obj=None):
# ``identifier`` cannot be edited on an existing report.
if request.path.endswith("/change/"):
return ["media_obj"]

def get_fieldsets(self, request, obj=None):
if obj is None:
return [
(
"Report details",
{"fields": ["status", "decision", "reason", "description"]},
),
("Media details", {"fields": ["media_obj"]}),
]
return [
(
"Report details",
{
"fields": [
"created_at",
"status",
"decision",
"reason",
"description",
"has_sensitive_text",
],
},
),
]

def _get_media_obj_data(self, obj):
tags_by_provider = {}
if obj.media_obj.tags:
for tag in obj.media_obj.tags:
tags_by_provider.setdefault(tag["provider"], []).append(tag["name"])

additional_data = {
"other_reports": self.get_other_reports(obj),
"identifier": obj.media_obj.identifier,
Expand All @@ -100,17 +120,11 @@ def _get_media_obj_data(self, obj):
"creator": obj.media_obj.creator or obj.media_obj.creator_url,
"creator_url": obj.media_obj.creator_url,
"url": obj.media_obj.url,
"tags": {},
"tags": tags_by_provider,
"reverse_media_url": reverse(
f"admin:api_{self.media_type}_change", args=[obj.media_obj.identifier]
),
}

if obj.media_obj.tags:
tags_by_provider = {}
for tag in obj.media_obj.tags:
tags_by_provider.setdefault(tag["provider"], []).append(tag["name"])
additional_data["tags"] = tags_by_provider
return additional_data

def change_view(self, request, object_id, form_url="", extra_context=None):
Expand All @@ -132,31 +146,11 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
def render_change_form(
self, request, context, add=False, change=False, form_url="", obj=None
):
context.update(
{
"add": add,
"change": change,
}
)
context.update({"add": add, "change": change})
return super().render_change_form(
request, context, add=add, change=change, form_url=form_url, obj=obj
)

def get_readonly_fields(self, request, obj=None):
if obj is None:
return []
readonly_fields = [
"created_at",
"reason",
"description",
"has_sensitive_text",
"media_obj_id",
]
# ``status`` cannot be changed on a finalised report.
if obj.status != PENDING:
readonly_fields.append("status")
return readonly_fields


class ImageReportAdmin(MediaReportAdmin):
media_type = "image"
Expand Down

0 comments on commit 4fe8a8f

Please sign in to comment.