forked from livid/v2ex-gae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avatar.py
46 lines (38 loc) · 1.67 KB
/
avatar.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
#!/usr/bin/env python
# coding=utf-8
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from v2ex.babel import Avatar
from v2ex.babel.security import *
from v2ex.babel.da import *
class AvatarHandler(webapp.RequestHandler):
def get(self, member_num, size):
avatar = GetKindByName('Avatar', 'avatar_' + str(member_num) + '_' + str(size))
if avatar:
self.response.headers['Content-Type'] = "image/png"
self.response.headers['Cache-Control'] = "max-age=172800, public, must-revalidate"
self.response.headers['Expires'] = "Sun, 25 Apr 2011 20:00:00 GMT"
self.response.out.write(avatar.content)
else:
self.redirect('/static/img/avatar_' + str(size) + '.png')
class NodeAvatarHandler(webapp.RequestHandler):
def get(self, node_num, size):
avatar = GetKindByName('Avatar', 'node_' + str(node_num) + '_' + str(size))
if avatar:
self.response.headers['Content-Type'] = "image/png"
self.response.headers['Cache-Control'] = "max-age=172800, public, must-revalidate"
self.response.headers['Expires'] = "Sun, 25 Apr 2011 20:00:00 GMT"
self.response.out.write(avatar.content)
else:
self.error(404)
def main():
application = webapp.WSGIApplication([
('/avatar/([0-9]+)/(large|normal|mini)', AvatarHandler),
('/navatar/([0-9]+)/(large|normal|mini)', NodeAvatarHandler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()