Commits

Anonymous committed 1394b80

Added gallery_sync command

Comments (0)

Files changed (3)

gnocchi_gallery/management/__init__.py

Empty file added.

gnocchi_gallery/management/commands/__init__.py

Empty file added.

gnocchi_gallery/management/commands/gallery_sync.py

+from django.core.management.base import BaseCommand, CommandError
+from django.conf import settings
+
+from gnocchi_gallery.models import Gallery, Image
+
+import os
+
+class Command(BaseCommand):
+    def handle(self, *args, **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:
+                    im = 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
+        print "Done!"