This repository has been archived by the owner on May 1, 2023. It is now read-only.
/
main.py
636 lines (433 loc) · 15.5 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#!/usr/bin/env python
#
# 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.
#
import logging
import urllib
import json
import webapp2
from models import Item
from models import Room
from models import Residence
from models import Inventory
from models import ItemRoomRelation
from google.appengine.api import users
from google.appengine.ext import ndb
import jinja2
import os
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
from google.appengine.ext.webapp import template
class MainHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
self.redirect("welcome")
else:
template_data = {}
template_data['login_url'] = users.create_login_url("welcome")
template = JINJA_ENVIRONMENT.get_template('login.html')
self.response.write(template.render(template_data))
class AdminHandler(webapp2.RequestHandler):
def get(self):
items = Item.query().fetch()
rooms = Room.query().fetch()
relations = ItemRoomRelation.query().fetch()
item_dicts = []
for item in items:
item_data = {}
item_data["name"] = item.name
item_data["category"] = item.category
item_data["price"] = item.price
item_data["encoded"] = urllib.quote(item.name)
item_dicts.append(item_data)
room_dicts = []
for room in rooms:
room_data = {}
room_data["name"] = room.name
room_dicts.append(room_data)
template_data = {}
template_data['items'] = item_dicts
template_data['rooms'] = room_dicts
template_data['relations'] = relations
template = JINJA_ENVIRONMENT.get_template('admin.html')
self.response.write(template.render(template_data))
# def post(self):
# self.get
# Rooms handler
class RoomHandler(webapp2.RequestHandler):
def get(self):
rooms = Room.query()
names = []
for room in rooms.fetch():
names.append(str(room.name))
logging.debug(names)
self.response.write(json.dumps(names))
def post(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url('/welcome')
self.response.write('<html><body>{}</body></html>'.format('<a href="' + login_url + '">Sign in</a>'))
return
residence = Residence.get_residence_by_user(user)
name = self.request.get("name").strip()
if name == '':
self.response.write('no room name')
return
room = Room(name=name, parent=residence.key)
room.put()
self.redirect("/room-tour?room=" + name)
# def put(self):
# name = self.request.get("name")
# Create item
# Get user inventory
# class BagHandler(webapp2.RequestHandler):
# def get(self):
# username = self.request.get("user")
# app_user = AppUser.query(AppUser.username==username).get()
# if app_user is None:
# app_user = AppUser(username=username)
# app_user.put()
# bag_key = Bag.get_user_bag_key(username)
# items = Item.query(ancestor=bag_key).fetch()
# items_serialized = []
# for item in items:
# items_serialized.append(json.dumps(item.to_dict()))
# self.response.write(items_serialized)
# Get average price of a category
class PriceHandler(webapp2.RequestHandler):
def get(self):
category = self.request.get("category")
if category is '':
self.response.write("no item type provided")
return
items = Item.query(Item.category==category).fetch()
prices = []
for item in items:
prices.append(item.price)
if len(prices) > 0:
self.response.write(sum(prices)/len(prices))
return
self.response.write(json.dumps(0))
# Get user total value
class ValueHandler(webapp2.RequestHandler):
def get(self):
username = self.request.get("user")
user = AppUser.query(AppUser.username==username).get()
if user is None:
user = AppUser(username=username)
user.put()
bag_key = Bag.get_user_bag_key(username)
items = Item.query(ancestor=bag_key).fetch()
value = 0
for item in items:
value += item.price
self.response.write(json.dumps(value))
class RoomItemHandler(webapp2.RequestHandler):
def get(self):
name = self.request.get("name")
if name is '':
self.response.write("no room name given")
return
room = Room.query(Room.name==name).get()
room.popularity += 1
room.put()
self.response.write(json.dumps(room.item_categories))
return
class ResidenceTourHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url('/welcome')
self.response.write('<html><body>{}</body></html>'.format('<a href="' + login_url + '">Sign in</a>'))
return
residence = Residence.get_residence_by_user(user)
inventory = Inventory.get_inventory_by_user(user)
other_homes = Residence.query(Residence.own==residence.own).fetch()
logging.error(other_homes)
rooms = []
for home in other_homes:
rooms += Room.query(ancestor=home.key).filter(Room.name!="miscellaneous").fetch()
room_count = {}
for room in rooms:
name = room.name
if name in room_count:
room_count[name] += 1
continue
room_count[name] = 1
room_count_final = {}
for room in room_count:
my_count = Room.query(ancestor=residence.key).filter(Room.name==room).count()
if my_count == 0:
room_count_final[str(room)] = ("", room_count[room] / max(len(other_homes), 1))
else:
up_count = str(my_count + 1)
my_tail = ""
if up_count[-1:] in ["0", "4", "5", "6", "7", "8", "9"]:
my_tail = "th"
elif up_count[-1:] in ["2"]:
my_tail = "nd"
elif up_count[-1:] in ["1"]:
my_tail = "st"
elif up_count[-1:] in ["3"]:
my_tail = "rd"
room_count_final[str(room)] = (" (" + up_count + my_tail + ")", room_count[room] / max(len(other_homes), 1) - 2 * my_count)
room_count_final = sorted(room_count_final.items(), key=lambda x: x[1][1], reverse=True)
template = JINJA_ENVIRONMENT.get_template('tourRoomPage.html')
template_data = {'rooms': room_count_final}
self.response.write(template.render(template_data))
class RoomTourHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url('/welcome')
self.response.write('<html><body>{}</body></html>'.format('<a href="' + login_url + '">Sign in</a>'))
return
name = self.request.get("room")
if name is '':
self.response.write("no room specified")
ResidenceTourHandler
residence = Residence.get_residence_by_user(user)
room = Room.query(ancestor=residence.key).filter(Room.name==name).get()
if room is None:
room = Room(name=name, parent=residence.key)
room.put()
other_rooms = Room.query(Room.name==name).fetch()
relations = []
for other_room in other_rooms:
relations += ItemRoomRelation.query(ItemRoomRelation.room==other_room.key).fetch()
item_count = {}
for relation in relations:
category = relation.item.get().category
if category in item_count:
item_count[category] += 1
continue
item_count[category] = 1
logging.error(item_count)
inventory = Inventory.get_inventory_by_user(user)
item_count_final = {}
for item in item_count:
my_count = Item.query(ancestor=inventory.key).filter(Item.category==item).count()
if my_count == 0:
item_count_final[str(item)] = ("", item_count[item] / max(len(other_rooms), 1))
else:
up_count = str(my_count + 1)
my_tail = ""
if up_count[-1:] in ["0", "4", "5", "6", "7", "8", "9"]:
my_tail = "th"
elif up_count[-1:] in ["2"]:
my_tail = "nd"
elif up_count[-1:] in ["1"]:
my_tail = "st"
elif up_count[-1:] in ["3"]:
my_tail = "rd"
logging.error( "(" + up_count + my_tail + ")")
item_count_final[str(item)] = ("(" + up_count + my_tail + ")", item_count[item] / max(len(other_rooms), 1) - 2 * my_count)
item_count_final = sorted(item_count_final.items(), key=lambda x: x[1][1], reverse=True)
logging.error(item_count_final)
template = JINJA_ENVIRONMENT.get_template('tourItemPage.html')
template_data = {}
template_data['items'] = item_count_final
template_data['room'] = room.name
self.response.write(template.render(template_data))
class HomeHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url('/home')
template_data = {}
template_data['login_url'] = login_url
template = JINJA_ENVIRONMENT.get_template('login.html')
self.response.write(template.render(template_data))
return
inventory = Inventory.get_inventory_by_user(user)
items = inventory.get_items().fetch()
total_price = 0
logout_url = users.create_logout_url("/")
relations = []
for item in items:
relations += ItemRoomRelation.query(ancestor=inventory.key).filter(ItemRoomRelation.item==item.key).fetch()
total_price += item.price
relation_data = {}
for relation in relations:
item = relation.item.get()
item_data = {'category': item.category, 'name': item.name, 'price': "${0:.2f}".format(item.price), 'encoded': urllib.quote(item.name)}
if relation.room.get().name in relation_data:
relation_data[relation.room.get().name].append(item_data)
continue
relation_data[str(relation.room.get().name)] = [item_data]
logging.error(relation)
logging.error(relation_data)
template_data = {}
template_data['relations'] = relation_data
template_data['total_price'] = "${0:.2f}".format(total_price)
template_data['logout_url'] = logout_url
template = JINJA_ENVIRONMENT.get_template('userHomePage.html')
self.response.write(template.render(template_data))
class AddItemHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url('/home')
template_data = {}
template_data['login_url'] = login_url
template = JINJA_ENVIRONMENT.get_template('login.html')
self.response.write(template.render(template_data))
return
category = self.request.get("category")
room = self.request.get("room")
items = Item.query(Item.category==category).fetch()
if category != '':
prices = []
for item in items:
prices.append(item.price)
logging.error(item.price)
price = sum(prices) / len(prices) if len(prices) > 0 else 0
else:
price = 0
template_data = {}
template_data["category"] = category
template_data["price"] = "{0:.2f}".format(price)
template_data["room"] = room
template_data['back_url'] = "/home" if room == "miscellaneous" else "/room-tour?room=" + room
template = JINJA_ENVIRONMENT.get_template('addItemTour.html')
self.response.write(template.render(template_data))
def post(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url('/home')
template_data = {}
template_data['login_url'] = login_url
template = JINJA_ENVIRONMENT.get_template('login.html')
self.response.write(template.render(template_data))
return
residence = Residence.get_residence_by_user(user)
category = self.request.get('category')
name = self.request.get('name')
price = self.request.get('price')
room_name = self.request.get("room")
if name.strip() == '' or category.strip() == '':
if room_name == "miscellaneous":
self.redirect("home")
else:
self.redirect("/room-tour?room=" + urllib.quote(room_name))
return
price = float(price)
inventory = Inventory.get_inventory_by_user(user)
item = Item(category=category, name=name, price=price, parent=inventory.key)
item.put()
residence = Residence.get_residence_by_user(user)
room = Room.query(ancestor=residence.key).filter(Room.name==room_name).get()
if room is None:
room = Room(name=room_name, parent=residence.key)
room.put()
relation = ItemRoomRelation(item=item.key, room=room.key, parent=inventory.key)
relation.put()
if room_name == "miscellaneous":
self.redirect("/home")
return
self.redirect("/room-tour?room=" + urllib.quote(room_name))
class WelcomeHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url('/home')
template_data = {}
template_data['login_url'] = login_url
template = JINJA_ENVIRONMENT.get_template('login.html')
self.response.write(template.render(template_data))
return
template_data = {}
template = JINJA_ENVIRONMENT.get_template('landingPage.html')
self.response.write(template.render(template_data))
class DeleteItemHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url('/welcome')
self.response.write('<html><body>{}</body></html>'.format('<a href="' + login_url + '">Sign in</a>'))
return
name = self.request.get("name")
inventory = Inventory.get_inventory_by_user(user)
item = Item.query(ancestor=inventory.key).filter(Item.name==name).get()
if item is not None:
for relation in ItemRoomRelation.query(ancestor=inventory.key).filter(ItemRoomRelation.item==item.key).fetch():
relation.key.delete()
item.key.delete()
self.redirect("home")
class StatusHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user is None:
self.redirect("/")
return
residence = Residence.get_residence_by_user(user)
status = self.request.get("status")
residence.own = True if status == "owner" else False
logging.error(residence.own)
residence.put()
self.redirect("home-tour")
class PieHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url('/home')
template_data = {}
template_data['login_url'] = login_url
template = JINJA_ENVIRONMENT.get_template('login.html')
self.response.write(template.render(template_data))
return
inventory = Inventory.get_inventory_by_user(user)
items = inventory.get_items().fetch()
logout_url = users.create_logout_url("/")
relations = []
for item in items:
relations += ItemRoomRelation.query(ancestor=inventory.key).filter(ItemRoomRelation.item==item.key).fetch()
i = 0
relation_data = {}
for relation in relations:
i += 1
if i > 5:
break
room_name = relation.room.get().name
if room_name in relation_data:
relation_data[room_name] += relation.item.get().price
else:
relation_data[room_name] = relation.item.get().price
i = 0
cols = ["#4789b", "#EF7014", "#4BA449", "#154995", "#757374"]
relation_data_final = []
for k in relation_data:
i += 1
relation_data_final.append({"title": str(k), "value": relation_data[k], "color": cols[i]})
relation_data_final = sorted(relation_data_final, key=lambda x: x["value"], reverse=True)
template_data = {}
template_data['relations'] = relation_data_final
self.response.write(json.dumps(relation_data_final))
app = webapp2.WSGIApplication([
('/', MainHandler)
, ('/room', RoomHandler)
, ('/admin', AdminHandler)
, ('/home-tour', ResidenceTourHandler)
, ('/room-tour', RoomTourHandler)
, ('/add', AddItemHandler)
, ('/home', HomeHandler)
, ('/welcome', WelcomeHandler)
, ('/delete', DeleteItemHandler)
, ('/status', StatusHandler)
, ('/pie', PieHandler)
], debug=True)