-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcontrollers.py
113 lines (87 loc) · 2.86 KB
/
controllers.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
import os
import sys
import wsgiref.handlers
import logging
import datetime
import urllib2
import settings
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from simplejson import dumps
from libs.lilcookies import *
from libs import oauth
from utils import *
from managers import *
template.register_template_library('filters')
def require_login(func):
def validate(self, *args):
user = users.get_current_user()
if user is None:
self.redirect(self.get_login_url())
return
else:
self.set_current_user(get_or_create_user(user))
self.user = get_or_create_user(user)
func(self, *args)
return validate
class BaseHandler(webapp.RequestHandler):
def get_login_url(self):
return users.create_login_url(self.request.uri)
def get_logout_url(self):
return users.create_logout_url("/")
def set_current_user(self, user):
self.user = user
def render(self, template_file, template_vars=None):
if template_vars is None:
template_vars = {}
if hasattr(self, 'user'):
template_vars['current_user'] = self.user
template_vars['debug'] = settings.DEBUG == True
template_vars['host'] = settings.get_host()
template_vars['login_url'] = self.get_login_url()
template_vars['logout_url'] = self.get_logout_url()
path = os.path.join(os.path.dirname(__file__), "views", template_file)
self.response.out.write(template.render(path, template_vars))
class Index(BaseHandler):
def get(self):
self.render('index.html', {})
class Dashboard(BaseHandler):
@require_login
def get(self):
self.render('dashboard.html', {})
class LoginHandler(BaseHandler):
def get(self):
auth_token = self.request.get("oauth_token")
auth_verifier = self.request.get("oauth_verifier")
user_info = settings.twitter_client.get_user_info(auth_token, auth_verifier=auth_verifier)
user = create_user(
user_info.get('id'),
user_info.get('username'),
user_info.get('name'),
user_info.get('picture'),
user_info.get('token'),
user_info.get('secret')
)
cookieutil = LilCookies(self, settings.SECURE_COOKIE)
cookieutil.set_secure_cookie(name = '_wda', value = str(user.twitter_id), expires_days= 365)
pending_pledge = cookieutil.get_secure_cookie('pending_pledge')
if pending_pledge:
pc = PledgeFavor()
pc.initialize(self.request, self.response)
pc.post(pending_pledge, user)
else:
self.redirect('/dashboard')
def main():
application = webapp.WSGIApplication([
#nav
('/', Index),
('/dashboard', Dashboard),
#oauth
('/oauth/verify', LoginHandler)
], debug=settings.DEBUG)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()