Skip to content

Commit 8eb348c

Browse files
committed
show thumbnail in snapshot admin list
1 parent 9d694e0 commit 8eb348c

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

django/gsmap/admin.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from django_json_widget.widgets import JSONEditorWidget
55
from django.utils.html import mark_safe
66
from django.contrib import messages
7+
import requests
78
from sortedm2m_filter_horizontal_widget.forms import SortedFilteredSelectMultiple
89
from gsmap.models import Municipality, Snapshot, Workspace
9-
import requests
1010

1111

1212
class MunicipalityAdmin(admin.OSMGeoAdmin):
@@ -48,28 +48,44 @@ class SnapshotAdmin(admin.OSMGeoAdmin):
4848
},
4949
}
5050

51-
list_display = ('id', 'title', 'municipality', 'permission', 'is_showcase',
52-
'created', 'modified')
51+
list_display = ('id', 'thumbnail_list_image', 'title', 'municipality',
52+
'permission', 'is_showcase', 'created', 'modified')
5353
list_filter = ('is_showcase', 'permission')
5454
search_fields = ['title', 'municipality__name', 'municipality__canton']
5555

56-
def _image_display(self, url, width=None):
57-
return mark_safe(
58-
'<a href="{url}" target="_blank">'
59-
'<img src="{url}" width="{width}" height={height} />'
60-
'</a>'.format(url=url, width=width, height='auto'))
56+
def _image_display(self, obj, width=None, link=True):
57+
if hasattr(obj, 'url'):
58+
html = '<img src="{url}" width="{width}" height={height} />'.format(
59+
url=obj.url, width=width, height='auto'
60+
)
61+
if link:
62+
html = '<a href="{url}" target="_blank">{html}</a>'.format(url=obj.url, html=html)
63+
return mark_safe(html)
64+
else:
65+
return '-'
6166

6267
def screenshot_generated_image(self, obj):
63-
return self._image_display(obj.screenshot_generated.url, width=300)
68+
return self._image_display(obj.screenshot_generated, width=300)
6469

6570
def thumbnail_generated_image(self, obj):
66-
return self._image_display(obj.thumbnail_generated.url, width=200)
71+
return self._image_display(obj.thumbnail_generated, width=200)
72+
73+
def thumbnail_list_image(self, obj):
74+
if obj.thumbnail_generated or obj.thumbnail_manual:
75+
return self._image_display(
76+
obj.thumbnail_generated or obj.thumbnail_manual,
77+
width=50, link=False
78+
)
79+
else:
80+
return '-'
81+
82+
thumbnail_list_image.short_description = 'thumbnail'
6783

6884
def screenshot_manual_image(self, obj):
69-
return self._image_display(obj.screenshot_manual.url, width=300)
85+
return self._image_display(obj.screenshot_manual, width=300)
7086

7187
def thumbnail_manual_image(self, obj):
72-
return self._image_display(obj.thumbnail_manual.url, width=200)
88+
return self._image_display(obj.thumbnail_manual, width=200)
7389

7490

7591
def save_model(self, request, obj, form, change):

0 commit comments

Comments
 (0)