changeset 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 (2013-09-20)
parents cc8de231df5a
children 378b55b81de3
files user_photos/admin.py user_photos/tests.py
diffstat 2 files changed, 29 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/user_photos/admin.py	Thu Sep 19 18:57:47 2013 -0500
+++ b/user_photos/admin.py	Thu Sep 19 19:24:56 2013 -0500
@@ -2,6 +2,7 @@
 from django.contrib import admin
 
 from user_photos.models import Photo
+from user_photos.s3 import delete_photos
 
 IMG_TAG = """<a href="{url}"><img src="{thumb_url}" alt="thumbnail" /></a>"""
 
@@ -11,9 +12,37 @@
     raw_id_fields = ['user']
     search_fields = ['user__username', 'user__email']
     list_display = ['__unicode__', 'thumbnail']
+    actions = ['custom_delete']
 
     def thumbnail(self, obj):
         return IMG_TAG.format(url=obj.url, thumb_url=obj.thumb_url)
     thumbnail.allow_tags = True
 
+    def custom_delete(self, request, qs):
+        """Custom delete in order to remove images from the S3 bucket in
+        addition to removing from the database.
+
+        """
+        delete_photos(qs)
+        count = len(qs)
+        qs.delete()
+
+        if count == 1:
+            msg_bit = "1 photo was"
+        else:
+            msg_bit = "{} photos were".format(count)
+
+        self.message_user(request, "{} successfully deleted.".format(msg_bit))
+    custom_delete.short_description = "Delete selected photos from DB & S3"
+
+    def get_actions(self, request):
+        """Remove the default delete selected action because we have installed
+        our own.
+
+        """
+        actions = super(PhotoAdmin, self).get_actions(request)
+        del actions['delete_selected']
+        return actions
+
+
 admin.site.register(Photo, PhotoAdmin)
--- a/user_photos/tests.py	Thu Sep 19 18:57:47 2013 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,16 +0,0 @@
-"""
-This file demonstrates writing tests using the unittest module. These will pass
-when you run "manage.py test".
-
-Replace this with more appropriate tests for your application.
-"""
-
-from django.test import TestCase
-
-
-class SimpleTest(TestCase):
-    def test_basic_addition(self):
-        """
-        Tests that 1 + 1 always equals 2.
-        """
-        self.assertEqual(1 + 1, 2)