1. Curtis Maloney
  2. gnocchi-gallery

Source

gnocchi-gallery / 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

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
        print "Done!"