This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
controller.py
270 lines (198 loc) · 7.23 KB
/
controller.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
import mysql.connector
import os
import matplotlib.pyplot as pt
# Configurations
from config import config
from dotenv import load_dotenv
load_dotenv() # Imports environemnt variables from the '.env' file
# ===================SQL Connectivity=================
# SQL Connection
connection = mysql.connector.connect(
host=config.get("DB_HOST"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASSWORD"),
database=config.get("DB_NAME"),
port="3306",
autocommit=config.get("DB_AUTOCOMMIT"),
)
cursor = connection.cursor(buffered=True)
# SQL functions
def checkUser(username, password=None):
cmd = f"Select count(username) from login where username='{username}' and BINARY password='{password}'"
cursor.execute(cmd)
cmd = None
a = cursor.fetchone()[0] >= 1
return a
def human_format(num):
if num < 1000:
return num
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000
return "%.1f%s" % (num, ["", "K", "M", "G", "T", "P"][magnitude])
def updatePassword(username, sec_ans, sec_que, password):
cmd = f"update login set password='{password}' where username='{username}' and sec_ans='{sec_ans}' and sec_que='{sec_que}' limit 1;"
cursor.execute(cmd)
cmd = f"select count(username) from login where username='{username}' and password='{password}' and sec_ans='{sec_ans}' and sec_que='{sec_que}';"
cursor.execute(cmd)
return cursor.fetchone()[0] >= 1
def updateUsername(oldusername, password, newusername):
cmd = f"update login set username='{newusername}' where username='{oldusername}' and password='{password}' limit 1;"
cursor.execute(cmd)
cmd = f"select count(username) from login where username='{newusername}' and password='{password}''"
cursor.execute(cmd)
return cursor.fetchone()[0] >= 1
def find_g_id(name):
cmd = f"select g_id from guests where name = '{name}'"
cursor.execute(cmd)
out = cursor.fetchone()[0]
return out
def checkin(g_id):
cmd = f"select * from reservations where g_id = '{g_id}';"
cursor.execute(cmd)
reservation = cursor.fetchall()
if reservation != []:
subcmd = f"update reservations set check_in = curdate() where g_id = '{g_id}' "
cursor.execute(subcmd)
return "successful"
else:
return "No reservations for the given Guest"
def checkout(id):
cmd = f"update reservations set check_out=current_timestamp where id={id} limit 1;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
# ============Python Functions==========
def acceptable(*args, acceptables):
"""
If the characters in StringVars passed as arguments are in acceptables return True, else returns False
"""
for arg in args:
for char in arg:
if char.lower() not in acceptables:
return False
return True
# Get all guests
def get_guests():
cmd = "select id, name, address, email_id, phone, created_at from guests;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return cursor.fetchall()
# Add a guest
def add_guest(name, address, email_id, phone):
cmd = f"insert into guests(name,address,email_id,phone) values('{name}','{address}','{email_id}',{phone});"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
# add a room
def add_room(room_no, price, room_type):
cmd = f"insert into rooms(room_no,price,room_type) values('{room_no}',{price},'{room_type}');"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
# Get All rooms
def get_rooms():
cmd = "select id, room_no, room_type, price, created_at from rooms;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return cursor.fetchall()
# Get all reservations
def get_reservations():
cmd = "select id, g_id, r_id, check_in, check_out, meal from reservations;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return cursor.fetchall()
# Add a reservation
def add_reservation(g_id, meal, r_id, check_in="now"):
cmd = f"insert into reservations(g_id,check_in,r_id, meal) values('{g_id}',{f'{chr(39) + check_in + chr(39)}' if check_in != 'now' else 'current_timestamp'},'{meal}','{r_id}');"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
# Get all room count
def get_total_rooms():
cmd = "select count(room_no) from rooms;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return cursor.fetchone()[0]
# Check if a room is vacant
def booked():
cmd = f"select count(ros.id) from reservations rs, rooms ros where rs.r_id = ros.id and rs.check_out is Null;"
cursor.execute(cmd)
return cursor.fetchone()[0]
def vacant():
return get_total_rooms() - booked()
def bookings():
cmd = f"select count(rs.id) from reservations rs , rooms ros where rs.r_id = ros.id and ros.room_type = 'D';"
cursor.execute(cmd)
deluxe = cursor.fetchone()[0]
cmd1 = f"select count(rs.id) from reservations rs , rooms ros where rs.r_id = ros.id and ros.room_type = 'N';"
cursor.execute(cmd1)
Normal = cursor.fetchone()[0]
return [deluxe, Normal]
# Get total hotel value
def get_total_hotel_value():
cmd = "select sum(price) from rooms;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
value = cursor.fetchone()[0]
return human_format(value)
def delete_reservation(id):
cmd = f"delete from reservations where id='{id}';"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def delete_room(id):
cmd = f"delete from rooms where id='{id}';"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def delete_guest(id):
cmd = f"delete from guests where id='{id}';"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def update_rooms(id, room_no, room_type, price):
cmd = f"update rooms set room_type = '{room_type}',price= {price}, room_no = {room_no} where id = {id};"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def update_guests(name, address, id, phone):
cmd = f"update guests set address = '{address}',phone = {phone} , name = '{name}' where id = {id};"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def update_reservations(
g_id, check_in, room_id, reservation_date, check_out, meal, type, id
):
cmd = f"update reservations set check_in = '{check_in}',check_out = '{check_out}',g_id = {g_id}, \
r_date = '{reservation_date}',meal = {meal},r_type='{type}', r_id = {room_id} where id= {id};"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def meals():
cmd = f"select sum(meal) from reservations;"
cursor.execute(cmd)
meals = cursor.fetchone()[0]
return human_format(meals)
def update_reservation(id, g_id, check_in, room_id, check_out, meal):
cmd = f"update reservations set check_in = '{check_in}', check_out = '{check_out}', g_id = {g_id}, meal = '{meal}', r_id = '{room_id}' where id= '{id}';"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True