-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
270 lines (198 loc) · 7.72 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_p_id(name):
cmd = f"select p_id from prisoners where name = '{name}'"
cursor.execute(cmd)
out = cursor.fetchone()[0]
return out
def A_llocate(p_id):
cmd = f"select * from prison_allocate where p_id = '{p_id}';"
cursor.execute(cmd)
prison_allocate = cursor.fetchall()
if prison_allocate != []:
subcmd = f"update prison_allocate set allocate = curdate() where p_id = '{p_id}' "
cursor.execute(subcmd)
return "successful"
else:
return "No prison_allocate for the given Prisoners"
def R_eleased(id):
cmd = f"update prison_allocate set released=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 prisoners
def get_prisoners():
cmd = "select id, name, address, sections, other_details, created_at from prisoners;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return cursor.fetchall()
# Add a Prisoners
def add_prisoners(name, address, sections, other_details):
cmd = f"insert into prisoners(name,address,sections,other_details) values('{name}','{address}','{sections}','{other_details}');"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
# add a Prison Cell
def add_prison_cell(prison_no, charges, prison_type):
cmd = f"insert into prison_cell(prison_no,charges,prison_type) values('{prison_no}',{charges},'{prison_type}');"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
# Get All prison_cell
def get_prison_cell():
cmd = "select id, prison_no, prison_type, charges, created_at from prison_cell;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return cursor.fetchall()
# Get all prison_allocate
def get_prison_allocate():
cmd = "select id, p_id, p_c_a_id, allocate, released, Supplies from prison_allocate;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return cursor.fetchall()
# Add a prison_allocate
def add_prison_allocate(p_id, Supplies, p_c_a_id, allocate="now"):
cmd = f"insert into prison_allocate(p_id,allocate,p_c_a_id, Supplies) values('{p_id}',{f'{chr(39) + allocate + chr(39)}' if allocate != 'now' else 'current_timestamp'},'{Supplies}','{p_c_a_id}');"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
# Get all Prison Cell count
def get_total_prison_cell():
cmd = "select count(prison_no) from prison_cell;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return cursor.fetchone()[0]
# Check if a Prison Cell is vacant
def allocated():
cmd = f"select count(p_c.id) from prison_allocate p_a, prison_cell p_c where p_a.p_c_a_id = p_c.id and p_a.released is Null;"
cursor.execute(cmd)
return cursor.fetchone()[0]
def vacant():
return get_total_prison_cell() - allocated()
def allocating():
cmd = f"select count(p_a.id) from prison_allocate p_a , prison_cell p_c where p_a.p_c_a_id = p_c.id and p_c.prison_type = 'T';"
cursor.execute(cmd)
terrorist = cursor.fetchone()[0]
cmd1 = f"select count(p_a.id) from prison_allocate p_a , prison_cell p_c where p_a.p_c_a_id = p_c.id and p_c.prison_type = 'G';"
cursor.execute(cmd1)
general = cursor.fetchone()[0]
return [terrorist, general]
# Get total hotel fine
def get_total_charges():
cmd = "select sum(charges) from prison_cell;"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
fine = cursor.fetchone()[0]
return human_format(fine)
def delete_prison_allocate(id):
cmd = f"delete from prison_allocate where id='{id}';"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def delete_prison_cell(id):
cmd = f"delete from prison_cell where id='{id}';"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def delete_prisoners(id):
cmd = f"delete from prisoners where id='{id}';"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def update_prison_cell(id, prison_no, prison_type, charges):
cmd = f"update prison_cell set prison_type = '{prison_type}',charges= {charges}, prison_no = {prison_no} where id = {id};"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def update_prisoners(name, address, id, other_details):
cmd = f"update prisoners set address = '{address}',other_details = '{other_details}' , name = '{name}' where id = {id};"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def update_prison_allocates(
p_id, allocate, prison_allocate_id, prison_allocate_date, released, Supplies, type, id
):
cmd = f"update prison_allocate set allocate = '{allocate}',released = '{released}',p_id = {p_id}, \
p_a_date = '{prison_allocate_date}',Supplies = {Supplies},p_c_a_type='{type}', p_c_a_id = {prison_allocate_id} where id= {id};"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True
def S_upplies():
cmd = f"select sum(Supplies) from prison_allocate;"
cursor.execute(cmd)
S_upplies = cursor.fetchone()[0]
return human_format(S_upplies)
def update_prison_allocate(id, p_id, allocate, prison_allocate_id, released, Supplies):
cmd = f"update prison_allocate set allocate = '{allocate}', released = '{released}', p_id = {p_id}, Supplies = '{Supplies}', p_c_a_id = '{prison_allocate_id}' where id= '{id}';"
cursor.execute(cmd)
if cursor.rowcount == 0:
return False
return True