# Create your views here. # django imports from django.conf import settings # other imports import zipfile import os import stat import shutil from datetime import datetime from tempfile import NamedTemporaryFile, mkdtemp import Image try: from cStringIO import StringIO except ImportError: from StringIO import StringIO # Handling settings here STOCKPHOTO_BASE = 'photos' # models from bn_django.photos.models import Gallery, Photo # views def manual_import_photos(thezipfile, thegallery): # Check if the gallery is valid gallery = thegallery; f = file(thezipfile); zip = zipfile.ZipFile(f) #date = the_date #if not date: date = datetime.date(datetime.now()) destdir= os.path.join(settings.MEDIA_ROOT, STOCKPHOTO_BASE, datetime.strftime(datetime.now(), "%Y/%m/%d/")) if not os.path.isdir(destdir): os.makedirs(destdir, 0775) for filename in zip.namelist(): photopath = os.path.join(destdir, os.path.basename(filename)) data = zip.read(filename) file_data = StringIO(data) try: Image.open(file_data) except: # don't save and process non Image files continue photo = file(photopath, "wb") photo.write(data) # Create the object if photopath.startswith(os.path.sep): photopath = photopath[len(settings.MEDIA_ROOT):] photo = Photo(image=photopath, date=date, photographer='Bryan Newbold', title = os.path.basename(filename), gallery_id = thegallery) # Save it -- the thumbnails etc. get created. photo.save() return