annotate user_photos/admin.py @ 973:6f55c086db1e
Guess file extension based on content-type.
When downloading a file, and no path is supplied to store it, guess the file
extension using mimetypes and the content-type header.
Also supply a unit test for the HotLinkImageForm.
author |
Brian Neal <bgneal@gmail.com> |
date |
Thu, 01 Oct 2015 19:44:45 -0500 |
parents |
e0e79451339d |
children |
|
rev |
line source |
bgneal@695
|
1 """Admin definitions for the user_photos application."""
|
bgneal@695
|
2 from django.contrib import admin
|
bgneal@695
|
3
|
bgneal@695
|
4 from user_photos.models import Photo
|
bgneal@720
|
5 from user_photos.s3 import delete_photos
|
bgneal@695
|
6
|
bgneal@719
|
7 IMG_TAG = """<a href="{url}"><img src="{thumb_url}" alt="thumbnail" /></a>"""
|
bgneal@695
|
8
|
bgneal@695
|
9 class PhotoAdmin(admin.ModelAdmin):
|
bgneal@695
|
10 date_hierarchy = 'upload_date'
|
bgneal@695
|
11 ordering = ['-upload_date']
|
bgneal@695
|
12 raw_id_fields = ['user']
|
bgneal@695
|
13 search_fields = ['user__username', 'user__email']
|
bgneal@703
|
14 list_display = ['__unicode__', 'thumbnail']
|
bgneal@720
|
15 actions = ['custom_delete']
|
bgneal@703
|
16
|
bgneal@703
|
17 def thumbnail(self, obj):
|
bgneal@719
|
18 return IMG_TAG.format(url=obj.url, thumb_url=obj.thumb_url)
|
bgneal@703
|
19 thumbnail.allow_tags = True
|
bgneal@695
|
20
|
bgneal@720
|
21 def custom_delete(self, request, qs):
|
bgneal@720
|
22 """Custom delete in order to remove images from the S3 bucket in
|
bgneal@720
|
23 addition to removing from the database.
|
bgneal@720
|
24
|
bgneal@720
|
25 """
|
bgneal@720
|
26 delete_photos(qs)
|
bgneal@720
|
27 count = len(qs)
|
bgneal@720
|
28 qs.delete()
|
bgneal@720
|
29
|
bgneal@720
|
30 if count == 1:
|
bgneal@720
|
31 msg_bit = "1 photo was"
|
bgneal@720
|
32 else:
|
bgneal@720
|
33 msg_bit = "{} photos were".format(count)
|
bgneal@720
|
34
|
bgneal@720
|
35 self.message_user(request, "{} successfully deleted.".format(msg_bit))
|
bgneal@720
|
36 custom_delete.short_description = "Delete selected photos from DB & S3"
|
bgneal@720
|
37
|
bgneal@720
|
38 def get_actions(self, request):
|
bgneal@720
|
39 """Remove the default delete selected action because we have installed
|
bgneal@720
|
40 our own.
|
bgneal@720
|
41
|
bgneal@720
|
42 """
|
bgneal@720
|
43 actions = super(PhotoAdmin, self).get_actions(request)
|
bgneal@720
|
44 del actions['delete_selected']
|
bgneal@720
|
45 return actions
|
bgneal@720
|
46
|
bgneal@720
|
47
|
bgneal@695
|
48 admin.site.register(Photo, PhotoAdmin)
|