-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
168 lines (153 loc) · 6.34 KB
/
database.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
from sqlalchemy import create_engine
from datetime import datetime,timedelta
engine = create_engine('sqlite:///database.db?check_same_thread=False', echo=True)
connection = engine.connect()
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey,insert,update,delete,select,and_,DateTime
metadata = MetaData()
students = Table('students',metadata,
Column('id',String,primary_key=True),
Column('name',String),
Column('student_class',String),
Column('phone_number',String))
violation = Table('violation',metadata,
Column('id',String),
Column('name',String),
Column('student_class',String),
Column('phone_number',String),
Column('time_of_violation',DateTime,default=datetime.now()),
Column("path_img",String))
account = Table('account',metadata,
Column('username',String, primary_key=True),
Column('password',String,))
def get_account(username, password):
get_account = select([account]).where(account.columns.username == username,account.columns.password == password)
query = connection.execute(get_account)
account_id = query.fetchall()
query.close()
if len(account_id) > 0 :
return True
def get_last_time_violation(id,number_violation):
check = select([violation]).where(violation.columns.id == id)
query = connection.execute(check)
last_time = query.fetchall()[number_violation-1].time_of_violation
return last_time
def get_number_of_violation(id):
check = select([violation]).where(violation.columns.id == id)
query = connection.execute(check)
number_of_violation = len(query.fetchall())
query.close()
return number_of_violation
def del_all_violation():
del_all = delete(violation)
query = connection.execute(del_all)
query.close()
def check_true_violation(id,name_img):
check = select([violation]).where(violation.columns.time_of_violation >= datetime.now()-timedelta(days=0,minutes=3))
query = connection.execute(check)
list_violation_two_min = query.fetchall()
query.close()
if len(list_violation_two_min) == 0:
add_violator(id,name_img)
return True
list_id = []
for students in list_violation_two_min:
list_id.append(students[0])
print(list_id)
if str(id) in list_id:
return False
add_violator(id, name_img)
return True
def get_violator_list():
get_all_violator = select([violation])
query = connection.execute(get_all_violator)
list_violation = query.fetchall()
query.close()
return list_violation
def add_violator(id,name_img):
get_one = select([students]).where(students.columns.id == id)
query = connection.execute(get_one)
students_array = query.fetchall()
query.close()
if len(students_array) == 0:
return False
student = students_array[0]
name_img = "violator_images/"+name_img
add = insert(violation).values(id=id,name=student[1],student_class=student[2],phone_number=student[3],path_img = name_img)
query = connection.execute(add)
query.close()
return True
def get_one_student(id):
get_one = select([students]).where(students.columns.id==id)
query = connection.execute(get_one)
student = query.fetchall()
if len(student) == 0:
return False
return student[0]
def del_all_students():
del_all = delete(students)
query = connection.execute(del_all)
query.close()
def get_student():
get_student = select([students])
query = connection.execute(get_student)
student = query.fetchall()
query.close()
return student
def check_student(id):
check_student = select([students]).where(students.columns.id==id)
query = connection.execute(check_student)
student_available = query.fetchall()
query.close()
if len(student_available) == 0:
return False
return True
def add_student(id,name,student_class,phone_number):
if check_student(id) == True:
return False
new_student = insert(students).values(id=id,name=name,student_class=student_class,phone_number=phone_number)
query = connection.execute(new_student)
query.close()
return True
def del_student(id):
search_student = delete(students).where(students.columns.id ==id)
query = connection.execute(search_student)
query.close()
def update_student(id,name="",student_class="",phone_number=""):
if name == "":
if student_class == "" and phone_number == "":
return False
elif student_class == "" and phone_number != "":
update_student = update(students).values(phone_number=phone_number).where(students.columns.id==id)
query = connection.execute(update_student)
query.close()
return True
elif student_class != "" and phone_number == "":
update_student = update(students).values(student_class=student_class).where(students.columns.id == id)
query = connection.execute(update_student)
query.close()
return True
else:
update_student = update(students).values(student_class=student_class,phone_number=phone_number).where(students.columns.id == id)
query = connection.execute(update_student)
query.close()
return True
else:
if student_class == "" and phone_number == "":
update_student = update(students).values(name=name).where(students.columns.id == id)
query = connection.execute(update_student)
query.close()
return True
elif student_class == "" and phone_number != "":
update_student = update(students).values(phone_number=phone_number,name=name).where(students.columns.id == id)
query = connection.execute(update_student)
query.close()
elif student_class != "" and phone_number == "":
update_student = update(students).values(student_class=student_class,name=name).where(students.columns.id == id)
query = connection.execute(update_student)
query.close()
return True
else:
update_student = update(students).values(student_class=student_class, phone_number=phone_number,name=name).where(students.columns.id == id)
query = connection.execute(update_student)
query.close()
return True