需求:
用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下
讲师视图:
- 管理班级,可创建班级,根据学员qq号把学员加入班级
- 可创建指定班级的上课纪录,注意一节上课纪录对应多条学员的上课纪录, 即每节课都有整班学员上, 为了纪录每位学员的学习成绩,需在创建每节上课纪录是,同时为这个班的每位学员创建一条上课纪录
- 为学员批改成绩, 一条一条的手动修改成绩
学员视图:
- 提交作业
- 查看作业成绩
- 一个学员可以同时属于多个班级,就像报了Linux的同时也可以报名Python一样, 所以提交作业时需先选择班级,再选择具体上课的节数
- 附加:学员可以查看自己的班级成绩排名
表结构:
根据需求先画表结构
程序目录结构:
表结构实例代码:
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
|
from sqlalchemy import String,Column,Integer,ForeignKey,DATE,Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from conf.settings import engine
############创建数据表结构######################3 Base = declarative_base()
# 班级与学生的对应关系表 teacher_m2m_class = Table( "teacher_m2m_class" ,Base.metadata,
Column( "teacher_id" , Integer, ForeignKey( "teacher.teacher_id" )),
Column( "class_id" , Integer, ForeignKey( "class.class_id" )),
)
# 班级与学生的对应关系表 class_m2m_student = Table( "class_m2m_student" ,Base.metadata,
Column( "class_id" ,Integer,ForeignKey( "class.class_id" )),
Column( "stu_id" , Integer, ForeignKey( "student.stu_id" )),
)
class Class_m2m_Lesson(Base):
'''班级和课节对应表'''
__tablename__ = "class_m2m_lesson"
id = Column(Integer, primary_key = True )
class_id = Column(Integer,ForeignKey( "class.class_id" ))
lesson_id = Column(Integer, ForeignKey( "lesson.lesson_id" ))
classes = relationship( "Class" ,backref = "class_m2m_lessons" )
lessons = relationship( "Lesson" , backref = "class_m2m_lessons" )
def __repr__( self ):
return "%s %s" % ( self .classes, self .lessons)
class Study_record(Base):
"上课记录"
__tablename__ = "study_record"
id = Column(Integer,primary_key = True )
class_m2m_lesson_id = Column(Integer,ForeignKey( "class_m2m_lesson.id" ))
stu_id = Column(Integer, ForeignKey( "student.stu_id" ))
status = Column(String( 32 ),nullable = False )
score = Column(Integer,nullable = True )
class_m2m_lessons = relationship( "Class_m2m_Lesson" ,backref = "my_study_record" )
students = relationship( "Student" , backref = "my_study_record" )
def __repr__( self ):
return "\033[35;0m%s,%s,状态:【%s】,成绩:【%s】\33[0m" % ( self .class_m2m_lessons, self .students, self .status, self .score)
class Teacher(Base):
"讲师"
__tablename__ = "teacher"
teacher_id = Column(Integer, primary_key = True )
teacher_name = Column(String( 32 ), nullable = False , unique = True ) #唯一
classes = relationship( "Class" , secondary = teacher_m2m_class, backref = "teachers" )
def __repr__( self ):
return "讲师:【%s】" % self .teacher_name
class Class(Base):
"班级"
__tablename__ = "class"
class_id = Column(Integer, primary_key = True )
class_name = Column(String( 32 ), nullable = False ,unique = True )
course = Column(String( 32 ), nullable = False )
students = relationship( "Student" ,secondary = class_m2m_student,backref = "classes" )
def __repr__( self ):
return "班级名:【%s】" % self .class_name
class Student(Base):
"学生"
__tablename__ = "student"
stu_id = Column(Integer, primary_key = True )
stu_name = Column(String( 32 ), nullable = False , unique = True )
QQ = Column(Integer(), nullable = False )
def __repr__( self ):
return "学生名:【%s】" % self .stu_name
class Lesson(Base):
"课节"
__tablename__ = "lesson"
lesson_id = Column(Integer, primary_key = True )
lesson_name = Column(String( 32 ), nullable = False , unique = True )
def __repr__( self ):
return "节次名:【%s】" % self .lesson_name
Base.metadata.create_all(engine) |
创建学习记录以及修改学生成绩:
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
|
def add_studyrecord( self ):
'''添加学习记录'''
class_name = input ( "\033[34;0m请输入要添加学习记录的班级名:\033[0m" )
class_obj = self .session.query(Class).filter_by(class_name = class_name).first()
if class_obj and class_obj.teachers[ 0 ] = = self .teacher_obj: # 输入的班级名存在且在属于当前老师管理
lesson_name = input ( "\033[34;0m请输入添加学习记录的课节名(lesson):\033[0m" )
lesson_obj = self .session.query(Lesson).filter_by(lesson_name = lesson_name).first()
if lesson_obj: # 输入的lesson名字存在
class_m2m_lesson_obj = self .session.query(Class_m2m_Lesson). filter (Class_m2m_Lesson.class_id = = class_obj.class_id). \
filter (Class_m2m_Lesson.lesson_id = = lesson_obj.lesson_id).first()
if class_m2m_lesson_obj: # 班级对应的课lesson表数据存在
study_record_obj = self .session.query(Study_record).filter_by(class_m2m_lesson_id = class_m2m_lesson_obj. id ).first()
if not study_record_obj: # 上课记录为创建
for student_obj in class_obj.students:
status = input ( "输入学生 %s 的上课状态(yes/no):" % student_obj.stu_name)
study_record_new = Study_record(class_m2m_lesson_id = class_m2m_lesson_obj. id ,
stu_id = student_obj.stu_id,
status = status)
self .session.add(study_record_new)
self .session.commit()
else :
print ( "\33[31;1m系统错误:当前上课记录已经创建\33[0m" )
else :
print ( "\33[31;1m系统错误:当前班级的lesson课节未创建\33[0m" )
else :
print ( "\33[31;1m系统错误:lesson未创建\33[0m" )
else :
print ( "\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m" )
def modify_scores( self ):
'''修改成绩'''
class_name = input ( "\033[34;0m请输入学习记录的班级名:\033[0m" )
class_obj = self .session.query(Class).filter_by(class_name = class_name).first()
if class_obj and class_obj.teachers[ 0 ] = = self .teacher_obj: # 输入的班级名存在且在属于当前老师管理
lesson_name = input ( "\033[34;0m请输入学习记录的课节名(lesson):\033[0m" )
lesson_obj = self .session.query(Lesson).filter_by(lesson_name = lesson_name).first()
if lesson_obj: # 输入的lesson名字存在
class_m2m_lesson_obj = self .session.query(Class_m2m_Lesson). filter (
Class_m2m_Lesson.class_id = = class_obj.class_id). \
filter (Class_m2m_Lesson.lesson_id = = lesson_obj.lesson_id).first()
if class_m2m_lesson_obj: # 班级对应的课lesson表数据存在
while True :
study_record_objs = self .session.query(Study_record). filter (
Study_record.class_m2m_lesson_id = = class_m2m_lesson_obj. id ). all ()
for obj in study_record_objs:
print (obj)
student_name = input ( "\033[34;0m输入要修改成绩的学生名:[Q 退出]\33[0m" )
if student_name = = "q" or student_name = = "Q" : break
student_obj = self .session.query(Student).filter_by(stu_name = student_name).first()
if student_obj:
study_record_obj = self .session.query(Study_record). filter (
Study_record.class_m2m_lesson_id = = class_m2m_lesson_obj. id ). filter (
Study_record.stu_id = = student_obj.stu_id).first()
if study_record_obj: # 上课记录存在
score = input ( "\033[34;0m输入修改后的成绩\33[0m" )
study_record_obj.score = score
self .session.commit()
else :
print ( "\33[31;1m系统错误:当前上课记录已经创建\33[0m" )
else :
print ( "\33[31;1m系统错误:当前班级的lesson课节未创建\33[0m" )
else :
print ( "\33[31;1m系统错误:lesson未创建\33[0m" )
else :
print ( "\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m" )
|
上面的两段代码实现了主要的功能,其他的功能都是套路,都一样。。。。
完整代码地址--》》https://coding.net/u/James_Lian/p/Whaterver/git/tree/master