changeset 330:3c951521e0ec

Fixing #152; POTD was saving thumbnail every night and making tons of thumbnail directories all over. Also added thumbnail images in the admin.
author Brian Neal <bgneal@gmail.com>
date Thu, 24 Feb 2011 03:45:50 +0000
parents 000c006fee97
children 1ea63aa3f21d
files gpp/potd/admin.py gpp/potd/models.py
diffstat 2 files changed, 50 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/potd/admin.py	Wed Feb 23 03:40:18 2011 +0000
+++ b/gpp/potd/admin.py	Thu Feb 24 03:45:50 2011 +0000
@@ -9,16 +9,42 @@
 from potd.models import Sequence
 
 
+IMG_TAG = '<img src="%s" alt="thumbnail" />'
+
+
 class PhotoAdmin(admin.ModelAdmin):
-    exclude = ('thumb', )
+    fields = ('photo', 'caption', 'description', 'user', 'potd_count')
     raw_id_fields = ('user', )
+    list_display = ('__unicode__', 'thumbnail')
+    actions = ('regen_thumbnail', )
 
     class Media:
         js = settings.GPP_THIRD_PARTY_JS['tiny_mce']
 
+    def thumbnail(self, obj):
+        return IMG_TAG % obj.thumb.url
+    thumbnail.allow_tags = True
+
+    def regen_thumbnail(self, request, qs):
+        """
+        Regenerates the thumbnail for the selected photos.
+        """
+        for photo in qs:
+            photo.generate_thumb()
+            photo.save()
+
+    regen_thumbnail.short_description = "Regenerate thumbs for selected photos"
+
+
 class CurrentAdmin(admin.ModelAdmin):
+    list_display = ('__unicode__', 'thumbnail')
     raw_id_fields = ('potd', )
 
+    def thumbnail(self, obj):
+        return IMG_TAG % obj.potd.thumb.url
+    thumbnail.allow_tags = True
+
+
 admin.site.register(Photo, PhotoAdmin)
 admin.site.register(Current, CurrentAdmin)
 admin.site.register(Sequence)
--- a/gpp/potd/models.py	Wed Feb 23 03:40:18 2011 +0000
+++ b/gpp/potd/models.py	Thu Feb 24 03:45:50 2011 +0000
@@ -45,7 +45,20 @@
         return ('potd-archive', [str(self.id)])
 
     def save(self, *args, **kwargs):
+        if not self.pk:
+            self.generate_thumb()
 
+        super(Photo, self).save(*args, **kwargs)
+        Sequence.objects.insert_photo(self.pk)
+
+    def delete(self):
+        Sequence.objects.remove_photo(self.pk)
+        super(Photo, self).delete()
+
+    def can_comment_on(self):
+        return Current.objects.get_current_id() == self.id
+
+    def generate_thumb(self):
         if self.thumb:
             self.thumb.delete(save=False)
 
@@ -59,16 +72,6 @@
         image.save(s, format)
         thumb_name = os.path.basename(self.photo.path)
         self.thumb.save(thumb_name, ContentFile(s.getvalue()), save=False)
-        
-        super(Photo, self).save(*args, **kwargs)
-        Sequence.objects.insert_photo(self.pk)
-
-    def delete(self):
-        Sequence.objects.remove_photo(self.pk)
-        super(Photo, self).delete()
-
-    def can_comment_on(self):
-        return Current.objects.get_current_id() == self.id
 
 
 class CurrentManager(models.Manager):
@@ -103,27 +106,25 @@
     def insert_photo(self, photo_id):
         current = Current.objects.get_current_id()
         if current is not None:
-            try:
-                s = self.get(pk=1)
-                seq = [int(x) for x in s.seq.split(',')]
-                if photo_id not in seq:
-                    i = seq.index(current)
-                    seq.insert(i + 1, photo_id)
-                    s.seq = ','.join([str(x) for x in seq])
-                    s.save()
-            except:
-                pass
+            s = self.get(pk=1)
+            seq = [int(x) for x in s.seq.split(',')]
+            if photo_id not in seq:
+                i = seq.index(current)
+                seq.insert(i + 1, photo_id)
+                s.seq = ','.join([str(x) for x in seq])
+                s.save()
 
     def remove_photo(self, photo_id):
         try:
             s = self.get(pk=1)
+        except Sequence.DoesNotExist:
+            pass
+        else:
             seq = [int(x) for x in s.seq.split(',')]
             if photo_id in seq:
                 seq.remove(photo_id)
                 s.seq = ','.join([str(x) for x in seq])
                 s.save()
-        except:
-            pass
 
 
 class Sequence(models.Model):