-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcubed.py
145 lines (92 loc) · 3.3 KB
/
cubed.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
#!/usr/bin/env python
import os
import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import util
from google.appengine.api import users
from models import User
from constants import *
#What the Template App Still Needs
#- Basic Security Features
#- Data validation decorator
#- Sign in
#- Must sign in decorator
# Decorators
def MustLogIn(inFunction):
"""Must Log in Decorator - Ensures that user is logged in"""
def outFunction(*args,**kwargs):
user = users.get_current_user()
if user:
return inFunction(*args,**kwargs)
else :
self.redirect(users.create_login_url("/save"))
return outFunction
# Home Page Handler
class MainHandler(webapp.RequestHandler):
def get(self):
content_template_values = {
}
self.response.out.write(RenderFullPage('page1.html', content_template_values))
# Page Handlers
class PageHandler1(webapp.RequestHandler):
def get(self):
template_values = {
}
path = os.path.join(os.path.dirname(__file__), 'templates/page1.html')
self.response.out.write(template.render(path, template_values))
class PageHandler2(webapp.RequestHandler):
@MustLogIn
def get(self):
saved_users = User.all().fetch(100)
template_values = {
'saved_users': saved_users
}
path = os.path.join(os.path.dirname(__file__), 'templates/page2.html')
self.response.out.write(template.render(path, template_values))
# Gets
class GetMethod(webapp.RequestHandler):
def get(self):
return 0
# Posts
class PostMethod(webapp.RequestHandler):
def post(self):
return 0
class SaveUser(webapp.RequestHandler):
@MustLogIn
def get(self):
google_user = users.get_current_user()
db_user = User.get_by_google_user(google_user)
if db_user:
self.redirect('/')
else:
db_user = User()
db_user.google_user = google_user
db_user.put()
self.redirect('/')
# Helpers
def RenderFullPage(template_file_name, content_template_values):
"""This re-renders the full page with the specified template."""
main_path = os.path.join('templates/index.html')
content_path = os.path.join('templates/' + template_file_name )
content = template.render(content_path, content_template_values)
user = users.get_current_user()
if user:
login = ("Welcome, %s! (<a href=\"%s\">Sign out</a>)" % (user.nickname(), users.create_logout_url("/")))
else:
login = ("<a href=\"%s\">Sign in With Google</a>." % users.create_login_url("/save"))
template_values = {
'CONTENT': content,
'LOGIN': login
}
return template.render(main_path, template_values)
appRoute = webapp.WSGIApplication( [('/page1', PageHandler1),
('/page2', PageHandler2),
('/save', SaveUser),
('/', MainHandler)
], debug=True)
def main():
run_wsgi_app(appRoute)
if __name__ == '__main__':
main()