-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.py
More file actions
59 lines (46 loc) · 1.75 KB
/
setup.py
File metadata and controls
59 lines (46 loc) · 1.75 KB
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
"""The setup module for first-time initialization of the app."""
import admin
from appengine_config import JINJA_ENVIRONMENT
from config import PATHS
from datastore import User
from datastore import OAuth
from datastore import DomainVerification
from error_handlers import Handle500
import webapp2
import xsrf
def _RenderSetupOAuthClientTemplate():
"""Render a setup page with inputs for client id and secret."""
entity = OAuth.GetOrInsertDefault()
domain_verification = DomainVerification.GetOrInsertDefault()
template_values = {
'client_id': entity.client_id,
'client_secret': entity.client_secret,
'dv_content': domain_verification.content,
}
template = JINJA_ENVIRONMENT.get_template('templates/setup_client.html')
return template.render(template_values)
class SetupOAuthClientHandler(webapp2.RequestHandler):
"""Setup a client in the datastore."""
@admin.RequireAppAdmin
def get(self):
"""Output the oauth and domain verification template."""
self.response.write(_RenderSetupOAuthClientTemplate())
@admin.RequireAppAdmin
@xsrf.XSRFProtect
def post(self):
"""Store client id, client secret, and domain verification content."""
client_id = self.request.get('client_id')
client_secret = self.request.get('client_secret')
OAuth.Update(client_id, client_secret)
OAuth.Flush()
dv_content = self.request.get('dv_content')
DomainVerification.Update(dv_content)
if User.GetCount() > 0:
self.redirect(PATHS['user_page_path'])
else:
self.redirect(PATHS['user_add_path'])
APP = webapp2.WSGIApplication([
(PATHS['setup_oauth_path'], SetupOAuthClientHandler),
], debug=True)
# This is the only way to catch exceptions from the oauth decorators.
APP.error_handlers[500] = Handle500