Python 程序:学员管理系统

Posted 水无

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 程序:学员管理系统相关的知识,希望对你有一定的参考价值。

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:123456@192.168.1.112/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以上是关于Python 程序:学员管理系统的主要内容,如果未能解决你的问题,请参考以下文章

Python面向对象版学员管理系统(附源码)

Python 程序:选课系统

Python开发程序:选课系统

Python开发程序:选课系统-改良版

学员管理系统(面向对象版)

python 学员管理系统