-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (107 loc) · 3.67 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
#!/usr/bin/env python3
#
# Copyright 2007 Google Inc.
#
# 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.
#
from __future__ import absolute_import
from __future__ import print_function
import datetime
import json
import os
import time
import jinja2
import webapp2
from webapp2_extras import securecookie
import google.appengine.api
import config
from signin import *
# Need a local app_secrets.py file containing the secrets.
# Use app_secrets.tmpl as a template.
import app_secrets
from google.appengine.ext import db
from google.appengine.api import namespace_manager
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
autoescape=True)
COOKIES = securecookie.SecureCookieSerializer(app_secrets.SECRET)
class BoardSpecs(db.Model):
spec = db.StringProperty()
date = db.StringProperty()
id = db.IntegerProperty();
class BaseHandler(webapp2.RequestHandler):
def SignedInIdentity(self):
if os.environ['SERVER_SOFTWARE'].find('Development') == 0:
return 'dev@home'
cookie = self.request.cookies.get('user')
user = COOKIES.deserialize('user', cookie)
if user is None:
template = JINJA_ENVIRONMENT.get_template('templates/signin.html')
self.response.write(template.render({}))
return None
else:
namespace_manager.set_namespace(user.replace('@', '_'))
return user
class MainHandler(BaseHandler):
def get(self):
user = self.SignedInIdentity()
if user is None: return
template = JINJA_ENVIRONMENT.get_template('templates/index.html')
self.response.write(template.render(user=user))
class saveSpecHandler(BaseHandler):
def post(self):
user = self.SignedInIdentity()
if user is None: return
print((self.request.body));
dict = json.loads(self.request.body);
entry = BoardSpecs()
if 'id' in dict:
id = int(dict['id']);
entry = BoardSpecs(key=db.Key.from_path('BoardSpecs', id));
entry.id = id;
entry.spec = json.dumps(dict['spec'])
entry.date = str(datetime.datetime.now())
entry.put()
if entry.id is None:
entry.id = entry.key().id();
entry.put();
self.response.write(entry.key().id())
class deleteSpecHandler(BaseHandler):
def post(self):
user = self.SignedInIdentity()
if user is None: return
print((self.request.body));
dict = json.loads(self.request.body);
id = int(dict['id']);
print(id);
entry = BoardSpecs(key=db.Key.from_path('BoardSpecs', id));
entry.delete()
self.response.write('OK')
class listSpecsHandler(BaseHandler):
def get(self):
user = self.SignedInIdentity()
if user is None: return
time.sleep(1)
entries = db.GqlQuery("SELECT * from BoardSpecs ORDER BY date DESC")
print(('list ', json.dumps([db.to_dict(e) for e in entries])))
self.response.out.write(json.dumps([db.to_dict(e) for e in entries]))
ROUTES = [
('/deleteSpec', deleteSpecHandler),
('/saveSpec', saveSpecHandler),
('/listSpecs', listSpecsHandler),
('/logout', Logout),
webapp2.Route(r'/login/<:.*>', Login, handler_method='any'),
('/.*', MainHandler),
]
app = webapp2.WSGIApplication(ROUTES, debug=True)
app = google.appengine.api.wrap_wsgi_app(app)