This repository has been archived by the owner on Sep 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
executable file
·156 lines (128 loc) · 4.33 KB
/
main.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
#!/usr/bin/env python
#
# Copyright 2010 Brad Fitzpatrick
#
# 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.
import os
import re
import logging
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
import consumer
import models
def GetCurrentUser(request):
"""Returns a User entity (OpenID or Google) or None."""
user = users.get_current_user()
if user:
return models.User(google_user=user)
session_id = request.cookies.get('session', '')
if not session_id:
return None
login = consumer.Login.get_by_key_name(session_id)
if not login:
return None
return models.User(openid_user=login.claimed_id)
class IndexHandler(webapp.RequestHandler):
def get(self):
user = GetCurrentUser(self.request)
template_values = {
"user": user,
}
self.response.out.write(template.render("index.html", template_values))
class SiteHandler(webapp.RequestHandler):
def get(self):
self.response.out.write("I'm a site page.")
class LoginHandler(webapp.RequestHandler):
def get(self):
next_url = self.request.get("next")
if not re.match(r'^/[\w/]*$', next_url):
next_url = '/'
#logging.info("next_url: %s", next_url)
user = GetCurrentUser(self.request)
google_login_url = users.create_login_url(next_url)
template_values = {
"user": user,
"google_login_url": google_login_url,
}
self.response.out.write(template.render("login.html", template_values))
class LogoutHandler(webapp.RequestHandler):
def get(self):
next_url = self.request.get("next")
if re.match(r'^/[\w/]*$', next_url):
next_url = '/'
user = GetCurrentUser(self.request)
if user:
user.LogOut(self, next_url)
else:
self.redirect(next_url)
class CreateHandler(webapp.RequestHandler):
def get(self):
user = GetCurrentUser(self.request)
if not user:
self.redirect('/s/login?next=/s/create')
return
template_values = {
"user": user,
}
self.response.out.write(template.render("create.html", template_values))
def post(self):
user = GetCurrentUser(self.request)
if not user:
self.redirect('/s/login?next=/s/create')
return
def error(msg):
self.response.out.write("Error creating project:<ul><li>%s</li></ul>." %
msg)
return
project_key = self.request.get('project')
if not project_key:
return error("No project specified.")
if not re.match(r'^[a-z][a-z0-9\.\-]*[a-z0-9]$', project_key):
return error("Project name must match regular expression " +
"<tt>/^[a-z][a-z0-9\.\-]*[a-z0-9]$/</tt>.")
project = models.Project.get_by_key_name(project_key)
if project:
return error("Project already exists: <a href='/%s'>%s</a>" %
(project_key, project_key))
user = user.GetOrCreateFromDatastore()
project = models.Project(key_name=project_key,
owner=user)
project.put()
self.redirect("/%s" % project_key)
class ProjectHandler(webapp.RequestHandler):
def get(self, project_key):
user = GetCurrentUser(self.request)
project = models.Project.get_by_key_name(project_key)
if not project:
self.response.set_status(404)
template_values = {
"user": user,
"project": project,
}
self.response.out.write(template.render("project.html", template_values))
def main():
application = webapp.WSGIApplication([
('/', IndexHandler),
('/s/create', CreateHandler),
('/s/login', LoginHandler),
('/s/logout', LogoutHandler),
('/s/.*', SiteHandler),
(r'/([a-z][a-z0-9\.\-]*[a-z0-9])/?', ProjectHandler),
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()