forked from aronsky/parking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.py
366 lines (315 loc) · 14.1 KB
/
handlers.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import os
import jinja2
import webapp2
from google.appengine.api import users
from datamodel import *
from shared import *
import json
JINJA_ENV = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainHandler(webapp2.RequestHandler):
def get(self):
logout_url = users.create_logout_url(self.request.uri)
user = users.get_current_user()
user.name = make_name(user)
user.inside = False
editablecars = list(Car.all().filter("owner = ", user).filter("plate != ", GUEST_PLATE))
usercars = editablecars
mustGetSpot = UserData.GetMustGetSpot()
multipleSpots = UserData.GetMultipleSpots()
spots = list(Spot.all().filter("future = ", False))
themename, subtheme, themecolor = UserData.GetTheme()
main = JINJA_ENV.get_template('templates/html/subpages/main.html')
downtime = JINJA_ENV.get_template('templates/html/subpages/downtime.html')
options = JINJA_ENV.get_template('templates/html/subpages/options.html')
future = JINJA_ENV.get_template('templates/html/subpages/future.html')
index = JINJA_ENV.get_template('templates/html/index.html')
mainpage_values = {
"logout_url": logout_url,
"user": user,
"mustGetSpot": mustGetSpot,
"multipleSpots": multipleSpots,
"freespots": len([spot for spot in spots if spot.free]),
"totalspots": len(spots),
"usercars": usercars,
"guestcar": Car.GuestCar(),
}
mainpage = main.render(mainpage_values)
downtime_values = {
"logout_url": logout_url,
"user": user,
"mustGetSpot": mustGetSpot,
}
downtimepage = downtime.render(downtime_values)
options_values = {
"logout_url": logout_url,
"editablecars": editablecars,
"themecolor": themecolor,
}
optionspage = options.render(options_values)
future_values = {
"logout_url": logout_url,
"multipleSpots": multipleSpots,
"freespots": len([spot for spot in spots if spot.free]),
"totalspots": len(spots),
"reservablespots": len([spot for spot in spots if not spot.reserved]),
"usercars": usercars,
"guestcar": Car.GuestCar(),
}
futurepage = future.render(future_values)
index_values = {
"themename": themename,
"subtheme": subtheme,
"themecolor": themecolor,
"mainpage": mainpage,
"optionspage": optionspage,
"futurepage": futurepage,
}
self.response.out.write(index.render(index_values))
class ScriptHandler(webapp2.RequestHandler):
def get(self):
multipleSpots = UserData.GetMultipleSpots()
script_tpl = JINJA_ENV.get_template('templates/js/main.js')
script_values = {
"multipleSpots": multipleSpots,
}
script = script_tpl.render(script_values)
self.response.headers["Content-Type"] = "text/javascript"
self.response.out.write(script)
class SetCarHandler(webapp2.RequestHandler):
def get(self):
try:
result = {}
result['plate'] = int(self.request.get('plate').replace('-',''))
result['make'] = self.request.get('make')
result['model'] = self.request.get('model')
result['color'] = self.request.get('color')
Car.Store(result['plate'], result['make'], result['model'], result['color'])
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))
class DeleteCarHandler(webapp2.RequestHandler):
def get(self):
try:
result = {}
result['plate'] = int(self.request.get('plate').replace('-',''))
Car.Delete(result['plate'])
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))
class GetSpotsHandler(webapp2.RequestHandler):
def get(self):
try:
user = users.get_current_user()
user.name = make_name(user)
user.inside = False
spots = Spot.all().filter('future = ', False)
json_inisde_spots = []
json_outside_spots = []
for spot in spots:
jspot = {}
jspot['number'] = spot.number
jspot['free'] = spot.free
jspot['comments'] = spot.comments
if not jspot['free']:
if spot.car.plate == GUEST_PLATE:
jspot['name'] = "Guest"
jspot['label'] = "Reserved"
jspot['plate'] = GUEST_PLATE
jspot['prettyplate'] = spot.car.prettyplate()
jspot['leavable'] = UserData.GetMultipleSpots()
else:
if spot.car.owner == user:
user.inside = True
jspot['leavable'] = True
jspot['name'] = make_name(spot.car.owner)
jspot['plate'] = spot.car.plate
jspot['prettyplate'] = spot.car.prettyplate()
jspot['label'] = spot.car.make + ' ' + spot.car.model
else:
jspot['parkable'] = True
if spot.outside:
json_outside_spots += [jspot]
else:
json_inisde_spots += [jspot]
result = {}
result['inside_spots'] = json_inisde_spots
result['outside_spots'] = json_outside_spots
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))
class GetFutureSpotsHandler(webapp2.RequestHandler):
def get(self):
try:
spots = Spot.all().filter('future = ', False)
json_inside_spots = []
json_outside_spots = []
for spot in spots:
jspot = {}
jspot['number'] = spot.number
jspot['reserved'] = spot.reserved is not None
jspot['comments'] = spot.reserved.comments if spot.reserved else ""
jspot['outside'] = spot.outside
if spot.reserved is not None and spot.reserved.car is not None:
if spot.reserved.car.plate == GUEST_PLATE:
jspot['name'] = "Guest"
jspot['label'] = "Reserved for a guest"
jspot['plate'] = GUEST_PLATE
else:
jspot['name'] = make_name(spot.reserved.car.owner)
jspot['label'] = spot.reserved.car.make + ' ' + spot.reserved.car.model
jspot['plate'] = spot.reserved.car.plate
if spot.outside:
json_outside_spots += [jspot]
else:
json_inside_spots += [jspot]
result = {}
result['inside_spots'] = json_inside_spots
result['outside_spots'] = json_outside_spots
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))
class GetTossHandler(webapp2.RequestHandler):
def get(self):
try:
user = users.get_current_user()
user.name = make_name(user)
allToss = TossReservation.all()
result = {}
result['reservedForTommorow'] = '0'
for toss in allToss:
if toss.car is not None and toss.car.plate != GUEST_PLATE:
if toss.car.owner == user:
result['reservedForTommorow'] = '1'
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))
class TakeSpotHandler(webapp2.RequestHandler):
def _take_specific(self):
spot = Spot.get(db.Key.from_path("Spot", self.request.get('spotnumber')))
car = Car.get(db.Key.from_path("Car", self.request.get('plate').replace('-','')))
comments = make_name(users.get_current_user()) if car.prettyplate() == Car.GuestCar().prettyplate() else self.request.get('comments')
spot.Take(car, comments=comments)
def _take_inside(self):
spot = Spot.all().filter("free =", True).filter("future =", False).filter("outside =", False).get()
car = Car.get(db.Key.from_path("Car", self.request.get('plate').replace('-','')))
comments = self.request.get('comments')
spot.Take(car, comments=comments)
def _take_outside(self):
spot = Spot.all().filter("free =", True).filter("future =", False).filter("outside =", True).get()
car = Car.get(db.Key.from_path("Car", self.request.get('plate').replace('-','')))
comments = self.request.get('comments')
spot.Take(car, comments=comments)
def get(self):
result = {}
try:
if self.request.get('spottype') == 'inside':
self._take_inside()
elif self.request.get('spottype') == 'outside':
self._take_outside()
else:
assert UserData.GetMultipleSpots(), Exception("This incident will be reported.")
self._take_specific()
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))
class LeaveSpotHandler(webapp2.RequestHandler):
def _leave_specific(self):
spot = Spot.get(db.Key.from_path("Spot", self.request.get('spotnumber')))
spot.Leave()
def _leave_any(self):
for spot in Spot.all():
if spot.car and spot.car.owner == users.get_current_user() and spot.car != Car.GuestCar():
spot.Leave()
def get(self):
result = {}
try:
if self.request.get('spotnumber') != "":
self._leave_specific()
else:
self._leave_any()
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))
class ReserveSpotHandler(webapp2.RequestHandler):
def get(self):
try:
result = {}
spot = Spot.get(db.Key.from_path("Spot", self.request.get('spotnumber')))
comments = self.request.get('comments') or make_name(users.get_current_user())
if bool(int(self.request.get('reserve'))):
car = Car.get(db.Key.from_path("Car", self.request.get('plate').replace('-','')))
spot.Reserve(True, car, comments)
else:
spot.Reserve(False)
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))
class SetThemeHandler(webapp2.RequestHandler):
def get(self):
try:
result = {}
UserData.SetTheme(
self.request.get('themename'),
self.request.get('subtheme'),
self.request.get('themecolor')
)
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))
class ReserveForTommorowHandler(webapp2.RequestHandler):
def get(self):
result = {}
try:
if bool(int(self.request.get('reserve'))):
toss = TossReservation()
preferSpotOutside = bool(int(self.request.get('preferSpotOutside')))
car = Car.get(db.Key.from_path("Car", self.request.get('plate').replace('-','')))
toss.Reserve(True, car, preferSpotOutside)
else:
for toss in TossReservation.all():
if toss.car is not None and toss.car.owner == users.get_current_user():
toss.Reserve(False)
result['result'] = 'success'
except Exception, e:
result = {"result": "error",
"reason": `e`,
"args" : self.request.arguments()
}
self.response.out.write(json.dumps(result))