python项目开发:学员管理系统
学员管理系统
#需求:
1.用户角色:讲师/学员,登陆后根据角色不同能做的事情不同
2.讲师视图
- 管理班级,可创建班级,根据学员qq号把学员加入班级
- 可创建指定班级的上课纪录,注意一节上课纪录对应多条学员的上课纪录, 即每节课都有整班学员上,
- 为了纪录每位学员的学习成绩,需在创建每节上课纪录是,同时为这个班的每位学员创建一条上课纪录
- 为学员批改成绩, 一条一条的手动修改成绩
3.学员视图
- 提交作业
- 查看作业成绩
- 一个学员可以同时属于多个班级,就像报了Linux的同时也可以报名Python一样, 所以提交作业时需先选择班级,再选择具体上课的节数
附加:学员可以查看自己的班级成绩排名
#注:开发过程中遇到的问题
- 只有当两张表都是用class()方法建表时,这两张表之间才能建立反查关联
- 当两张通过class()方法建立的表要建立多对多关系时,第三张表必须使用Table()方法来建立
- Table()方法中建立联合唯一索引:UniqueConstraint("lesson_id","class_id",name="lesson_class_id")
- Table()方法不是通过类来建立映射关系的,因此不能通过"类名"."字段名"来查询
- 获取刚插入数据的主键id,只需刷新一下即可(session.flush())
#业务逻辑:
#数据表设计:
#代码实例
ReadMe.txt
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#博客地址:https://www.cnblogs.com/BUPT-MrWu/p/10626405.html #学员管理系统 - 程序要求: 1.用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 2.讲师视图 - 管理班级,可创建班级,根据学员qq号把学员加入班级 - 可创建指定班级的上课纪录,注意一节上课纪录对应多条学员的上课纪录, 即每节课都有整班学员上, - 为了纪录每位学员的学习成绩,需在创建每节上课纪录是,同时为这个班的每位学员创建一条上课纪录 - 为学员批改成绩, 一条一条的手动修改成绩 3.学员视图 - 提交作业 - 查看作业成绩 - 一个学员可以同时属于多个班级,就像报了Linux的同时也可以报名Python一样, 所以提交作业时需先选择班级,再选择具体上课的节数 - 附加:学员可以查看自己的班级成绩排名 #目录结构 |--StudentManageSystem |--bin |--start.py #程序入口 |--conf |--settings.py #配置 |--core |--main.py #主逻辑交互 |--models |--datasheet.py #数据表 |--modules |--authentication.py #登陆模块 |--student_center.py #学生中心 |--teacher_center.py #讲师中心 |--logs #可扩展日志模块 |--ReadMe.txt #初始数据库 - 默认创建两名教师 - id: 1 name: alex password: 111111 - id: 2 name: MrWu password: 222222 - 默认创建20天教学周期
bin//start.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os,sys BASE_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) sys.path.append(BASE_dir) from core import main if __name__ == '__main__': obj = main.MainLogic()
conf//settings.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
conn = "mysql+pymysql://root:187847@localhost/testdb?charset=utf8" #数据库连接方式 min_score = 0 #批改作业默认最小分数 max_score = 100 #批改作业默认最大分数 class_days = 20 #课程默认最大周期为20天
core//main.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from modules import student_center,teacher_center from models import datasheet from conf import settings from sqlalchemy.orm import sessionmaker class MainLogic(object): def __init__(self): self.__initialization() self.interactive() def interactive(self): while True: menu = ''' 1.学生中心 2.讲师中心 q.退出 ''' print("\033[1;33m欢迎来到学员管理系统\033[0m".center(40,"*").strip(),menu) user_choice = input("input your choice ID>>>:") if user_choice == "1": student_center.Student() elif user_choice =="2": teacher_center.Teacher() elif user_choice == "q": print("感谢您使用学员管理系统,退出ing......") break def __initialization(self): '''初始化数据库''' Session = sessionmaker(bind=datasheet.engine) session = Session() query_teacher = session.query(datasheet.Teacher).filter(datasheet.Teacher.id>=1).all() if not query_teacher: tea_obj = datasheet.Teacher(name="alex",password="111111") tea_obj2 = datasheet.Teacher(name="MrWu",password="222222") session.add_all([tea_obj,tea_obj2]) session.commit() query_lesson = session.query(datasheet.Lesson).filter(datasheet.Lesson.id>=1).all() if not query_lesson: lesson_list = [] day = 1 while day <= settings.class_days: lesson_obj = datasheet.Lesson(class_day=day) lesson_list.append(lesson_obj) day += 1 session.add_all(lesson_list) session.commit() session.close() return
modules//authentication.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from sqlalchemy.orm import sessionmaker from models import datasheet Session_class = sessionmaker(bind=datasheet.engine) session = Session_class() def auth(auth_type): def out_wrapper(func): def wrapper(): if auth_type == "auth_student": stu_id = input("请输入学号ID>>>:") password = input("请输入密码password>>>:") query_stu_obj = session.query(datasheet.Student).filter( datasheet.Student.id==stu_id, datasheet.Student.password==password).first() if query_stu_obj: res = func() return res,stu_id else: print("学号ID或密码输入错误!") return "false" elif auth_type == "auth_teacher": tea_id = input("请输入职工号ID>>>:") password = input("请输入密码password>>>:") query_tea_obj = session.query(datasheet.Teacher).filter( datasheet.Teacher.id==tea_id, datasheet.Teacher.password==password).first() if query_tea_obj: res = func() return res,tea_id else: print("教职工号ID或密码输入错误!") return "false" return wrapper return out_wrapper @auth(auth_type="auth_student") def auth_student_center(): return "true" @auth(auth_type="auth_teacher") def auth_teacher_center(): return "true"
modules//student_center.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import random from models import datasheet from modules import authentication from sqlalchemy.orm import sessionmaker class Student(object): def __init__(self): Session_obj = sessionmaker(bind=datasheet.engine) self.session = Session_obj() self.interactive() def interactive(self): while True: menu = ''' 1.学员注册 2.上传作业 3.查看成绩 q.退出 ''' print("\033[1;33m欢迎来到学生管理系统/学员中心\033[0m".center(40, "*"), menu) user_choice = input("input your choice ID>>>:") if user_choice == "1": self.sign_up() elif user_choice == "2": self.up_work() elif user_choice == "3": self.check_grade() elif user_choice == "q": print("\033[1;34m感谢您使用学生管理系统/学员中心\033[0m") break def sign_up(self): '''学员注册''' while True: name = input("input your name>>>:") qq = input("input yur QQ number>>>:") if not name or not qq: continue query_student = self.session.query(datasheet.Student).filter(datasheet.Student.qq == qq).first() if not query_student: password = str(random.randint(10000, 100000)) stu_obj = datasheet.Student(name=name, qq=qq, password=password) self.session.add(stu_obj) self.session.commit() query_stu_obj = self.session.query(datasheet.Student).filter(datasheet.Student.qq == qq).first() print("学员[name: %s]注册成功" % (name)) print("请记住登陆信息:\n" "学号[ID :%s]\n" "密码[password: %s]" % (query_stu_obj.id, password)) break else: print("此学员已注册成功!") break def up_work(self): '''上传作业''' learn_record_obj,lesson_day,lesson_class_id = self.__learn_record() if learn_record_obj.homework: print("【lesson day: %s】作业已上传!" % (lesson_day)) else: learn_record_obj.homework ="Y" self.session.commit() print("【lesson day: %s】作业上传完毕"%(lesson_day)) def check_grade(self): '''查看成绩、排名''' learn_record_obj,lesson_day,lesson_class_id = self.__learn_record() score = learn_record_obj.score if not score: print("作业还未批改完毕!") else: fewer_score_count = self.session.query(datasheet.LearnRecord).filter( datasheet.LearnRecord.score>score, datasheet.LearnRecord.lesson_class_id==lesson_class_id).count() print("【lesson_day: %s】【grade: %s】【ranking: %s】"%(lesson_day,score,fewer_score_count+1)) def __learn_record(self): '''登陆后,获取learn_record_obj''' res = authentication.auth_student_center() if res[0] == "false": return stu_id = res[1] stu_obj = self.session.query(datasheet.Student).filter(datasheet.Student.id == stu_id).first() while True: class_id = input("请输入班级ID>>>:") lesson_day = input("请输入班级lesson day>>>:") query_lesson_obj = self.session.query(datasheet.Lesson).filter( datasheet.Lesson.class_day == lesson_day).first() if not query_lesson_obj: print("此班级lesson day不存在,请重新输入!") continue query_class_obj = self.session.query(datasheet.Class).filter(datasheet.Class.id == class_id).first() if not query_class_obj: print("此班级不存在,请重新输入!") continue if stu_obj not in query_class_obj.student: print("您不是此班级的学生!") continue lesson_class_obj = self.session.query(datasheet.lesson_MtoM_class).filter( datasheet.lesson_MtoM_class.class_id == class_id, datasheet.lesson_MtoM_class.lesson_id == query_lesson_obj.id).first() learn_record_obj = self.session.query(datasheet.LearnRecord).filter( datasheet.LearnRecord.stu_id == stu_id, datasheet.LearnRecord.lesson_class_id == lesson_class_obj.id).first() if not learn_record_obj: print("此次班级课节还未完成,无法查询!") continue break return learn_record_obj,lesson_day,lesson_class_obj.id
modules//teacher_center.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from modules import authentication from models import datasheet from conf import settings from sqlalchemy import func from sqlalchemy.orm import sessionmaker class Teacher(object): def __init__(self): Session_class = sessionmaker(bind=datasheet.engine) self.session = Session_class() self.tea_id = self.__auth() self.interactive() def interactive(self): while True: menu = ''' 1.创建班级 2.招收学员 3.创建学习记录 4.批改作业 q.退出 ''' print("\033[1;33m欢迎来到学生管理系统/讲师中心\033[0m".center(40, "*"), menu) choice = input("请输入选择ID>>>:") if choice == "1": self.create_class() elif choice == "2": self.enrolling_student() elif choice == "3": self.create_record() elif choice == "4": self.correct_work_score() elif choice == "q": print("\033[1;34m感谢您使用学生管理系统/讲师中心\033[0m") break def create_class(self): '''创建班级,开设课程''' while True: max_count = settings.class_days # 课程最大周期数 course = input("请输入您创建班级的课程名>>>:") if not course: continue class_day = int(input("请输入课程周期天数[最大为:%s]>>>:" % (max_count))) if class_day > max_count: print("输入错误!") break class_obj = datasheet.Class(course=course) tea_obj = self.session.query(datasheet.Teacher).filter(datasheet.Teacher.id == self.tea_id).first() class_obj.teacher = [tea_obj, ] self.session.add(class_obj) self.session.commit() self.session.flush() class_lesson_list = [] count = 1 while count <= class_day: lesson_class_obj = datasheet.lesson_MtoM_class(lesson_id=count,class_id=class_obj.id) class_lesson_list.append(lesson_class_obj) count += 1 self.session.add_all(class_lesson_list) self.session.commit() if_continue = input("创建[%s班级]成功,是否继续创建?Y/N>>>:" % (course)) if if_continue == "Y": continue else: break def enrolling_student(self): '''招收学员''' while True: class_list = [] qq = input("请输入招收学员的QQ号>>>:") if not qq: continue stu_obj = self.session.query(datasheet.Student).filter(datasheet.Student.qq == qq).first() if not stu_obj: print("不存在此学生!") break tea_obj = self.session.query(datasheet.Teacher).filter(datasheet.Teacher.id == self.tea_id).first() all_class_obj = tea_obj.banji class_obj_list = [] #存放班级实例 while True: class_id = input("请输入学员加入的班级ID>>>:") query_class_obj = self.session.query(datasheet.Class).filter(datasheet.Class.id==class_id).first() if query_class_obj not in all_class_obj: print("不存在此班级或您对此班级没有权限!") continue if stu_obj in query_class_obj.student: print("班级[ID:%s]中已存在此学生!"%(class_id)) continue if_continue = input("该学生是否要加入其他班级?Y/N>>>:") if if_continue == "Y": class_obj_list.append(query_class_obj) continue else: class_obj_list.append(query_class_obj) break stu_obj.banji = class_obj_list self.session.add(stu_obj) self.session.commit() if_continue = input("招收[学员%s]成功,是否继续招收新的学员?Y/N>>>:" % (stu_obj.name)) if if_continue == "Y": continue else: break def create_record(self): '''为学生创建上课记录''' while True: class_id = input("请输入班级ID:>>>:") tea_obj = self.session.query(datasheet.Teacher).filter(datasheet.Teacher.id == self.tea_id).first() all_class_obj = tea_obj.banji query_class_obj = self.session.query(datasheet.Class).filter(datasheet.Class.id==class_id).first() if query_class_obj not in all_class_obj: print("班级不存在或您对此班级没有权限!") break class_day = input("请输入班级lesson day>>>:") lesson_obj = self.session.query(datasheet.Lesson).filter(datasheet.Lesson.class_day == class_day).first() if not lesson_obj: print("输入错误!") break lesson_class_obj = self.session.query(datasheet.lesson_MtoM_class).filter( datasheet.lesson_MtoM_class.lesson_id == lesson_obj.id, datasheet.lesson_MtoM_class.class_id == class_id).first() if not lesson_class_obj: print("不存在这个班级课节!") break lesson_class_id = lesson_class_obj.id query_learn_record = lesson_class_obj.learn_record if not query_learn_record: pass else: print("班级[ID:%s] lesson day[day:%s]的记录已存在!"%(class_id,class_day)) break all_student_obj = query_class_obj.student learn_record_list = [] # 存放LearnRecord实例 for stu_obj in all_student_obj: while True: if_absence = input("请输入学生[姓名: %s qq: %s]是否正常上课?Y/N>>>:" % (stu_obj.name, stu_obj.qq)) if if_absence == "Y" or if_absence == "N": obj = datasheet.LearnRecord(stu_id=stu_obj.id, lesson_class_id=lesson_class_id, status=if_absence) learn_record_list.append(obj) break else: print("输入错误,请重新输入!") self.session.add_all(learn_record_list) self.session.commit() if_continue = input("为班级[ID: %s]创建学习记录完毕,是否为其他班级创建学习记录?Y/N>>>:"%(class_id)) if if_continue == "Y": continue else: break def correct_work_score(self): '''为学生批改成绩''' while True: class_id = input("请输入班级ID:>>>:") class_day = input("请输入班级lesson day>>>:") if not class_id or not class_day:continue lesson_obj = self.session.query(datasheet.Lesson).filter(datasheet.Lesson.class_day==class_day).first() if not lesson_obj: print("不存在此班级lesson day!") break lesson_class_obj = self.session.query(datasheet.lesson_MtoM_class).filter( datasheet.lesson_MtoM_class.class_id==class_id, datasheet.lesson_MtoM_class.lesson_id==lesson_obj.id).first() if not lesson_class_obj: print("不存在此班级课节!") break all_learn_record = lesson_class_obj.learn_record if not all_learn_record: print("此班级课节未完成,无法批改成绩!") break for learn_record_obj in all_learn_record: while True: print("学号ID: %s 作业完成情况: %s 考勤记录: %s"%(learn_record_obj.stu_id, learn_record_obj.homework,learn_record_obj.status)) score = int(input("请为该学生打上成绩>>>:")) if score >= settings.min_score and score <= settings.max_score: learn_record_obj.score = score break else: print("输入范围超过限制,请重新输入!") self.session.commit() if_continue = input("班级[ID:%s]的作业以批改完毕,是否继续批改其他班级的作业?Y/N>>>:") if if_continue == "Y": continue else: break def __auth(self): res = authentication.auth_teacher_center() if res[0] == "false": return return res[1]
models//datasheet.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import sqlalchemy from conf import settings from sqlalchemy import Column,Table,create_engine from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Integer,String,Enum,Date,ForeignKey,UniqueConstraint engine = create_engine(settings.conn) Base = declarative_base() stu_MtoM_class = Table( "stu_MtoM_class",Base.metadata, Column('stu_id',Integer,ForeignKey("student.id")), Column("class_id",Integer,ForeignKey("banji.id")) ) tea_MtoM_class = Table( "tea_MtoM_class",Base.metadata, Column("tea_id",Integer,ForeignKey("teacher.id")), Column("class_id",Integer,ForeignKey("banji.id")), ) # lesson_MtoM_class = Table( # "lesson_MtoM_class",Base.metadata, # Column("id",Integer,primary_key=True), # Column("lesson_id",Integer,ForeignKey("lesson.id")), # Column("class_id",Integer,ForeignKey("banji.id")), # UniqueConstraint("lesson_id","class_id",name="lesson_class_id") #建立联合唯一索引 # ) class lesson_MtoM_class(Base): __tablename__ = "lesson_MtoM_class" __table_args__ = (UniqueConstraint("lesson_id","class_id",name="lesson_class_id"),) id = Column(Integer,primary_key=True) lesson_id = Column(Integer,ForeignKey("lesson.id")) class_id = Column(Integer,ForeignKey("banji.id")) class Student(Base): __tablename__ = 'student' id = Column(Integer,primary_key=True) name = Column(String(32),nullable=False) qq = Column(String(32),unique=True,nullable=False) password = Column(String(16),nullable=False) banji = relationship("Class",secondary="stu_MtoM_class",backref="student") learn_record = relationship("LearnRecord",backref="student") class Teacher(Base): __tablename__ = 'teacher' id = Column(Integer,primary_key=True) name = Column(String(32),nullable=False) password = Column(String(16),nullable=False) banji = relationship("Class",secondary="tea_MtoM_class",backref="teacher") class Class(Base): __tablename__ = 'banji' id = Column(Integer,primary_key=True) course = Column(String(32),nullable=False) class Lesson(Base): __tablename__ = "lesson" id = Column(Integer,primary_key=True) class_day = Column(Integer,unique=True,nullable=False) # banji = relationship("Class",secondary="lesson_MtoM_class",backref="lesson")#不能通过第三张类表建立反查关联 class LearnRecord(Base): __tablename__ = 'learn_record' __table_args__ = (UniqueConstraint("stu_id","lesson_class_id",name="stu_lesson_class_id"),) id = Column(Integer,primary_key=True) stu_id = Column(Integer,ForeignKey("student.id")) lesson_class_id = Column(Integer,ForeignKey("lesson_MtoM_class.id")) status = Column(Enum("Y","N"),nullable=False) homework = Column(Enum("Y","N")) score = Column(Integer) lesson_MtoM_class = relationship("lesson_MtoM_class",backref="learn_record") Base.metadata.create_all(engine)
#运行实例