-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.py
154 lines (103 loc) · 4.68 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
import webapp2
import jinja2
import os
from google.appengine.api import users
from google.appengine.ext import ndb
from models import *
from funcs import *
from datetime import datetime, timedelta
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True
)
class BookingHandler(webapp2.RequestHandler):
def __init__(self, request, response):
self.initialize(request, response)
checklogin(users.get_current_user(), self)
self.room = ndb.Key(urlsafe=self.request.get('k')).get()
self.query = Booking.query(ancestor=ndb.Key(Booking, 'Booking')).filter(Booking.room_id == self.room.key.id())
self.template_values = {
'room' : self.room,
'has_bookings': self.has_bookings(),
'url' : users.create_logout_url(self.request.uri),
'url_string' : 'logout'
}
def get_bookings(self):
return self.query.order(Booking.created).fetch()
def has_bookings(self):
return self.query.count(limit=1) > 0
def delete_booking(self):
booking = ndb.Key(urlsafe=self.request.get('d')).get()
if booking and booking.room_id == self.room.key.id():
booking.key.delete()
self.redirect('booking?k=' + self.room.key.urlsafe())
def delete_room(self):
if not self.template_values['has_bookings']:
self.room.key.delete()
self.redirect('/')
else:
self.redirect('booking?k=' + self.room.key.urlsafe())
def get(self):
self.response.headers['Content-Type'] = 'text/html'
if self.request.get('d'):
self.delete_booking()
elif self.request.get('remove'):
self.delete_room()
elif self.request.get('deleteAll'):
now = datetime.now().replace(microsecond=0)
for b in self.query.fetch(projection=[Booking.time_to]):
if b.time_to <= now:
b.key.delete()
self.redirect('booking?k=' + self.room.key.urlsafe())
self.template_values.update({
'booking': Booking(),
'bookings': self.get_bookings()
})
template = JINJA_ENVIRONMENT.get_template('booking_form.html')
self.response.write(template.render(self.template_values))
def post(self):
self.response.headers['Content-Type'] = 'text/html'
button = self.request.get('button')
error = ''
if button == 'Book':
booking = Booking(parent=ndb.Key(Booking, 'Booking'))
booking.room_id = self.room.key.id()
booking.name = self.request.get('name').strip()
if not booking.name:
error = "Please type in your full name"
booking.email = self.request.get('email').strip()
if not booking.email:
error = "Please type in a correct email address"
people = self.request.get('no_of_people')
if not people or people == '0':
error = "No of People must be greater than 1"
else:
booking.no_of_people = int(people)
if self.request.get('from') and self.request.get('to'):
booking.time_from = datetime.strptime(self.request.get('from'), '%Y-%m-%dT%H:%M')
booking.time_to = datetime.strptime(self.request.get('to'), '%Y-%m-%dT%H:%M')
if booking.time_to > booking.time_from and booking.time_from > datetime.now():
clash = False
for b in self.query.filter(Booking.time_to > booking.time_from).fetch(projection=[Booking.time_from]):
clash = b.time_from <= booking.time_to
if clash:
break
if not clash:
booking.put()
self.redirect('/booking?k=' + self.room.key.urlsafe())
else:
error = "Choose a date that does not overlap with an existing booking"
else:
error = "Select future dates and make sure To is later than From "
else:
error = "Select correct values for From and To"
self.template_values.update({
'booking': booking,
'bookings': self.get_bookings(),
'error' : error
})
template = JINJA_ENVIRONMENT.get_template('booking_form.html')
self.response.write(template.render(self.template_values))
else:
self.redirect('/')