-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.py
executable file
·173 lines (151 loc) · 4.92 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
#
# Copyright 2015 - present The Direct Authors. All Rights Reserved.
#
# 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 webapp2
import os
import json
import yaml
import jinja2
import urllib
import logging
from models import Spec
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.api import users
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_dir),
autoescape=True,
variable_start_string='{{{',
variable_end_string='}}}'
)
config = yaml.safe_load(open('config.yaml'))
class BaseHandler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a,**kw)
def render_str(self, template, params):
t = jinja_environment.get_template(template)
if self.adminCheck():
params['admin'] = True;
return t.render(params)
def render(self, template, kw):
self.write(self.render_str(template, kw))
def adminCheck(self):
user = users.get_current_user()
if user:
if users.is_current_user_admin():
return True;
else:
return False
else:
return False
class MainHandler(BaseHandler):
def get(self):
if config is not None and 'headers' in config:
for key, value in config['headers'].items():
self.response.headers.add_header(key, value)
self.render('index.html', {'title': 'Motion Spec'})
class ApiHandler(webapp2.RequestHandler):
# CREATE
def post(self):
find_key = db.Key.from_path('Spec', 1)
key_batch = db.allocate_ids(find_key, 1)
spec = Spec(id=key_batch[0])
new_id = spec.put().id()
if self.request.body != '':
self.put(new_id)
self.response.headers['Content-Type'] = 'application/json'
self.response.write(json.dumps({'id':new_id}))
# VIEW
def get(self, id=None):
self.response.headers.add_header('Access-Control-Allow-Origin', '*')
self.response.headers.add_header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS')
self.response.headers.add_header('Access-Control-Allow-Headers', 'X-Requested-With, content-type')
if id:
spec = Spec.get_by_id(int(id))
if spec:
self.response.write(spec.json)
else:
self.response.set_status('404')
else:
group = self.request.get('group')
sort_by = self.request.get('sortby')
owner = self.request.get('owner')
specs = Spec.get_infos(1000, group, sort_by, owner) # TODO: Increased output from 100 to 1000, must implement pagination eventually
self.response.write(json.dumps(specs))
# EDIT
def put(self, id=None):
if id is None:
self.response.set_status('404')
return
spec = Spec.get_by_id(int(id))
if spec:
# Spec Owner
payload = json.loads(self.request.body)
owner = payload['permissions']['owner']
# Current User
user = users.get_current_user()
if user and user.nickname() == owner:
spec.load_json_with_id(self.request.body, id)
spec.put()
else:
self.response.set_status('403')
else:
self.response.set_status('404')
# DELETE
def delete(self, id=None):
if id is None:
self.response.set_status('404')
return
spec = Spec.get_by_id(int(id))
if spec:
spec.key.delete()
else:
self.response.set_status('404')
# Get current user
class UserHandler(webapp2.RequestHandler):
def get(self, id=None):
user = users.get_current_user()
if user:
self.response.write(user.nickname())
else:
# self.response.write('andershaig')
self.response.set_status('403')
class LoginHandler(webapp2.RequestHandler):
def get(self, id=None):
user = users.get_current_user()
if user:
self.redirect('/')
else:
self.redirect(users.create_login_url('/'))
class LogoutHandler(webapp2.RequestHandler):
def get(self, id=None):
user = users.get_current_user()
if user:
self.redirect(users.create_logout_url('/'))
else:
self.redirect('/')
# Setup Routes
app = webapp2.WSGIApplication([
# CLIENT SIDE PAGES (LET ANGULAR HANDLE ROUTING)
(r'/', MainHandler),
# OTHER SERVER PAGES / ENDPOINTS
(r'/api/spec', ApiHandler),
(r'/api/spec/(\d+)', ApiHandler),
(r'/user', UserHandler),
(r'/login', LoginHandler),
(r'/logout', LogoutHandler)
], debug=True)