1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 = '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
|