From b88372f0327976c31e6b508b26729bab60743f82 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Tue, 6 Feb 2007 23:34:08 -0800 Subject: photos app roughed in, style tweaked TODO: search code --- bn_django/photos/manual_import.py | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 bn_django/photos/manual_import.py (limited to 'bn_django/photos/manual_import.py') diff --git a/bn_django/photos/manual_import.py b/bn_django/photos/manual_import.py new file mode 100644 index 0000000..a9b5448 --- /dev/null +++ b/bn_django/photos/manual_import.py @@ -0,0 +1,64 @@ +# 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 = '/home/bnewbold/bn-project/media/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 -- cgit v1.2.3