comparison user_photos/admin.py @ 720:e0e79451339d

Modify user_photos admin delete to delete from S3 bucket.
author Brian Neal <bgneal@gmail.com>
date Thu, 19 Sep 2013 19:24:56 -0500
parents cc8de231df5a
children
comparison
equal deleted inserted replaced
719:cc8de231df5a 720:e0e79451339d
1 """Admin definitions for the user_photos application.""" 1 """Admin definitions for the user_photos application."""
2 from django.contrib import admin 2 from django.contrib import admin
3 3
4 from user_photos.models import Photo 4 from user_photos.models import Photo
5 from user_photos.s3 import delete_photos
5 6
6 IMG_TAG = """<a href="{url}"><img src="{thumb_url}" alt="thumbnail" /></a>""" 7 IMG_TAG = """<a href="{url}"><img src="{thumb_url}" alt="thumbnail" /></a>"""
7 8
8 class PhotoAdmin(admin.ModelAdmin): 9 class PhotoAdmin(admin.ModelAdmin):
9 date_hierarchy = 'upload_date' 10 date_hierarchy = 'upload_date'
10 ordering = ['-upload_date'] 11 ordering = ['-upload_date']
11 raw_id_fields = ['user'] 12 raw_id_fields = ['user']
12 search_fields = ['user__username', 'user__email'] 13 search_fields = ['user__username', 'user__email']
13 list_display = ['__unicode__', 'thumbnail'] 14 list_display = ['__unicode__', 'thumbnail']
15 actions = ['custom_delete']
14 16
15 def thumbnail(self, obj): 17 def thumbnail(self, obj):
16 return IMG_TAG.format(url=obj.url, thumb_url=obj.thumb_url) 18 return IMG_TAG.format(url=obj.url, thumb_url=obj.thumb_url)
17 thumbnail.allow_tags = True 19 thumbnail.allow_tags = True
18 20
21 def custom_delete(self, request, qs):
22 """Custom delete in order to remove images from the S3 bucket in
23 addition to removing from the database.
24
25 """
26 delete_photos(qs)
27 count = len(qs)
28 qs.delete()
29
30 if count == 1:
31 msg_bit = "1 photo was"
32 else:
33 msg_bit = "{} photos were".format(count)
34
35 self.message_user(request, "{} successfully deleted.".format(msg_bit))
36 custom_delete.short_description = "Delete selected photos from DB & S3"
37
38 def get_actions(self, request):
39 """Remove the default delete selected action because we have installed
40 our own.
41
42 """
43 actions = super(PhotoAdmin, self).get_actions(request)
44 del actions['delete_selected']
45 return actions
46
47
19 admin.site.register(Photo, PhotoAdmin) 48 admin.site.register(Photo, PhotoAdmin)