-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1352 lines (1250 loc) · 58.2 KB
/
app.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ? Cross-origin Resource Sharing - here it allows the view and core applications deployed on different ports to communicate. No need to know anything about it since it's only used once
from flask_cors import CORS, cross_origin
# ? Python's built-in library for JSON operations. Here, is used to convert JSON strings into Python dictionaries and vice-versa
import json
# ? flask - library used to write REST API endpoints (functions in simple words) to communicate with the client (view) application's interactions
# ? request - is the default object used in the flask endpoints to get data from the requests
# ? Response - is the default HTTP Response object, defining the format of the returned data by this api
from flask import *
# ? sqlalchemy is the main library we'll use here to interact with PostgresQL DBMS
import sqlalchemy
# ? Just a class to help while coding by suggesting methods etc. Can be totally removed if wanted, no change
from typing import Dict
from datetime import date
from hashlib import sha256
import random
import time
import datetime
# ? web-based applications written in flask are simply called apps are initialized in this format from the Flask base class. You may see the contents of `__name__` by hovering on it while debugging if you're curious
app = Flask(__name__)
import os
# ? Just enabling the flask app to be able to communicate with any request source
CORS(app)
# ? building our `engine` object from a custom configuration string
# ? for this project, we'll use the default postgres user, on a database called `postgres` deployed on the same machine
YOUR_POSTGRES_PASSWORD = "postgres"
connection_string = f"postgresql://postgres:{YOUR_POSTGRES_PASSWORD}@127.0.0.1/IT2002-App"
engine = sqlalchemy.create_engine(
"postgresql://postgres:postgres@localhost/IT2002-App", pool_pre_ping=True
)
# ? `db` - the database (connection) object will be used for executing queries on the connected database named `postgres` in our deployed Postgres DBMS
db = engine.connect()
## Secret key for sessions
secret_key = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
## Config for photos being shown in the folder
today = date.today()
# ? A dictionary containing
data_types = {
'boolean': 'BOOL',
'integer': 'INT',
'text': 'TEXT',
'time': 'TIME',
}
# ? @app.get is called a decorator, from the Flask class, converting a simple python function to a REST API endpoint (function)
@app.get("/table")
def get_relation():
# ? This method returns the contents of a table whose name (table-name) is given in the url `http://localhost:port/table?name=table-name`
# ? Below is the default way of parsing the arguments from http url's using flask's request object
relation_name = request.args.get('name', default="", type=str)
# ? We use try-except statements for exception handling since any wrong query will crash the whole flow
try:
# ? Statements are built using f-strings - Python's formatted strings
# ! Use cursors for better results
statement = sqlalchemy.text(f"SELECT * FROM {relation_name};")
# ? Results returned by the DBMS after execution are stored into res object defined in sqlalchemy (for reference)
res = db.execute(statement)
# ? committing the statement writes the db state to the disk; note that we use the concept of rollbacks for safe DB management
db.commit()
# ? Data is extracted from the res objects by the custom function for each query case
# ! Note that you'll have to write custom handling methods for your custom queries
data = generate_table_return_result(res)
# ? Response object is instantiated with the formatted data and returned with the success code 200
return Response(data, 200)
except Exception as e:
# ? We're rolling back at any case of failure
db.rollback()
# ? At any error case, the error is returned with the code 403, meaning invalid request
# * You may customize it for different exception types, in case you may want
return Response(str(e), 403)
# ? a flask decorator listening for POST requests at the url /table-create
@app.post("/table-create")
def create_table():
# ? request.data returns the binary body of the POST request
data = request.data.decode()
try:
# ? data is converted from stringified JSON to a Python dictionary
table = json.loads(data)
# ? data, or table, is an object containing keys to define column names and types of the table along with its name
statement = generate_create_table_statement(table)
# ? the remaining steps are the same
db.execute(statement)
db.commit()
return Response(statement.text)
except Exception as e:
db.rollback()
return Response(str(e), 403)
@app.post("/table-insert")
# ? a flask decorator listening for POST requests at the url /table-insert and handles the entry insertion into the given table/relation
# * You might wonder why PUT or a similar request header was not used here. Fundamentally, they act as POST. So the code was kept simple here
def insert_into_table():
# ? Steps are common in all of the POST behaviors. Refer to the statement generation for the explanatory
data = request.data.decode()
try:
insertion = json.loads(data)
statement = generate_insert_table_statement(insertion)
db.execute(statement)
db.commit()
return Response(statement.text)
except Exception as e:
db.rollback()
return Response(str(e), 403)
@app.post("/table-update")
# ? a flask decorator listening for POST requests at the url /table-update and handles the entry updates in the given table/relation
def update_table():
# ? Steps are common in all of the POST behaviors. Refer to the statement generation for the explanatory
data = request.data.decode()
try:
update = json.loads(data)
statement = generate_update_table_statement(update)
db.execute(statement)
db.commit()
return Response(statement.text, 200)
except Exception as e:
db.rollback()
return Response(str(e), 403)
@app.post("/entry-delete")
# ? a flask decorator listening for POST requests at the url /entry-delete and handles the entry deletion in the given table/relation
def delete_row():
# ? Steps are common in all of the POST behaviors. Refer to the statement generation for the explanatory
data = request.data.decode()
try:
delete = json.loads(data)
statement = generate_delete_statement(delete)
db.execute(statement)
db.commit()
return Response(statement.text)
except Exception as e:
db.rollback()
return Response(str(e), 403)
def generate_table_return_result(res):
# ? An empty Python list to store the entries/rows/tuples of the relation/table
rows = []
# ? keys of the SELECT query result are the columns/fields of the table/relation
columns = list(res.keys())
# ? Constructing the list of tuples/rows, basically, restructuring the object format
for row_number, row in enumerate(res):
rows.append({})
for column_number, value in enumerate(row):
rows[row_number][columns[column_number]] = value
# ? JSON object with the relation data
output = {}
output["columns"] = columns # ? Stores the fields
output["rows"] = rows # ? Stores the tuples
"""
The returned object format:
{
"columns": ["a","b","c"],
"rows": [
{"a":1,"b":2,"c":3},
{"a":4,"b":5,"c":6}
]
}
"""
# ? Returns the stringified JSON object
return json.dumps(output)
def generate_delete_statement(details: Dict):
# ? Fetches the entry id for the table name
table_name = details["relationName"]
id = details["deletionId"]
# ? Generates the deletion query for the given entry with the id
statement = f"DELETE FROM {table_name} WHERE id={id};"
return sqlalchemy.text(statement)
def generate_update_table_statement(update: Dict):
# ? Fetching the table name, entry/tuple id and the update body
table_name = update["name"]
id = update["id"]
body = update["body"]
# ? Default for the SQL update statement
statement = f"UPDATE {table_name} SET "
# ? Constructing column-to-value maps looping
for key, value in body.items():
statement += f"{key}=\'{value}\',"
# ?Finalizing the update statement with table and row details and returning
statement = statement[:-1]+f" WHERE {table_name}.id={id};"
return sqlalchemy.text(statement)
def generate_insert_table_statement(insertion: Dict):
# ? Fetching table name and the rows/tuples body object from the request
table_name = insertion["name"]
body = insertion["body"]
valueTypes = insertion["valueTypes"]
# ? Generating the default insert statement template
statement = f"INSERT INTO {table_name} "
# ? Appending the entries with their corresponding columns
column_names = "("
column_values = "("
for key, value in body.items():
column_names += (key+",")
if valueTypes[key] == "TEXT" or valueTypes[key] == "TIME":
column_values += (f"\'{value}\',")
else:
column_values += (f"{value},")
# ? Removing the last default comma
column_names = column_names[:-1]+")"
column_values = column_values[:-1]+")"
# ? Combining it all into one statement and returning
#! You may try to expand it to multiple tuple insertion in another method
statement = statement + column_names+" VALUES "+column_values+";"
return sqlalchemy.text(statement)
def generate_create_table_statement(table: Dict):
# ? First key is the name of the table
table_name = table["name"]
# ? Table body itself is a JSON object mapping field/column names to their values
table_body = table["body"]
# ? Default table creation template query is extended below. Note that we drop the existing one each time. You might improve this behavior if you will
# ! ID is the case of simplicity
statement = f"DROP TABLE IF EXISTS {table_name}; CREATE TABLE {table_name} (id serial NOT NULL PRIMARY KEY,"
# ? As stated above, column names and types are appended to the creation query from the mapped JSON object
for key, value in table_body.items():
statement += (f"{key}"+" "+f"{value}"+",")
# ? closing the final statement (by removing the last ',' and adding ');' termination and returning it
statement = statement[:-1] + ");"
return sqlalchemy.text(statement)
# JUST TESTING
def insert_values_into_users(table: Dict):
table_name = table["name"]
# ? Table body itself is a JSON object mapping field/column names to their values
table_body = table["body"]
statement = f"INSERT INTO users VALUES('')"
return sqlalchemy.text(statement)
## Create the users table
def create_users_table():
create_users_statement = """
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY,
password_hash VARCHAR(255) NOT NULL,
user_name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
phone_number VARCHAR(20) UNIQUE NOT NULL,
gender VARCHAR(6) CHECK(gender = 'male' or gender = 'female'),
age INTEGER NOT NULL
);
"""
try:
statement = sqlalchemy.text(create_users_statement)
db.execute(statement)
db.commit()
except Exception as e:
db.rollback()
return Response(str(e), 403)
# Landing/first page of the website
@app.route("/", methods = ['GET'])
def landing_page():
cookies = request.cookies.get('session_cookies')
listings = f"SELECT * FROM property WHERE availability = 'yes' ORDER BY room_rate LIMIT 3"
statement = sqlalchemy.text(listings)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
if get_user_cookies(cookies) != None:
return render_template('home.html', userID=get_user_cookies(cookies), preferredlisting = res_tuple)
else:
return render_template('landing.html', preferredlisting = res_tuple)
# Home page of the website
@app.route('/home', methods = ['GET', 'POST'])
def home():
cookies = request.cookies.get('session_cookies')
if get_user_cookies(cookies) != None:
listings = f"SELECT * FROM property WHERE availability = 'yes' ORDER BY room_rate LIMIT 3"
statement = sqlalchemy.text(listings)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
return render_template('home.html', userID=get_user_cookies(cookies), preferredlisting = res_tuple)
else:
return redirect(url_for('login'))
# Create a route for the login page
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# Call the sign_in function and pass user_id and password
user_id = int(request.form['user_id'])
password = request.form['password']
# admin_login = request.form['admin_login']
passwordhash = sha256(password.encode('utf-8')).hexdigest()
if sign_in(user_id,passwordhash) == False:
# Show an error message if login fails
error = Markup('''<div class="alert">
<span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
Incorrect userID or Password.
</div>''')
return render_template('login.html', invalid = error)
## Implementing native sessions for users without additional plugins
## Cookies are generated by using SHA256(SecretKey+UserID+TimeStamp)
user_cookies = secret_key + str(user_id) + str(time.time())
hashedcookies = sha256(user_cookies.encode('utf-8')).hexdigest()
update_cookies(user_id, hashedcookies)
## Set cookies
resp = redirect(url_for("home"))
resp.set_cookie('session_cookies', hashedcookies)
return resp
return render_template('login.html')
# Create a route for the signout of user
@app.route('/logout', methods=['GET', 'POST'])
def logout():
if request.method == 'POST' or request.method == 'GET':
cookies = request.cookies.get('session_cookies')
remove_command = f"UPDATE users SET session_cookies = null WHERE session_cookies = '{cookies}'"
statement = sqlalchemy.text(remove_command)
db.execute(statement)
db.commit()
## Set cookies
resp = redirect(url_for('landing_page'))
resp.set_cookie('session_cookies', '0')
return resp
return render_template('landing.html')
# Create a route for the sign up page
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
# Call the sign_up function and pass user_id and password
## Get current max userID, select that as the userID for the current user
user_id_statement = "SELECT MAX(user_id) from users"
statement = sqlalchemy.text(user_id_statement)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
user_id = res_tuple[0][0] + 1
password = request.form['password']
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
gender = request.form['gender']
age = request.form['age']
passwordhash = str(sha256(password.encode('utf-8')).hexdigest())
if sign_up(user_id):
# Redirect to the login page after successful signup
try:
print("Trying")
user_cookies = secret_key + str(user_id) + str(time.time())
hashedcookies = sha256(user_cookies.encode('utf-8')).hexdigest()
update_cookies(user_id, hashedcookies)
insert_command = f"INSERT INTO users VALUES ({user_id}, '{passwordhash}', '{name}', '{email}', '{phone}', '{gender}', {age}, '{hashedcookies}');"
statement = sqlalchemy.text(insert_command)
db.execute(statement)
db.commit()
resp = redirect(url_for("home"))
resp.set_cookie('session_cookies', hashedcookies)
return resp
except Exception as e:
db.rollback()
return Response(str(e),403)
if request.method == 'GET':
user_id_statement = "SELECT MAX(user_id) from users"
statement = sqlalchemy.text(user_id_statement)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
user_id = res_tuple[0][0] + 1
print(user_id)
return render_template('signup.html', user_id = user_id)
@app.route('/user_profile', methods = ['GET', 'POST'])
def user_profile():
cookies = request.cookies.get('session_cookies')
if get_user_id(cookies) != None:
user_id = get_user_id(cookies)
user_query = f"SELECT * FROM users WHERE user_id = {user_id};"
prev_bookings_query = f"""SELECT b.booking_id, p.address, p.property_type, b.start_date, b.end_date, b.booking_date, b.status
FROM booking b
LEFT JOIN property p
ON b.property_id = p.property_id
WHERE b.student_id = {user_id}
"""
try:
user_res = db.execute(sqlalchemy.text(user_query))
booking_res = db.execute(sqlalchemy.text(prev_bookings_query))
user_tuple = user_res.fetchall()
booking_tuple = booking_res.fetchall()
user_name = user_tuple[0][2]
email = user_tuple[0][3]
phone_number = user_tuple[0][4]
gender = user_tuple[0][5]
age = user_tuple[0][6]
db.commit()
return render_template('user_profile.html', bookings = booking_tuple,
user_id = user_id,
user_name = user_name,
email = email,
phone_number = phone_number,
gender = gender,
age = age)
except Exception as e:
db.rollback()
return Response(str(e), 403)
else:
return redirect("/login")
@app.route('/confirmbooking', methods =['POST'])
def confbooking():
cookies = request.cookies.get('session_cookies')
booking_id = request.form['bookingID']
## Checking if the user is authorized to change the property
user_id = get_user_id(cookies)
if checkauthproperty(user_id, booking_id) == True:
if getcurrstatus(booking_id) == "confirmed":
toggleproperty(booking_id, "processing")
elif getcurrstatus(booking_id) == "processing":
toggleproperty(booking_id, "confirmed")
return redirect("/user_profile")
def checkauthproperty(user_id, booking):
# Check for the property and the user_id, ensuring that the result is not null
user_id_statement = f"SELECT * from booking WHERE booking_id = {booking} and student_id = {user_id}"
statement = sqlalchemy.text(user_id_statement)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
if res_tuple != None:
return True
else:
return False
def getcurrstatus(booking):
property_status = f"SELECT status from booking WHERE booking_id = {booking}"
statement = sqlalchemy.text(property_status)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
if res_tuple[0][0] == "processing":
return "processing"
else:
return "confirmed"
def toggleproperty(booking, status):
property_update = f"UPDATE booking SET status = '{status}' WHERE booking_id = {booking}"
statement = sqlalchemy.text(property_update)
res = db.execute(statement)
db.commit()
return None
@app.route('/user_profile/edit', methods = ['POST'])
def edit_user_profile():
cookies = request.cookies.get('session_cookies')
if get_user_id(cookies) != None:
user_id = get_user_id(cookies)
field_to_edit = request.form['field']
updated_field = request.form['updated']
if field_to_edit != "password":
update_query = f"""UPDATE users
SET {field_to_edit} = '{updated_field}'
WHERE user_id = {user_id};"""
elif field_to_edit== "password":
passwordhash = str(sha256(updated_field.encode('utf-8')).hexdigest())
update_query = f"""UPDATE users
SET password_hash = '{passwordhash}'
WHERE user_id = {user_id};"""
try:
db.execute(sqlalchemy.text(update_query))
db.commit()
return redirect('/user_profile')
except Exception as e:
db.rollback()
return Response(str(e), 403)
else:
return redirect("/login")
@app.route('/user_profile/review', methods = ['GET'])
def view_reviews():
cookies = request.cookies.get('session_cookies')
if get_user_id(cookies) != None:
user_id = get_user_id(cookies)
prev_reviews_query = f"""SELECT r.review_date, p.address, p.property_type, r.rating, r.review
FROM review r
LEFT JOIN property p
ON r.property_id = p.property_id
WHERE r.reviewer_id = {user_id};
"""
address_query = f"""SELECT p.address
FROM booking b
LEFT JOIN property p
ON b.property_id = p.property_id
WHERE b.student_id = {user_id}
AND b.status = 'confirmed';
"""
try:
reviews_res = db.execute(sqlalchemy.text(prev_reviews_query))
address_res = db.execute(sqlalchemy.text(address_query))
reviews_tuple = reviews_res.fetchall()
address_tuple = tuple(map(lambda x: x[0], address_res.fetchall()))
db.commit()
return render_template('user_reviews.html', reviews = reviews_tuple, addresses = address_tuple)
except Exception as e:
db.rollback()
return Response(str(e), 403)
else:
return redirect("/login")
@app.route("/user_profile/review/submit_review", methods = ['POST'])
def submit_review():
cookies = request.cookies.get('session_cookies')
if get_user_id(cookies) != None:
user_id = get_user_id(cookies)
latest_review_id = """SELECT MAX(review_id)
FROM review;
"""
address = request.form['address']
rating = request.form['rating']
review = request.form['review']
property_id_query = f"SELECT property_id FROM property WHERE address = '{address}'"
try:
max_review_id = db.execute(sqlalchemy.text(latest_review_id))
property_id_res = db.execute(sqlalchemy.text(property_id_query))
max_review = max_review_id.fetchall()
property_id = property_id_res.fetchall()
next_review_id = max_review[0][0] + 1
insert_review = f"""INSERT INTO review (review_id, reviewer_id, property_id, rating, review)
values ({next_review_id}, {user_id}, {property_id[0][0]}, {rating}, '{review}');"""
db.execute(sqlalchemy.text(insert_review))
db.commit()
return redirect('/user_profile/review')
except Exception as e:
db.rollback()
return Response(str(e), 403)
return redirect('/login')
@app.route('/user_profile/properties', methods = ['GET'])
def view_properties():
cookies = request.cookies.get('session_cookies')
if get_user_id(cookies) != None:
user_id = get_user_id(cookies)
properties_query = f"""SELECT p.property_id, p.start_date, p.end_date, p.address, p.property_type, p.num_rooms, p.availability, p.room_rate
FROM property p, users u
WHERE p.owner_id = u.user_id
AND u.user_id = {user_id};
"""
property_id_query = f"""SELECT p.property_id
FROM property p, users u
WHERE p.owner_id = u.user_id
AND u.user_id = {user_id};
"""
property_cols = f"""SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'property'
ORDER BY ordinal_position;
"""
try:
properties_res = db.execute(sqlalchemy.text(properties_query))
property_id_res = db.execute(sqlalchemy.text(property_id_query))
property_cols_res = db.execute(sqlalchemy.text(property_cols))
property_tuple = properties_res.fetchall()
property_cols_tuple = tuple(map(lambda x: x[0], property_cols_res.fetchall()))
property_id_tuple = tuple(map(lambda x: x[0], property_id_res.fetchall()))
db.commit()
return render_template('user_property.html', properties = property_tuple, property_ids = property_id_tuple, property_cols = property_cols_tuple)
except Exception as e:
db.rollback()
return Response(str(e), 403)
else:
return redirect("/login")
@app.route("/user_profile/properties/edit_property", methods = ['POST'])
def edit_properties():
string_fields_list = ['start_date', 'end_date', 'address', 'property_type', 'availability']
cookies = request.cookies.get('session_cookies')
if get_user_id(cookies) != None:
user_id = get_user_id(cookies)
id_to_edit = request.form['edit_property']
field_to_update = request.form['field_to_update']
updated = request.form['updated']
if field_to_update in string_fields_list:
updated = f"'{updated}'"
try:
update_property = f"UPDATE property SET {field_to_update} = {updated} WHERE property_id = {id_to_edit}"
db.execute(sqlalchemy.text(update_property))
db.commit()
return redirect('/user_profile/properties')
except Exception as e:
db.rollback()
return Response(str(e), 403)
return redirect('/login')
@app.route("/user_profile/properties/list_property", methods = ['POST'])
def list_properties():
cookies = request.cookies.get('session_cookies')
if get_user_id(cookies) != None:
user_id = get_user_id(cookies)
latest_property_id = """SELECT MAX(property_id)
FROM property;
"""
start_date = request.form['start_date']
end_date = request.form['end_date']
address = request.form['address']
property_type = request.form['property_type']
num_rooms = request.form['num_rooms']
availability = request.form['availability']
room_rate = request.form['room_rate']
try:
max_property_id = db.execute(sqlalchemy.text(latest_property_id))
max_review = max_property_id.fetchall()
next_property_id = max_review[0][0] + 1
insert_property = f"""INSERT INTO property (property_id, owner_id, start_date,
end_date, address, property_type, num_rooms, availability, room_rate)
VALUES ({next_property_id}, {user_id}, '{start_date}',
'{end_date}', '{address}', '{property_type}', {num_rooms}, '{availability}', {room_rate});"""
db.execute(sqlalchemy.text(insert_property))
db.commit()
return redirect('/user_profile/properties')
except Exception as e:
db.rollback()
return Response(str(e), 403)
return redirect('/login')
# Function is used to insert the cookies into the database -> Allowing for synchronious sessions within the webpages
def update_cookies(user_id, cookies):
update_cookies = f"UPDATE users SET session_cookies = '{cookies}' WHERE user_id = {user_id}"
statement = sqlalchemy.text(update_cookies)
res = db.execute(statement)
db.commit()
return True
# Gets the password of the user, from the user_id
def get_password(user_id):
password_query = f'SELECT password_hash FROM users WHERE user_id = {user_id};'
statement = sqlalchemy.text(password_query)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
if res_tuple == None:
return "jsjqwjasjsyj"
elif len(res_tuple) > 0:
password = res_tuple[0][0]
print(password)
return password
# Takes user_id and password as input and checks if the user exists in the database.
# Return True if the user exists and the password is correct otherwise, return False.
def sign_in(user_id, password):
stored_password= get_password(user_id)
if password != stored_password:
return False
else:
return True
# This functions checks and returns the user_name that corresponds to the cookies presented
def get_user_cookies(cookies):
find_user = f"SELECT user_name from users WHERE session_cookies = '{cookies}'"
statement = sqlalchemy.text(find_user)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
if len(res_tuple) > 0:
name = res_tuple[0][0]
return name
else:
return None
# This function returns ID from cookies
def get_user_id(cookies):
find_user = f"SELECT user_id from users WHERE session_cookies = '{cookies}'"
statement = sqlalchemy.text(find_user)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
if len(res_tuple) > 0:
id = res_tuple[0][0]
return id
else:
return None
# Takes user_id and password and creates a new user
# Add fields for name, email, phone number, gender & age
# Returns false if the user exists, and true if there's no existing user
def sign_up(user_id):
user_id_check = f'SELECT user_id FROM users WHERE user_id = {user_id}'
res = db.execute(sqlalchemy.text(user_id_check))
res_tuple = res.fetchall()
db.commit()
if(len(res_tuple) > 0): #user_id exists in the table already
return False
return True
# This path displays all of the property according to the filter that the user has set on the homepage
@app.route('/filter', methods = ['GET', 'POST'])
def property_list():
if request.method == "POST":
filter_type = request.form['type']
if filter_type == "size":
size = request.form['size']
if size == "1room":
sql_size = 1
elif size == "2room":
sql_size = 2
elif size == "3room":
sql_size = 3
elif size == "4room":
sql_size = 4
elif size == "5room":
sql_size = 5
res = filter_size(sql_size)
elif filter_type == "price":
if request.form['price'] == "lowhigh":
res = filter_price("low")
elif request.form['price'] == "highlow":
res = filter_price("high")
elif filter_type == "date":
## Check date, whether to filter by earlier/later properties. Sufficient to group by ID, as they are sequentially inserted
if request.form['size'] == "new":
res = filter_age("low")
elif request.form['size'] == "old":
res = filter_age("high")
elif filter_type == "senddate":
## Continue to work on this
start_date = request.form['startdate']
end_date = request.form['enddate']
res = filter_date(start_date, end_date)
return render_template('property_list.html', filtered = res)
else:
return render_template('property_list.html')
def filter_size(type):
find_property = f"SELECT * from property WHERE num_rooms = {type}"
statement = sqlalchemy.text(find_property)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
return res_tuple
def filter_price(type):
if type == "low":
find_property = f"SELECT * from property ORDER BY (room_rate)"
if type == "high":
find_property = f"SELECT * from property ORDER BY (room_rate) DESC"
statement = sqlalchemy.text(find_property)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
return res_tuple
def filter_age(type):
if type == "low":
find_property = f"SELECT * from property ORDER BY (property_id)"
if type == "high":
find_property = f"SELECT * from property ORDER BY (property_id) DESC"
statement = sqlalchemy.text(find_property)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
return res_tuple
def filter_date(start,end):
## Filter listings which fits in the timeframe selected
find_timeframe = f"SELECT * FROM property WHERE property.property_id NOT IN(SELECT property.property_id FROM property LEFT JOIN booking on property.property_id = booking.property_id WHERE ((booking.start_date BETWEEN '{start}' AND '{end}') OR (booking.end_date BETWEEN '{start}' AND '{end}')))"
statement = sqlalchemy.text(find_timeframe)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
return res_tuple
# This path displays the property that the user is looking at and displays it on the webpage
@app.route('/property', methods=['GET', 'POST'])
def Property():
if request.method == "POST":
property_id = request.form['getproperty']
cookies = request.cookies.get('session_cookies')
if get_user_cookies(cookies) != None:
property_tuple = getProperty(property_id)
return render_template('property.html', currlisting = property_tuple)
else:
redirect(url_for('/'))
def getProperty(id):
find_property = f"SELECT * from property WHERE property_id = {id}"
statement = sqlalchemy.text(find_property)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
return res_tuple
# Checks if the user can book first, through the property_booking method
# If available -> bookslot, which inserts the booking into the bookings table
# Else -> throw an error saying the property is not available
@app.route('/book', methods = ['GET','POST'])
def book():
if request.method == 'POST':
cookies = request.cookies.get('session_cookies')
property_id = request.form['property_id']
start_time = request.form['startdate']
end_time = request.form['enddate']
# Find user id
user_id = get_user_id(cookies)
# Check for valid cookies and valid property
if user_id != None and validbookingtime(property_id, start_time, end_time) == True:
## Book the property, validity check done in browser
bookingID = bookingproperty(property_id, user_id, start_time, end_time)
bookingdetails = getbooking(bookingID)
## Variables that are displayed in the bookingdetails
bookingid = bookingdetails[0][0]
property_id = bookingdetails[0][1]
start_date = bookingdetails[0][3].strftime("%d/%m/%Y")
end_date = bookingdetails[0][4].strftime("%d/%m/%Y")
if bookingdetails != None:
return render_template('confirmation.html', booking_id = bookingid, property_id = property_id, start_date = start_date, end_date = end_date)
else:
error = Markup('''<div class="alert">
<span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
Timeframe unavailable, choose another timeframe.
</div>''')
return render_template('property.html', invalid = error)
return redirect(url_for("home"))
def bookingproperty(property_id, user_id, start_time, end_time):
bookingID = random.randrange(0,999999)
booking_date = datetime.date.today()
book_property = f"INSERT INTO booking VALUES ({bookingID}, {property_id}, {user_id}, '{start_time}', '{end_time}', '{booking_date}', 'processing')"
statement = sqlalchemy.text(book_property)
res = db.execute(statement)
db.commit()
validbookingtime(property_id, start_time, end_time)
return bookingID
def getbooking(bookingID):
bookingDetails = f"SELECT * FROM booking WHERE booking_id = {bookingID}"
statement = sqlalchemy.text(bookingDetails)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
return res_tuple
## Grab the unix timing of all of the booking, check which ones will conflict with the selected start_time
def validbookingtime(property_id, start_time, end_time):
## Initialize temp lists
res_1 = []
res_2 = []
findtimings = f"SELECT start_date, end_date FROM booking WHERE property_id = {property_id}"
statement = sqlalchemy.text(findtimings)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
for time1 in res_tuple:
res_1.append(time.mktime(time1[0].timetuple()))
res_1.append(time.mktime(time1[1].timetuple()))
res_2.append(res_1)
res_1 = []
unixstartTime = time.mktime(datetime.datetime.strptime(start_time, "%Y-%m-%d").timetuple())
unixendTime = time.mktime(datetime.datetime.strptime(end_time, "%Y-%m-%d").timetuple())
print(unixstartTime)
print(unixendTime)
print(res_2)
## Iterate through the entire list to ensure that there are no instances where the start time and/or the end time is between the two values
for time_list in res_2:
if time_list[0] <= unixstartTime <= time_list[1]:
return False
if time_list[0] <= unixendTime <= time_list[1]:
return False
return True
# Routing to the home admin page
@app.route('/admin/home')
def admin_page():
return render_template('admin.html')
# Landing page for admins - provides special admin sign-in
# redirects to admin homepage after completion
@app.route('/admin', methods = ['GET','POST'])
def admin_signin_page():
if request.method == 'POST':
# Call the sign_in function and pass user_id and password
user_id = int(request.form['user_id'])
password = request.form['password']
if admin_sign_in(user_id,password) == False:
# Show an error message if login fails
error = 'Invalid user ID or password. Please try again.'
print("Error")
return render_template('admin_signin.html', error=error)
print("redirecting....")
return redirect('/admin/home')
return render_template('admin_signin.html')
# Function to assist in admin sign-in
# Checks if user is in the administrator table and if the passsword is correct
def admin_sign_in(user_id,password_hash):
admin_check = f'SELECT user_id, password_hash FROM administrator WHERE user_id = {user_id};'
statement = sqlalchemy.text(admin_check)
res = db.execute(statement)
db.commit()
res_tuple = res.fetchall()
print(password_hash)
print(res_tuple[0][1])
if len(res_tuple) > 0 and res_tuple[0][1] == password_hash:
return True
else:
return False
# Admin management page
# Update the admin tables for extra permissions and displays current admins and their permissions
@app.route('/admin/update_admin', methods = ['GET','POST'])
def admin_update():
if request.method == 'POST':
user_id = int(request.form['user_id'])
new_permissions = request.form['permissions']
update_query = f"""UPDATE administrator
SET permissions = '{new_permissions}'
WHERE user_id = {user_id};"""
try:
db.execute(sqlalchemy.text(update_query))
db.commit()
return redirect('/admin/update_admin')
except Exception as e:
db.rollback()
return Response(str(e), 403)
current_admins_query = "SELECT user_id, permissions FROM administrator;"
current_admins = db.execute(sqlalchemy.text(current_admins_query))
admins_tuple = current_admins.fetchall()
db.commit()
return render_template('admin_update.html', admins_tuple = admins_tuple)
# Adds an admin to the admistrator table
# uses admin_check to check validity
@app.route('/admin/add_admin', methods = ['POST'])
def add_admin():
user_id = int(request.form['user_id'])
password = request.form['password']
permissions = request.form['permissions']
if admin_check(user_id):
print('checked!')
insert_statement = f"""INSERT INTO administrator values (
{user_id}, '{password}', '{permissions}'
)
"""
try:
db.execute(sqlalchemy.text(insert_statement))
db.commit()
return redirect('/admin/update_admin')
except Exception as e:
db.rollback()
return Response(str(e), 403)
return redirect('/admin/update_admin')
# Checks if the user already exists and if it is not currently already in the administrator table
def admin_check(user_id):
user_id_check = f"""SELECT user_id
FROM users
WHERE user_id = {user_id}"""
admin_exists_check = f"""SELECT user_id
FROM administrator
WHERE user_id = {user_id}"""
user_exists = db.execute(sqlalchemy.text(user_id_check))
admin_exists = db.execute(sqlalchemy.text(admin_exists_check))
users_tuple = user_exists.fetchall()
admin_tuple = admin_exists.fetchall()
db.commit()
if(len(users_tuple) > 0 and len(admin_tuple) == 0):
return True
return False
# Part of the admin management page, and remove the admin from the administrator table
@app.route('/admin/remove_admin', methods = ['POST'])
def remove_admin():
user_id = request.form['user_id']
remove_admin_statement = f'DELETE FROM administrator WHERE user_id = {user_id}'
try:
db.execute(sqlalchemy.text(remove_admin_statement))
db.commit()
return redirect('/admin/update_admin')