/
album.py
197 lines (171 loc) · 6.33 KB
/
album.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
# !/usr/bin/env python
#
# Copyright 2008 CPedia.com.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = 'Ping Chen'
import gdata.photos.service
import gdata.media
import gdata.geo
import atom
import cgi
import wsgiref.handlers
import os
import re
import datetime
import calendar
import logging
import string
from xml.etree import ElementTree
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.api import memcache
import authorized
import view
import util
from model import Album
import gdata.urlfetch
gdata.service.http_request_handler = gdata.urlfetch
class BaseRequestHandler(webapp.RequestHandler):
def generate(self, template_name, template_values={}):
values = {
'request': self.request,
}
values.update(template_values)
directory = os.path.dirname(__file__)
view.ViewPage(cache_time=0).render(self, template_name,values)
def getAlbumFeedEntry(self,album_username):
key_albums = "albums_"+ album_username
try:
feed = memcache.get(key_albums)
except Exception:
feed = None
if not feed:
gd_client = gdata.photos.service.PhotosService()
try:
feed = gd_client.GetUserFeed(user= album_username)
except Exception:
return None
memcache.add(key=key_albums, value=feed, time=3600)
return feed
def validatorFeedAndReturnTemplate(self,feed,album_username,usernames):
if feed == None:
template_values ={
'error':"Can not retrieve the picasaweb album(s) of user '"+album_username+"', please " +
"make sure you set the correct picasaweb account."
}
else:
template_values = {
'username':album_username,
'usernames':usernames,
'albums':feed.entry,
}
return template_values
class MainPage(BaseRequestHandler):
def get(self):
usernames = util.getAlbumList()
if usernames:
defaultAlbum = usernames[0]
album_username = defaultAlbum.album_username
feed = self.getAlbumFeedEntry(defaultAlbum.album_username)
template_values = self.validatorFeedAndReturnTemplate(feed,album_username,usernames)
else:
template_values ={
'error':"Please set picasaweb album(s) in the system configuration."
}
self.generate('album_main.html',template_values)
class UserHandler(BaseRequestHandler):
def get(self, username):
usernames = util.getAlbumList()
album = db.Query(Album).filter('album_username =',username).get()
if album is None:
self.redirect("/albums")
feed = self.getAlbumFeedEntry(album.album_username)
template_values = self.validatorFeedAndReturnTemplate(feed,album.album_username,usernames)
self.generate('album_main.html',template_values)
class UserPrivateHandler(BaseRequestHandler):
@authorized.authSub("albums")
def get(self, username):
usernames = util.getAlbumList()
album = db.Query(Album).filter('album_username =',username).get()
if album is None:
self.redirect("/albums")
feed = self.getAlbumFeedEntry(album.album_username)
template_values = self.validatorFeedAndReturnTemplate(feed,album.album_username,usernames)
self.generate('album_main.html',template_values)
class AlbumHandler(BaseRequestHandler):
#@authorized.authSub('albums')
def get(self, username, album_name):
gd_client = gdata.photos.service.PhotosService()
key_photos = "photos_"+username+"_"+album_name
try:
feed_photos = memcache.get(key_photos)
except Exception:
feed_photos = None
if not feed_photos:
feed_photos = gd_client.GetFeed(
'/data/feed/api/user/%s/album/%s?kind=photo' % (
username, album_name))
memcache.add(key=key_photos, value=feed_photos, time=3600)
key_albums = "albums_"+ username
try:
feed_albums = memcache.get(key_albums)
except Exception:
feed_albums = None
if not feed_albums:
feed_albums = gd_client.GetUserFeed(user=username)
memcache.add(key=key_albums, value=feed_albums, time=3600)
album = None
for album_ in feed_albums.entry:
if album_.name.text == album_name:
album = album_
break
template_values = {
'photos': feed_photos.entry,
'album': album,
'username':username,
'album_name':album_name,
}
self.generate('album_view.html',template_values)
#deprecated
class PhotoHandler(BaseRequestHandler):
def get(self, username, album_name, photoId):
gd_client = gdata.photos.service.PhotosService()
feed = gd_client.GetFeed(
'/data/feed/api/user/%s/album/%s?kind=photo' % (
username, album_name))
i=0
for photo in feed.entry:
if photo.gphoto_id.text == photoId:
break
i=i+1
cur_photo = feed.entry[i]
if(i==0):
next_photo = feed.entry[i+1]
pre_photo = None
else:
pre_photo = feed.entry[i-1]
if i==len(feed.entry)-1:
next_photo = None
else:
next_photo = feed.entry[i+1]
template_values = {
'cur_photo': cur_photo,
'pre_photo': pre_photo,
'next_photo': next_photo,
'username':username,
'album_name':album_name,
}
self.generate('album_photo_view.html',template_values)