老男孩Day13作业:ORM学员管理系统

一、作业需求:

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

讲师视图:

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

学员视图:

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

 

二、

一、作业需求:

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

讲师视图:

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

学员视图:

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

二、博客地址:https://www.cnblogs.com/catepython/p/9105669.html

三、运行环境

操作系统:Win10

Python:3.6.4rcl

Pycharm:2017.3.4

四 、具体实现

1、借鉴了大神们的表设计结构(本人借鉴的是Assassinの 博客地址是:https://www.cnblogs.com/lianzhilei/archive/2016/10/30/6013847.html)

2、根据表结构设计在create_table.py中创建相应的表

3、根据需求搭好界面性的程序框架

4、根据程序流程图和表结构逐步完成各模块函数

5、边写函数发放边调试、测试等


五、测试流程

1、在测试调试过程中也发现了一些bug

例如:

学生QQ是唯一,但先输入用户名在输入已存在的QQ号这时程序报了错(现已修复)

2、也经常发现了跨表数据的现象

例如:

查询学员成绩排名是把不在同一个班级或课节的学员也查出来(多做了一些判断已修复类似现象)

3、对程序中也完善了一些唯一、不能为空、成绩必须是整数0-100范围判断...

六、备注
1、Assassinの的表设计很完美,几乎没有冗余数据,但查询关联起来对于我等菜鸟还真是有点伤脑。
readme

 

 

三、程序流程图

四、数据结构图

 

 五、程序架构图

 

六、核心代码

bin目录

#-*-coding:utf-8 -*-
# Author: D.Gray

from core import main

if __name__ == '__main__':

    start = main.Action()
    start.func()
start

conf目录 配置文件

#-*-coding:utf-8 -*-
# Author: D.Gray
import sqlalchemy
from sqlalchemy import create_engine

engine = create_engine('mysql+pymysql://root:admin1988@localhost/Manager?charset=utf8')
setting

core目录 主程序文件

#-*-coding:utf-8 -*-
# Author: D.Gray
import sqlalchemy
from conf.setting import engine
from sqlalchemy.orm import sessionmaker
from database.create_table import Teacher,Class
from src.Student_view import Student_manager
from src.Teacher_view import Teacher_manager
class Action(object):
    def __init__(self):
        Session_class = sessionmaker(bind=engine)
        self.session = Session_class()
        self.initialize_database()

    def func(self):
        while True:
            mesg = '''\033[35;1m                       欢迎进入CLASS_SYSTEM系统
                             1 讲师视图
                             2 学生视图
                             q 退出管理系统\033[0m
            
            '''
            print(mesg)
            use_choice = input("\033[34;0m请输入你进入的视图:\033[0m")
            if use_choice == '1':
                Teacher_manager(self.session)
            elif use_choice == '2':
                Student_manager(self.session)
            elif use_choice == 'q':
                exit("\033[31;0m感谢使用管理系统,\033[0m")
                break
            else:
                print("\033[31;1m请输入正确的选项\033[0m")

    def initialize_database(self):
        rest = self.session.query(Teacher).filter(Teacher.teacher_id>0).all()
        if not rest:
            c1 = Class(class_name = 'S14',course = 'Python')
            t1 = Teacher(teacher_name = 'Alex')

            t1.teacher_class = [c1]
            self.session.add_all([c1,t1])
            self.session.commit()
            print('数据添加成')
main

database目录  数据文件

#-*-coding:utf-8 -*-
# Author: D.Gray
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,relationship
from sqlalchemy import Column,String,Integer,Table,ForeignKey
from conf.setting import engine

Base = declarative_base()

teacher_m2m_class = Table(
    'teacher_m2m_class',Base.metadata,
    Column('teacher_id',Integer,ForeignKey('teacher.teacher_id'),nullable=False),
    Column('class_id',Integer,ForeignKey('class.class_id'),nullable=False)
)

class_m2m_student = Table(
    'class_m2m_student',Base.metadata,
    Column('class_id',Integer,ForeignKey('class.class_id'),nullable=False),
    Column('stu_id',Integer,ForeignKey('student.stu_id'),nullable=False)
)

class Teacher(Base):
    __tablename__ = 'teacher'
    teacher_id = Column(Integer,primary_key=True)
    teacher_name = Column(String(64),nullable=False)
    teacher_class = relationship('Class',secondary = teacher_m2m_class,backref = 'teacher_keys')

    def __repr__(self):
        return '讲师:【%s】'%(self.teacher_name)

class Class(Base):
    __tablename__ = 'class'
    class_id = Column(Integer,primary_key=True)
    class_name = Column(String(64),nullable=False)
    course = Column(String(64),nullable=False)
    class_student = relationship('Student',secondary = class_m2m_student,backref = 'class_keys')

    def __repr__(self):
        return '班级:【%s】'%(self.class_name)

class Student(Base):
    __tablename__ = 'student'
    stu_id = Column(Integer,primary_key=True)
    stu_name = Column(String(64),nullable=False)
    QQ = Column(String(64),nullable=False,unique=True)

    def __repr__(self):
        return '学生名:【%s】'%(self.stu_name)

class Lesson(Base):
    __tablename__ = 'lesson'
    lesson_id = Column(Integer,primary_key=True)
    lesson_name = Column(String(64),nullable=False)

    def __repr__(self):
        return '课节名:【%s】'%(self.lesson_name)

class Study_record(Base):
    __tablename__ = 'study_record'
    id = Column(Integer,primary_key=True)
    class_m2m_lesson_id = Column(Integer,ForeignKey('class_m2m_lesson.id'),nullable=False)
    stu_id = Column(Integer,ForeignKey('student.stu_id'),nullable=False)
    status = Column(String(64),nullable=False,default='Y')
    score = Column(Integer,nullable=False,default=0)

    class_m2m_lessons = relationship('class_m2m_lesson',backref = 'study_keys')
    study_students = relationship('Student',backref = 'study_student_keys')

    def __repr__(self):
        return "\033[32;0m%s,%s,状态:【%s】,成绩:【%s】\33[0m"\
               %(self.class_m2m_lessons,self.study_students,self.status,self.score)


class class_m2m_lesson(Base):
    __tablename__ = 'class_m2m_lesson'
    id = Column(Integer,primary_key=True)
    class_id = Column(Integer,ForeignKey('class.class_id'),nullable=False)
    lesson_id = Column(Integer,ForeignKey('lesson.lesson_id'),nullable=False)
    classes = relationship('Class',backref = 'class_lesson_keys')
    lessons = relationship('Lesson',backref = 'lesson_class_keys')

    def __repr__(self):
        return '<%s  %s>'%(self.class_id,self.lesson_id)

Base.metadata.create_all(engine)
print('创建表成功')
create_table

src目录 主要类文件

# -*- coding:utf-8 -*-
# Author:D.Gray
from database.create_table import Class,Teacher,Student,class_m2m_lesson,class_m2m_student,Lesson,Study_record
class Teacher_manager(object):
    def __init__(self,session):
        self.session = session
        self.authentication()
        self.handle()

    def authentication(self):
        '''
        教师认证函数
        :return:
        '''
        while True:
            teacher_input = input("\033[33;0m请输入讲师名:\033[0m").strip()
            self.teacher_obj = self.session.query(Teacher).filter(Teacher.teacher_name == teacher_input).first()
            #print(teacher_obj)
            if self.teacher_obj is None:
                print("\33[31;1m输入错误:请输入有效的讲师名\33[0m")
                continue
            else:
                break

    def handle(self):
        '''
        教师操作视图
        :return:
        '''
        while True:
            print("\033[35;1m欢迎【%s】进入讲师管理系统\n"
                  "1、显示可管理的班级\n"
                  "2、创建班级\n"
                  "3、添加学员\n"
                  "4、添加课程节次\n"
                  "5、创建上课记录\n"
                  "6、修改学员成绩\n"
                  "exit 退出管理系统\n\033[0m"%self.teacher_obj.teacher_name)
            self.dic = {
                '1':self.show_classes,
                '2':self.add_class,
                '3':self.add_student,
                '4':self.add_lesson,
                '5':self.add_study_record,
                '6':self.modify_scores,
                'exit':self.exit
            }
            action_input = input("\033[34;0m请输入进行操作的命令:\033[0m")
            if action_input in self.dic.keys():
               self.dic[action_input]()
            else:
                print("\033[31;0m请输入有效操作的命令\033[0m")

    def show_classes(self):
        '''
        查看所有管理的班级
        :return:
        '''
        for class_obj in self.teacher_obj.teacher_class:
            print('\033[32;1m%s  班级名:%s   课程:%s\033[0m'
                  % (self.teacher_obj.teacher_name, class_obj.class_name, class_obj.course))

    def add_class(self):
        '''
        添加教室
        :return:
        '''
        class_input = input("\033[33;0m请输入创建班级的名称:\033[0m").capitalize()
        class_obj = self.session.query(Class).filter(Class.class_name==class_input).first()
        if class_obj is None:
            course_input = input("\033[33;0m请输入创建班级的课程:\033[0m").capitalize()
            class_obj =  Class(class_name = class_input,course = course_input)
            self.teacher_obj.teacher_class.append(class_obj)
            self.session.commit()
            print("\033[32;1m班级创建成功\033[0m")
        else:
            print("\33[31;1m系统错误:班级已经存在\33[0m")

    def add_student(self):
        '''
        添加学生
        :return:
        '''
        class_input = input("\033[33;0m请输入要添加学员的班级名:\033[0m").capitalize()
        class_obj = self.session.query(Class).filter(Class.class_name == class_input).first()
        if class_obj and class_obj.teacher_keys[0] == self.teacher_obj:
            stu_input = input("\033[33;0m请输入要添加的学员名:\033[0m").capitalize()
            stu_obj = self.session.query(Student).filter(Student.stu_name == stu_input).first()
            if stu_obj is None:
                QQ_input = input("\033[33;0m请输入要添加的学员QQ:\033[0m").capitalize()
                if QQ_input.isdigit():
                    QQ_input = int(QQ_input)
                    stu_qq_obj = self.session.query(Student).filter(Student.QQ == QQ_input).first()
                    if stu_qq_obj is None:
                        stu_obj = Student(stu_name = stu_input,QQ = QQ_input)
                        class_obj.class_student.append(stu_obj)
                        self.session.add(stu_obj)
                        self.session.commit()
                        print('\033[32;1m学员添加成功\033[0m')
                    else:
                        print('\033[31;1mQQ号已存在\033[0m')
                else:
                    print('\033[31;1mQQ号必须是数子\033[0m')
            else:
                print("\33[31;1m系统错误:学员已经存在\33[0m")
        else:
            print("\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m")

    def add_lesson(self):
        '''
        添加课程节次
        :return:
        '''
        class_input = input("\033[33;0m请输入要添加节次的班级名:\033[0m").capitalize()
        class_obj = self.session.query(Class).filter(Class.class_name == class_input).first()
        if class_obj and class_obj.teacher_keys[0] == self.teacher_obj:
            lesson_input = input("\033[33;0m请输入添加的lesson名(类day1):\033[0m")
            lesson_obj = self.session.query(Lesson).filter(Lesson.lesson_name == lesson_input).first()
            if lesson_obj is None:      #所需添加的lesson名不存在可创建
                lesson_obj = Lesson(lesson_name = lesson_input)
                self.session.add(lesson_obj)
                self.session.commit()
                print('\033[32;1mlesson表数据创建成功\033[0m')

                #在class_m2m_lesson创建一条关联数据
                rest = (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 rest is None:
                    rest = class_m2m_lesson(class_id = class_obj.class_id,lesson_id = lesson_obj.lesson_id)
                    self.session.add(rest)
                    self.session.commit()
                    print('\033[32;1m class_m2m_lesson表数据创建成功\033[0m')
            else:
                print("\33[31;1m系统错误:课节已经存在\33[0m")
        else:
            print("\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m")


    def add_study_record(self):
        '''
        添加上课记录数据
        :return:
        '''
        class_input = input("\033[33;0m请输入要添加记录的班级名:\033[0m").capitalize()
        class_obj = self.session.query(Class).filter(Class.class_name == class_input).first()
        if class_obj and class_obj.teacher_keys[0] == self.teacher_obj:
            lesson_input = input("\033[33;0m请输入添加记录的lesson名(类day1):\033[0m")
            lesson_obj = self.session.query(Lesson).filter(Lesson.lesson_name == lesson_input).first()
            if lesson_obj is not None:                      #课节已存在
                class_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_lesson_obj is not None:
                    study_record_obj = self.session.query(Study_record).\
                        filter(Study_record.class_m2m_lesson_id == class_lesson_obj.id).first()
                    if study_record_obj is not None:
                        while True:
                            print('\033[32;1m已有上课记录的学生信息\033[0m')
                            for student in class_obj.class_student:  # 循环遍历学生是否已在上课记录表中已记录
                                stu_id_obj = self.session.query(Study_record). \
                                    filter(Study_record.stu_id == student.stu_id).\
                                    filter(Study_record.class_m2m_lesson_id == class_lesson_obj.id).first()
                                if stu_id_obj is not None:  # 打印有上课记录的学生信息
                                    print('\033[32;1m学生名:【%s】  Status:【%s】   '
                                          '成绩:【%s】\033[0m'
                                          %(student.stu_name,stu_id_obj.status,stu_id_obj.score))
                            stu_input = input("\033[33;0m请输入要添加记录的学生名[Q 退出]:\033[0m").capitalize()
                            if stu_input == 'Q':break
                            stu_obj = self.session.query(Student).filter(Student.stu_name == stu_input).first()
                            if stu_obj is not None:     # 判断输入的学生名是否存在
                                study_stu_obj = self.session.query(Study_record).\
                                    filter(Study_record.stu_id == stu_obj.stu_id).\
                                    filter(Study_record.class_m2m_lesson_id == class_lesson_obj.id).first()
                                if study_stu_obj is None:       #判断该学生在Study_record表中有记录
                                    status_input = input("添加%s 的上课状态(Y/N):" % stu_obj.stu_name).capitalize()
                                    if status_input != 'N': status_input = 'Y'
                                    while True:
                                        score_input = input("添加 %s 的成绩:"
                                                            % stu_obj.stu_name).capitalize()
                                        if score_input.isdigit():
                                            score_input = int(score_input)
                                            if score_input >= 0 and score_input <= 100:
                                                study_record_obj_new = Study_record(
                                                    class_m2m_lesson_id = class_lesson_obj.id,
                                                    stu_id = stu_obj.stu_id,
                                                    status = status_input,
                                                    score = score_input
                                                )
                                                self.session.add(study_record_obj_new)
                                                self.session.commit()
                                                print('\033[32;1m记录添加完成\033[0m')
                                                break
                                            else:
                                                print('\033[31;1m请输入成绩超出有效范围\033[0m')
                                        else:
                                            print('\033[31;1m请输入有效成绩\033[0m')
                                    else:
                                        print("\33[31;1m系统错误:该学生上课记录已存在\33[0m")
                            else:
                                print("\33[31;1m系统错误:该学生未存在\33[0m")
                else:
                    print("\33[31;1m系统错误:class_m2m_lesson表中无记录\33[0m")
            else:
                print("\33[31;1m系统错误:课节不存在\33[0m")
        else:
            print("\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m")


    def modify_scores(self):
        '''
        修改学生成绩函数
        :return:
        '''
        class_input = input("\033[33;0m请输入要添加记录的班级名:\033[0m").capitalize()
        class_obj = self.session.query(Class).filter(Class.class_name == class_input).first()
        if class_obj and class_obj.teacher_keys[0] == self.teacher_obj:
            lesson_input = input("\033[33;0m请输入添加记录的lesson名(类day1):\033[0m")
            lesson_obj = self.session.query(Lesson).filter(Lesson.lesson_name == lesson_input).first()
            if lesson_obj is not None:  # 课节已存在
                class_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_lesson_obj is not None:    #判断该课节在class_m2m_lesson表是否存在
                    study_record_obj = self.session.query(Study_record). \
                        filter(Study_record.class_m2m_lesson_id == class_lesson_obj.id).first()
                    if study_record_obj is not None:
                        while True:
                            print('\033[32;1m已有上课记录的学生信息\033[0m')
                            for student in class_obj.class_student:  # 循环遍历学生是否已在上课记录表中已记录
                                stu_id_obj = self.session.query(Study_record). \
                                    filter(Study_record.stu_id == student.stu_id).\
                                    filter(Study_record.class_m2m_lesson_id == class_lesson_obj.id).first()
                                if stu_id_obj is not None:  # 打印未在有上课记录的学生信息
                                    print('\033[32;1m学生名:【%s】  Status:【%s】   '
                                          '成绩:【%s】\033[0m'
                                          %(student.stu_name,stu_id_obj.status,stu_id_obj.score))
                            stu_input = input("\033[33;0m请输入要修改记录的学生名[Q 退出]:\033[0m").capitalize()
                            if stu_input == 'Q':break
                            # 判断输入的学生名是否存在
                            stu_obj = self.session.query(Student).filter(Student.stu_name == stu_input).first()
                            if stu_obj is not None:
                                #判断该学生在Study_record表中有记录
                                study_stu_obj = self.session.query(Study_record).\
                                    filter(Study_record.stu_id == stu_obj.stu_id).\
                                    filter(Study_record.class_m2m_lesson_id == class_lesson_obj.id).first()
                                if study_stu_obj is not None:       #该学生已有上课记录
                                    status_input = input("修改%s 的上课状态(Y/N):" % stu_obj.stu_name).capitalize()
                                    if status_input != 'N': status_input = 'Y'
                                    while True:
                                        score_input = input("修改 %s 的成绩:"
                                                            % stu_obj.stu_name).capitalize()
                                        if score_input.isdigit():
                                            score_input = int(score_input)
                                            if score_input >= 0 and score_input <= 100:
                                                study_stu_obj.status = status_input
                                                study_stu_obj.score = score_input
                                                self.session.commit()
                                                print('\033[32;1m记录修改完成\033[0m')
                                                break
                                            else:
                                                print('\033[31;1m请输入成绩超出有效范围\033[0m')
                                        else:
                                            print('\033[31;1m请输入有效成绩\033[0m')
                                else:
                                    print("\33[31;1m系统错误:该学生上课记录未存在\33[0m")
                            else:
                                print("\33[31;1m系统错误:该学生未存在\33[0m")
                    else:
                        print("\33[31;1m系统错误:上课记录未存在\33[0m")
                else:
                    print("\33[31;1m系统错误:class_m2m_lesson表中无记录\33[0m")
            else:
                print("\33[31;1m系统错误:课节不存在\33[0m")
        else:
            print("\33[31;1m输入错误:班级不存在或没有权限管理此班级\33[0m")

    def exit(self):
        exit('\033[31;0m感谢使用管理系统,\033[0m')
teacher_view
# -*- coding:utf-8 -*-
# Author:D.Gray
from database.create_table import Class,Teacher,Student,class_m2m_lesson,class_m2m_student,Lesson,Study_record
import sqlalchemy
from sqlalchemy import func
class Student_manager(object):
    def __init__(self,session):
        self.session = session
        self.authentication()
        self.handle()

    def authentication(self):
        '''
        学生认证函数
        :return:
        '''
        while True:
            student_input = input("\033[33;0m请输入学生名:\033[0m").strip().capitalize()
            self.student_obj = self.session.query(Student).filter(Student.stu_name == student_input).first()
            if self.student_obj:
                print('\033[32;0m学生ID:%s\n所属班级:%s\033[0m'%(self.student_obj.stu_id,self.student_obj.class_keys))
                break
            else:
                print("\33[31;1m输入错误:请输入有效的学生名\33[0m")
                continue


    def handle(self):
        '''
        学生视图执行函数
        :return:
        '''
        while True:
            print("\033[35;1m欢迎【%s】进入学员管理系统\n"
                  "1、上传作业\n"
                  "2、查看作业成绩\n"
                  "3、查看班级排名\n"
                  "exit 退出管理系统\n\033[0m" % self.student_obj.stu_name)
            user_func = input("\033[34;0m请输入进行操作的命令:\033[0m")
            self.dic = {
                '1':self.up_homework,
                '2':self.show_homework,
                '3':self.show_rank,
                'exit':self.exit
            }
            if user_func in self.dic.keys():
                self.dic[user_func]()
            else:
                print("\033[31;0m请输入有效操作的命令\033[0m")


    def up_homework(self):
        '''
        上传作业函数
        :return:
        '''
        #print('in the up_homework')
        class_name = input("\033[33;0m请输入班级名:\033[0m").capitalize().strip()
        class_obj = self.student_obj.class_keys
        for classes in class_obj:
            if classes.class_name == class_name:
                if class_name:                           #判断学生是否存在所输入班级中
                    lesson_name = input("\033[33;0m请输入课节名:\033[0m").capitalize().strip()
                    lesson_obj = self.session.query(Lesson).filter(Lesson.lesson_name == lesson_name).first()
                    if lesson_obj:          #判断课节是否存在
                        class_m2m_lesson_obj = self.session.query(class_m2m_lesson).filter(
                            class_m2m_lesson.class_id == classes.class_id
                        ).filter(class_m2m_lesson.lesson_id == lesson_obj.lesson_id).first()
                        if class_m2m_lesson_obj:    #判断班级和课节是否在class_m2m_lesson表中
                            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==self.student_obj.stu_id).first()

                            if study_record_obj:        #判断测试学生在study_record表是否有记录
                                if study_record_obj.status == 'N':      #判断用户作业是否已提交
                                    print('\033[32;0m作业提交情况:未提交\033[0m' )
                                    up_input = input('\033[33;0m您本次课节作业是否选择提交(Y/N)>>>:\033[0m')\
                                        .strip().capitalize()
                                    if up_input == 'Y':
                                        study_record_obj.status = up_input
                                        self.session.commit()
                                        print('\033[32;0m本次课节作业提交完成\033[0m')
                                    else:
                                        print('\033[31;0m请按时提交本次课节作业\033[0m')
                                        break
                                else:
                                    print('\033[32;0m您本次课节作业情况:已提交\033[0m')
                            else:
                                print("\033[31;1m系统错误:未有该上课记录\033[0m")
                        else:
                            print("\033[31;1m系统错误:class_m2m_lesson表未创建\033[0m")
                    else:
                        print("\33[31;1m系统错误:课节未创建\33[0m")
            else:
                print("\33[31;1m输入错误:班级不存在或学生不在此班级\33[0m")


    def show_homework(self):
        '''
        查看成绩函数
        :return:
        '''
        class_name = input("\033[33;0m请输入班级名:\033[0m").capitalize().strip()
        class_obj = self.student_obj.class_keys
        for classes in class_obj:
            if classes.class_name == class_name:
                if class_name:  # 判断学生是否存在所输入班级中
                    lesson_name = input("\033[33;0m请输入课节名:\033[0m").capitalize().strip()
                    lesson_obj = self.session.query(Lesson).filter(Lesson.lesson_name == lesson_name).first()
                    if lesson_obj:
                        class_m2m_lesson_obj = self.session.query(class_m2m_lesson).filter(
                            class_m2m_lesson.class_id == classes.class_id
                        ).filter(class_m2m_lesson.lesson_id == lesson_obj.lesson_id).first()
                        if class_m2m_lesson_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 == self.student_obj.stu_id).first()

                            if study_record_obj:
                                if study_record_obj.status == 'N':
                                    print('\033[32;0m作业提交情况:未提交\033[0m')
                                    print('\033[31;0m请按时提交本次课节作业\033[0m')
                                else:
                                    print('\033[32;0m本次课节作业成绩:【%s】分\033[0m'%study_record_obj.score)
                                    break
                            else:
                                print("\033[31;1m系统错误:未有该上课记录\033[0m")
                        else:
                            print("\033[31;1m系统错误:class_m2m_lesson表未创建\033[0m")
                    else:
                        print("\33[31;1m系统错误:课节未创建\33[0m")
            else:
                print("\33[31;1m输入错误:班级不存在或学生不在此班级\33[0m")

    def show_rank(self):
        '''
        查看课节成绩排名函数
        :return:
        '''
        class_name = input("\033[33;0m请输入班级名:\033[0m").capitalize().strip()
        class_obj = self.student_obj.class_keys
        for classes in class_obj:
            if classes.class_name == class_name:
                if class_name:  # 判断学生是否存在所输入班级中
                    lesson_name = input("\033[33;0m请输入课节名:\033[0m").capitalize().strip()
                    lesson_obj = self.session.query(Lesson).filter(Lesson.lesson_name == lesson_name).first()
                    if lesson_obj:
                        class_m2m_lesson_obj = self.session.query(class_m2m_lesson).filter(
                            class_m2m_lesson.class_id == classes.class_id
                        ).filter(class_m2m_lesson.lesson_id == lesson_obj.lesson_id).first()
                        if class_m2m_lesson_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 == self.student_obj.stu_id).first()

                            if study_record_obj:
                                if study_record_obj.status == 'N':
                                    print('\033[32;0m作业提交情况:未提交\033[0m')
                                    print('\033[31;0m请按时提交本次课节作业\033[0m')
                                else:
                                    print('\033[32;0m本次课节作业成绩:【%s】分\033[0m' % study_record_obj.score)
                                    #成绩从大到小排序
                                    study_score_obj = self.session.query(Study_record.stu_id,Study_record.score).\
                                        filter(Study_record.score>0).\
                                        filter(Study_record.class_m2m_lesson_id == class_m2m_lesson_obj.id).\
                                        order_by(Study_record.score.desc()).limit(10)  #只取前十名成绩
                                    print('成绩排名列表:')
                                    for study_score in study_score_obj:
                                        student_obj = self.session.query(Student).\
                                            filter(Student.stu_id == study_score.stu_id).first()
                                        print('%s  成绩:【%s】'%(student_obj.stu_name,study_score.score))

                            else:
                                print("\033[31;1m系统错误:未有该上课记录\033[0m")
                        else:
                            print("\033[31;1m系统错误:class_m2m_lesson表未创建\033[0m")
                    else:
                        print("\33[31;1m系统错误:课节未创建\33[0m")
            else:
                print("\33[31;1m输入错误:班级不存在或学生不在此班级\33[0m")

    def exit(self):
        exit('\033[31;0m感谢使用管理系统,\033[0m')
student_view

 

posted @ 2018-05-29 16:31  空s蝉灬  阅读(635)  评论(0编辑  收藏  举报