Commits

Anonymous committed e3b5dcc

Added gallery_sync call to Gallery model
Added sync call in admin
Set height/width to readonly in admin

  • Participants
  • Parent commits c5d56d4

Comments (0)

Files changed (4)

File gnocchi/gallery/admin.py

 
 class ImageInline(admin.TabularInline):
     model = models.Image
+    readonly_fields = ('width', 'height',)
 
 class GalleryAdmin(admin.ModelAdmin):
     list_display = ('name', 'slug',)
     inlines = (
         ImageInline,
     )
+    def sync_images(self, request, queryset):
+        for gallery in queryset.all():
+            gallery.sync_images()
+    actions = [sync_images]
 
 admin.site.register(models.Gallery, GalleryAdmin)
 
     list_display = ('thumb', 'gallery', 'image', 'name', 'order', 'width', 'height',)
     list_filter = ('gallery', 'order', 'width', 'height',)
     list_editable = ('order',)
+    readonly_fields = ('width', 'height',)
     inlines = [
         InlineAttributeAdmin,
     ]

File gnocchi/gallery/management/commands/gallery_sync.py

 from django.core.management.base import BaseCommand
-from django.conf import settings
 
-from gnocchi.gallery.models import Gallery, Image
-
-import os
+from gnocchi.gallery.models import Gallery
 
 class Command(BaseCommand):
     def handle(self, **kwargs):
-        root = os.path.join(settings.MEDIA_ROOT, 'gallery/')
-        for slug in os.listdir(root):
-            try:
-                gallery = Gallery.objects.get(slug=slug)
-            except Gallery.DoesNotExist:
-                print "No gallery matching %s. Skipping..." % slug
-                continue
-            print "Scanning gallery: %s" % gallery
-            for name in os.listdir(os.path.join(root, slug)):
-                filename = os.path.join('gallery', slug, name)
-                try:
-                    image = Image.objects.get(image=filename)
-                except Image.DoesNotExist:
-                    print "Adding Image: %s" % filename
-                    image = Image.objects.create(
-                        gallery=gallery,
-                        image=filename,
-                    )
-            print "Finished: %s" % gallery
+        for gallery in Gallery.objects.all():
+            gallery.sync_images()
         print "Done!"

File gnocchi/gallery/models.py

 from django.db import models
+from django.conf import settings
 import os
 
 from gnocchi.tools.attr import AttrHelper
     def __unicode__(self):
         return self.name
 
+    def sync_images(self):
+        for name in os.listdir(os.path.join(settings.MEDIA_ROOT, 'gallery', self.slug)):
+            filename = os.path.join('gallery', self.slug, name)
+            try:
+                image = self.images.get(image=filename)
+            except Image.DoesNotExist:
+                image = Image.objects.create(
+                    gallery=self,
+                    image=filename,
+                )
+
 def upload_to(instance, filename):
     return os.path.join('gallery', instance.gallery.slug, filename)
 
 from setuptools import setup, find_packages
 
 setup( name='gnocchi-gallery',
-    version = '1.1',
+    version = '1.1.1',
     description = 'A simple gallery app',
     author = 'Curtis Maloney',
     author_email = 'curtis@tinbrain.net',