Python 程序:学员管理系统
1、需求
2、表结构
3、readme
4、目录结构
5、代码
6、测试样图
一、需求
需求:
角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下
讲师视图:
管理班级,可创建班级,根据学员qq号把学员加入班级
可创建指定班级的上课纪录,注意一节上课纪录对应多条学员的上课纪录, 即每节课都有整班学员上, 为了纪录每位学员的学习成绩,需在创建每节上课纪录是,同时为这个班的每位学员创建一条上课纪录
为学员批改成绩, 一条一条的手动修改成绩
学员视图:
提交作业
查看作业成绩
一个学员可以同时属于多个班级,就像报了Linux的同时也可以报名Python一样, 所以提交作业时需先选择班级,再选择具体上课的节数
附加:学员可以查看自己的班级成绩排名
二、表结构
三、readme
作者:zz 版本: 学员管理系统 版本 v0.1 开发环境: python3.6 程序介绍: 管理界面: 讲师视图 学员视图 创建讲师 创建学员 讲师视图: 显示可管理的班级 创建班级 为学员分配班级 添加课程计划 创建上课记录 修改学员成绩 学员视图: 上传作业 查看作业成绩 查看班级排名:当前班级日排名和总排名
readme
四、目录结构
五、代码
1 from conf import settings 2 from modules import home_page 3 from db import table_structure 4 5 if __name__ == ‘__main__‘: 6 obj = home_page.page() 7 obj.func()
start
1 import os,sys 2 3 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #程序主目录: 4 sys.path.append(BASE_DIR) #添加环境变量 5 6 from sqlalchemy import create_engine 7 engine = create_engine("mysql+pymysql://root:[email protected]/managedb?charset=utf8",encoding = ‘utf-8‘)
setting
1 from sqlalchemy import Table, Column, Integer,String,DATE,ForeignKey 2 from sqlalchemy.orm import relationship 3 from sqlalchemy.ext.declarative import declarative_base 4 from conf.settings import engine 5 6 Base = declarative_base() 7 student_m2m_classes = Table("student_m2m_classes",Base.metadata, #多对多 8 Column("student_id",Integer,ForeignKey("student.stu_id")), 9 Column("classes_id",Integer,ForeignKey("classes.cls_id")) 10 ) 11 teacher_m2m_classes = Table("teacher_m2m_classes",Base.metadata, #多对多 12 Column("teacher_id", Integer, ForeignKey("teacher.teach_id")), 13 Column("classes_id", Integer, ForeignKey("classes.cls_id")), 14 ) 15 class Student(Base): 16 __tablename__ = "student" 17 stu_id = Column(Integer,primary_key=True) 18 stu_QQ = Column(Integer) 19 stu_name = Column(String(64), nullable=False) 20 stu_pswd = Column(String(64), nullable=False) 21 classes = relationship(‘Classes‘,secondary=student_m2m_classes,backref=‘student‘) 22 def __repr__(self): 23 return "学生姓名:【%s】"%self.stu_name 24 class Classes(Base): 25 __tablename__ = "classes" 26 cls_id = Column(Integer,primary_key=True) 27 cls_name = Column(String(64), nullable=False,unique=True) 28 cls_course = Column(String(64), nullable=False,unique=True) 29 def __repr__(self): 30 return "班级名:【%s】"%self.cls_name 31 class Teacher(Base): 32 __tablename__ = "teacher" 33 teach_id = Column(Integer, primary_key=True) 34 teach_name = Column(String(32), nullable=False, unique=True) #唯一 35 teach_pswd = Column(String(32), nullable=False) 36 classes = relationship("Classes", secondary=teacher_m2m_classes, backref="teacher") 37 def __repr__(self): 38 return "讲师:【%s】"%self.teach_name 39 class Day(Base): 40 __tablename__ = "day" 41 day_id = Column(Integer, primary_key=True) 42 day_name = Column(String(32), nullable=False, unique=True) 43 def __repr__(self): 44 return "DAY:【%s】"%self.day_name 45 class Classes_m2m_Day(Base): 46 __tablename__ = "classes_m2m_day" 47 id = Column(Integer, primary_key=True) 48 class_id = Column(Integer,ForeignKey("classes.cls_id")) 49 day_id = Column(Integer, ForeignKey("day.day_id")) 50 classes = relationship("Classes",backref="classes_m2m_day") 51 day = relationship("Day", backref="classes_m2m_day") 52 def __repr__(self): 53 return "%s %s" % (self.classes,self.day) 54 class Study_record(Base): 55 __tablename__ = "study_record" 56 id = Column(Integer,primary_key=True) 57 class_m2m_day_id = Column(Integer,ForeignKey("classes_m2m_day.id")) 58 class_id = Column(Integer,ForeignKey("classes.cls_id")) 59 stu_id = Column(Integer, ForeignKey("student.stu_id")) 60 status = Column(String(64),nullable=False) 61 score = Column(Integer,nullable=True) 62 home_work = Column(String(64),nullable=False) 63 class_m2m_lessons = relationship("Classes_m2m_Day",backref="study_record") 64 students = relationship("Student", backref="study_record") 65 classes = relationship("Classes", backref="study_record") 66 67 def __repr__(self): 68 return "\033[35;0m%s,%s,状态:【%s】,成绩:【%s】\33[0m"%(self.class_m2m_lessons,self.students,self.status,self.score) 69 70 Base.metadata.create_all(engine) #创建表结构
table_structure
1 from sqlalchemy.orm import sessionmaker 2 3 from db import table_structure 4 5 Session_class = sessionmaker(bind=table_structure.engine) #创建与数据库的会话session class ,注意,这里返回给session的是个class,不是实例 6 session = Session_class() #生成session实例,类似cursor 7 # s1 = table_structure.Student(stu_id = 1,stu_QQ = 111111,stu_name = "aa") 8 # s2 = table_structure.Student(stu_id = 2,stu_QQ = 222222,stu_name = "bb") 9 # s3 = table_structure.Student(stu_id = 3,stu_QQ = 333333,stu_name = "cc") 10 # 11 # c1 = table_structure.Classes(cls_id = 1,cls_name = "P01",cls_course = "python") 12 # c2 = table_structure.Classes(cls_id = 2,cls_name = "L01",cls_course = "linux") 13 # 14 # s1.classes = [c1,c2] 15 # s2.classes = [c1,] 16 # s3.classes = [c1,] 17 # d1 = table_structure.Day(day_id = 1,day_name = "day_1") 18 # d2 = table_structure.Day(day_id = 2,day_name = "day_2") 19 # d3 = table_structure.Day(day_id = 3,day_name = "day_3") 20 # d4 = table_structure.Day(day_id = 4,day_name = "day_4") 21 22 # teacher_obj = session.query(table_structure.Teacher).filter_by(teach_id = 3).first() 23 # print(teacher_obj) 24 # 25 # t1 = table_structure.Teacher(teach_id = 1,teach_name = "teacher_01") 26 # t2 = table_structure.Teacher(teach_id = 2,teach_name = "teacher_02") 27 # 28 # t1.classes = [c1,c2] 29 # t2.classes = [c2,] 30 # classes_obj = session.query(table_structure.Classes).filter(table_structure.Classes.cls_id == "1").first() 31 # print(classes_obj.cls_id) 32 # day_obj = session.query(table_structure.Day).filter_by(day_name="day_1").first() 33 # print(day_obj) 34 # class_m2m_day_obj = session.query(table_structure.Classes_m2m_Day).filter(table_structure.Classes_m2m_Day.class_id == classes_obj.cls_id). \ 35 # filter(table_structure.Classes_m2m_Day.day_id == day_obj.day_id).first() 36 # print(class_m2m_day_obj) 37 # print(classes_obj.student) 38 # for student_obj in classes_obj.student: 39 # print(student_obj) 40 # study_record_obj = table_structure.Study_record(class_m2m_day_id=class_m2m_day_obj.id, 41 # stu_id=student_obj.stu_id, 42 # status="yes") 43 # session.add(study_record_obj) 44 45 # 46 # study_record_objs = session.query(table_structure.Study_record).filter(table_structure.Study_record.class_m2m_day_id==class_m2m_day_obj.id).all() 47 # for obj in study_record_objs: 48 # print(obj) 49 # student_obj = session.query(table_structure.Student).filter_by(stu_name="cc").first() 50 # study_record_obj = session.query(table_structure.Study_record).filter( 51 # table_structure.Study_record.class_m2m_day_id==class_m2m_day_obj.id).filter( 52 # table_structure.Study_record.stu_id == student_obj.stu_id).first() 53 # study_record_obj.score= 85 54 55 56 # day_obj = session.query(table_structure.Day).filter(table_structure.Day.day_id == "4").first() 57 58 # classes_obj.day.append(d1) 59 # print(classes_obj) 60 # 61 # session.add_all([s1,s2,s3,c1,c2,d1,d2,d3,d4,t1,t2]) 62 63 # m1 = table_structure.Classes_m2m_Day(id = 4,class_id = classes_obj.cls_id,day_id = day_obj.day_id) 64 65 # session.add_all([d1,d2,d3,d4,]) 66 67 # session.delete(teacher_obj) 68 # session.add_all([m1,]) 69 # classes_obj = session.query(table_structure.Classes).filter(table_structure.Classes.cls_id == "1").first() 70 # print(classes_obj.student) 71 72 session.commit()
session_maker
1 from sqlalchemy.orm import sessionmaker 2 from conf import settings 3 from conf.settings import engine 4 from db import table_structure 5 from db.table_structure import Teacher,Student,Classes 6 from modules.teacher_view import TeacherView 7 from modules.student_view import StudentView 8 9 class page(object): 10 def __init__(self): 11 Session = sessionmaker(bind=engine) 12 self.session = Session() 13 def func(self): 14 while True: 15 print("\033[34;1m***欢迎进入学员管理系统***\033[0m") 16 print("\033[32;1m1、讲师视图\033[0m") 17 print("\033[32;1m2、学员视图\033[0m") 18 print("\033[32;1m3、创建讲师\033[0m") 19 print("\033[32;1m4、创建学员\033[0m") 20 print("\033[31;1m按【q】键退出!\033[0m") 21 user_choise = input(">>:") 22 if user_choise == ‘1‘: 23 print("\033[34;1m==========讲师视图==========\033[0m") 24 TeacherView(self.session) 25 elif user_choise == ‘2‘: 26 print("\033[34;1m==========学员视图==========\033[0m") 27 StudentView(self.session) 28 elif user_choise == ‘3‘: 29 print("\033[34;1m==========创建讲师==========\033[0m") 30 self.creat_teacher() 31 elif user_choise == ‘4‘: 32 print("\033[34;1m==========创建学员==========\033[0m") 33 self.creat_student() 34 elif user_choise == ‘q‘: 35 print("\033[34;1m谢谢使用!\033[0m") 36 break 37 else: 38 print("\033[31;1m请输入正确的编号!\033[0m") 39 def creat_teacher(self): 40 ‘‘‘创建讲师‘‘‘ 41 rest = self.session.query(Teacher).filter(Teacher.teach_id>=0).all() 42 print(rest) 43 while True: 44 teacher_name = input("\033[34;0m请输入讲师名:\033[0m").strip() 45 if self.session.query(Teacher).filter_by(teach_name=teacher_name).first(): 46 print("\33[31;1m输入错误:讲师名已存在\33[0m") 47 continue 48 else: 49 break 50 teacher_pswd = input("\033[34;0m请输入密码:\033[0m").strip() 51 t = Teacher(teach_name = teacher_name,teach_pswd = teacher_pswd) 52 self.session.add_all([t,]) 53 self.session.commit() 54 def creat_student(self): 55 ‘‘‘创建学生‘‘‘ 56 rest = self.session.query(Student).filter(Student.stu_id>=0).all() 57 print(rest) 58 while True: 59 stu_name = input("\033[34;0m请输入学员姓名:\033[0m").strip() 60 if self.session.query(Student).filter_by(stu_name=stu_name).first(): 61 print("\33[31;1m输入错误:学员已存在\33[0m") 62 continue 63 else: 64 break 65 stu_pswd = input("\033[34;0m请输入密码:\033[0m").strip() 66 stu_QQ = input("\033[34;0m请输入QQ号码:\033[0m").strip() 67 s = Student(stu_name = stu_name,stu_QQ = stu_QQ,stu_pswd = stu_pswd) 68 self.session.add_all([s,]) 69 self.session.commit()
home_page
1 from db import table_structure 2 from db.table_structure import Teacher,Classes,Student,Day,Classes_m2m_Day,Study_record 3 from sqlalchemy.sql import func 4 5 class StudentView(object): 6 def __init__(self,session): 7 self.session = session 8 self.auth() 9 def auth(self): 10 ‘‘‘登录认证‘‘‘ 11 while True: 12 student_name = input("\033[34;0m请输入学员名:\033[0m").strip() 13 self.student_obj = self.session.query(Student).filter_by(stu_name=student_name).first() 14 if not self.student_obj: 15 print("\33[31;1m输入错误:学员不存在\33[0m") 16 continue 17 else: 18 student_pswd = input("\033[34;0m请输入密码:\033[0m").strip() 19 if student_pswd ==self.student_obj.stu_pswd: 20 print("\033[36;1m登录成功!\033[0m") 21 self.handler() 22 else: 23 print("\33[31;1m密码错误!\33[0m") 24 break 25 def handler(self): 26 print("\33[34;1m***欢迎【%s】进入学员管理系统***\33[0m"%self.student_obj.stu_name) 27 while True: 28 print("\33[32;1m***************************\n\33[0m" 29 "[up_homework] \33[32;1m上传作业\n\33[0m" 30 "[show_results] \33[32;1m查看作业成绩\n\33[0m" 31 "[show_rank] \33[32;1m查看班级排名\n\33[0m" 32 "[exit] \33[32;1m退出管理系统\n\033[0m" 33 "\33[32;1m***************************\n\33[0m") 34 cmd = input("\033[34;0m请输入进行操作的命令:\033[0m") 35 if hasattr(self, cmd): 36 getattr(self, cmd)() 37 else: 38 print("\33[31;1m【%s】命令不存在!\33[0m"%cmd) 39 def up_homework(self): 40 "上传作业" 41 class_name = input("\033[34;0m请输入班级名:\033[0m") 42 class_obj = self.session.query(Classes).filter_by(cls_name=class_name).first() 43 if class_obj and self.student_obj in class_obj.student: #输入的班级名存在且学生属于此班级 44 day_name = input("\033[34;0m请输入的课节名(例:day_1):\033[0m") 45 day_obj = self.session.query(Day).filter_by(day_name=day_name).first() 46 if day_obj: # 输入的day存在 47 class_m2m_day_obj = self.session.query(Classes_m2m_Day).filter(Classes_m2m_Day.class_id==class_obj.cls_id). 48 filter(Classes_m2m_Day.day_id==day_obj.day_id).first() 49 if class_m2m_day_obj: # 班级对应的课day表数据存在 50 study_record_obj = self.session.query(Study_record).filter( 51 Study_record.class_m2m_day_id == class_m2m_day_obj.id).filter( 52 Study_record.stu_id == self.student_obj.stu_id).first() 53 if study_record_obj: # 上课记录存在 54 home_work = input("上传作业:") 55 study_record_obj.home_work = home_work 56 self.session.commit() 57 print("上传成功!") 58 else: 59 print("\33[31;1m系统错误:当前上课记录未创建\33[0m") 60 else: 61 print("\33[31;1m系统错误:当前班级对应课程计划未创建\33[0m") 62 else: 63 print("\33[31;1m系统错误:课程计划未创建\33[0m") 64 else: 65 print("\33[31;1m系统错误:班级不存在或不属于此班级\33[0m") 66 def show_results(self): 67 "查看成绩" 68 class_name = input("\033[34;0m请输入班级名:\033[0m") 69 class_obj = self.session.query(Classes).filter_by(cls_name=class_name).first() 70 if class_obj and self.student_obj in class_obj.student: #输入的班级名存在且学生属于此班级 71 class_m2m_day_obj = self.session.query(Classes_m2m_Day).filter(Classes_m2m_Day.class_id==class_obj.cls_id).all() 72 if class_m2m_day_obj: 73 for class_m2m_day in class_m2m_day_obj: 74 study_record_obj = self.session.query(Study_record).filter(Study_record.class_m2m_day_id==class_m2m_day.id). 75 filter(Study_record.stu_id==self.student_obj.stu_id).first() 76 if study_record_obj: 77 print("%s 学生:【%s】 成绩:【%s】"%(class_m2m_day,self.student_obj.stu_name,study_record_obj.score)) 78 else: 79 print("\33[31;1m%s上课记录未创建\33[0m"%class_m2m_day) 80 else: 81 print("\33[31;1m系统错误:当前班级对应课程计划未创建\33[0m") 82 else: 83 print("\33[31;1m系统错误:班级不存在或不属于此班级\33[0m") 84 def show_rank(self): 85 class_name = input("\033[34;0m请输入班级名:\033[0m") 86 class_obj = self.session.query(Classes).filter_by(cls_name=class_name).first() 87 if class_obj and self.student_obj in class_obj.student: #输入的班级名存在且学生属于此班级 88 while True: 89 choice = input("\033[34;0m1、查看每日成绩排名;2、查看总成绩排名;【Q】退出:\033[0m") 90 if choice == "1": 91 class_m2m_day_obj = self.session.query(Classes_m2m_Day).filter(Classes_m2m_Day.class_id==class_obj.cls_id).all() 92 if class_m2m_day_obj: 93 for class_m2m_day in class_m2m_day_obj: 94 ret = self.session.query(Study_record).filter(Study_record.class_m2m_day_id==class_m2m_day.id).order_by(Study_record.score.desc()).all() 95 for i in ret: 96 print(i) 97 else: 98 print("\33[31;1m系统错误:当前班级对应课程计划未创建\33[0m") 99 elif choice == "2": 100 ret_1 = self.session.query(Study_record.stu_id,func.sum(Study_record.score).label("record")).101 filter(Study_record.class_id == class_obj.cls_id).group_by(Study_record.stu_id).order_by("record").all() 102 for i in ret_1: 103 s = self.session.query(Student).filter_by(stu_id = i.stu_id).all() 104 print(s,i.record) 105 elif choice == "q"or"Q": 106 break 107 else: 108 continue 109 else: 110 print("\33[31;1m系统错误:班级不存在或不属于此班级\33[0m") 111 def exit(self): 112 exit()
student_view
1 from db import table_structure 2 from db.table_structure import Teacher,Classes,Student,Day,Classes_m2m_Day,Study_record 3 4 class TeacherView(object): 5 def __init__(self,session): 6 self.session = session 7 self.auth() 8 def auth(self): 9 ‘‘‘登录认证‘‘‘ 10 while True: 11 teacher_name = input("\033[34;0m请输入讲师名:\033[0m").strip() 12 self.teacher_obj = self.session.query(Teacher).filter_by(teach_name=teacher_name).first() 13 if not self.teacher_obj: 14 print("\33[31;1m输入错误:讲师名不存在\33[0m") 15 continue 16 else: 17 teacher_pswd = input("\033[34;0m请输入密码:\033[0m").strip() 18 if teacher_pswd ==self.teacher_obj.teach_pswd: 19 print("\033[36;1m登录成功!\033[0m") 20 self.handler() 21 else: 22 print("\33[31;1m密码错误!\33[0m") 23 break 24 def handler(self): 25 print("\33[34;1m***欢迎【%s】进入讲师管理系统***\33[0m"%self.teacher_obj.teach_name) 26 while True: 27 print("\33[32;1m*******************************\n\33[0m" 28 "[show_classes] \33[32;1m显示可管理的班级\n\33[0m" 29 "[add_class] \33[32;1m创建班级\n\33[0m" 30 "[add_student_to_class] \33[32;1m为学员分配班级\n\33[0m" 31 "[add_day] \33[32;1m添加课程计划\n\33[0m" 32 "[add_studyrecord] \33[32;1m创建上课记录\n\33[0m" 33 "[modify_scores] \33[32;1m修改学员成绩\n\33[0m" 34 "[exit] \33[32;1m退出管理系统\n\33[0m" 35 "\33[32;1m*******************************\n\33[0m") 36 cmd = input("\033[34;0m请输入进行操作的命令:\033[0m") 37 if hasattr(self, cmd): 38 getattr(self, cmd)() 39 else: 40 print("\33[31;1m【%s】命令不存在!\33[0m"%cmd) 41 def show_classes(self): 42 ‘‘‘查看所有的班级‘‘‘ 43 for class_obj in self.teacher_obj.classes: 44 print("讲师:【%s】\t班级:【%s】\t类型:【%s】" % (self.teacher_obj.teach_name, 45 class_obj.cls_name,class_obj.cls_course)) 46 def add_class(self): 47 ‘‘‘创建班级‘‘‘ 48 class_name = input("\033[34;0m请输入创建班级的名称:\033[0m") 49 course = input("\033[34;0m请输入创建班级的类型:\033[0m") 50 class_obj = self.session.query(Classes).filter_by(cls_name=class_name).first() 51 class_new = Classes(cls_name=class_name,cls_course=course) 52 if not class_obj: 53 self.teacher_obj.classes.append(class_new) 54 self.session.add(class_new) 55 self.session.commit() 56 print("\033[34;1m班级创建成功\033[0m") 57 else: 58 print(class_obj.teacher) 59 if self.teacher_obj in class_obj.teacher: 60 print("\033[34;1m讲师【%s】对应的班级【%s:%s】已存在\033[0m"%(self.teacher_obj.teach_name,class_name,course)) 61 else: 62 self.teacher_obj.classes.append(class_obj) 63 self.session.commit() 64 print("\033[34;1m班级已存在,添加讲师成功\033[0m") 65 66 def add_student_to_class(self): 67 ‘‘‘为学员分配班级‘‘‘ 68 class_name = input("\033[34;0m请输入要添加学员的班级名:\033[0m") 69 class_obj = self.session.query(Classes).filter_by(cls_name=class_name).first() 70 if class_obj and self.teacher_obj in class_obj.teacher: 71 stu_name = input("\033[34;0m请输入学员的姓名:\033[0m") 72 student_obj = self.session.query(Student).filter_by(stu_name=stu_name).first() 73 if student_obj: 74 if student_obj in class_obj.student: 75 print("\33[31;1m系统错误:学员已经存在\33[0m") 76 else: 77 class_obj.student.append(student_obj) 78 self.session.commit() 79 print("\033[34;1m学员添加成功\033[0m") 80 else: 81 print("\33[31;1m学员未创建\33[0m") 82 else: 83 print("\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m") 84 85 def add_day(self): 86 ‘‘‘添加课程计划‘‘‘ 87 class_name = input("\033[34;0m请输入要添加课程计划的班级名:\033[0m") 88 class_obj = self.session.query(Classes).filter_by(cls_name=class_name).first() 89 if class_obj and self.teacher_obj in class_obj.teacher: #输入的班级名存在且在属于当前老师管理 90 day_name = input("\033[34;0m请输入添加的课程计划:\033[0m") 91 day_obj = self.session.query(Day).filter_by(day_name=day_name).first() 92 if not day_obj: #输入的lesson名字不存在 93 print("\033[34;0m课程计划不存在:创建成功!\033[0m") 94 day_obj = Day(lesson_name=day_name) #增加lesson表数据 95 self.session.add(day_obj) 96 self.session.commit() 97 else: 98 print("\33[31;1m课程计划已存在\33[0m") 99 rest = self.session.query(Classes_m2m_Day).filter(Classes_m2m_Day.class_id==class_obj.cls_id).100 filter(Classes_m2m_Day.day_id==day_obj.day_id).first() 101 if not rest: #如果class_m2m_day没有存储班级和课程计划的对应关系 102 class_m2m_day_new = Classes_m2m_Day(class_id=class_obj.cls_id,day_id=day_obj.day_id) 103 self.session.add(class_m2m_day_new) #创建class_m2m_day对应关系 104 self.session.commit() 105 print("\33[31;1m班级对应课程计划添加成功\33[0m") 106 else: 107 print("\33[31;1m班级对应课程计划已存在\33[0m") 108 else: 109 print("\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m") 110 111 def add_studyrecord(self): 112 ‘‘‘添加学习记录‘‘‘ 113 class_name = input("\033[34;0m请输入要添加学习记录的班级名:\033[0m") 114 class_obj = self.session.query(Classes).filter_by(cls_name=class_name).first() 115 if class_obj and self.teacher_obj in class_obj.teacher: #输入的班级名存在且在属于当前老师管理 116 day_name = input("\033[34;0m请输入添加学习记录的课节名(例:day_1):\033[0m") 117 day_obj = self.session.query(Day).filter_by(day_name=day_name).first() 118 if day_obj: # 输入的day名字存在 119 rest = self.session.query(Classes_m2m_Day).filter(Classes_m2m_Day.class_id==class_obj.cls_id).120 filter(Classes_m2m_Day.day_id==day_obj.day_id).first() 121 if rest: # 班级对应的课day表数据存在 122 study_record_obj = self.session.query(Study_record).filter_by(class_m2m_day_id=rest.id).first() 123 if not study_record_obj: # 上课记录未创建 124 for student_obj in class_obj.student: 125 status = input("输入学生 %s 的上课状态(yes/no):"%student_obj.stu_name) 126 study_record_new = Study_record(class_m2m_day_id=rest.id, 127 class_id=class_obj.cls_id, 128 stu_id=student_obj.stu_id, 129 status=status) 130 self.session.add(study_record_new) 131 self.session.commit() 132 else: 133 print("\33[31;1m系统错误:当前上课记录已经创建\33[0m") 134 else: 135 print("\33[31;1m系统错误:当前班级的对应的课程计划未创建\33[0m") 136 else: 137 print("\33[31;1m系统错误:课程计划未创建\33[0m") 138 else: 139 print("\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m") 140 141 def modify_scores(self): 142 ‘‘‘修改成绩‘‘‘ 143 class_name = input("\033[34;0m请输入要添加学习记录的班级名:\033[0m") 144 class_obj = self.session.query(Classes).filter_by(cls_name=class_name).first() 145 if class_obj and self.teacher_obj in class_obj.teacher: #输入的班级名存在且在属于当前老师管理 146 day_name = input("\033[34;0m请输入添加学习记录的课节名(例:day_1):\033[0m") 147 day_obj = self.session.query(Day).filter_by(day_name=day_name).first() 148 if day_obj: # 输入的day名字存在 149 rest = self.session.query(Classes_m2m_Day).filter(Classes_m2m_Day.class_id==class_obj.cls_id).150 filter(Classes_m2m_Day.day_id==day_obj.day_id).first() 151 if rest: # 班级对应的课day表数据存在 152 while True: 153 study_record_obj = self.session.query(Study_record).filter_by(class_m2m_day_id=rest.id).all() 154 for obj in study_record_obj: 155 print(obj) 156 student_name = input("\033[34;0m输入要修改成绩的学生名:[Q 退出]\33[0m") 157 if student_name == "q" or student_name == "Q":break 158 student_obj = self.session.query(Student).filter_by(stu_name=student_name).first() 159 if student_obj: 160 study_record_obj = self.session.query(Study_record).filter( 161 Study_record.class_m2m_day_id==rest.id).filter( 162 Study_record.stu_id == student_obj.stu_id).first() 163 if study_record_obj: # 上课记录存在 164 score = input("\033[34;0m输入修改后的成绩:\33[0m") 165 study_record_obj.score= score 166 self.session.commit() 167 else: 168 print("\33[31;1m系统错误:上课记录不存在\33[0m") 169 else: 170 print("\33[31;1m系统错误:学生不存在\33[0m") 171 else: 172 print("\33[31;1m系统错误:当前上课记录已经创建\33[0m") 173 else: 174 print("\33[31;1m系统错误:当前班级的对应的课程计划未创建\33[0m") 175 else: 176 print("\33[31;1m系统错误:课程计划未创建\33[0m") 177 else: 178 print("\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m") 179 180 def exit(self): 181 exit()
teacher_view
六、测试样图
原文地址:https://www.cnblogs.com/hy0822/p/9348220.html
时间: 2024-11-04 14:52:27