1+ import os
2+
13from django import forms
24from django .contrib import admin
35from django .urls import reverse
46from django .utils .html import format_html
7+ from easy_thumbnails .files import get_thumbnailer
8+ from image_cropping import ImageCroppingMixin
59from website .models import Award
610from website .admin .admin_site import ml_admin_site
711from sortedm2m_filter_horizontal_widget .forms import SortedFilteredSelectMultiple
@@ -28,12 +32,12 @@ def clean(self):
2832
2933
3034@admin .register (Award , site = ml_admin_site )
31- class AwardAdmin (admin .ModelAdmin ):
35+ class AwardAdmin (ImageCroppingMixin , admin .ModelAdmin ):
3236 form = AwardAdminForm
3337
3438 # get_recipient_names / get_project_names are methods on the Award model;
3539 # their column headers come from each method's short_description.
36- list_display = ('title' , 'organization' , 'date' ,
40+ list_display = ('title' , 'get_display_thumbnail' , ' organization' , 'date' ,
3741 'get_recipient_names' , 'get_project_names' , 'award_type' )
3842
3943 list_filter = ('award_type' , 'date' )
@@ -80,7 +84,7 @@ def get_fieldsets(self, request, obj=None):
8084 'fields' : ['url' , 'description' ],
8185 }),
8286 ('Display' , {
83- 'fields' : ['badge' , 'badge_alt_text' ],
87+ 'fields' : ['badge' , 'badge_cropping' , ' badge_alt_text' ],
8488 'description' : 'Optional. On the Awards page, faculty honors show a medal icon, '
8589 'student awards show the recipient’s photo, and project awards '
8690 'show the project thumbnail. Upload a badge/logo here to override '
@@ -97,4 +101,19 @@ def formfield_for_manytomany(self, db_field, request, **kwargs):
97101 """
98102 if db_field .name == 'recipients' or db_field .name == 'projects' :
99103 kwargs ['widget' ] = SortedFilteredSelectMultiple ()
100- return super ().formfield_for_manytomany (db_field , request , ** kwargs )
104+ return super ().formfield_for_manytomany (db_field , request , ** kwargs )
105+
106+ def get_display_thumbnail (self , obj ):
107+ """Square preview of the uploaded badge (with its crop applied) in the
108+ changelist, mirroring the SponsorAdmin/NewsAdmin logo columns. Awards
109+ without a custom badge fall back to a medal icon on the public page, so
110+ there's nothing to show here."""
111+ if obj .badge and os .path .isfile (obj .badge .path ):
112+ thumbnailer = get_thumbnailer (obj .badge )
113+ options = {'size' : (50 , 50 ), 'crop' : True , 'box' : obj .badge_cropping }
114+ thumbnail_url = thumbnailer .get_thumbnail (options ).url
115+ return format_html ('<img src="{}" height="50" width="50" '
116+ 'style="object-fit: cover; border-radius: 5%;"/>' , thumbnail_url )
117+ return '—'
118+
119+ get_display_thumbnail .short_description = 'Badge'
0 commit comments