-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
206 lines (166 loc) · 6.46 KB
/
user.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
from hashlib import md5
from flask_login import UserMixin
import SQL
class User(UserMixin):
type = ""
def verify(username: str, password: str):
if isExist(username):
psw = md5(password.encode('UTF-8')).hexdigest()
pswget = SQL.selectone(
'''
select password
from accounts
where account='%s'
''' % username)
if len(pswget) == 0:
return False
if psw == pswget['password']:
return True
else:
return False
else:
return False
def register(usr, psw, typ, name, sex, age: int, dept, grade: int, classs: int):
SQL.cur.execute('''
select account from accounts where account='%s'
''' % usr)
usrget = SQL.cur.fetchall()
if len(usrget) > 0:
if usr == usrget[0]['account']:
return 2
pswmd5 = md5(psw.encode('UTF-8')).hexdigest()
instr = '''insert into accounts (account, password, type)
values ('%s', '%s', '%s')''' % (usr, pswmd5, typ)
if typ == 'student':
SQL.cur.execute('select count(Num) cnt from students')
num = 'stu' + str(int(SQL.cur.fetchall()[0]['cnt']) + 1)
stustr = '''insert into students (Name, Num, Age, Class, Grade, Dept, sex, account)
values ('%s', '%s', %d, %d, %d, '%s', '%s', '%s')''' % (name, num, age, classs, grade, dept, sex, usr)
SQL.cur.execute(stustr)
SQL.conn.commit()
elif typ == 'teacher':
SQL.cur.execute('select count(Num) cnt from teachers')
num = 'tch' + str(int(SQL.cur.fetchall()[0]['cnt']) + 1)
stustr = '''insert into teachers (name, sex, age, num, dept, account)
values ('%s', '%s', %d, '%s', '%s', '%s')''' % (name, sex, age, num, dept, usr)
SQL.cur.execute(stustr)
SQL.conn.commit()
try:
SQL.cur.execute(instr)
SQL.conn.commit()
except:
return 0
else:
return 1
def gettype(user) -> str:
try:
type = SQL.select('select type from accounts where account=\'%s\'' % user)[0]['type']
except:
return ''
else:
return type
def isExist(usr):
try:
SQL.select('select account from accounts where account=\'%s\'' % usr)[0]['account']
except:
return False
else:
return True
def isExistStu(num):
try:
SQL.select('select Num from students where Num=\'%s\'' % num)[0]['Num']
except:
return False
else:
return True
def isExistTch(num):
try:
SQL.select('select num from teachers where num=\'%s\'' % num)[0]['num']
except:
return False
else:
return True
def setStatus(pswnew, name, sex, age: int, dept, grade: int, classs: int, user):
if pswnew != '':
pswmd5 = md5(pswnew.encode('UTF-8')).hexdigest()
sqlstr = '''update accounts
set password = '%s'
where account = '%s'
''' % (pswmd5, user)
SQL.cur.execute(sqlstr)
SQL.conn.commit()
if gettype(user) == 'student':
sqlstr = '''update students
set Name='%s', sex='%s', Age='%d', Dept='%s', Grade='%d', Class='%d'
where account = '%s'
''' % (name, sex, age, dept, grade, classs, user)
elif gettype(user) == 'teacher':
sqlstr = '''update teachers
set name='%s', sex='%s', age='%d', dept='%s'
where account = '%s'
''' % (name, sex, age, dept, user)
SQL.cur.execute(sqlstr)
SQL.conn.commit()
def getname(user) -> str:
if gettype(user) == 'student':
return SQL.select('select Name from students where account=\'%s\'' % user)[0]['Name']
elif gettype(user) == 'teacher':
return SQL.select('select name from teachers where account=\'%s\'' % user)[0]['name']
def getnum(user) -> str:
if gettype(user) == 'student':
return SQL.select('select Num from students where account=\'%s\'' % user)[0]['Num']
elif gettype(user) == 'teacher':
return SQL.select('select num from teachers where account=\'%s\'' % user)[0]['num']
def getage(user) -> str:
if gettype(user) == 'student':
return SQL.select('select Age from students where account=\'%s\'' % user)[0]['Age']
elif gettype(user) == 'teacher':
return SQL.select('select age from teachers where account=\'%s\'' % user)[0]['age']
def getsex(user) -> str:
if gettype(user) == 'student':
return SQL.select('select sex from students where account=\'%s\'' % user)[0]['sex']
elif gettype(user) == 'teacher':
return SQL.select('select sex from teachers where account=\'%s\'' % user)[0]['sex']
def getdept(user) -> str:
if gettype(user) == 'student':
return SQL.select('select Dept from students where account=\'%s\'' % user)[0]['Dept']
elif gettype(user) == 'teacher':
return SQL.select('select dept from teachers where account=\'%s\'' % user)[0]['dept']
def getgrade(user) -> str:
if gettype(user) == 'student':
return SQL.select('select Grade from students where account=\'%s\'' % user)[0]['Grade']
elif gettype(user) == 'teacher':
return 0 # SQL.select('select grade from teachers where account=\'%s\'' % user)[0]['grade']
def getclass(user) -> str:
if gettype(user) == 'student':
return SQL.select('select Class from students where account=\'%s\'' % user)[0]['Class']
elif gettype(user) == 'teacher':
return 0 # SQL.select('select class from teachers where account=\'%s\'' % user)[0]['class']
def getaccount(num: str) -> str:
if num[:3] == 'stu':
acc = SQL.select('select account from students where Num = \'%s\'' % num)[0]['account']
elif num[:3] == 'tch':
acc = SQL.select('select account from teachers where num = \'%s\'' % num)[0]['account']
return acc
def logout():
global loguser
global log
global usrtyp
loguser = ''
usrtyp = ''
log = 0
def delstu(num: str):
SQL.cur.execute('delete from score where Snum=\'%s\'' % num)
SQL.conn.commit()
def delcourse(num: str, Cnum: str):
try:
SQL.cur.execute('delete from score where Cnum=\'%s\' and Snum=\'%s\'' % (Cnum, num))
SQL.conn.commit()
return True
except:
return False
def scorein(num: str, Cname: str, score: int, grade: int, Cnum: str, Lnum: int):
SQL.cur.execute('''insert into score(Snum, course, score, grade, Cnum, Lnum)
values ('%s', '%s', %d, %d, '%s', %d)
''' % (num, Cname, score, grade, Cnum, Lnum))
SQL.conn.commit()