From ad5a9ecc98fc977029501555e2a0fd4d9cd6999d Mon Sep 17 00:00:00 2001 From: amy wieliczka Date: Tue, 9 Apr 2024 13:49:19 -0700 Subject: [PATCH] Make custom admin fields request-session-index aware --- exhibits/admin.py | 48 ++++++++++++++++++++++++++++++++++------------ exhibits/models.py | 2 +- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/exhibits/admin.py b/exhibits/admin.py index 601a33a1..40c3c165 100644 --- a/exhibits/admin.py +++ b/exhibits/admin.py @@ -2,6 +2,7 @@ from django.contrib import admin +from django.http import HttpRequest from django.utils.safestring import mark_safe from django import forms from django.db import models @@ -27,23 +28,32 @@ def link_to_exhibit_item(self, instance): return mark_safe(f"Click to add custom crop, link, title, and metadata") link_to_exhibit_item.short_description="Link to Exhibit Item" + def get_queryset(self, request: HttpRequest) -> models.QuerySet: + self.calisphere_index = request.session.get('index') + return super().get_queryset(request) + def img_display(self, instance): - if instance.imgUrl(): - return mark_safe("") + if instance.imgUrl(self.calisphere_index): + return mark_safe("") else: return None + img_display.short_description = "Thumbnail" class LessonPlanItemInline(admin.TabularInline): model = ExhibitItem classes = ('collapse',) fields = ['lesson_plan_order', 'publish', 'item_id', 'essay', 'render_as', 'img_display', 'citations', 'citations_render_as', 'link_to_exhibit_item'] - # 'imgUrl', 'custom_crop', 'custom_link', 'custom_title', 'custom_metadata', 'metadata_render_as' + # 'img_url', 'custom_crop', 'custom_link', 'custom_title', 'custom_metadata', 'metadata_render_as' extra = 0 formfield_overrides = { models.TextField: {'widget': forms.Textarea(attrs={'cols': 50, 'rows': 5})} } - readonly_fields = ['imgUrl', 'img_display', 'link_to_exhibit_item'] + readonly_fields = ['img_url', 'img_display', 'link_to_exhibit_item'] + + def get_queryset(self, request: HttpRequest) -> models.QuerySet: + self.calisphere_index = request.session.get('index') + return super().get_queryset(request) def link_to_exhibit_item(self, instance): link = reverse("admin:exhibits_exhibititem_change", args=[instance.id]) @@ -51,35 +61,45 @@ def link_to_exhibit_item(self, instance): link_to_exhibit_item.short_description="Link to Lesson Plan Item" def img_display(self, instance): - if instance.imgUrl(): - return mark_safe("") + if instance.imgUrl(self.calisphere_index): + return mark_safe("") else: return None img_display.short_description = "Thumbnail" + def img_url(self, instance): + return instance.imgUrl(self.calisphere_index) + class HistoricalEssayItemInline(admin.TabularInline): model = ExhibitItem classes = ('collapse',) fields = ['historical_essay_order', 'publish', 'item_id', 'essay', 'render_as', 'img_display', 'citations', 'citations_render_as', 'link_to_exhibit_item'] - # 'imgUrl', 'custom_crop', 'custom_link', 'custom_title', 'custom_metadata', 'metadata_render_as' + # 'img_url', 'custom_crop', 'custom_link', 'custom_title', 'custom_metadata', 'metadata_render_as' extra = 0 formfield_overrides = { models.TextField: {'widget': forms.Textarea(attrs={'cols': 50, 'rows': 5})} } - readonly_fields = ['imgUrl', 'img_display', 'link_to_exhibit_item'] + readonly_fields = ['img_url', 'img_display', 'link_to_exhibit_item'] def link_to_exhibit_item(self, instance): link = reverse("admin:exhibits_exhibititem_change", args=[instance.id]) return mark_safe(f"Click to add custom crop, link, title, and metadata") link_to_exhibit_item.short_description="Link to Historical Essay Item" + def get_queryset(self, request: HttpRequest) -> models.QuerySet: + self.calisphere_index = request.session.get('index') + return super().get_queryset(request) + def img_display(self, instance): - if instance.imgUrl(): - return mark_safe("") + if instance.imgUrl(self.calisphere_index): + return mark_safe("") else: return None img_display.short_description = "Thumbnail" + def img_url(self, instance): + return instance.imgUrl(self.calisphere_index) + class NotesItemInline(admin.TabularInline): model = NotesItem classes = ('collapse',) @@ -257,9 +277,13 @@ class ExhibitItemAdmin(admin.ModelAdmin): readonly_fields = ['container_type', 'container', 'container_order', 'img_display', 'return_to_container'] + def get_queryset(self, request: HttpRequest) -> models.QuerySet: + self.calisphere_index = request.session.get('index') + return super().get_queryset(request) + def img_display(self, instance): - if instance.imgUrl(): - return mark_safe("") + if instance.imgUrl(self.calisphere_index): + return mark_safe("") else: return None img_display.short_description = "Thumbnail" diff --git a/exhibits/models.py b/exhibits/models.py index 97873f79..f7953ec5 100644 --- a/exhibits/models.py +++ b/exhibits/models.py @@ -563,7 +563,7 @@ def indexedData(self, index): else: return None - def imgUrl(self, index="solr"): + def imgUrl(self, index): if self.custom_crop: return settings.THUMBNAIL_URL + "crop/210x210/" + self.custom_crop.name else: