Skip to content

Commit

Permalink
Add UI for decisions, similar to the UI for reports
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed May 27, 2024
1 parent 515c682 commit c808389
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions api/api/admin/media_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,83 @@ class MediaDecisionAdmin(admin.ModelAdmin):
media_type = None
through_model = None

#############
# List view #
#############

list_display = (
"id",
"created_on",
"moderator",
"action",
"notes",
"media_ids",
)
list_filter = ("moderator", "action")
list_prefetch_related = ("media_objs",)
search_fields = ("notes", *_production_deferred("media_objs__identifier"))

@admin.display(description="Media objs")
def media_ids(self, obj):
through_objs = getattr(obj, f"{self.media_type}decisionthrough_set").all()
text = []
for obj in through_objs:
path = reverse(
f"admin:api_{self.media_type}_change", args=(obj.media_obj.id,)
)
text.append(f'• <a href="{path}">{obj.media_obj}</a>')
return format_html("<br>".join(text))

###############
# Change view #
###############

def get_readonly_fields(self, request, obj=None):
if obj is None:
return ()
# These fields only make sense after a decision has been created.
# Moderator is set automatically and cannot be changed.
return (
"created_on",
"moderator",
)

def get_exclude(self, request, obj=None):
if obj is None: # Create form
# Moderator is set automatically and cannot be changed.
return ("moderator",)
return ()

def get_inlines(self, request, obj=None):
if obj is None:
# New decision, can make changes to the media objects.
is_mutable = True
else:
# Once created, media objects associated with decisions are
# immutable.
is_mutable = False

class MediaDecisionThroughAdmin(admin.TabularInline):
model = self.through_model
extra = 1
autocomplete_fields = _production_deferred("media_obj")
raw_id_fields = _non_production_deferred("media_obj")

def has_add_permission(self, request, obj=None):
return is_mutable and super().has_change_permission(request, obj)

def has_change_permission(self, request, obj=None):
return is_mutable and super().has_change_permission(request, obj)

def has_delete_permission(self, request, obj=None):
return is_mutable and super().has_delete_permission(request, obj)

return (MediaDecisionThroughAdmin,)

def save_model(self, request, obj, form, change):
obj.moderator = request.user
return super().save_model(request, obj, form, change)


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

0 comments on commit c808389

Please sign in to comment.