1212from django .utils .html import format_html # for formatting thumbnails
1313from easy_thumbnails .files import get_thumbnailer # for generating thumbnails
1414import os # for checking if thumbnail file exists
15+ import logging
1516from website .admin .admin_site import ml_admin_site
1617
18+ _logger = logging .getLogger (__name__ )
19+
1720class YearListFilter (admin .SimpleListFilter ):
1821 title = 'year' # a label for our filter
1922 parameter_name = 'year' # you can put anything here
@@ -46,6 +49,12 @@ class Media:
4649 # Add a filter to the right sidebar that allows us to filter by year
4750 list_filter = (YearListFilter , 'project' )
4851
52+ # Search by headline or author name (News previously had no search box).
53+ search_fields = ['title' , 'author__first_name' , 'author__last_name' ]
54+
55+ # Year/month/day drill-down at the top of the changelist (News is date-driven).
56+ date_hierarchy = 'date'
57+
4958 # Define 'author' as an auto-complete field. We must then also define "search_fields"
5059 # in PersonAdmin or we'll receive a Django error
5160 autocomplete_fields = ['author' ]
@@ -55,12 +64,18 @@ class Media:
5564
5665 def get_display_thumbnail (self , obj ):
5766 if obj .image and os .path .isfile (obj .image .path ):
58- # Use easy_thumbnails to generate a thumbnail
59- thumbnailer = get_thumbnailer (obj .image )
60- thumbnail_options = {'size' : (NEWS_THUMBNAIL_SIZE [0 ], NEWS_THUMBNAIL_SIZE [1 ]), 'crop' : True }
61- thumbnail_url = thumbnailer .get_thumbnail (thumbnail_options ).url
62-
63- return format_html ('<img src="{}" height="50" style="border-radius: 5%;"/>' , thumbnail_url )
67+ try :
68+ # Use easy_thumbnails to generate a thumbnail
69+ thumbnailer = get_thumbnailer (obj .image )
70+ thumbnail_options = {'size' : (NEWS_THUMBNAIL_SIZE [0 ], NEWS_THUMBNAIL_SIZE [1 ]), 'crop' : True }
71+ thumbnail_url = thumbnailer .get_thumbnail (thumbnail_options ).url
72+
73+ return format_html ('<img src="{}" height="50" style="border-radius: 5%;"/>' , thumbnail_url )
74+ except Exception :
75+ # A single corrupt/unreadable image must not 500 the entire News
76+ # changelist (the column is rendered for every row).
77+ _logger .warning ("Could not generate admin thumbnail for News id=%s" , obj .pk , exc_info = True )
78+ return 'No Thumbnail'
6479 return 'No Thumbnail'
6580
6681 get_display_thumbnail .short_description = 'Thumbnail'
0 commit comments