当前位置: 移动技术网 > IT编程>脚本编程>Python > 学员管理系统(mysql)

学员管理系统(mysql)

2018年10月22日  | 移动技术网IT编程  | 我要评论

风流仕途,前妻回家下载,5156无忧无虑

主题:学员管理系统

需求:

用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下

讲师视图:

  •   管理班级,可创建班级,根据学员qq号把学员加入班级
  •   可创建指定班级的上课纪录,注意一节上课纪录对应多条学员的上课纪录, 即每节课都有整班学员上, 为了纪录每位学员的学习成绩,需在创建每节上课纪录是,同时为这个班的每位学员创建一条上课纪录
  •   为学员批改成绩, 一条一条的手动修改成绩

学员视图:

  • 提交作业
  • 查看作业成绩
  • 一个学员可以同时属于多个班级,就像报了linux的同时也可以报名python一样, 所以提交作业时需先选择班级,再选择具体上课的节数
  • 附加:学员可以查看自己的班级成绩排名

表结构:

根据需求先画表结构

 

程序目录结构:

 

表结构实例代码:

 

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)
创建表结构
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")
创建学习记录以及修改学生成绩

上面的两段代码实现了主要的功能,其他的功能都是套路,都一样。。。。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网