This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
89 lines (75 loc) · 3.05 KB
/
handler.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Base handler, used in most of handlers.
"""
import webapp2
from google.appengine.ext import ndb
from webapp2_extras import jinja2
from webapp2_extras import sessions
from xml.dom import minidom
import model
import urllib2
IP_URL = "http://api.hostip.info/?ip="
class Base(webapp2.RequestHandler):
""" The other handlers inherit from this class. Provides some helper methods
for rendering a template.
The webapp2.cached_property decorator does two things:
1. Caches the value after the first time the property is accessed (rather than
recalculate it each time). Our session store and session aren’t going to
change in the course of the request, so it will speed things up to cache them.
2. Converts the method into a property like using the builtin @property
decorator so you can access it like self.session['some_var'] rather than
self.session()['some_var'].
"""
def dispatch(self):
""" One of the advantadges of webapp2 over webapp is that you can wrap the
dispatching process of webapp2.RequestHandler to perform actions before
and/or after the requested method is dispatched. You can do this overriding
the webapp2.RequestHandler.dispatch() method. This can be useful, for
example, to test if requirements were met before actually dispatching the
requested method, or to perform actions in the response object after the
method was dispatched.
"""
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def session_store(self):
return sessions.get_store(request=self.request)
@webapp2.cached_property
def session(self):
""" Returns a session using the default cookie key.
"""
return self.session_store.get_session(backend='securecookie')
def jinja2_factory(self, app):
""" True ninja method for attaching additional globals or filters to jinja2.
"""
j = jinja2.Jinja2(app)
j.environment.filters.update({
})
j.environment.globals.update({
'uri': webapp2.uri_for,
})
return j
@webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(factory=self.jinja2_factory)
def render(self, _template, **context):
rv = self.jinja2.render_template(_template, **context)
self.response.write(rv)
def write(self, string):
return self.response.out.write(unicode(string))
def write_ndb(self, uid, **kwargs):
user = model.User()
user.populate(uid=uid,
name=kwargs['name'],
city=kwargs['city'],
country=kwargs['country'],
photo=kwargs['photo'],
friends=kwargs['friends'],
token=kwargs['token'])
user.key = ndb.Key(model.User, user.uid)
user.put()