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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# Create your views here.
# django imports
from django.conf import settings
from django import forms, http, template
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponse
# 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
try:
STOCKPHOTO_BASE = settings.STOCKPHOTO_BASE.strip('/')
except AttributeError:
STOCKPHOTO_BASE = 'photos'
# models
from bn_django.photos.models import Gallery, Photo
# views
class ImportForm(forms.Form):
zipfile = forms.FileField()
photographer = forms.CharField()
date = forms.DateField()
def valid_zipfile(self, field_data, all_data):
zip_file = StringIO(field_data['content'])
zip = zipfile.ZipFile(zip_file)
return not zip.testzip()
@login_required
def import_photos(request, thegallery):
"""Import a batch of photographs uploaded by the user.
Import a batch of photographs uploaded by the user, all with
the same information for gallery, photographer and date. The
title will be set from the filename, and the description will be
blank. Self-respecting photographers will edit the fields for
each photograph; this is just a way to get a bunch of photographs
uploaded quickly.
The photographs should be wrapped up in a zip archive. The
archive will be unpacked (and flattened) in a temporary directory,
and all image files will be identified and imported into the
gallery. Other files in the archive will be silently ignored.
After importing the images, the view will display a page which may
contain the number of images imported, and a link to the gallery
into which the images were imported.
"""
# Check if the gallery is valid
gallery = get_object_or_404(Gallery, pk=thegallery)
# And that the user has permission to add photos
if not request.user.has_perm('gallery.add_photo'):
return http.HttpResponseForbidden("No permission to add photos")
if request.POST:
#new_data = request.POST.copy()
#new_data.update(request.FILES)
form = ImportForm(request.POST, request.FILES)
if not form.is_valid():
return render_to_response('photos/import_form.html',
dict(form=form, gallery=gallery))
# So now everything is okay
f = request.FILES['zipfile'] # the zip"file"
zip = zipfile.ZipFile(f)
date = request.POST['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)
f = photo
# Create the object
if photopath.startswith(os.path.sep):
photopath = photopath[len(settings.MEDIA_ROOT):]
photo = Photo(image=photopath, date=date,
photographer=request.POST['photographer'],
title = 'untitled',
gallery_id = thegallery)
# Save it -- the thumbnails etc. get created.
photo.save()
# Try to harvest EXIF data
try:
import EXIF
tags = EXIF.process_file(open(photo.image.path))
try:
if tags.has_key('Image DateTime'):
exifdate = tags['Image DateTime'].printable
photo.date = apply(datetime, map(int, exifdate.split(' ')[0].split(':')))
except Exception, E:
print E
pass
try:
if tags.has_key('EXIF UserComment'):
exiftitle = tags['EXIF UserComment'].printable
if not exiftitle == []:
photo.title = exiftitle
except Exception, E:
print E
pass
try:
if tags.has_key('Image Orientation'):
exifrot = tags['EXIF UserComment'].printable
if exifrot == 'Rotated 90 CCW':
#DO ROTATION
pass
except Exception, E:
print E
pass
except Exception, E:
print E
pass
finally:
photo.save()
# And jump to the directory for this gallery
response = http.HttpResponseRedirect(gallery.get_absolute_url())
response['Pragma'] = 'no cache'
response['Cache-Control'] = 'no-cache'
return response
else:
form = ImportForm()
return render_to_response('photos/import_form.html',
dict(form=form, gallery=gallery))
# request,
#@login_required
#def export(request, thegallery):
#"""Export a gallery to a zip file and send it to the user.
#"""
## Check if the gallery is valid
#gallery = get_object_or_404(Gallery, pk=thegallery)
#
## gather up the photos into a new directory
#tmpdir = mkdtemp()
#for photo in gallery.photo_set.all():
#shutil.copy(photo.get_image_filename(),
#tmpdir)
#files = [ os.path.join(tmpdir, ff) for ff in os.listdir(tmpdir) ]
#outfile = NamedTemporaryFile()
#zf = zipfile.ZipFile(outfile, "w",
#compression=zipfile.ZIP_DEFLATED)
#for filename in files:
#zf.write(filename, arcname=os.path.basename(filename))
#zf.close()
##outfile.flush()
#outfile.seek(0)
#shutil.rmtree(tmpdir)
#response = HttpResponse(outfile)
#response['Content-Type'] = "application/zip"
#response['Content-Length'] = str(os.stat(outfile.name)[stat.ST_SIZE])
#response['Content-Disposition'] = "attachment; filename=photos.zip"
#return response
#
|