This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
745 lines (700 loc) · 18.4 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# cython: language_level=3
#!./env python
# -*- coding: utf-8 -*-
"""
# @Author : Albert Wang
# @Time : 2022-05-01 15:48:29
# @Description :
# @Email : [email protected]
# @FilePath : /ComputerScienceThesis/Database.py
# @LastTime : 2022-05-01 15:48:30
# @LastAuthor : Albert Wang
# @Software : Vscode
# @Copyright Notice : Copyright (c) 2022 Albert Wang 王子睿, All Rights Reserved.
"""
# cython: language_level=3
#!./env python
# -*- coding: utf-8 -*-
"""
# @Author : Albert Wang
# @Time : 2022-04-08 11:17:23
# @Description :
# @Email : [email protected]
# @FilePath : /ComputerScienceThesis/src/Database.py
# @LastTime : 2022-04-12 23:40:55
# @LastAuthor : Albert Wang
# @Software : Vscode
# @Copyright Notice : Copyright (c) 2022 Albert Wang 王子睿, All Rights Reserved.
"""
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from faker import Faker
import hashlib, time
from random import randint
from isort import code
app = Flask(__name__)
app.config.from_object("App.Config.Dev")
db = SQLAlchemy(app)
class Department(db.Model):
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
nullable=False,
index=True,
comment="id序列",
doc="这是部门的id序列主键",
)
id_parent = db.Column(
db.Integer,
nullable=False,
index=True,
comment="id序列",
doc="这是上级部门的id序列",
)
name = db.Column(
db.String(64),
nullable=False,
index=True,
comment="名称",
doc="部门的名称",
)
def __init__(self, id, id_parent, name):
self.id = id
self.id_parent = id_parent
self.name = name
def __repr__(self):
return "<department id %r>" % self.id
class Type(db.Model):
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
index=True,
comment="id序列",
doc="这是分类的id序列主键",
)
name = db.Column(
db.String(60),
nullable=False,
index=True,
comment="分类名称",
doc="分类名称",
)
def __init__(self, id, name):
self.id = id
self.name = name
def __repr__(self):
return "<Type id %r>" % self.id
class Admin(db.Model):
"""用户表"""
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
index=True,
comment="id序列",
doc="这是管理员的id序列主键",
)
login = db.Column(
db.String(20),
unique=True,
nullable=False,
index=True,
comment="管理员的登录名称",
doc="这是管理员的登录名称",
)
password = db.Column(
db.String(32),
nullable=False,
index=True,
comment="密码",
doc="这是管理员的密码",
)
name = db.Column(
db.String(60),
nullable=False,
index=True,
comment="管理员姓名",
doc="这是管理员的姓名",
)
status = db.Column(
db.Integer,
nullable=False,
index=True,
comment="id序列",
doc="这是管理员的状态,0为弃用,1为启用",
)
def __init__(self, id, login, password, name, status):
self.id = id
self.login = login
self.password = password
self.name = name
self.status = status
def __repr__(self):
return "<id %r>" % self.id
class Teacher(db.Model):
"""
教师
"""
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
index=True,
comment="id序列",
doc="这是管理员的id序列主键",
)
##TODO:Warning:这里有一个外键序列
department_id = db.Column(
db.Integer,
nullable=False,
index=True,
comment="外键序列",
doc="注意这是部门的外键序列",
)
login = db.Column(
db.String(20),
unique=True,
nullable=False,
index=True,
comment="老师的登录名称",
doc="这是老师的登录名称",
)
password = db.Column(
db.String(32),
nullable=False,
index=True,
comment="密码",
doc="这是管理员的密码",
)
code = db.Column(
db.String(10),
nullable=False,
unique=True,
index=True,
comment="学号",
doc="这是老师对应的号码",
)
name = db.Column(
db.String(60),
nullable=False,
index=True,
comment="管理员姓名",
doc="这是管理员的姓名",
)
cid = db.Column(
db.String(18),
index=True,
unique=True,
comment="身份证",
doc="身份证号",
)
phone = db.Column(
db.String(11), unique=True, nullable=False, index=True, comment="手机号", doc="手机号"
)
email = db.Column(
db.String(128),
unique=True,
nullable=False,
index=True,
comment="邮箱地址",
doc="邮箱地址",
)
in_time = db.Column(
db.Integer, nullable=False, index=True, comment="入职时间", doc="入职时间"
)
out_time = db.Column(db.Integer, index=True, comment="离开职时间", doc="离开时间")
info = db.Column(
db.Text,
comment="个人介绍",
doc="个人介绍",
)
subscribe_1 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
subscribe_2 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
subscribe_3 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
subscribe_4 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
subscribe_5 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
def __init__(
self,
id,
department_id,
login,
password,
code,
name,
cid,
phone,
email,
in_time,
out_time,
info,
subscribe_1,
subscribe_2,
subscribe_3,
subscribe_4,
subscribe_5,
):
self.id = id
self.department_id = department_id
self.login = login
self.password = password
self.code = code
self.name = name
self.cid = cid
self.email = email
self.phone = phone
self.in_time = in_time
self.out_time = out_time
self.info = info
self.subscribe_1 = subscribe_1
self.subscribe_2 = subscribe_2
self.subscribe_3 = subscribe_3
self.subscribe_4 = subscribe_4
self.subscribe_5 = subscribe_5
def __repr__(self):
return "<Teacher id %r>" % self.id
class Student(db.Model):
"""
学生
"""
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
index=True,
comment="id序列",
doc="这是学生的id序列主键",
)
##TODO:Warning:这里有一个外键序列
department_id = db.Column(
db.Integer,
nullable=False,
index=True,
comment="外键序列",
doc="注意这是部门的外键序列",
)
login = db.Column(
db.String(20),
unique=True,
nullable=False,
index=True,
comment="学生的登录名称",
doc="这是学生的登录名称",
)
password = db.Column(
db.String(32),
nullable=False,
index=True,
comment="密码",
doc="这是管理员的密码",
)
code = db.Column(
db.String(10),
nullable=False,
unique=True,
index=True,
comment="学号",
doc="这是学生对应的号码",
)
name = db.Column(
db.String(60),
nullable=False,
index=True,
comment="管理员姓名",
doc="这是管理员的姓名",
)
cid = db.Column(
db.String(18),
index=True,
unique=True,
comment="身份证",
doc="身份证号",
)
phone = db.Column(
db.String(11), unique=True, nullable=False, index=True, comment="手机号", doc="手机号"
)
email = db.Column(
db.String(128),
nullable=False,
unique=True,
index=True,
comment="邮箱地址",
doc="邮箱地址",
)
in_time = db.Column(
db.Integer, nullable=False, index=True, comment="入职时间", doc="入职时间"
)
out_time = db.Column(db.Integer, index=True, comment="离开职时间", doc="离开时间")
info = db.Column(
db.Text,
comment="个人介绍",
doc="个人介绍",
)
subscribe_1 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
subscribe_2 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
subscribe_3 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
subscribe_4 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
subscribe_5 = db.Column(
db.Integer,
index=True,
comment="外键序列",
doc="注意这是订阅类别的外键序列",
)
l_name = db.Column(db.String(60), index=True, comment="联系人姓名", doc="联系人电话")
l_phone = db.Column(db.String(11), index=True, comment="联系人电话", doc="联系人电话")
add = db.Column(db.String(255), index=True, comment="家庭住址", doc="家庭住址")
def __init__(
self,
id,
department_id,
login,
password,
code,
name,
cid,
phone,
email,
in_time,
out_time,
info,
l_name,
l_phone,
add,
subscribe_1,
subscribe_2,
subscribe_3,
subscribe_4,
subscribe_5,
):
self.id = id
self.department_id = department_id
self.login = login
self.password = password
self.code = code
self.name = name
self.cid = cid
self.email = email
self.phone = phone
self.in_time = in_time
self.out_time = out_time
self.info = info
self.l_name = l_name
self.l_phone = l_phone
self.add = add
self.subscribe_1 = subscribe_1
self.subscribe_2 = subscribe_2
self.subscribe_3 = subscribe_3
self.subscribe_4 = subscribe_4
self.subscribe_5 = subscribe_5
def __repr__(self):
return "<Student id %r>" % self.id
class Class(db.Model):
"""
课程
"""
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
index=True,
comment="id序列",
doc="这是课程的id序列主键",
)
name = db.Column(
db.String(60),
nullable=False,
index=True,
comment="课程姓名",
doc="这是课程的姓名",
)
teacher_id = db.Column(
db.Integer,
nullable=False,
index=True,
comment="外键序列",
doc="注意这是老师的外键序列",
)
begintime = db.Column(
db.Integer,
nullable=False,
index=True,
comment="开始时间",
doc="具体的开始时间",
)
endtime = db.Column(
db.Integer,
nullable=False,
index=True,
comment="结束时间",
doc="课程的结束时间",
)
user_count = db.Column(
db.Integer,
nullable=False,
index=True,
comment="课程总人数",
doc="课程总人数",
)
def __init__(self, id, name, teacher_id, begintime, endtime, user_count):
self.id = id
self.name = name
self.teacher_id = teacher_id
self.begintime = begintime
self.endtime = endtime
self.user_count = user_count
def __repr__(self):
return "<Class id %r>" % self.id
class Score(db.Model):
"""
学生
"""
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
index=True,
comment="id序列",
doc="这是课程的id序列主键",
)
student_id = db.Column(
db.Integer,
nullable=False,
index=True,
comment="外键序列",
doc="注意这是学生的外键序列",
)
class_id = db.Column(
db.Integer,
nullable=False,
index=True,
comment="外键序列",
doc="注意这是学生的外键序列",
)
score = db.Column(
db.Float(3, 1),
nullable=False,
index=True,
comment="课程成绩",
doc="学生的课程成绩分数",
)
k_time = db.Column(
db.Integer,
nullable=False,
index=True,
comment="考试时间",
doc="考试时间",
)
def __init__(self, id, student_id, class_id, score, k_time):
self.id = id
self.student_id = student_id
self.class_id = class_id
self.score = score
self.score = score
self.k_time = k_time
def __repr__(self):
return "<Student id %r>" % self.id
class Announcement(db.Model):
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
index=True,
comment="id序列",
doc="这是公告的id序列主键",
)
name = db.Column(
db.String(128),
nullable=False,
index=True,
comment="公告的名称和标题",
doc="公告的名称和标题",
)
type_id = db.Column(
db.Integer,
nullable=False,
index=True,
comment="公告的分类类别",
doc="公告的分类类别",
)
filedir = db.Column(
db.Text,
nullable=False,
comment="公告的文件路径",
doc="公告的文件路径",
)
add_time = db.Column(
db.Integer, nullable=False, index=True, comment="添加时间", doc="添加时间"
)
def __init__(self, id, name, type_id, filedir, add_time):
self.id = id
self.name = name
self.type_id = type_id
self.filedir = filedir
self.add_time = add_time
def __repr__(self):
return "<Type id %r>" % self.id
def password_init(password):
db_salt = "e625fb438d13f7f6defbbacb1f0a6d84"
temp = password + db_salt
jmd5 = hashlib.md5(temp.encode(encoding="UTF-8")).hexdigest()
return jmd5
db.create_all()
fake = Faker("zh_CN")
department_list_extend = [
Department(id=0, id_parent=0, name=fake.country()),
Department(id=1, id_parent=0, name=fake.country()),
Department(id=2, id_parent=0, name=fake.country()),
Department(id=3, id_parent=0, name=fake.country()),
]
department_list = [
Department(id=i, id_parent=randint(0, 3), name=fake.country()) for i in range(4, 30)
]
department_list.extend(department_list_extend)
type_list = [Type(id=i, name=fake.job()) for i in range(1, 10)]
admin_name = [fake.user_name() for i in range(20)]
admin_list = [
Admin(
id=i,
login=admin_name[i - 1],
name=fake.name(),
password=password_init(admin_name[i - 1]),
status=1,
)
for i in range(1, 20)
]
teacher_name = [fake.user_name() for i in range(30)]
teacher_list = [
Teacher(
id=i,
department_id=randint(0, 29),
login=teacher_name[i - 1],
password=password_init(teacher_name[i - 1]),
code=str(fake.random_number(10)),
name=fake.name(),
cid=fake.ssn(),
phone=fake.phone_number(),
email=fake.email(),
in_time=time.time(),
out_time=time.time(),
info=fake.text(),
subscribe_1=randint(1, 9),
subscribe_2=randint(1, 9),
subscribe_3=randint(1, 9),
subscribe_4=randint(1, 9),
subscribe_5=randint(1, 9),
)
for i in range(1, 30)
]
student_name = [fake.user_name() for i in range(100)]
student_list = [
Student(
id=i,
department_id=randint(0, 29),
login=student_name[i - 1],
password=password_init(student_name[i - 1]),
code=str(fake.random_number(10)),
name=fake.name(),
cid=fake.ssn(),
phone=fake.phone_number(),
email=fake.email(),
in_time=time.time(),
out_time=time.time(),
info=fake.text(),
subscribe_1=randint(1, 9),
subscribe_2=randint(1, 9),
subscribe_3=randint(1, 9),
subscribe_4=randint(1, 9),
subscribe_5=randint(1, 9),
l_name=fake.name(),
l_phone=fake.phone_number(),
add=fake.address(),
)
for i in range(1, 100)
]
class_list = [
Class(
id=i,
name=fake.company(),
teacher_id=randint(1, 29),
begintime=time.time(),
endtime=time.time(),
user_count=randint(15, 100),
)
for i in range(1, 40)
]
score_list = [
Score(
id=i,
student_id=randint(1, 99),
class_id=randint(1, 39),
score=fake.pyfloat(left_digits=2, right_digits=2, positive=True),
k_time=time.time(),
)
for i in range(1, 200)
]
announcement_list = [
Announcement(
id=i,
name=fake.sentence(),
type_id=randint(1, 9),
filedir=fake.file_path(),
add_time=time.time(),
)
for i in range(1, 30)
]
db.session.bulk_save_objects(department_list)
db.session.bulk_save_objects(type_list)
db.session.bulk_save_objects(admin_list)
db.session.bulk_save_objects(teacher_list)
db.session.bulk_save_objects(student_list)
db.session.bulk_save_objects(class_list)
db.session.bulk_save_objects(score_list)
db.session.bulk_save_objects(announcement_list)
db.session.commit()
print("测试用数据库初始化完成")