-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
303 lines (234 loc) · 10.4 KB
/
app.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# App Engine now requires Django version to be set explicitly.
# http://code.google.com/appengine/docs/python/tools/libraries.html#Django
from google.appengine.dist import use_library
use_library('django', '0.96')
import config
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from util import api_call, render_template, read_json, write_json, write_image
from models import Shareable, Album, Image
from auth import verify_auth
api_call.auth = verify_auth(error_code=401)
class AlbumRootHandler(webapp.RequestHandler):
@api_call(config.ALBUM_ROOT_URL, 'List existing albums')
def get(self):
"""GET handler for gallery albums.
URL pattern: /albums
Returns 200 OK with JSON data structure containing list of albums.
Returns Content-type: application/json.
Returns 401 UNAUTHORIZED to all calls if authorization fails.
"""
write_json(self, [album.to_dict() for album in Album.all()])
@api_call(config.ALBUM_ROOT_URL, 'Create a new empty album', { 'name': 'text' })
def post(self):
"""POST handler for gallery albums.
URL pattern: /albums
POST data must contain album metadata: 'name'.
Returns 201 CREATED with JSON data structure describing new album.
Returns Content-type: application/json.
Also returns Location header pointing to API URL for album details.
Include 'wrapjson' parameter in POST to wrap returned JSON in
a <textarea>. This also changes the returned Content-type to text/html.
If request is poorly formatted returns 400 BAD REQUEST.
Returns 401 UNAUTHORIZED to all calls if authorization fails.
"""
try:
data = dict(((str(k), v) for k, v in self.request.POST.items()))
album = Album(album_id=config.ALBUM_ID_GENERATOR(),
**data)
except:
data = {}
self.error(400)
else:
if not config.DEMO_MODE:
album.put()
data = album.to_dict()
self.response.headers['Location'] = data['url']
self.response.set_status(201)
write_json(self, data, wrapjson='wrapjson' in self.request.POST)
class AlbumHandler(webapp.RequestHandler):
@api_call(config.ALBUM_URL_TEMPLATE_STRING, 'Get information about an album')
def get(self, album_id):
"""GET handler for a particular gallery album.
URL pattern: /albums/${album_id}
If album exists, returns 200 OK with JSON album data structure.
Returns Content-type: application/json.
If album doesn't exist, returns 404 NOT FOUND.
Returns 401 UNAUTHORIZED to all calls if authorization fails.
"""
q = Album.all().filter('album_id =', album_id)
album = q.get()
if not album:
return self.error(404)
write_json(self, album.to_dict())
@api_call(config.ALBUM_URL_TEMPLATE_STRING, 'Delete an album')
def delete(self, album_id):
"""DELETE handler for gallery album.
URL pattern: /albums/${album_id}
If album exists, returns 200 OK.
If album doesn't exist, returns 404 NOT FOUND.
Also deletes all images associated with this album.
Returns 401 UNAUTHORIZED to all calls if authorization fails.
"""
q = Album.all().filter('album_id =', album_id)
album = q.get()
if not album:
return self.error(404)
if not config.DEMO_MODE:
q = Image.all().filter('album =', album)
for image in q:
image.delete()
album.delete()
class ImageRootHandler(webapp.RequestHandler):
@api_call(config.IMAGE_ROOT_URL_TEMPLATE_STRING, 'List all images in an album')
def get(self, album_id):
"""GET handler for images in a particular gallery album.
URL pattern: /albums/${album_id}/images
If album exists, returns 200 OK with JSON image data structure.
Returns Content-type: application/json.
If album doesn't exist, returns 404 NOT FOUND.
Returns 401 UNAUTHORIZED to all calls if authorization fails.
"""
q = Album.all().filter('album_id =', album_id)
album = q.get()
if not album:
return self.error(404)
images = Image.all().filter("album =", album)
write_json(self, [image.to_dict() for image in images])
@api_call(config.IMAGE_ROOT_URL_TEMPLATE_STRING, 'Add an image to an album', { 'name': 'text', 'file': 'file' })
def post(self, album_id):
"""POST handler for a gallery image.
URL pattern: /albums/${album_id}/images
POST data must be of type multipart/form and contain image as 'file'.
POST data must also contain image metadata: 'name'.
Image filename must include an extension.
Returns 201 CREATED with JSON data structure describing new image.
Returns Content-type: application/json.
Also returns Location header pointing to API URL for image details.
Include 'wrapjson' parameter in POST to wrap returns JSON in
a <textarea>. This also changes the returned Content-type to text/html.
If album doesn't exist, returns 404 NOT FOUND.
If request is poorly formatted returns 400 BAD REQUEST.
Returns 401 UNAUTHORIZED to all calls if authorization fails.
"""
q = Album.all().filter('album_id =', album_id)
album = q.get()
if not album:
return self.error(404)
try:
data = dict(((str(k), v) for k, v in self.request.POST.items()))
if 'file' in data:
data['extension'] = data['file'].filename.split('.')[-1].lower()
if data['extension'] == data['file'].filename:
data['extension'] = ''
else:
data['extension'] = '.' + data['extension']
data['image_data'] = data['file'].file.read()
image = Image(image_id=config.IMAGE_ID_GENERATOR(),
album=album,
**data)
except:
data = {}
self.error(400)
else:
if not config.DEMO_MODE:
image.put()
data = image.to_dict()
self.response.headers['Location'] = data['url']
self.response.set_status(201)
write_json(self, data, wrapjson='wrapjson' in self.request.POST)
class ImageHandler(webapp.RequestHandler):
@api_call(config.IMAGE_URL_TEMPLATE_STRING, 'Get information about an image')
def get(self, album_id, image_id, extension=None):
"""GET handler for GGB image metadata and files.
URL pattern: /albums/${album_id}/images/${image_id}(${extension})
If called without a file extension:
If image exists, returns 200 OK with JSON image data structure.
Returns Content-type: application/json.
If image doesn't exist, returns 404 NOT FOUND.
If called with a file extension:
If image exists and has the matching extension, returns the image.
Returned Content-type matches the image format.
Otherwise returns 404 NOT FOUND.
Returns 401 UNAUTHORIZED to all calls if authorization fails.
"""
q = Album.all().filter('album_id =', album_id)
album = q.get()
if not album:
return self.error(404)
q = Image.all().filter('album =', album).filter('image_id =', image_id)
image = q.get()
if not image:
return self.error(404)
if not extension:
data = image.to_dict()
return write_json(self, image.to_dict())
if extension != image.extension:
return self.error(404)
write_image(self, image.image_data, image.extension)
@api_call(config.IMAGE_URL_TEMPLATE_STRING, 'Delete an image')
def delete(self, album_id, image_id, extension=None):
"""DELETE handler for gallery images.
URL pattern: /albums/${album_id}/images/${image_id}
If image exists, returns 200 OK.
If image doesn't exist, returns 404 NOT FOUND.
Returns 401 UNAUTHORIZED to all calls if authorization fails.
"""
q = Album.all().filter('album_id =', album_id)
album = q.get()
if not album:
return self.error(404)
q = Image.all().filter('album =', album).filter('image_id =', image_id)
image = q.get()
if not image:
return self.error(404)
if extension and extension != image.extension:
return self.error(404)
if not config.DEMO_MODE:
image.delete()
class ShareHandler(webapp.RequestHandler):
def get(self, hash, extension=None):
q = Album.all().filter('hash =', hash)
album = q.get()
if album:
if extension:
return self.error(404)
q = Image.all().filter('album =', album)
return self.response.out.write(render_template('album.html', {
'name': album.name,
'images': q,
}))
q = Image.all().filter('hash =', hash)
image = q.get()
if image:
if not extension:
return self.response.out.write(render_template('image.html',
{ 'image': image }))
elif image.extension == extension:
return write_image(self, image.image_data, extension)
else:
return self.error(404)
return self.error(404)
class AdminHandler(webapp.RequestHandler):
def get(self):
self.response.out.write(render_template('admin.html', {
'api_calls': api_call.calls,
'demo_mode': config.DEMO_MODE,
}))
class DefaultHandler(webapp.RequestHandler):
def get(self):
self.error(404)
wsgi_paths = [
(config.IMAGE_URL_REGEX, ImageHandler),
(config.IMAGE_ROOT_URL_REGEX, ImageRootHandler),
(config.ALBUM_URL_REGEX, AlbumHandler),
(config.ALBUM_ROOT_URL, AlbumRootHandler),
(config.SHARE_URL_REGEX, ShareHandler),
(config.ADMIN_URL, AdminHandler),
('/.*', DefaultHandler),
]
def main():
run_wsgi_app(webapp.WSGIApplication(wsgi_paths, debug=True))
if __name__ == "__main__":
main()