-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
317 lines (276 loc) · 13 KB
/
Copy pathserver.py
File metadata and controls
317 lines (276 loc) · 13 KB
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
# imported modules
from flask import Flask, jsonify, request, render_template, redirect, abort
# import testing function from test_mode.py
from test_mode import test
# import database module
import sqlite3
# api framework
app = Flask(__name__)
database = "booking.db"
class Event:
def __init__(self, name, capacity):
self.name = name
self.capacity = capacity
def get_name(self):
return self.name
def get_capacity(self):
return self.capacity
class Room:
def __init__(self, name, capacity):
self.name = name
self.capacity = capacity
def get_name(self):
return self.name
def get_capacity(self):
return self.capacity
class Timetable:
@staticmethod
def get_slot(room, day, time):
return Timetable.create_timetable(room)[day][time]
@staticmethod
def get_days():
return ["Mon", "Tues", "Wed", "Thurs", "Fri"];
@staticmethod
def get_time_slots():
return ["08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00"]
@staticmethod
def get_rooms():
rooms = []
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
result = cursor.execute("SELECT * from rooms").fetchall()
for room in result:
current_room = Room(room[0], room[1])
rooms.append(current_room)
return rooms
@staticmethod
def create_timetable(room):
try:
# initialize timetable
dic = {}
days = Timetable.get_days()
time_slots = Timetable.get_time_slots()
# create the room list with all the rooms in room_file.txt
# no events at the start
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
result = cursor.execute("SELECT * from timetable where name = ?", (room,)).fetchone()
i = 1
for day in days:
dic[day] = {}
for time in time_slots:
dic[day][time] = result[i].split()[time_slots.index(time)]
i += 1
return dic
except:
return "There are no rooms registered!"
class API:
def __init__(self):
app.run(host="127.0.0.1", port="5000", threaded=True)
@app.route("/", methods=["GET", "POST"])
def display_timetable():
if request.method == "POST":
option = request.values.get('redirect')
if option == "room_timetable":
return redirect("/timetable/room")
elif option == "availability":
return redirect("/timetable/available")
elif option == "test_mode":
return redirect("/timetable/admin/test/mode")
elif option == "room_list":
return redirect("/timetable/rooms")
elif option == "error_handling":
return redirect("/timetable/admin/test/error")
elif option == "add_room":
return redirect("/timetable/room/add")
else:
return redirect("/timetable/book")
else:
return render_template("index.html")
# error handling
# not found
@app.errorhandler(404)
def page_not_found(page):
uri_list = []
for rule in app.url_map.iter_rules():
if ":" not in rule.rule:
uri_list.append(rule.rule)
return render_template('404.html', uri_list=uri_list), 404
# server fault
@app.errorhandler(500)
def overload(page):
print(page)
return render_template('500.html'), 500
# bad request
@app.errorhandler(400)
def bad_request(page):
return render_template('400.html'), 400
@app.route("/timetable/room", methods=["POST", "GET"])
def room_timetable():
if request.method == "POST":
try:
room_number = request.values.get('room')
return render_template("timetable.html", timetable=Timetable.create_timetable(room_number),room_number=room_number, time_slots=Timetable.get_time_slots(), days=Timetable.get_days())
except:
return "There are currently no rooms!"
else:
return render_template("room.html", room_names=Timetable.get_rooms())
@app.route("/timetable/book", methods=["GET", "POST", "PUT"])
def book_room():
try:
if request.method == "POST":
# check if form data or json
if request.get_json() is None:
room = request.values.get('room')
time = request.values.get('time')
day = request.values.get('day')
event = request.values.get('event')
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
result = cursor.execute("SELECT * from timetable where name = ?", (room,)).fetchone()
time_index = Timetable.get_time_slots().index(time)
day_index = Timetable.get_days().index(day) + 1
current_day_events = result[day_index].split()
current_event = current_day_events[time_index]
if current_event == "Free":
current_day_events[time_index] = event
update_query = 'UPDATE timetable set {} = ? where name = ?'.format(day)
str_day_events = " ".join(current_day_events)
cursor.execute(update_query, (str_day_events, room))
check = cursor.execute("SELECT * from timetable where name = ?", (room,)).fetchall()
return "Room {} has been booked for {} on {} at {}".format(room, event, day, time)
else:
return "Room {} is not available on {} at {}".format(room, day, time)
else:
booking_details = request.get_json()
# get the booking information from json passed to server
room = booking_details["room"]
day = booking_details["day"]
time = booking_details["time"]
event = booking_details["event"]
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
result = cursor.execute("SELECT * from timetable where name = ?", (room,)).fetchone()
time_index = Timetable.get_time_slots().index(time)
day_index = Timetable.get_days().index(day) + 1
current_day_events = result[day_index].split()
current_event = current_day_events[time_index]
if current_event == "Free":
current_day_events[time_index] = event
update_query = 'UPDATE timetable set {} = ? where name = ?'.format(day)
str_day_events = " ".join(current_day_events)
cursor.execute(update_query, (str_day_events, room))
check = cursor.execute("SELECT * from timetable where name = ?", (room,)).fetchall()
return "Room {} has been booked for {} on {} at {}".format(room, event, day, time)
else:
return "Room {} is not available on {} at {}".format(room, day, time)
else:
return render_template("book.html", time_slots=Timetable.get_time_slots(), days=Timetable.get_days(), rooms=Timetable.get_rooms())
except:
return "There are no rooms currently!"
@app.route('/timetable/available', methods=["GET", "POST"])
def index():
try:
if request.method == 'POST':
# get form data
room = request.values.get('room')
time = request.values.get('time')
day = request.values.get('day')
time_slot = Timetable.get_slot(room, day, time)
# check if time slot is free or not
if time_slot == "Free":
return "<h2> Result: Available </h2> {} is available for {} at {}".format(room, day, time)
else:
return "<h2> Result: Not Available </h2> {} has event: {} on {} at {}".format(room, time_slot, day, time)
else:
return render_template("available.html", time_slots=Timetable.get_time_slots(), days=Timetable.get_days(),
rooms=Timetable.get_rooms())
except:
return "There are no rooms currently!"
@app.route("/timetable/rooms", methods=["GET"])
def rooms():
room_list = []
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
result = cursor.execute("SELECT * FROM rooms").fetchall()
for room in result:
room_list.append(room)
return render_template("room_list.html", room_list=room_list)
@app.route("/timetable/room/add", methods=["GET", "POST"])
def add_room():
if request.method == "POST":
try:
# get room number from form
name = request.values.get('name')
capacity = int(request.values.get('capacity'))
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
# check if room already exist
check = cursor.execute("SELECT * FROM rooms where name = ?", (name,)).fetchone()
if check is not None:
return "Room {} already exist....".format(name)
else:
# initialize the timetable to be free
default_timetable = "Free " * 10
cursor.execute(''' INSERT OR IGNORE INTO rooms values(?, ?)''', (name, capacity,))
cursor.execute(''' INSERT OR IGNORE INTO timetable values(?, ?,?, ?, ? ,?)''', (name, default_timetable, default_timetable, default_timetable, default_timetable, default_timetable))
return "Room: {} successfully added!".format(name)
except Exception as e:
if e.__class__.__name__ == "value_error":
return "Please enter a number...."
else:
return "Somethings went wrong, please try again...."
else:
return render_template("add_room.html")
@app.route("/timetable/room/<string:room_number>", methods=["GET"])
def fetch_room(room_number):
# testing purposes may or may not be used
# different response depending which user agent is requesting data e.g browser, command line
if "python" in str(request.user_agent):
return jsonify(Timetable.createTimetable(room_number))
else:
return render_template("timetable.html", timetable=Timetable.create_timetable(room_number),
time_slots=Timetable.get_time_slots(), days=Timetable.get_days())
@app.route("/timetable/<string:room_number>&<string:day>&<string:time>", methods=["get"])
def check_timetable(room_number, day, time):
# check the availability of a room
time_slot = Timetable.create_timetable(room_number)[day][time]
if time_slot == "free":
return "<h2> result </h2> {} is available for {} at {}".format(room_number, day, time)
else:
return "<h2> result </h2> {} has event: {} on {} at {}".format(room_number, time_slot, day, time)
@app.route("/timetable/admin/test/mode", methods=["GET", "POST"])
def run_test_mode():
if request.method == "POST":
# try except if a number not entered
# get number of request from user
request_number = int(request.values.get('request_number'))
# call the test function from imported test_mode.py
return test(request_number)
else:
return render_template("test.html")
@app.route("/timetable/admin/test/error", methods=["GET", "POST"])
def generate_error():
if request.method == "POST":
# get error number from form
error_number = int(request.values.get('error_choice'))
abort(error_number)
else:
return render_template("error.html")
if __name__ == "__main__":
# create database tables if not exist
init_connection = sqlite3.connect(database)
init_cursor = init_connection.cursor()
init_cursor.execute('''CREATE TABLE if not exists rooms (
name primary key,
capacity integer default 0)''')
init_cursor.execute('''create table if not exists timetable (
name primary key,
Mon text,
Tues text,
Wed text,
Thurs text,
Fri text )''')
init_connection.close()
# start api
API()