-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
110 lines (73 loc) · 2.94 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
import webapp2
import jinja2
from google.appengine.api import users
from google.appengine.ext import ndb
from models import *
from handlers import *
import os
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True
)
class MainPage(webapp2.RequestHandler):
def __init__(self, request, response):
self.initialize(request, response)
user = users.get_current_user()
if user:
url = users.create_logout_url(self.request.uri)
url_string = 'logout'
else:
url = users.create_login_url(self.request.uri)
url_string = 'login'
self.template_values = {
'url' : url,
'url_string' : url_string,
'user' : user
}
self.query = Room.query(ancestor=ndb.Key(Room, 'Room'))
def get_rooms(self):
return self.query.fetch(projection=[Room.number])
def get(self):
self.response.headers['Content-Type'] = 'text/html'
if self.template_values['user']:
self.template_values.update({'rooms' : self.get_rooms()})
template = JINJA_ENVIRONMENT.get_template('main.html')
self.response.write(template.render(self.template_values))
def post(self):
self.response.headers['Content-Type'] = 'text/html'
if not self.template_values['user']:
self.redirect('/')
button = self.request.get('button')
if button == 'Create':
room_number = self.request.get('no')
room_number = int(room_number) if room_number else 0
error = ''
if room_number > 0:
if self.query.filter(Room.number == room_number).count() == 0:
Room(parent=ndb.Key(Room, 'Room'), number=room_number).put()
self.redirect('/')
else:
error = "A room with the same room number already exists"
else:
error = "Please type in a room number greater than 1"
self.template_values['error'] = error
self.template_values['rooms'] = self.get_rooms()
elif button == 'Filter':
bookings = []
date = self.request.get('date')
if date:
date = datetime.strptime(date, '%Y-%m-%d').date()
for b in Booking.query(ancestor=ndb.Key(Booking, 'Booking')).fetch():
if b.time_from.date() <= date and b.time_to.date() >= date:
bookings.append(b)
self.template_values.update({
'bookings' : bookings,
'filter' : date
})
template = JINJA_ENVIRONMENT.get_template('main.html')
self.response.write(template.render(self.template_values))
app = webapp2.WSGIApplication([
('/', MainPage),
('/booking', BookingHandler)
], debug = True)