-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
289 lines (223 loc) · 8.77 KB
/
models.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
import settings, logging
import flickr
from google.appengine.ext import db
from google.appengine.api import memcache
class PhotoBackend():
user_id = None
thumb_size = 72
imgmax = 640
thumb_cropped = True
def __init__(self, user):
self.user_id = user.GetUsername()
self.thumb_size = user.thumb_size
self.imgmax = user.full_size
self.thumb_cropped = user.thumb_cropped
def GetAllAlbums(self):
return []
def GetFeaturedAlbums(self, featured=[]):
albums = self.GetAllAlbums()
featured_albums = []
for album in albums:
if album['title'] not in featured:
continue
featured_albums.append(album)
# Sort by name
featured_albums.sort(key=lambda a: a['title'])
if 'all' in featured:
featured_albums.append({'id': 'all', 'title': 'all'})
return featured_albums
def GetPhotosInAlbum(self, album, featured=[]):
return []
def GetSinglePhoto(self, album, photo_id):
return None
def CacheGet(self, key):
if not settings.MEMCACHE_ENABLED or not key:
return None
return memcache.get(key)
def CacheSet(self, key, value):
if not settings.MEMCACHE_ENABLED or not key:
return True
return memcache.set(key, value)
def CacheClear(self):
pass
class PicasaBackend(PhotoBackend):
gdata = None
ALBUM_FEED_URI = '/data/feed/api/user/%s/album/%s?kind=photo&thumbsize=%s&imgmax=%s'
def __init__(self, user):
PhotoBackend.__init__(self, user)
import gdata.photos.service
import gdata.alt.appengine
self.gdata = gdata.photos.service.PhotosService()
gdata.alt.appengine.run_on_appengine(self.gdata)
def GetAllAlbums(self):
logging.info('GetAllAlbums called')
# check memcache
key = 'picasa_albums'
albums = self.CacheGet(key)
if albums:
return albums
albums = []
albums_feed = self.gdata.GetUserFeed(user=self.user_id, kind='album')
for album in albums_feed.entry:
albums.append({
'id': album.gphoto_id.text,
'title': album.title.text,
})
albums.sort(key=lambda a: a['title'])
# set memcache
self.CacheSet(key, albums)
return albums
def GetPhotosInAlbum(self, album, featured=[]):
logging.info('GetPhotosInAlbum called')
if self.thumb_cropped:
thumb_size = "%dc" % self.thumb_size
else:
thumb_size = "%du" % self.thumb_size
# check memcache
key = "picasa_album_%s_%s_%s" % (album, thumb_size, self.imgmax)
photos = self.CacheGet(key)
if photos:
return photos
photos = []
albums = []
if album == 'all':
all_albums = self.GetFeaturedAlbums(featured)
for a in all_albums:
albums.append(a['title'])
else:
albums.append(album)
logging.info('Got albums %s' % str(albums))
for a in albums:
feed = self.ALBUM_FEED_URI % (self.user_id, a, thumb_size, self.imgmax)
logging.info('GetPhotosInAlbum feed is %s' % feed)
try:
photos_feed = self.gdata.GetFeed(feed)
for photo in photos_feed.entry:
pic = {
'id': photo.gphoto_id.text,
'height': photo.width.text,
'width': photo.height.text,
'thumb_url': photo.media.thumbnail[0].url,
'url': photo.media.content[0].url,
'name': photo.title.text,
}
photos.append(pic)
except:
pass
# set memcache
self.CacheSet(key, photos)
return photos
def CacheClear(self):
keys = []
albums = self.GetAllAlbums()
for a in albums:
keys.append("picasa_album_%s_%sc_%s" % (album['title'], self.thumb_size, self.imgmax))
keys.append("picasa_album_%s_%su_%s" % (album['title'], self.thumb_size, self.imgmax))
albums.append('picasa_albums')
memcache.delete_multi(keys)
class FlickrBackend(PhotoBackend):
flickr = None
def __init__(self, user):
PhotoBackend.__init__(self, user)
flickr.API_KEY = '36fbcb5322bdab1866dff9622f161400'
self.flickr = flickr.User(self.user_id)
def GetAllAlbums(self):
logging.info('GetAllAlbums called')
# check memcache
key = 'flickr_albums'
albums = self.CacheGet(key)
if albums:
return albums
albums = []
sets = self.flickr.getPhotosets()
for s in sets:
albums.append({
'id': s.id,
'title': s.title,
})
# set memcache
self.CacheSet(key, albums)
return albums
def GetPhotosInAlbum(self, album, featured=[]):
logging.info('GetPhotosInAlbum called')
if self.thumb_cropped:
thumb_size = "Square"
else:
thumb_size = "Thumbnail"
if self.imgmax < 600:
img_size = "Medium"
else:
img_size = "Large"
# check memcache
key = "album_%s_%s_%s" % (album, thumb_size, img_size)
photos = self.CacheGet(key)
if photos:
return photos
photos = []
sets = self.GetAllAlbums()
set_id = None
for s in sets:
if s['title'] == album:
set_id = s['id']
break
if not set_id:
return photos
logging.info('Found photoset %s with id %s' % (album, set_id))
photoset = flickr.Photoset(set_id, album, False)
for photo in photoset.getPhotos():
pic = {
'id': photo.id,
'height': 480,
'width': 640,
'thumb_url': photo.getURL(size=thumb_size, urlType='source'),
'url': photo.getURL(size=img_size, urlType='source'),
'name': photo.title,
}
photos.append(pic)
# set memcache
self.CacheSet(key, photos)
return photos
def CacheClear(self):
keys = []
albums = self.GetAllAlbums()
#for a in albums:
# keys.append("album_%s_%sc_%s" % (album['title'], self.thumb_size, self.imgmax))
# keys.append("album_%s_%su_%s" % (album['title'], self.thumb_size, self.imgmax))
#albums.append('albums')
#memcache.delete_multi(albums)
class UserPrefs(db.Model):
user = db.UserProperty(required=True)
photo_backend = db.IntegerProperty(choices=settings.PHOTO_BACKENDS, default=settings.PHOTO_BACKEND_PICASA, required=True)
site_title = db.StringProperty(default='photo')
site_header = db.StringProperty(default='photo')
thumb_size = db.IntegerProperty(choices=settings.THUMB_SIZES, default=settings.THUMB_SIZE_DEFAULT, required=True)
thumb_cropped = db.BooleanProperty(default=settings.THUMB_CROPPED_DEFAULT, required=True)
full_size = db.IntegerProperty(choices=settings.FULL_SIZES, default=settings.FULL_SIZE_DEFAULT, required=True)
homepage_size = db.IntegerProperty(choices=settings.FULL_SIZES, default=settings.HOMEPAGE_SIZE_DEFAULT, required=True)
homepage_album = db.StringProperty()
featured_albums = db.StringListProperty()
picasa_id = db.StringProperty(default='default')
flickr_id = db.StringProperty()
merchant_id = db.StringProperty()
analytics_id = db.StringProperty()
def GetUsername(self):
if self.photo_backend == settings.PHOTO_BACKEND_PICASA:
return self.picasa_id
elif self.photo_backend == settings.PHOTO_BACKEND_FLICKR:
return self.flickr_id
else:
return None
def SetUsername(self, user_id):
if self.photo_backend == settings.PHOTO_BACKEND_PICASA:
self.picasa_id = user_id
elif self.photo_backend == settings.PHOTO_BACKEND_FLICKR:
self.flickr_id = user_id
else:
return None
def GetPhotoBackend(self):
if self.photo_backend == settings.PHOTO_BACKEND_PICASA:
return PicasaBackend(self)
elif self.photo_backend == settings.PHOTO_BACKEND_FLICKR:
return FlickrBackend(self)
else:
return None