forked from xcwill/CMeKG_labelingPlatform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
306 lines (249 loc) · 9.5 KB
/
db.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
from models import User,Code,File,Task,Task_User,Task_File,Group,Task_Group,Group_user
from _init_ import db
class DB:
@staticmethod
def add_user(username, email, password, role):
user = User(username=username, email=email, password=password, role=role)
db.session.add(user)
db.session.commit()
@staticmethod
def query_all():
return User.query.all()
@staticmethod
def query_by_username(username):
return User.query.filter_by(username=username).first()
@staticmethod
def query_by_role(role):
return User.query.filter_by(role=role)
@staticmethod
def query_by_email(email):
return User.query.filter_by(email=email).first()
def check_login(self, username, email, password):
if self.query_by_username(username) is not None or self.query_by_email(email) is not None:
if self.query_by_username(username) is not None and self.query_by_username(username).password == password:
return 1
elif self.query_by_email(email) is not None and self.query_by_email(email).password == password:
return 1
else:
return 0
else:
return -1
def new_password(self, username, email, password):
if self.query_by_username(username) is not None or self.query_by_email(email) is not None:
if self.query_by_username(username) is not None:
u = self.query_by_username(username)
u.password = password
db.session.add(u)
db.session.commit()
if self.query_by_email(email) is not None:
u = self.query_by_email(email)
u.password = password
db.session.add(u)
db.session.commit()
def add_code(self, email, code, time):
if self.query_code_by_email(email) is not None:
c = self.query_code_by_email(email)
c.code = code
c.time = time
db.session.add(c)
db.session.commit()
else:
code = Code(email=email, code=code, time=time)
db.session.add(code)
db.session.commit()
@staticmethod
def query_code_by_email(email):
return Code.query.filter_by(email=email).first()
@staticmethod
def add_file(name, type, count, time, user, words):
file = File(name=name, type=type, count=count, time=time, user=user, words=words)
db.session.add(file)
db.session.commit()
@staticmethod
def query_file_by_name(name):
return File.query.filter_by(name=name).first()
@staticmethod
def query_all_files():
return File.query.all()
@staticmethod
def add_task_user(name, username):
t_u = Task_User(name=name, username=username)
db.session.add(t_u)
db.session.commit()
@staticmethod
def query_all_tasks():
return Task.query.all()
@staticmethod
def query_task_by_user(username):
return Task_User.query.filter_by(username=username)
@staticmethod
def query_task_by_name(name):
return Task.query.filter_by(name=name).first()
@staticmethod
def add_task(name, ty, count, time, user, words):
t = Task(name=name, type=ty, count=count, time=time, user=user, words=words)
db.session.add(t)
db.session.commit()
@staticmethod
def query_task_exist(name):
return Task.query.filter_by(name=name).first()
@staticmethod
def query_task_user_exist(name, username):
return Task_User.query.filter_by(name=name, username=username).first()
@staticmethod
def add_task_file(task_name, file_name):
t_f = Task_File(task_name=task_name, file_name=file_name)
db.session.add(t_f)
db.session.commit()
@staticmethod
def query_task_file_exist(task_name, file_name):
return Task_File.query.filter_by(task_name=task_name, file_name=file_name).first()
@staticmethod
def query_task_file_exist_by_file_name(file_name):
return Task_File.query.filter_by(file_name=file_name)
@staticmethod
def query_task_file_by_task(task_name):
return Task_File.query.filter_by(task_name=task_name)
@staticmethod
def del_task_by_name(name):
t = Task.query.filter_by(name=name).first()
db.session.delete(t)
db.session.commit()
@staticmethod
def del_task_user_by_name(name):
ta = Task_User.query.filter_by(name=name).all()
for t in ta:
db.session.delete(t)
db.session.commit()
@staticmethod
def del_task_user_by_name_username(name, username):
t = Task_User.query.filter_by(name=name, username=username).first()
db.session.delete(t)
db.session.commit()
@staticmethod
def del_file_by_file_name(name):
t = File.query.filter_by(name=name).first()
db.session.delete(t)
db.session.commit()
@staticmethod
def del_task_file_by_task_name(name):
ta = Task_File.query.filter_by(task_name=name).all()
for t in ta:
db.session.delete(t)
db.session.commit()
@staticmethod
def del_task_file_by_file_name(file_name):
ta = Task_File.query.filter_by(file_name=file_name).all()
for t in ta:
db.session.delete(t)
db.session.commit()
@staticmethod
def del_task_file_by_task_name_file_name(task_name, file_name):
t = Task_File.query.filter_by(task_name=task_name, file_name=file_name).first()
db.session.delete(t)
db.session.commit()
@staticmethod
def query_task_user_by_name(name):
return Task_User.query.filter_by(name=name)
@staticmethod
def upd_task_name(name, new_name):
t = Task.query.filter_by(name=name).first()
t.name = new_name
db.session.commit()
@staticmethod
def upd_file_name(name, new_name):
t = File.query.filter_by(name=name).first()
t.name = new_name
db.session.commit()
@staticmethod
def upd_task_file_by_file_name(file_name, new_name):
fs = Task_File.query.filter_by(file_name=file_name)
for f in fs:
f.file_name = new_name
db.session.commit()
@staticmethod
def query_all_groups():
return Group.query.all()
@staticmethod
def add_new_group(group_name):
g = Group(group_name=group_name)
db.session.add(g)
db.session.commit()
@staticmethod
def del_group_by_group_name(group_name):
g = Group.query.filter_by(group_name=group_name).first()
db.session.delete(g)
db.session.commit()
@staticmethod
def query_group_exist(group_name):
return Group.query.filter_by(group_name=group_name).first()
@staticmethod
def query_task_group_by_task_name(name):
return Task_Group.query.filter_by(name=name)
@staticmethod
def query_task_group_by_group_name(group_name):
return Task_Group.query.filter_by(group_name=group_name)
@staticmethod
def query_task_group_exist(task_name, group_name):
return Task_Group.query.filter_by(name=task_name, group_name=group_name).first()
@staticmethod
def query_task_group_exist_by_group_name(group_name):
return Task_Group.query.filter_by(group_name=group_name)
@staticmethod
def add_task_group(name, group_name):
t_g = Task_Group(name=name, group_name=group_name)
db.session.add(t_g)
db.session.commit()
@staticmethod
def del_task_group_by_task_name(name):
ta = Task_Group.query.filter_by(name=name).all()
for t in ta:
db.session.delete(t)
db.session.commit()
@staticmethod
def del_task_group_by_group_name(group_name):
ta = Task_Group.query.filter_by(group_name=group_name).all()
for t in ta:
db.session.delete(t)
db.session.commit()
@staticmethod
def del_task_group_by_task_name_group_name(task_name, group_name):
t = Task_Group.query.filter_by(name=task_name, group_name=group_name).first()
db.session.delete(t)
db.session.commit()
@staticmethod
def query_group_user_by_group_name(group_name):
return Group_user.query.filter_by(group_name=group_name)
@staticmethod
def query_group_user_by_username(username):
return Group_user.query.filter_by(username=username)
@staticmethod
def query_group_user_exist(group_name, username):
return Group_user.query.filter_by(group_name=group_name, username=username).first()
@staticmethod
def add_group_user(group_name, username):
g_u = Group_user(group_name=group_name, username=username)
db.session.add(g_u)
db.session.commit()
@staticmethod
def del_group_user_by_group_name(group_name):
ta = Group_user.query.filter_by(group_name=group_name).all()
for t in ta:
db.session.delete(t)
db.session.commit()
@staticmethod
def del_group_user_by_group_name_username(group_name, username):
t = Group_user.query.filter_by(group_name=group_name, username=username).first()
db.session.delete(t)
db.session.commit()
@staticmethod
def upd_group_name(group_name, new_name):
t = Group.query.filter_by(group_name=group_name).first()
t.group_name = new_name
db.session.commit()
@staticmethod
def upd_task_group_name(group_name, new_name):
ts = Task_Group.query.filter_by(group_name=group_name)
for t in ts:
t.group_name = new_name
db.session.commit()