bgneal@695: """Admin definitions for the user_photos application.""" bgneal@695: from django.contrib import admin bgneal@695: bgneal@695: from user_photos.models import Photo bgneal@720: from user_photos.s3 import delete_photos bgneal@695: bgneal@719: IMG_TAG = """thumbnail""" bgneal@695: bgneal@695: class PhotoAdmin(admin.ModelAdmin): bgneal@695: date_hierarchy = 'upload_date' bgneal@695: ordering = ['-upload_date'] bgneal@695: raw_id_fields = ['user'] bgneal@695: search_fields = ['user__username', 'user__email'] bgneal@703: list_display = ['__unicode__', 'thumbnail'] bgneal@720: actions = ['custom_delete'] bgneal@703: bgneal@703: def thumbnail(self, obj): bgneal@719: return IMG_TAG.format(url=obj.url, thumb_url=obj.thumb_url) bgneal@703: thumbnail.allow_tags = True bgneal@695: bgneal@720: def custom_delete(self, request, qs): bgneal@720: """Custom delete in order to remove images from the S3 bucket in bgneal@720: addition to removing from the database. bgneal@720: bgneal@720: """ bgneal@720: delete_photos(qs) bgneal@720: count = len(qs) bgneal@720: qs.delete() bgneal@720: bgneal@720: if count == 1: bgneal@720: msg_bit = "1 photo was" bgneal@720: else: bgneal@720: msg_bit = "{} photos were".format(count) bgneal@720: bgneal@720: self.message_user(request, "{} successfully deleted.".format(msg_bit)) bgneal@720: custom_delete.short_description = "Delete selected photos from DB & S3" bgneal@720: bgneal@720: def get_actions(self, request): bgneal@720: """Remove the default delete selected action because we have installed bgneal@720: our own. bgneal@720: bgneal@720: """ bgneal@720: actions = super(PhotoAdmin, self).get_actions(request) bgneal@720: del actions['delete_selected'] bgneal@720: return actions bgneal@720: bgneal@720: bgneal@695: admin.site.register(Photo, PhotoAdmin)