summaryrefslogtreecommitdiffstats
path: root/bn_django/photos
diff options
context:
space:
mode:
Diffstat (limited to 'bn_django/photos')
-rw-r--r--bn_django/photos/manual_import.py64
1 files changed, 64 insertions, 0 deletions
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