一、业务逻辑

二、设计表结构

三、代码结构

start.py

import os, sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) if __name__ == '__main__':
from core import main
main.Management_center()

main.py   处理不同角色请求

 from core.color import Colors
from core.center_admin import Center_admin
from core.center_student import Center_student
from core.center_teacher import Center_teacher
from core.create_table import User
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import hashlib class Management_center(object):
def __init__(self):
engine = create_engine("mysql+pymysql://root:123456@10.0.0.15/studentdb?charset=utf8")#, echo=True)
Session_class = sessionmaker(bind=engine)
self.session = Session_class()
self.init_db()
self.interactive() def init_db(self):
from core import create_table
#创建表结构
create_table.create_table()
print(Colors('init database done..', 'green'))
#初始化admin管理员用户
if not self.session.query(User).filter(User.username=='admin').first():
md5 = hashlib.md5()
md5.update('admin'.encode('utf-8'))
md5_password = md5.hexdigest()
user_obj_admin = User(username='admin', password=md5_password, permission=0)
self.session.add_all([user_obj_admin, ])
self.session.commit()
print(Colors('初始化admin管理员用户完成!', 'green')) def interactive(self):
while True:
print("欢迎来到学员管理系统".center(45, '-'))
msg = """
1.管理员中心
2.讲师中心
3.学生中心
4.退出
"""
print(Colors(msg))
print("".center(50, '-'))
choice = input(Colors('请输入要进入的中心:', 'cyan')).strip()
if choice == '':
Center_admin(self.session)
elif choice == '':
Center_teacher(self.session)
elif choice == '':
Center_student(self.session)
elif choice == '':
exit('Bye')
else:
print(Colors('请输入正确的选项!', 'red'))

main.py

center_admin.py    管理员中心

 # Author:ton
# -*- coding: utf-8 -*-
from core.color import Colors
from core import commons
from core import auth
from core.create_table import Course_m2m_class, User, School, Course, Teacher, Class, Student, Lesson, C2c_m2m_lesson,\
Study_record, school_m2m_course, school_m2m_teacher, school_m2m_student, student_m2m_class
import hashlib class Center_admin(object):
"""管理员中心"""
def __init__(self, session):
self.session = session
self.handle() @auth.auth(permission=0)
def handle(self):
while True:
class_dic = {
'':Manage_school,
'':Manage_course,
'':Manage_teacher,
'':Manage_class,
'':Manage_student,
}
print("管理员中心".center(45, '-'))
msg = """
1.校区管理
2.课程管理
3.讲师管理
4.班级管理
5.学员管理
6.退出并返回系统主界面
"""
print(Colors(msg))
print('当前用户:%s' %self.user.username)
print("".center(50, '-'))
choice = input(Colors('请输入要执行的指令:', 'cyan')).strip()
if choice == '':break
if class_dic.get(choice):
class_dic.get(choice)(self.session, self.user)
else:
print(Colors('无效的指令!', 'red')) class Manage_school(object):
"""校区管理"""
def __init__(self, session, user):
self.session = session
self.user = user
self.handle() def handle(self):
while True:
print("校区管理界面".center(45, '-'))
msg = """
创建校区 create_school
查看校区 display_school
删除校区 delete_school
修改校区 update_school
添加/删除课程 add_course/del_course
查看各校区已有课程 display_sch_courses
添加/删除讲师 add_teacher/del_teacher
查看各校区已有讲师 display_sch_teachers
查看各校区已有班级 display_sch_classes
添加/删除学员 add_student/del_student
查看各校区已有学员 display_sch_students
返回管理员中心 b
"""
print(Colors(msg))
print('当前用户:%s' %self.user.username)
print("".center(50, '-'))
choice = input(Colors('请输入要执行的指令(b 返回):', 'cyan')).strip()
if choice == 'b':break
if hasattr(self, choice):
getattr(self, choice)()
else:
print(Colors('无效的指令!', 'red')) def create_school(self):
while True:
add_school_name = input(Colors('请输入要创建的校区名(b 返回):', 'cyan')).strip()
if add_school_name == 'b':break
if not add_school_name:continue
#判断该校区名是否存在
sch_obj = self.session.query(School).filter(School.name==add_school_name).first()
if sch_obj:
print(Colors('该校区名已存在!', 'red'))
continue
while True:
add_school_address = input(Colors('请输入校区地址(b 返回):', 'cyan')).strip()
if add_school_address == 'b':break
if not add_school_address:continue
#更新School表
sch_obj = School(name=add_school_name, address=add_school_address)
self.session.add_all([sch_obj, ])
self.session.commit()
print(Colors('创建校区成功!', 'green'))
break def display_school(self):
return commons.display_school(self.session) def delete_school(self):
while True:
#判断School表是否为空
if not self.display_school():break
del_school_name = input(Colors('请输入要删除的校区名(b 返回):', 'cyan')).strip()
if not del_school_name:continue
if del_school_name == 'b':break
#判断该校区名是否存在
sch_obj = self.session.query(School).filter(School.name==del_school_name).first()
if not sch_obj:
print(Colors('该校区名不存在!', 'red'))
continue
else:
#删除School表记录
self.session.delete(sch_obj)
self.session.commit()
print(Colors('删除校区成功!', 'green')) def update_school(self):
while True:
#判断School表是否为空
if not self.display_school():break
update_school_name = input(Colors('请输入要修改的校区名(b 返回):', 'cyan')).strip()
if not update_school_name:continue
if update_school_name == 'b':break
#判断该校区名是否存在
sch_obj = self.session.query(School).filter(School.name==update_school_name).first()
if not sch_obj:
print(Colors('该校区名不存在!', 'red'))
continue
else:
#修改School表记录
column_school = [{'name':'校区名'}, {'address':'地址'}, ]
res = commons.update_table(self.session, column_school, sch_obj)
if not res:return def add_course(self):
"""为某校区增设课程"""
sch_obj = commons.choice_school(self.session)
if not sch_obj:return
while True:
#查看所有课程并判断Course表是否为空
if not commons.display_course(self.session):return
#展示该校区已有课程信息
commons.display_sch_courses(sch_obj)
add_course_name = input(Colors('请输入要增设的课程名(b 返回):', 'cyan')).strip()
if not add_course_name:continue
if add_course_name == 'b':break
#判断该课程名是否存在
cou_obj = self.session.query(Course).filter(Course.name==add_course_name).first()
if not cou_obj:
print(Colors('该课程名不存在!', 'red'))
continue
#判断该校区是否已经有该课程
if cou_obj not in sch_obj.courses:
#更新school_m2m_course表
sch_obj.courses.append(cou_obj)
self.session.commit()
print(Colors('增设课程成功!', 'green'))
else:
print(Colors('该校区已经有该课程!', 'red')) def display_sch_courses(self):
"""查看各校区已有课程"""
sch_obj_li = self.session.query(School).all()
if sch_obj_li:
for sch_obj in sch_obj_li:
#展示某校区所有课程信息
commons.display_sch_courses(sch_obj)
else:
print(Colors('现在还没有校区,请先创建校区!', 'red')) def del_course(self):
sch_obj = commons.choice_school(self.session)
if not sch_obj:return
while True:
#展示该校区已有课程信息
res = commons.display_sch_courses(sch_obj)
if not res:return
del_course_name = input(Colors('请输入要删除的课程名(b 返回):', 'cyan')).strip()
if not del_course_name:continue
if del_course_name == 'b':break
#判断该校区是否存在该课程
cou_obj = self.session.query(Course).join(school_m2m_course).join(School).filter(School.name==sch_obj.name).filter(Course.name==del_course_name).first()
if not cou_obj:
print(Colors('该校区没有该课程!', 'red'))
continue
else:
#删除该校区的该课程
sch_obj.courses.remove(cou_obj)
self.session.commit()
print(Colors('删除课程[%s]成功!'%cou_obj.name, 'green')) def add_teacher(self):
sch_obj = commons.choice_school(self.session)
if not sch_obj:return
while True:
#查看所有讲师并判断Teacher表是否为空
if not commons.display_teacher(self.session):return
#展示该校区已有讲师信息
commons.display_sch_teachers(sch_obj)
add_tea_card_id = input(Colors('请输入要创建的讲师身份证号码(b 返回):', 'cyan')).strip()
if not add_tea_card_id:continue
if add_tea_card_id == 'b':break
#判断该讲师身份证号是否存在
tea_obj = self.session.query(Teacher).filter(Teacher.card_id==add_tea_card_id).first()
if not tea_obj:
print(Colors('该讲师身份证号不存在!', 'red'))
continue
#判断该校区是否已经有该讲师
if tea_obj not in sch_obj.teachers:
#更新school_m2m_teacher表
sch_obj.teachers.append(tea_obj)
self.session.commit()
print(Colors('添加讲师成功!', 'green'))
else:
print(Colors('该校区已经有该讲师!', 'red')) def display_sch_teachers(self):
"""查看各校区已有讲师"""
sch_obj_li = self.session.query(School).all()
if sch_obj_li:
for sch_obj in sch_obj_li:
#展示某校区所有讲师信息
commons.display_sch_teachers(sch_obj)
else:
print(Colors('现在还没有校区,请先创建校区!', 'red')) def del_teacher(self):
sch_obj = commons.choice_school(self.session)
if not sch_obj:return
while True:
#展示该校区已有讲师信息
res = commons.display_sch_teachers(sch_obj)
if not res:return
del_tea_card_id = input(Colors('请输入要删除的讲师身份证号码(b 返回):', 'cyan')).strip()
if not del_tea_card_id:continue
if del_tea_card_id == 'b':break
#判断该校区是否存在该讲师
tea_obj = self.session.query(Teacher).join(school_m2m_teacher).join(School).filter(School.name==sch_obj.name).filter(Teacher.card_id==del_tea_card_id).first()
if not tea_obj:
print(Colors('校区[%s]没有该讲师身份证号码[%s]!'%(sch_obj.name, del_tea_card_id), 'red'))
continue
else:
#删除该校区的该讲师
sch_obj.teachers.remove(tea_obj)
self.session.commit()
print(Colors('删除讲师[%s]成功!'%tea_obj.name, 'green')) def display_sch_classes(self):
"""查看各校区已有班级"""
sch_obj_li = self.session.query(School).all()
if sch_obj_li:
for sch_obj in sch_obj_li:
#展示某校区所有班级信息
commons.display_sch_classes(sch_obj)
else:
print(Colors('现在还没有校区,请先创建校区!', 'red')) def add_student(self):
sch_obj = commons.choice_school(self.session)
if not sch_obj:return
while True:
#查看所有学员并判断Student表是否为空
if not commons.display_student(self.session):return
#展示该校区已有学员信息
commons.display_sch_students(sch_obj)
add_stu_qq = input(Colors('请输入要添加的学员QQ(b 返回):', 'cyan')).strip()
if not add_stu_qq:continue
if add_stu_qq == 'b':break
#判断该学员QQ是否存在
stu_obj = self.session.query(Student).filter(Student.qq==add_stu_qq).first()
if not stu_obj:
print(Colors('该学员QQ[%s]不存在!'%add_stu_qq, 'red'))
continue
#判断该校区是否已经有该学员
if stu_obj not in sch_obj.students:
#更新school_m2m_student表
sch_obj.students.append(stu_obj)
self.session.commit()
print(Colors('添加学员[%s]成功!'%stu_obj.name, 'green'))
else:
print(Colors('校区[%s]已经有学员[%s]!'%(sch_obj.name, stu_obj.name), 'red')) def display_sch_students(self):
"""查看各校区已有学员"""
sch_obj_li = self.session.query(School).all()
if sch_obj_li:
for sch_obj in sch_obj_li:
#展示某校区所有学员信息
commons.display_sch_students(sch_obj)
else:
print(Colors('现在还没有校区,请先创建校区!', 'red')) def del_student(self):
sch_obj = commons.choice_school(self.session)
if not sch_obj:return
while True:
#展示该校区已有学员信息
res = commons.display_sch_students(sch_obj)
if not res:return
del_stu_qq = input(Colors('请输入要删除的学员QQ(b 返回):', 'cyan')).strip()
if not del_stu_qq:continue
if del_stu_qq == 'b':break
#判断该校区是否存在该学员
stu_obj = self.session.query(Student).join(school_m2m_student).join(School).filter(School.name==sch_obj.name).filter(Student.qq==del_stu_qq).first()
if not stu_obj:
print(Colors('校区[%s]不存在该学员QQ[%s]!'%(sch_obj.name, del_stu_qq), 'red'))
continue
else:
#删除该校区的该学员
sch_obj.students.remove(stu_obj)
self.session.commit()
print(Colors('删除学员[%s]成功!'%stu_obj.name, 'green')) class Manage_course(object):
def __init__(self, session, user):
self.session = session
self.user = user
self.handle() def handle(self):
while True:
print("课程管理界面".center(45, '-'))
msg = """
创建课程 create_course
查看课程 display_course
删除课程 delete_course
修改课程 update_course
返回管理员中心 b
"""
print(Colors(msg))
print('当前用户:%s' %self.user.username)
print("".center(50, '-'))
choice = input(Colors('请输入要执行的指令(b 返回):', 'cyan')).strip()
if choice == 'b':break
if hasattr(self, choice):
getattr(self, choice)()
else:
print(Colors('无效的指令!', 'red')) def create_course(self):
while True:
add_course_name = input(Colors('请输入要创建的课程名(b 返回):', 'cyan')).strip()
if add_course_name == 'b':break
if not add_course_name:continue
cou_obj = self.session.query(Course).filter(Course.name==add_course_name).first()
#判断该课程名是否存在
if cou_obj:
print(Colors('该课程名已存在!', 'red'))
continue
while True:
add_course_price = input(Colors('请输入课程价格(b 返回):', 'cyan')).strip()
if add_course_price == 'b':break
if not add_course_price:continue
if not add_course_price.isdigit():
print(Colors('无效的价格,请输入数字!', 'red'))
continue
#更新Course表
cou_obj = Course(name=add_course_name, price=add_course_price)
self.session.add_all([cou_obj, ])
self.session.commit()
print(Colors('创建课程成功!', 'green'))
break def display_course(self):
return commons.display_course(self.session) def delete_course(self):
while True:
#判断Course表是否为空
if not self.display_course():break
del_course_name = input(Colors('请输入要删除的课程名(b 返回):', 'cyan')).strip()
if not del_course_name:continue
if del_course_name == 'b':break
#判断该课程名是否存在
cou_obj = self.session.query(Course).filter(Course.name==del_course_name).first()
if not cou_obj:
print(Colors('该课程名不存在!', 'red'))
continue
else:
#删除Course表记录
self.session.delete(cou_obj)
self.session.commit()
print(Colors('删除课程成功!', 'green')) def update_course(self):
while True:
#判断Course表是否为空
if not self.display_course():break
update_course_name = input(Colors('请输入要修改的课程名(b 返回):', 'cyan')).strip()
if not update_course_name:continue
if update_course_name == 'b':break
#判断该课程名是否存在
cou_obj = self.session.query(Course).filter(Course.name==update_course_name).first()
if not cou_obj:
print(Colors('该课程名不存在!', 'red'))
continue
else:
#修改Course表记录
column_course = [{'name':'课程名'}, {'price':'价格'}, ]
res = commons.update_table(self.session, column_course, cou_obj)
if not res:return class Manage_teacher(object):
def __init__(self, session, user):
self.session = session
self.user = user
self.handle() def handle(self):
while True:
print("讲师管理界面".center(45, '-'))
msg = """
创建讲师 create_teacher
查看讲师 display_teacher
删除讲师 delete_teacher
修改讲师 update_teacher
添加/删除任课的班级课程 add_c2c/del_c2c
查看各讲师任课的班级课程 display_tea_c2c
返回管理员中心 b
"""
print(Colors(msg))
print('当前用户:%s' %self.user.username)
print("".center(50, '-'))
choice = input(Colors('请输入要执行的指令(b 返回):', 'cyan')).strip()
if choice == 'b':break
if hasattr(self, choice):
getattr(self, choice)()
else:
print(Colors('无效的指令!', 'red')) def create_teacher(self):
while True:
add_teacher_card_id = input(Colors('请输入要创建的讲师身份证号码(b 返回):', 'cyan')).strip()
if add_teacher_card_id == 'b':break
if not add_teacher_card_id:continue
if not add_teacher_card_id[:-1].isdigit() or not len(add_teacher_card_id) == 18:
print(Colors('无效的身份证号码,请输入正确格式身份证号码!', 'red'))
continue
tea_obj = self.session.query(Teacher).filter(Teacher.card_id==add_teacher_card_id).first()
if tea_obj:
print(Colors('该身份证号码已存在!', 'red'))
continue
while True:
add_teacher_name = input(Colors('请输入要创建的讲师名(b 返回):', 'cyan')).strip()
if add_teacher_name == 'b':break
if not add_teacher_name:continue
user_obj_id = commons.create_user(self.session, 1)
if not user_obj_id:continue
#更新teacher表
tea_obj = Teacher(name=add_teacher_name, card_id=add_teacher_card_id, user_id=user_obj_id)
self.session.add_all([tea_obj, ])
self.session.commit()
print(Colors('创建讲师成功!', 'green'))
break def display_teacher(self):
return commons.display_teacher(self.session) def delete_teacher(self):
while True:
print("开始删除讲师".center(45, '-'))
#判断Teacher表是否为空
if not self.display_teacher():break
del_tea_card_id = input(Colors('请输入要删除的讲师的身份证号码(b 返回):', 'cyan')).strip()
if not del_tea_card_id:continue
if del_tea_card_id == 'b':break
#判断该讲师身份证号码是否存在
tea_obj = self.session.query(Teacher).filter(Teacher.card_id==del_tea_card_id).first()
if not tea_obj:
print(Colors('该讲师身份证号码不存在!', 'red'))
continue
else:
#删除Teacher表记录
self.session.delete(tea_obj)
#删除User表
self.session.delete(tea_obj.user)
self.session.commit()
print(Colors('删除讲师成功!同时该讲师账号已被注销!', 'green')) def update_teacher(self):
while True:
#判断Teacher表是否为空
if not self.display_teacher():break
update_tea_card_id = input(Colors('请输入要修改的讲师的身份证号码(b 返回):', 'cyan')).strip()
if not update_tea_card_id:continue
if update_tea_card_id == 'b':break
#判断该讲师身份证号码是否存在
tea_obj = self.session.query(Teacher).filter(Teacher.card_id==update_tea_card_id).first()
if not tea_obj:
print(Colors('该讲师身份证号码不存在!', 'red'))
continue
else:
#修改Teacher表记录
column_teacher = [{'name':'讲师名'}, {'card_id':'身份证号码'},]
res = commons.update_table(self.session, column_teacher, tea_obj)
if not res:return def add_c2c(self):
tea_obj = commons.choice_teacher(self.session)
if not tea_obj:return
while True:
#选择该讲师任教的校区
if not commons.display_tea_schools(tea_obj):return
sch_name = input(Colors('请选择任教的校区名(b 结束操作):', 'cyan')).strip()
if sch_name == 'b':return
if not sch_name:continue
sch_obj = self.session.query(School).join(school_m2m_teacher).join(Teacher).\
filter(Teacher.name==tea_obj.name).filter(School.name==sch_name).first()
if not sch_obj:
print(Colors('讲师[%s]没有任教该校区[%s]!'%(tea_obj.name, sch_name), 'red'))
continue
while True:
#判断该校区是否有班级,有则继续选择讲师任课的班级
if not commons.display_sch_classes(sch_obj):break
commons.display_tea_sch_c2c(tea_obj, sch_obj)
cls_name = input(Colors('请输入讲师[%s]任课的班级名(b 返回):'%tea_obj.name, 'cyan')).strip()
if cls_name == 'b':break
if not cls_name:continue
cls_obj = self.session.query(Class).join(School).filter(School.name==sch_name).filter(Class.name==cls_name).first()
if not cls_obj:
print(Colors('校区[%s]不存在班级[%s]!'%(sch_name, cls_name), 'red'))
continue
while True:
#判断该班级是否有关联课程,有则继续选择讲师任课的课程
if not commons.display_cls_courses(cls_obj):break
commons.display_tea_cls_courses(self.session, tea_obj, cls_obj)
cou_name = input(Colors('请输入讲师[%s]任课的课程名(b 返回):'%tea_obj.name, 'cyan')).strip()
if cou_name == 'b':break
if not cou_name:continue
cou_obj = self.session.query(Course).join(Course_m2m_class).filter(Course_m2m_class.class_id==cls_obj.id).filter(Course.name==cou_name).first()
if not cou_obj:
print(Colors('班级[%s]没有添加课程[%s]!'%(sch_name, cls_name), 'red'))
continue
add_c2c_obj = self.session.query(Course_m2m_class).filter(Course_m2m_class.course_id==cou_obj.id).\
filter(Course_m2m_class.class_id==cls_obj.id).filter(Course_m2m_class.teacher_id.isnot(None)).first()
if add_c2c_obj:
used_tea_obj = self.session.query(Teacher).join(Course_m2m_class).filter(Course_m2m_class.course_id==cou_obj.id)\
.filter(Course_m2m_class.class_id==cls_obj.id).first()
print(Colors('该班级-课程[%s]已有任课讲师%s!请先删除该班级-课程的任课讲师'%(add_c2c_obj, used_tea_obj.name), 'red'))
continue
add_c2c_obj = self.session.query(Course_m2m_class).filter(Course_m2m_class.course_id==cou_obj.id).\
filter(Course_m2m_class.class_id==cls_obj.id).first()
#更新course_m2m_class表
print(add_c2c_obj)
tea_obj.course_m2m_classes.append(add_c2c_obj)
self.session.commit()
print(Colors('添加任课的班级-课程[%s]成功!'%add_c2c_obj, 'green')) def del_c2c(self):
while True:
tea_obj = commons.choice_teacher(self.session)
if not tea_obj:return
if not tea_obj.course_m2m_classes:
print(Colors('讲师[%s]还没有添加任课的班级-课程'%tea_obj.name, 'red'))
continue
while True:
#选择该讲师任教的校区
if not commons.display_tea_schools(tea_obj):return
sch_name = input(Colors('请选择任教的校区名(b 结束操作):', 'cyan')).strip()
if sch_name == 'b':return
if not sch_name:continue
sch_obj = self.session.query(School).join(school_m2m_teacher).join(Teacher).\
filter(Teacher.id==tea_obj.id).filter(School.name==sch_name).first()
if not sch_obj:
print(Colors('讲师[%s]没有任教该校区[%s]!'%(tea_obj.name, sch_name), 'red'))
continue
while True:
#判断该讲师是否有任课的班级,有则继续选择删除任课的班级
if not commons.display_tea_sch_c2c(tea_obj, sch_obj):break
cls_name = input(Colors('请输入要删除任课的班级名(b 返回):', 'cyan')).strip()
if cls_name == 'b':break
if not cls_name:continue
while True:
cou_name = input(Colors('请输入要删除任课的课程名(b 返回):', 'cyan')).strip()
if cou_name == 'b':break
if not cou_name:continue
c2c_obj = self.session.query(Course_m2m_class).join(Course).join(Class).\
filter(Class.school_id==sch_obj.id).filter(Class.name==cls_name).filter(Course.name==cou_name).first()
if c2c_obj not in tea_obj.course_m2m_classes:
print(Colors('讲师[%s]没有任课班级-课程[%s %s]'%(tea_obj.name, cls_name, cou_name), 'red'))
continue
#更新course_m2m_class表
tea_obj.course_m2m_classes.remove(c2c_obj)
self.session.commit()
print(Colors('删除任课的班级-课程[%s]成功!'%c2c_obj, 'green')) def display_tea_c2c(self):
"""查看各讲师任教的各校区任课的班级课程"""
tea_obj_li = self.session.query(Teacher).all()
if tea_obj_li:
for tea_obj in tea_obj_li:
for sch_obj in tea_obj.schools:
commons.display_tea_sch_c2c(tea_obj, sch_obj)
else:
print(Colors('现在还没有讲师,请先创建讲师!', 'red')) class Manage_student(object):
def __init__(self, session, user):
self.session = session
self.user = user
self.handle() def handle(self):
while True:
print("学员管理界面".center(45, '-'))
msg = """
创建学员 create_student
查看学员 display_student
删除学员 delete_student
修改学员 update_student
返回管理员中心 b
"""
print(Colors(msg))
print('当前用户:%s' %self.user.username)
print("".center(50, '-'))
choice = input(Colors('请输入要执行的指令(b 返回):', 'cyan')).strip()
if choice == 'b':break
if hasattr(self, choice):
getattr(self, choice)()
else:
print(Colors('无效的指令!', 'red')) def create_student(self):
while True:
add_stu_qq = input(Colors('请输入学生qq(b 返回):', 'cyan')).strip()
if add_stu_qq == 'b':return
if not add_stu_qq:continue
if not add_stu_qq.isdigit() or not len(add_stu_qq) > 3:
print(Colors('无效的qq号,请输入正确格式qq号!', 'red'))
continue
stu_obj = self.session.query(Student).filter(Student.qq==add_stu_qq).first()
if stu_obj:
print(Colors('该qq号已存在!', 'red'))
continue
while True:
add_stu_name = input(Colors('请输入要创建的学生名(b 返回):', 'cyan')).strip()
if add_stu_name == 'b':return
if not add_stu_name:continue
user_obj_id = commons.create_user(self.session, 2)
if not user_obj_id:continue
#更新student表
stu_obj = Student(name=add_stu_name, qq=add_stu_qq, user_id=user_obj_id)
self.session.add_all([stu_obj, ])
self.session.commit()
print(Colors('创建学生成功!', 'green'))
break def display_student(self):
return commons.display_student(self.session) def delete_student(self):
while True:
#判断Student表是否为空
if not self.display_student():break
del_stu_qq = input(Colors('请输入要删除的学生的QQ号码(b 返回):', 'cyan')).strip()
if not del_stu_qq:continue
if del_stu_qq == 'b':break
#判断该学生qq号码是否存在
stu_obj = self.session.query(Student).filter(Student.qq==del_stu_qq).first()
if not stu_obj:
print(Colors('该学生qq号码不存在!', 'red'))
continue
else:
#删除Student表记录
self.session.delete(stu_obj)
#删除User表
self.session.delete(stu_obj.user)
self.session.commit()
print(Colors('删除学生成功!同时该学生账号已被注销!', 'green')) def update_student(self):
while True:
#判断Student表是否为空
if not self.display_student():break
update_stu_qq = input(Colors('请输入要修改的学生的QQ号码(b 返回):', 'cyan')).strip()
if not update_stu_qq:continue
if update_stu_qq == 'b':break
#判断该学生qq号码是否存在
stu_obj = self.session.query(Student).filter(Student.qq==update_stu_qq).first()
if not stu_obj:
print(Colors('该学生qq号码不存在!', 'red'))
continue
else:
#修改Student表记录
column_student = [{'name':'学生名'}, {'qq':'QQ号码'},]
res = commons.update_table(self.session, column_student, stu_obj)
if not res:return class Manage_class(object):
def __init__(self, session, user):
self.session = session
self.user = user
self.handle() def handle(self):
while True:
print("课程管理界面".center(45, '-'))
msg = """
创建班级 create_class
查看班级 display_class
删除班级 delete_class
修改班级 update_class
添加/删除课程 add_course/del_course
添加/删除学员 add_student/del_student
查看各班级的课程 display_cls_course
查看各班级的学员 display_cls_student
返回管理员中心 b
"""
print(Colors(msg))
print('当前用户:%s' %self.user.username)
print("".center(50, '-'))
choice = input(Colors('请输入要执行的指令(b 返回):', 'cyan')).strip()
if choice == 'b':break
if hasattr(self, choice):
getattr(self, choice)()
else:
print(Colors('无效的指令!', 'red')) def create_class(self):
sch_obj = commons.choice_school(self.session)
if not sch_obj:return
while True:
add_class_name = input(Colors('请输入要创建的班级名(b 返回):', 'cyan')).strip()
if add_class_name == 'b':return
if not add_class_name:continue
#判断该校区是否已经存在该班级
cls_obj = self.session.query(Class).join(School).filter(School.name==sch_obj.name).filter(Class.name==add_class_name).first()
if cls_obj:
print(Colors('该校区已有该班级!', 'red'))
continue
#更新Class表
cls_obj = Class(name=add_class_name, school_id=sch_obj.id)
self.session.add_all([cls_obj, ])
self.session.commit()
print(Colors('创建班级成功!', 'green')) def display_class(self):
return commons.display_class(self.session) def delete_class(self):
while True:
#判断Class表是否为空
if not self.display_class():break
del_class_name = input(Colors('请输入要删除的班级名(b 返回):', 'cyan')).strip()
if del_class_name == 'b':return
sch_name = input(Colors('请输入删除的班级所属校区名(b 返回):', 'cyan')).strip()
if sch_name == 'b':return
#判断该校区是否存在该班级
cls_obj = self.session.query(Class).join(School).filter(Class.name==del_class_name).filter(School.name==sch_name).first()
if not cls_obj:
print(Colors('该校区[%s]下不存在班级[%s]!'%(sch_name, del_class_name), 'red'))
continue
#删除Class表记录
self.session.delete(cls_obj)
self.session.commit()
print(Colors('删除班级成功!', 'green')) def update_class(self):
while True:
#判断Class表是否为空
if not self.display_class():break
update_class_name = input(Colors('请输入要修改的班级名(b 返回):', 'cyan')).strip()
if update_class_name == 'b':return
sch_name = input(Colors('请输入要修改班级所属校区名(b 返回):', 'cyan')).strip()
if sch_name == 'b':return
#判断该校区是否存在该班级
cls_obj = self.session.query(Class).join(School).filter(Class.name==update_class_name).filter(School.name==sch_name).first()
if not cls_obj:
print(Colors('该校区[%s]下不存在班级[%s]!'%(sch_name, update_class_name), 'red'))
continue
#修改Class表记录
column_class = [{'name':'班级名'},]
res = commons.update_table(self.session, column_class, cls_obj)
if not res:return def add_course(self):
"""为某班级添加课程"""
sch_obj, cls_obj = commons.choice_class(self.session)
if not cls_obj:return
while True:
#展示所选校区所有课程信息,并判断所选校区已有课程是否为空
if not commons.display_sch_courses(sch_obj):return
#展示该班级已有课程信息
commons.display_cls_courses(cls_obj)
add_cou_name = input(Colors('请输入要添加的课程名(b 返回):', 'cyan')).strip()
if not add_cou_name:continue
if add_cou_name == 'b':break
#判断所选校区是否存在该课程名
cou_obj = self.session.query(Course).join(school_m2m_course).join(School).filter(School.id==sch_obj.id).filter(Course.name==add_cou_name).first()
if not cou_obj:
print(Colors('所选校区[%s]没有该课程[%s]!'%(sch_obj.name, add_cou_name), 'red'))
continue
#判断该班级是否已经有该课程
if cou_obj not in cls_obj.courses:
#更新course_m2m_class表
cls_obj.courses.append(cou_obj)
self.session.commit()
print(Colors('班级[%s]添加课程[%s]成功!'%(cls_obj.name, cou_obj.name), 'green'))
else:
print(Colors('班级[%s]已经有该课程[%s]!'%(cls_obj.name, cou_obj.name), 'red')) def del_course(self):
sch_obj, cls_obj = commons.choice_class(self.session)
if not cls_obj:return
while True:
#展示该班级已有课程信息,没有则退出
if not commons.display_cls_courses(cls_obj):return
del_cou_name = input(Colors('请输入要删除的课程名(b 返回):', 'cyan')).strip()
if not del_cou_name:continue
if del_cou_name == 'b':break
#判断该班级是否存在该课程
cou_obj = self.session.query(Course).join(Course_m2m_class).join(Class).filter(Class.name==cls_obj.name).filter(Course.name==del_cou_name).first()
if not cou_obj:
print(Colors('班级[%s]没有该课程[%s]!'%(cls_obj.name, del_cou_name), 'red'))
continue
else:
#删除该班级的该课程
cls_obj.courses.remove(cou_obj)
self.session.commit()
print(Colors('班级[%s]删除课程[%s]成功!'%(cls_obj.name, cou_obj.name), 'green')) def display_cls_course(self):
"""查看各校区下各班级已有课程"""
sch_obj_li = self.session.query(School).all()
if sch_obj_li:
for sch_obj in sch_obj_li:
print('校区[%s]:'%sch_obj.name)
if sch_obj.classes:
for cls_obj in sch_obj.classes:
#展示某班级所有课程信息
commons.display_cls_courses(cls_obj)
else:
print(Colors('现在还没有校区,请先创建校区!', 'red'))
else:
print(Colors('现在还没有校区,请先创建校区!', 'red')) def add_student(self):
"""为某班级添加学员"""
sch_obj, cls_obj = commons.choice_class(self.session)
if not cls_obj:return
while True:
#展示所选校区所有学员信息,并判断所选校区已有学员是否为空
if not commons.display_sch_students(sch_obj):return
#展示该班级已有学员信息
commons.display_cls_students(cls_obj)
add_stu_qq = input(Colors('请输入要添加的学员QQ(b 返回):', 'cyan')).strip()
if not add_stu_qq:continue
if add_stu_qq == 'b':break
#判断所选校区是否存在该学员QQ
stu_obj = self.session.query(Student).join(school_m2m_student).join(School).filter(School.id==sch_obj.id).filter(Student.qq==add_stu_qq).first()
if not stu_obj:
print(Colors('所选校区[%s]没有该学员QQ[%s]!'%(sch_obj.name, add_stu_qq), 'red'))
continue
#判断该班级是否已经有该学员
if stu_obj not in cls_obj.students:
#更新student_m2m_class表
cls_obj.students.append(stu_obj)
self.session.commit()
print(Colors('班级[%s]添加学员[%s]成功!'%(cls_obj.name, stu_obj.name), 'green'))
else:
print(Colors('班级[%s]已经有该学员[%s]!'%(cls_obj.name, stu_obj.name), 'red')) def del_student(self):
sch_obj, cls_obj = commons.choice_class(self.session)
if not cls_obj:return
while True:
#展示该班级已有学员信息,没有则退出
if not commons.display_cls_students(cls_obj):return
del_stu_qq = input(Colors('请输入要删除的学员QQ(b 返回):', 'cyan')).strip()
if not del_stu_qq:continue
if del_stu_qq == 'b':break
#判断该班级是否存在该学员
stu_obj = self.session.query(Student).join(student_m2m_class).filter(student_m2m_class.class_id==cls_obj.id).filter(Student.qq==del_stu_qq).first()
if not stu_obj:
print(Colors('班级[%s]没有该学员QQ[%s]!'%(cls_obj.name, del_stu_qq), 'red'))
continue
else:
#删除该班级的该学员
cls_obj.students.remove(stu_obj)
self.session.commit()
print(Colors('班级[%s]删除学员[%s]成功!'%(cls_obj.name, stu_obj.name), 'green')) def display_cls_student(self):
"""查看各校区下各班级已有学员"""
sch_obj_li = self.session.query(School).all()
if sch_obj_li:
for sch_obj in sch_obj_li:
print('校区[%s]:'%sch_obj.name)
if sch_obj.classes:
for cls_obj in sch_obj.classes:
#展示某班级所有学员信息
commons.display_cls_students(cls_obj)
else:
print(Colors('现在还没有校区,请先创建校区!', 'red'))
else:
print(Colors('现在还没有校区,请先创建校区!', 'red')) def display_course(self):
"""展示该校区所有课程信息"""
if not len(self.school.courses) == 0:
print('该校区已有课程信息如下:')
column_course = [{'name':'课程名'}, {'price':'价格'}, ]
fmt_cou_table = commons.fmt_table(column_course, self.school.courses)
print(fmt_cou_table)
return True
else:
print(Colors('该校区还没有课程,请先为该校区增设课程!', 'red')) def delete_course(self):
while True:
#判断该校区的课程是否为空
if not self.display_course():break
del_course_name = input(Colors('请输入要删除的课程名(b 返回):', 'cyan')).strip()
if not del_course_name:continue
if del_course_name == 'b':break
#判断该校区是否存在该课程
cou_obj = self.session.query(Course).join(school_m2m_course).join(School).filter(School.name==self.school.name).filter(Course.name==del_course_name).first()
if not cou_obj:
print(Colors('该校区没有该课程!', 'red'))
continue
else:
#删除该校区的该课程
self.school.courses.remove(cou_obj)
self.session.commit()
print(Colors('该校区删除课程成功!', 'green')) def display_teacher(self):
"""展示该校区所有讲师信息"""
if self.school.teachers:
column_teacher = [{'name':'讲师名'}, {'card_id':'身份证号码'}, {'user':'账号信息'}]
fmt_tea_table = commons.fmt_table(column_teacher, self.school.teachers)
print(Colors('该校区已有讲师信息如下:'))
print(fmt_tea_table)
return True
else:
print(Colors('该校区还没有讲师,请先为该校区创建讲师!', 'red')) def create_teacher(self):
add_teacher_name = None
add_teacher_card_id = None
add_username = None
add_password = None
sch_obj = None
cou_m2m_cls_li = []
while True:
add_teacher_name = input(Colors('请输入要创建的讲师名(b 返回):', 'cyan')).strip()
if add_teacher_name == 'b':return
if not add_teacher_name:continue
break
while True:
add_teacher_card_id = input(Colors('请输入讲师身份证号码(b 返回):', 'cyan')).strip()
if add_teacher_card_id == 'b':return
if not add_teacher_card_id:continue
if not add_teacher_card_id[:-1].isdigit() or not len(add_teacher_card_id) == 18:
print(Colors('无效的身份证号码,请输入正确格式身份证号码!', 'red'))
continue
tea_obj = self.session.query(Teacher).filter(Teacher.card_id==add_teacher_card_id).first()
if tea_obj:
print(Colors('该身份证号码已存在!', 'red'))
continue
break
while True:
add_username = input(Colors('请输入该讲师用于登陆的用户名(b 返回):', 'cyan')).strip()
if add_username == 'b':return
if not add_username:continue
user_obj = self.session.query(User).filter(User.username==add_username).first()
if user_obj:
print(Colors('该用户名已存在!', 'red'))
continue
break
while True:
add_password = input(Colors('请输入该讲师用于登陆的密码(b 返回):', 'cyan')).strip()
if add_password == 'b':return
if not add_password:continue
md5 = hashlib.md5()
md5.update(add_password.encode('utf-8'))
add_password = md5.hexdigest()
break
#选择该讲师归属的校区
while while_flag:
res = self.display_school()
if not res:
while_flag = False
break
sch_name = input(Colors('请输入该讲师归属的校区名(b 返回):', 'cyan')).strip()
if sch_name == 'b':while_flag = False
if not sch_name:continue
sch_obj = self.session.query(School).filter(School.name==sch_name).first()
if not sch_obj:
print(Colors('该校区名不存在!', 'red'))
continue
break
#判断该校区是否有班级,有则继续选择讲师任课的班级
if while_flag:
if sch_obj.classes:
choice_cls_obj_li = []
choice_cls_name_li = []
while while_flag:
self.display_class(sch_name=sch_name)
print('已选择班级:%s'%choice_cls_name_li)
cls_name = input(Colors('请输入该讲师任课的班级名(b 返回|end 结束选择班级):', 'cyan')).strip()
if cls_name == 'b':
while_flag = False
break
if cls_name == 'end':break
if not cls_name:continue
cls_obj = self.session.query(Class).join(School).filter(School.name==sch_name).filter(Class.name==cls_name).first()
if not cls_obj:
print(Colors('校区[%s]不存在[%s]该班级!'%(sch_name, cls_name), 'red'))
continue
if cls_obj in choice_cls_obj_li:
print(Colors('已经选择过该班级', 'red'))
continue
#判断该班级是否有关联课程,有则继续选择讲师任课的课程
if cls_obj.courses:
choice_cou_obj_li = []
choice_cou_name_li = []
while while_flag:
print('班级[%s]的所有课程:'%cls_obj.name)
column_course = [{'name':'课程名'},]
fmt_cou_table = commons.fmt_table(column_course, cls_obj.courses)
print(fmt_cou_table)
print('已选择课程:%s'%choice_cou_name_li)
cou_name = input(Colors('请输入该讲师任课的课程名(b 返回|end 结束选择课程):', 'cyan')).strip()
if cou_name == 'b':
while_flag = False
break
if cou_name == 'end':
if len(choice_cou_obj_li) != 0:
for cou_obj in choice_cou_obj_li:
cou_m2m_cls_li.append(self.session.query(Course_m2m_class).filter(Course_m2m_class.course_id==cou_obj.id).filter(Course_m2m_class.class_id==cls_obj.id).first())
choice_cls_obj_li.append(cls_obj)
choice_cls_name_li.append(cls_name)
break
if not cou_name:continue
cou_obj = self.session.query(Course).join(Course_m2m_class).join(Class).filter(Class.name==cls_obj.name).filter(Course.name==cou_name).first()
if not cou_obj:
print(Colors('班级[%s]不存在[%s]该课程!'%(cls_obj.name, cou_name), 'red'))
continue
if cou_obj in choice_cou_obj_li:
print(Colors('已经选择过该课程', 'red'))
continue
choice_cou_obj_li.append(cou_obj)
choice_cou_name_li.append(cou_name)
else:
print(Colors('该班级还没有关联课程!', 'red'))
else:
print(Colors('该校区还没有设立班级,已经跳过为该讲师选择任课班级和课程的步骤!', 'red'))
if while_flag:
#至此,Teacher/course_m2m_class/User/school_m2m_teacher表所有外键关联确认完毕
#更新User表
user_obj = User(username=add_username, password=add_password, permission=1)
self.session.add_all([user_obj, ])
self.session.commit()
#更新teacher表
user_obj = self.session.query(User).filter(User.username==add_username).first()
tea_obj = Teacher(name=add_teacher_name, card_id=add_teacher_card_id, user_id=user_obj.id)
self.session.add_all([tea_obj, ])
#更新school_m2m_teacher表
tea_obj.schools = [sch_obj,]
self.session.commit()
#判断该校区是否有班级且选择的班级是否有课程,没有则不更新Course_m2m_class表
if not len(cou_m2m_cls_li) == 0:
#更新course_m2m_class表
tea_obj.course_m2m_classes = cou_m2m_cls_li
self.session.commit()
print(Colors('创建讲师成功!', 'green')) def create_student(self):
while_flag = True
add_stu_name = None
add_stu_qq = None
add_username = None
add_password = None
sch_obj = None
cls_obj = None
while while_flag:
add_stu_name = input(Colors('请输入要创建的学生名(b 返回):', 'cyan')).strip()
if add_stu_name == 'b':while_flag = False
if not add_stu_name:continue
break
while while_flag:
add_stu_qq = input(Colors('请输入学生qq(b 返回):', 'cyan')).strip()
if add_stu_qq == 'b':while_flag = False
if not add_stu_qq:continue
if not add_stu_qq.isdigit() or not len(add_stu_qq) > 3:
print(Colors('无效的qq号,请输入正确格式qq号!', 'red'))
continue
stu_obj = self.session.query(Student).filter(Student.qq==add_stu_qq).first()
if stu_obj:
print(Colors('该qq号已存在!', 'red'))
continue
else:
break
while while_flag:
add_username = input(Colors('请输入该学生用于登陆的用户名(b 返回):', 'cyan')).strip()
if add_username == 'b':while_flag = False
if not add_username:continue
#判断该用户名是否存在
user_obj = self.session.query(User).filter(User.username==add_username).first()
if user_obj:
print(Colors('该用户名已存在!', 'red'))
continue
break
while while_flag:
add_password = input(Colors('请输入该学生用于登陆的密码(b 返回):', 'cyan')).strip()
if add_password == 'b':while_flag = False
if not add_password:continue
md5 = hashlib.md5()
md5.update(add_password.encode('utf-8'))
add_password = md5.hexdigest()
break
#选择该学生注册的校区
while while_flag:
res = self.display_school()
if not res:while_flag = False
sch_name = input(Colors('请输入该学生注册的校区名(b 返回):', 'cyan')).strip()
if sch_name == 'b':while_flag = False
if not sch_name:continue
sch_obj = self.session.query(School).filter(School.name==sch_name).first()
if not sch_obj:
print(Colors('该校区名不存在!', 'red'))
continue
break
#判断该校区是否有班级,有则继续选择学生报名的班级,没有则退出
if sch_obj.classes:
while while_flag:
self.display_class(sch_name=sch_name)
cls_name = input(Colors('请输入学生报名的班级名(b 返回):', 'cyan')).strip()
if cls_name == 'b':while_flag = False
if not cls_name:continue
cls_obj = self.session.query(Class).join(School).filter(School.name==sch_name).filter(Class.name==cls_name).first()
if not cls_obj:
print(Colors('校区[%s]不存在[%s]该班级!'%(sch_name, cls_name), 'red'))
continue
break
else:
print(Colors('该校区还没有设立班级,请先为该校区增设班级!', 'red'))
return
#至此,Student/User/school_m2m_student/student_m2m_class表所有外键关联确认完毕
#更新User表
user_obj = User(username=add_username, password=add_password, permission=2)
self.session.add_all([user_obj, ])
self.session.commit()
#更新student表
user_obj = self.session.query(User).filter(User.username==add_username).first()
stu_obj = Student(name=add_stu_name, qq=add_stu_qq, user_id=user_obj.id)
self.session.add_all([stu_obj, ])
#更新school_m2m_student表
stu_obj.schools = [sch_obj,]
self.session.commit()
#判断该校区是否有班级,没有则不更新student_m2m_class表
if cls_obj:
#更新course_m2m_class表
stu_obj.classes = [cls_obj,]
self.session.commit()
print(Colors('创建学生成功!', 'green')) def create_class(self):
sch_obj = None #要创建班级的校区
add_class_name = None #新增班级名
tea_obj = None #该班级的班主任
choice_cou_obj_li = None #该班级关联的课程列表
while True:
#判断School表是否为空
res = Manage_school(self.session, self.user).display_school()
if not res:break
choice_school_name = input(Colors('请输入要创建班级的校区名(b 返回):', 'cyan')).strip()
if not choice_school_name:continue
if choice_school_name == 'b':return
#判断该校区名是否存在
sch_obj = self.session.query(School).filter(School.name==choice_school_name).first()
if not sch_obj:
print(Colors('该校区名不存在!', 'red'))
continue
break
while True:
add_class_name = input(Colors('请输入要创建的班级名(b 返回):', 'cyan')).strip()
if add_class_name == 'b':return
if not add_class_name:continue
#判断该校区是否已经存在该班级
cls_obj = self.session.query(Class).join(School).filter(School.name==sch_obj.name).filter(Class.name==add_class_name).first()
if cls_obj:
print(Colors('该校区已有该班级!', 'red'))
continue
break
#判断该校区是否存在讲师
if sch_obj.teachers:
while True:
Manage_teacher.display_teacher()
add_class_tea_card_id = input(Colors('请输入该班级的班主任(讲师)身份证号(b 返回):', 'cyan')).strip()
if add_class_tea_card_id == 'b':
while_flag = False
break
if not add_class_tea_card_id:continue
tea_obj = self.session.query(Teacher).join(school_m2m_teacher).join(School).filter(School.name==self.school.name).filter(Teacher.card_id==add_class_tea_card_id).first()
if not tea_obj:
print(Colors('该校区不存在该讲师身份证号!', 'red'))
continue
break
else:
print(Colors('该校区还没有讲师,已经跳过为该班级指定班主任(讲师)的步骤!', 'red'))
if while_flag:
#判断该校区是否存在课程,并为该班级关联课程
if self.school.courses:
choice_cou_obj_li = []
choice_cou_name_li = []
while while_flag:
self.display_course()
print(Colors('该班级已关联课程名:%s'%choice_cou_name_li, 'cyan'))
cou_name = input(Colors('请输入该班级关联的课程名(b 返回|end 结束关联课程):', 'cyan')).strip()
if cou_name == 'b':
while_flag = False
break
if cou_name == 'end':break
if not cou_name:continue
cou_obj = self.session.query(Course).join(school_m2m_course).join(School).filter(School.name==self.school.name).filter(Course.name==cou_name).first()
if not cou_obj:
print(Colors('该校区不存在课程[%s]!'%cou_name, 'red'))
continue
if cou_obj in choice_cou_obj_li:
print(Colors('该班级已关联该课程[%s]'%cou_name, 'red'))
continue
choice_cou_obj_li.append(cou_obj)
choice_cou_name_li.append(cou_name)
else:
print(Colors('该校区还没有增设课程,已经跳过为该班级关联课程的步骤!', 'red'))
if while_flag:
#更新Class表
if tea_obj:
cls_obj = Class(name=add_class_name, school_id=self.school.id, class_teacher_id=tea_obj.id)
else:
cls_obj = Class(name=add_class_name, school_id=self.school.id)
self.session.add_all([cls_obj, ])
self.session.commit()
#更新course_m2m_class表
if choice_cou_obj_li:
cls_obj = self.session.query(Class).join(School).filter(School.name==self.school.name).filter(Class.name==add_class_name).first()
cls_obj.courses = choice_cou_obj_li
self.session.commit()
print(Colors('创建班级成功!', 'green'))

center_admin.py

center_teacher.py    讲师中心

 # Author:ton
# -*- coding: utf-8 -*-
from core.color import Colors
from core import commons
from core import auth
from core.create_table import Course_m2m_class, User, School, Course, Teacher, Class, Student, Lesson, C2c_m2m_lesson,\
Study_record, school_m2m_course, school_m2m_teacher, school_m2m_student, student_m2m_class class Center_teacher(object):
"""讲师中心"""
def __init__(self, session):
self.session = session
self.handle() @auth.auth(permission=1)
def handle(self):
self.tea_obj = self.user.teacher[0]
while True:
print("讲师中心".center(45, '-'))
msg = """
创建指定班级-课程、天数的上课纪录 create_c2c_m2m_lesson
查看上课记录 display_tea_c2c_m2m_lessons
批改成绩 correcting_results
退出并返回系统主界面 q
"""
print(Colors(msg))
print('当前讲师:%s' %self.tea_obj.name)
print("".center(50, '-'))
choice = input(Colors('请输入要执行的指令:', 'cyan')).strip()
if choice == 'q':break
if hasattr(self, choice):
getattr(self, choice)()
else:
print(Colors('无效的指令!', 'red')) def create_c2c_m2m_lesson(self):
while True:
if not commons.display_tea_classes(self.session, self.tea_obj):return
cls_name = input(Colors('请指定班级名(b返回):', 'cyan')).strip()
if cls_name == 'b':break
if not cls_name:continue
#判断该讲师是否有任课该班级名
cls_obj = self.session.query(Class).join(Course_m2m_class).filter(Course_m2m_class.teacher_id==self.tea_obj.id).filter(Class.name==cls_name).first()
if not cls_obj:
print(Colors('讲师[%s]没有任课该班级[%s]!'%(self.tea_obj.name, cls_name), 'red'))
continue
while True:
if not commons.display_tea_cls_courses(self.session, self.tea_obj, cls_obj):return
cou_name = input(Colors('请指定课程名(b返回):', 'cyan')).strip()
if cou_name == 'b':break
if not cou_name:continue
#判断该讲师是否有任课该班级-课程
c2c_obj = self.session.query(Course_m2m_class).join(Course).filter(Course_m2m_class.teacher_id==self.tea_obj.id)\
.filter(Course_m2m_class.class_id==cls_obj.id).filter(Course.name==cou_name).first()
if not c2c_obj:
print(Colors('讲师[%s]没有任课[班级%s课程%s]!'%(self.tea_obj.name, cls_name, cou_name), 'red'))
continue
while True:
print(Colors('选定的班级-课程[%s]'%c2c_obj, 'cyan'))
les_name = input(Colors('请指定天数(例如day01)(b返回):', 'cyan')).strip()
if les_name == 'b':break
if not les_name:continue
#该天数不存在则更新Lesson表
les_obj = self.session.query(Lesson).filter(Lesson.name==les_name).first()
if not les_obj:
les_obj = Lesson(name=les_name)
self.session.add(les_obj)
self.session.commit()
#该上课记录不存在则更新C2c_m2m_lesson表
c2c_m2m_les_obj = self.session.query(C2c_m2m_lesson).filter(C2c_m2m_lesson.lesson_id==les_obj.id)\
.filter(C2c_m2m_lesson.course_m2m_class_id==c2c_obj.id).first()
if c2c_m2m_les_obj:
print(Colors('该上课记录已存在!', 'red'))
continue
c2c_m2m_les_obj = C2c_m2m_lesson(course_m2m_class_id=c2c_obj.id, lesson_id=les_obj.id)
self.session.add(c2c_m2m_les_obj)
self.session.commit()
#更新该班级所有学员的学习记录
for stu_obj in cls_obj.students:
stu_rec_obj = Study_record(c2c_m2m_lesson_id=c2c_m2m_les_obj.id, student_id=stu_obj.id)
self.session.add(stu_rec_obj)
self.session.flush()
self.session.commit() print(Colors('创建上课纪录成功!', 'green')) def display_tea_c2c_m2m_lessons(self):
for sch_obj in self.tea_obj.schools:
commons.display_tea_sch_c2c_m2m_lessons(self.session, self.tea_obj, sch_obj) def correcting_results(self):
"""批改作业"""
while True:
for sch_obj in self.session.query(School).join(school_m2m_teacher).join(Teacher).filter(Teacher.id==self.tea_obj.id).all():
commons.display_tea_sch_studyRecords(self.session, self.tea_obj, sch_obj)
stu_rec_id = input(Colors('请输入批改的学习记录ID(b返回):', 'cyan')).strip()
if stu_rec_id == 'b':break
if not stu_rec_id:continue
stu_rec_obj = self.session.query(Study_record).join(C2c_m2m_lesson).join(Course_m2m_class).\
filter(Course_m2m_class.teacher_id==self.tea_obj.id).filter(Study_record.id==stu_rec_id).first()
if not stu_rec_obj:
print(Colors('无权限或者没有该学习记录!', 'red'))
continue
score = input(Colors('请输入批改后的分数(b返回):', 'cyan')).strip()
if score == 'b':break
if not score:continue
#更新Study_record表
stu_rec_obj.score = score
self.session.commit()
print(Colors('批改作业成功!', 'green'))

center_teacher.py

center_student.py    学员中心

 from core.color import Colors
from core import commons
from core import auth
from core.create_table import Course_m2m_class, User, School, Course, Teacher, Class, Student, Lesson, C2c_m2m_lesson,\
Study_record, school_m2m_course, school_m2m_teacher, school_m2m_student, student_m2m_class class Center_student(object):
"""学员中心"""
def __init__(self, session):
self.session = session
self.handle() @auth.auth(permission=2)
def handle(self):
self.stu_obj = self.user.student[0]
while True:
print("学员中心".center(45, '-'))
msg = """
签到(提交作业) sign_in
查看作业成绩 display_score
成绩排名 pai
退出并返回系统主界面 q
"""
print(Colors(msg))
print('当前学员:%s' %self.stu_obj.name)
print("".center(50, '-'))
choice = input(Colors('请输入要执行的指令:', 'cyan')).strip()
if choice == 'q':break
if hasattr(self, choice):
getattr(self, choice)()
else:
print(Colors('无效的指令!', 'red')) def sign_in(self):
while True:
for sch_obj in self.session.query(School).join(school_m2m_student).join(Student).filter(Student.id==self.stu_obj.id).all():
commons.display_stu_sch_studyRecords(self.session, self.stu_obj, sch_obj)
stu_rec_id = input(Colors('请输入要签到的学习记录ID(b返回):', 'cyan')).strip()
if stu_rec_id == 'b':break
if not stu_rec_id:continue
stu_rec_obj = self.session.query(Study_record).filter(Study_record.student_id==self.stu_obj.id).filter(Study_record.id==stu_rec_id).first()
if not stu_rec_obj:
print(Colors('无权限或者没有该学习记录!', 'red'))
continue
#更新Study_record表
stu_rec_obj.status = 'yes'
self.session.commit()
print(Colors('签到成功!', 'green')) def display_score(self):
for sch_obj in self.session.query(School).join(school_m2m_student).join(Student).filter(Student.id==self.stu_obj.id).all():
commons.display_stu_sch_studyRecords(self.session, self.stu_obj, sch_obj) def pai(self):
for sch_obj in self.session.query(School).join(school_m2m_student).join(Student).filter(Student.id==self.stu_obj.id).all():
commons.display_stu_sch_cls_score(self.session, self.stu_obj, sch_obj)

center_student.py

commons.py

 from tabulate import tabulate
from core.color import Colors
from sqlalchemy import Column, Integer, String, Table, MetaData, ForeignKey, func,DATE, Enum
from core.create_table import User, School, Course, Teacher, Student, Class, Course_m2m_class, C2c_m2m_lesson, Study_record, student_m2m_class
import hashlib def fmt_table(column_table, table_obj_li):
headers = []
table = []
for column_dic in column_table:
for column_name in column_dic:
headers.append(column_dic[column_name])
for obj in table_obj_li:
row = []
for column_dic in column_table:
for column_name in column_dic:
row.append(getattr(obj, column_name))
table.append(row)
else:
return tabulate(table, headers, tablefmt='psql') def update_table(session, column_table, table_obj):
for column_dic in column_table:
for column_name in column_dic:
while True:
new_value = input(Colors('请输入修改后的%s(b 退出修改操作):'%column_dic[column_name], 'cyan')).strip()
if not new_value:continue
if new_value == 'b':return
setattr(table_obj, column_name, new_value)
break
session.commit()
print(Colors('更新%s表成功!'%table_obj.__tablename__, 'green'))
return True def create_user(session, permission):
while True:
add_username = input(Colors('请输入用于登陆的用户名(b 返回):', 'cyan')).strip()
if add_username == 'b':return
if not add_username:continue
user_obj = session.query(User).filter(User.username==add_username).first()
if user_obj:
print(Colors('该用户名已存在!', 'red'))
continue
while True:
add_password = input(Colors('请输入用于登陆的密码(b 返回):', 'cyan')).strip()
if add_password == 'b':break
if not add_password:continue
md5 = hashlib.md5()
md5.update(add_password.encode('utf-8'))
add_password = md5.hexdigest()
#更新User表
user_obj = User(username=add_username, password=add_password, permission=permission)
session.add_all([user_obj, ])
session.commit()
user_obj = session.query(User).filter(User.username==add_username).first()
return user_obj.id def choice_school(session):
while True:
#判断School表是否为空,为空则退出
if not display_school(session):return
choice_school_name = input(Colors('请输入校区名(b 返回):', 'cyan')).strip()
if choice_school_name == 'b':return
if not choice_school_name:continue
#判断该校区名是否存在
sch_obj = session.query(School).filter(School.name==choice_school_name).first()
if not sch_obj:
print(Colors('该校区名不存在!', 'red'))
continue
return sch_obj def choice_teacher(session):
while True:
#判断Teacher表是否为空,为空则退出
if not display_teacher(session):return
choice_tea_card_id = input(Colors('请输入讲师身份证号码(b 返回):', 'cyan')).strip()
if choice_tea_card_id == 'b':return
if not choice_tea_card_id:continue
#判断该讲师身份证号码是否存在
tea_obj = session.query(Teacher).filter(Teacher.card_id==choice_tea_card_id).first()
if not tea_obj:
print(Colors('该讲师身份证号码不存在!', 'red'))
continue
return tea_obj def choice_class(session):
while True:
sch_obj = choice_school(session)
if not sch_obj:return
while True:
#判断该校区的班级是否为空,为空则break
if not display_sch_classes(sch_obj):break
choice_cls_name = input(Colors('请输入班级名(b 返回):', 'cyan')).strip()
if choice_cls_name == 'b':break
if not choice_cls_name:continue
#判断该班级名是否存在
cls_obj = session.query(Class).filter(Class.name==choice_cls_name).first()
if not cls_obj:
print(Colors('校区[%s]没有该班级[%s]!'%(sch_obj.name, choice_cls_name), 'red'))
continue
return sch_obj, cls_obj def display_school(session):
sch_obj_li = session.query(School).all()
if sch_obj_li:
print('所有校区信息如下:')
column_school = [{'name':'校区名'}, {'address':'地址'}, ]
fmt_sch_table = fmt_table(column_school, sch_obj_li)
print(fmt_sch_table)
return True
else:
print(Colors('现在还没有校区,请先创建校区!', 'red')) def display_course(session):
cou_obj_li = session.query(Course).all()
if not len(cou_obj_li) == 0:
print('所有课程信息如下:')
column_course = [{'name':'课程名'}, {'price':'价格'}, ]
fmt_cou_table = fmt_table(column_course, cou_obj_li)
print(fmt_cou_table)
return True
else:
print(Colors('现在还没有课程,请先创建课程!', 'red')) def display_teacher(session):
tea_obj_li = session.query(Teacher).all()
if tea_obj_li:
column_teacher = [{'name':'讲师名'}, {'card_id':'身份证号码'}, {'user':'账号信息'}, {'schools':'所属校区信息'}, {'course_m2m_classes':'任课班级和课程信息'}]
fmt_tea_table = fmt_table(column_teacher, tea_obj_li)
print(Colors('所有讲师信息如下:'))
print(fmt_tea_table)
return True
else:
print(Colors('现在还没有讲师,请先创建讲师!', 'red')) def display_student(session):
stu_obj_li = session.query(Student).all()
if stu_obj_li:
column_student = [{'name':'学员名'}, {'qq':'QQ号'}, {'user':'账号信息'}, {'schools':'所属校区信息'}, {'classes':'报名班级信息'}]
fmt_stu_table = fmt_table(column_student, stu_obj_li)
print(Colors('所有学员信息如下:'))
print(fmt_stu_table)
return True
else:
print(Colors('现在还没有学员,请先创建学员!', 'red')) def display_class(session):
cls_obj_li = session.query(Class).all()
if cls_obj_li:
column_class = [{'name':'班级名'}, {'school':'所属校区信息'}, {'courses':'关联课程'}, {'students':'该班学员信息'}]
fmt_cls_table = fmt_table(column_class, cls_obj_li)
print(Colors('所有班级信息如下:'))
print(fmt_cls_table)
return True
else:
print(Colors('现在还没有班级,请先创建班级!', 'red')) def display_sch_courses(sch_obj):
"""展示某校区所有课程信息"""
if sch_obj.courses:
print('校区[%s]已有课程信息如下:'%sch_obj.name)
column_course = [{'name':'课程名'}, {'price':'价格'}, ]
fmt_cou_table = fmt_table(column_course, sch_obj.courses)
print(fmt_cou_table)
return True
else:
print(Colors('校区[%s]还没有课程'%sch_obj.name, 'red')) def display_sch_teachers(sch_obj):
"""展示某校区所有讲师信息"""
if sch_obj.teachers:
print('校区[%s]已有讲师信息如下:'%sch_obj.name)
column_teacher = [{'name':'讲师名'}, {'card_id':'身份证号码'}, {'user':'账号信息'}, {'schools':'所属校区信息'}, {'course_m2m_classes':'任课班级和课程信息'}]
fmt_tea_table = fmt_table(column_teacher, sch_obj.teachers)
print(fmt_tea_table)
return True
else:
print(Colors('校区[%s]还没有讲师'%sch_obj.name, 'red')) def display_sch_students(sch_obj):
"""展示某校区所有学员信息"""
if sch_obj.students:
print('校区[%s]已有学员信息如下:'%sch_obj.name)
column_student = [{'name':'学员名'}, {'qq':'QQ号'}, {'user':'账号信息'}, {'schools':'所属校区信息'}, {'classes':'报名班级信息'}]
fmt_stu_table = fmt_table(column_student, sch_obj.students)
print(fmt_stu_table)
return True
else:
print(Colors('校区[%s]还没有学员'%sch_obj.name, 'red')) def display_sch_classes(sch_obj):
"""展示某校区所有班级信息"""
if sch_obj.classes:
print('校区[%s]已有班级信息如下:'%sch_obj.name)
column_class = [{'name':'班级名'}, {'school':'所属校区信息'}, {'courses':'关联课程'}, {'students':'该班学员信息'}]
fmt_cls_table = fmt_table(column_class, sch_obj.classes)
print(fmt_cls_table)
return True
else:
print(Colors('校区[%s]还没有班级'%sch_obj.name, 'red')) def display_cls_courses(cls_obj):
"""展示某班级已有课程信息"""
if not cls_obj.courses:
print(Colors('班级[%s]还没有添加课程'%cls_obj.name, 'red'))
return
print('班级[%s]关联的所有课程信息如下:'%cls_obj.name)
column_course = [{'name':'课程名'},]
fmt_cou_table = fmt_table(column_course, cls_obj.courses)
print(fmt_cou_table)
return True def display_cls_students(cls_obj):
"""展示某班级已有学员信息"""
if not cls_obj.students:
print(Colors('班级[%s]还没有添加学员'%cls_obj.name, 'red'))
return
print('班级[%s]的所有学员信息如下:'%cls_obj.name)
column_student = [{'name':'学员名'}, {'qq':'QQ号'}, {'user':'账号信息'}, {'schools':'所属校区信息'}, {'classes':'报名班级信息'}]
fmt_stu_table = fmt_table(column_student, cls_obj.students)
print(fmt_stu_table)
return True def display_tea_schools(tea_obj):
if not tea_obj.schools:
print(Colors('讲师[%s]还没有任教的校区,请在校区管理界面添加讲师!'%tea_obj.name, 'red'))
return
print('讲师[%s]任教的所有校区信息如下:'%tea_obj.name)
column_school = [{'name':'校区名'}, {'address':'地址'}, ]
fmt_sch_table = fmt_table(column_school, tea_obj.schools)
print(fmt_sch_table)
return True def display_tea_classes(session, tea_obj):
"""展示某讲师任课的所有班级"""
cls_obj_li = session.query(Class).join(Course_m2m_class).filter(Course_m2m_class.teacher_id==tea_obj.id).all()
if not cls_obj_li:
print(Colors('讲师[%s]还没有添加任课的班级,请在讲师管理界面添加班级-课程!'%tea_obj.name, 'red'))
return
print('讲师[%s]任课的所有班级信息如下:'%tea_obj.name)
column_class = [{'name':'班级名'}, {'school':'所属校区信息'}, {'courses':'关联课程'}, {'students':'该班学员信息'}]
fmt_cls_table = fmt_table(column_class, cls_obj_li)
print(fmt_cls_table)
return True def display_tea_cls_courses(session, tea_obj, cls_obj):
"""展示某讲师任课的某班级的所有任课课程"""
cou_obj_li = session.query(Course).join(Course_m2m_class).filter(Course_m2m_class.teacher_id==tea_obj.id).filter(Course_m2m_class.class_id==cls_obj.id).all()
if not cou_obj_li:
print(Colors('讲师[%s]没有任课班级[%s]!'%(tea_obj.name, cls_obj.name), 'red'))
return
print('讲师[%s]任课的校区%s班级[%s]的所有任课课程信息如下:'%(tea_obj.name, cls_obj.school.name, cls_obj.name))
column_course = [{'name':'课程名'},]
fmt_cls_table = fmt_table(column_course, cou_obj_li)
print(fmt_cls_table)
return True def display_tea_sch_c2c(tea_obj, sch_obj):
"""展示讲师任教的某一校区所任课的班级-课程"""
#找出讲师任教的某一校区所任课的班级-课程
c2c_obj_li = select_tea_sch_c2c(tea_obj, sch_obj)
if len(c2c_obj_li)==0:
print(Colors('讲师%s任教的校区%s还没有任课的班级-课程'%(tea_obj.name, sch_obj.name), 'red'))
return
c2c_name_dic = {}
for c2c_obj in c2c_obj_li:
if not c2c_name_dic.get(c2c_obj.glass.name):
c2c_name_dic[c2c_obj.glass.name] = [c2c_obj.course.name,]
else:
c2c_name_dic[c2c_obj.glass.name].append(c2c_obj.course.name)
print('讲师%s任教的校区%s任课的班级-课程信息如下:'%(tea_obj.name, sch_obj.name))
for cls_name in c2c_name_dic:
print('\t班级[%s]:'%cls_name)
for cou_name in c2c_name_dic[cls_name]:
print('\t\t课程[%s]'%cou_name)
return True def display_tea_sch_c2c_m2m_lessons(session, tea_obj, sch_obj):
"""展示某讲师任教的某校区的上课记录"""
#找出讲师任教的某校区所任课的班级-课程
c2c_obj_li = select_tea_sch_c2c(session, tea_obj, sch_obj)
if len(c2c_obj_li)==0:
print(Colors('讲师%s任教的校区%s还没有任课的班级-课程'%(tea_obj.name, sch_obj.name), 'red'))
return
#循环找出c2c的上课记录
c2c_les_obj_li = []
c2c_les_obj_dic = {}
for c2c_obj in c2c_obj_li:
# c2c_les_obj_li.extend(c2c_obj.c2c_m2m_lessons)
c2c_les_obj_li.extend(session.query(C2c_m2m_lesson).filter(C2c_m2m_lesson.course_m2m_class_id==c2c_obj.id).all())
if len(c2c_les_obj_li)==0:
print(Colors('讲师%s任教的校区%s还没有上课记录'%(tea_obj.name, sch_obj.name), 'red'))
return
for c2c_les_obj in c2c_les_obj_li:
if not c2c_les_obj_dic.get(c2c_les_obj.c2c.glass.name):
c2c_les_obj_dic[c2c_les_obj.c2c.glass.name] = [c2c_les_obj,]
else:
c2c_les_obj_dic[c2c_les_obj.c2c.glass.name].append(c2c_les_obj)
print('讲师%s任教的校区%s的上课记录信息如下:'%(tea_obj.name, sch_obj.name))
for cls_name in c2c_les_obj_dic:
print('\t班级[%s]:'%cls_name)
for c2c_les_obj in c2c_les_obj_dic[cls_name]:
print('\t\t%s'%c2c_les_obj)
return True def display_tea_sch_studyRecords(session, tea_obj, sch_obj):
"""展示某讲师任教的某校区任课的班级-课程的所有学习记录"""
#找出讲师任教的某校区任课的班级-课程的所有学习记录
stu_rec_obj_li = select_tea_sch_stuRec(session, tea_obj, sch_obj)
stu_rec_obj_dic = {}
if len(stu_rec_obj_li)==0:
print(Colors('讲师%s任教的校区%s还没有上课记录'%(tea_obj.name, sch_obj.name), 'red'))
return
for stu_rec_obj in stu_rec_obj_li:
if not stu_rec_obj_dic.get(stu_rec_obj.c2c_m2m_lesson.c2c.glass.name):
stu_rec_obj_dic[stu_rec_obj.c2c_m2m_lesson.c2c.glass.name] = [stu_rec_obj,]
else:
stu_rec_obj_dic[stu_rec_obj.c2c_m2m_lesson.c2c.glass.name].append(stu_rec_obj)
print('讲师%s任教的校区%s的学员学习记录如下:'%(tea_obj.name, sch_obj.name))
for cls_name in stu_rec_obj_dic:
print('\t班级%s:'%cls_name)
for stu_rec_obj in stu_rec_obj_dic[cls_name]:
print('\t\t%s'%stu_rec_obj)
return True def display_stu_sch_studyRecords(session, stu_obj, sch_obj):
"""展示某学员注册的某校区的学习记录"""
#找出学员注册的某校区报名班级的所有学习记录
stu_rec_obj_li = select_stu_sch_stuRec(session, stu_obj, sch_obj)
stu_rec_obj_dic = {}
if len(stu_rec_obj_li)==0:
print(Colors('学员%s注册的校区%s还没有学习记录'%(stu_obj.name, sch_obj.name), 'red'))
return
for stu_rec_obj in stu_rec_obj_li:
if not stu_rec_obj_dic.get(stu_rec_obj.c2c_m2m_lesson.c2c.glass.name):
stu_rec_obj_dic[stu_rec_obj.c2c_m2m_lesson.c2c.glass.name] = [stu_rec_obj,]
else:
stu_rec_obj_dic[stu_rec_obj.c2c_m2m_lesson.c2c.glass.name].append(stu_rec_obj)
print('学员%s注册的校区%s的学习记录如下:'%(stu_obj.name, sch_obj.name))
for cls_name in stu_rec_obj_dic:
print('\t班级%s:'%cls_name)
for stu_rec_obj in stu_rec_obj_dic[cls_name]:
print('\t\t%s'%stu_rec_obj)
return True def display_stu_sch_cls_score(session, stu_obj, sch_obj):
"""展示某学员注册的某学校下报名的班级的成绩排名"""
#找出学员注册的某校区报名班级的所有学习记录
stu_rec_obj_li = select_stu_sch_stuRec(session, stu_obj, sch_obj)
stu_rec_obj_dic = {}
if len(stu_rec_obj_li)==0:
print(Colors('学员%s注册的校区%s还没有学习记录'%(stu_obj.name, sch_obj.name), 'red'))
return for stu_rec_obj in stu_rec_obj_li:
if not stu_rec_obj_dic.get(stu_rec_obj.c2c_m2m_lesson.c2c.glass.name):
stu_rec_obj_dic[stu_rec_obj.c2c_m2m_lesson.c2c.glass.name] = [stu_rec_obj,]
else:
stu_rec_obj_dic[stu_rec_obj.c2c_m2m_lesson.c2c.glass.name].append(stu_rec_obj)
print('学员%s注册的校区%s的学习记录如下:'%(stu_obj.name, sch_obj.name))
for cls_name in stu_rec_obj_dic:
print('\t班级%s:'%cls_name)
obj_li = session.query(Student.qq, func.sum(Study_record.score)).join(Study_record).join(C2c_m2m_lesson).join(Course_m2m_class).join(Class).\
filter(Class.name==cls_name).filter(Class.school_id==sch_obj.id).group_by(Study_record.student_id).order_by(func.sum(Study_record.score)).all()
print(obj_li)
sort_stu_qq_li = []
for obj in obj_li:
sort_stu_qq_li.append(obj[0])
print(sort_stu_qq_li)
print('当前班级总成绩排名%s'%(len(sort_stu_qq_li)-sort_stu_qq_li.index(stu_obj.qq)))
# for stu_rec_obj in stu_rec_obj_dic[cls_name]:
# print('\t\t%s'%stu_rec_obj)
return True def select_tea_sch_c2c(session, tea_obj, sch_obj):
"""找出讲师任教的某一校区所任课的班级-课程"""
tmp_c2c_obj_li = []
c2c_obj_li = []
cls_obj_li = session.query(Class).filter(Class.school_id==sch_obj.id).all()
for cls_obj in cls_obj_li:
tmp_c2c_obj_li.extend(session.query(Course_m2m_class).filter(Course_m2m_class.class_id==cls_obj.id).all())
for c2c_obj in tmp_c2c_obj_li:
if c2c_obj.teacher_id == tea_obj.id:
c2c_obj_li.append(c2c_obj)
return c2c_obj_li def select_tea_sch_stuRec(session, tea_obj, sch_obj):
"""找出讲师任教的某校区任课的班级-课程的所有学习记录"""
#找出讲师任教的某校区所任课的班级-课程
c2c_obj_li = select_tea_sch_c2c(session, tea_obj, sch_obj)
if len(c2c_obj_li)==0:
print(Colors('讲师%s任教的校区%s还没有任课的班级-课程'%(tea_obj.name, sch_obj.name), 'red'))
return
#循环找出c2c的上课记录
c2c_les_obj_li = []
stu_rec_obj_li = []
for c2c_obj in c2c_obj_li:
c2c_les_obj_li.extend(session.query(C2c_m2m_lesson).filter(C2c_m2m_lesson.course_m2m_class_id==c2c_obj.id).all())
for c2c_les_obj in c2c_les_obj_li:
stu_rec_obj_li.extend(session.query(Study_record).filter(Study_record.c2c_m2m_lesson_id==c2c_les_obj.id).all())
return stu_rec_obj_li def select_stu_sch_stuRec(session, stu_obj, sch_obj):
"""找出学员注册的某校区报名的班级的所有学习记录"""
#找出学员注册的某校区报名的班级
cls_obj_li = session.query(Class).join(student_m2m_class).join(Student).filter(Student.id==stu_obj.id).filter(Class.school_id==sch_obj.id).all()
if len(cls_obj_li)==0:
print(Colors('学员%s注册的校区%s还没有报名班级'%(stu_obj.name, sch_obj.name), 'red'))
return
#循环找出该班级下该学员的学习记录
stu_rec_obj_li = []
for cls_obj in cls_obj_li:
stu_rec_obj_li.extend(session.query(Study_record).join(C2c_m2m_lesson).join(Course_m2m_class).\
filter(Course_m2m_class.class_id==cls_obj.id).filter(Study_record.student_id==stu_obj.id).all())
return stu_rec_obj_li

create_table.py

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Table, ForeignKey, Enum, PrimaryKeyConstraint, UniqueConstraint
from sqlalchemy.orm import relationship # 指定字符集、最大连接池数
engine = create_engine("mysql+pymysql://root:123456@10.0.0.15/studentdb?charset=utf8", max_overflow=5) Base = declarative_base() school_m2m_course = Table('school_m2m_course', Base.metadata,
Column('school_id', Integer, ForeignKey('school.id'), nullable=False),
Column('course_id', Integer, ForeignKey('course.id'), nullable=False),
UniqueConstraint('school_id', 'course_id', name='sch_id_cou_id_uk'),
)
school_m2m_teacher = Table('school_m2m_teacher', Base.metadata,
Column('school_id', Integer, ForeignKey('school.id'), nullable=False),
Column('teacher_id', Integer, ForeignKey('teacher.id'), nullable=False),
UniqueConstraint('school_id', 'teacher_id', name='sch_id_tea_id_uk'),
)
school_m2m_student = Table('school_m2m_student', Base.metadata,
Column('school_id', Integer, ForeignKey('school.id'), nullable=False),
Column('student_id', Integer, ForeignKey('student.id'), nullable=False),
UniqueConstraint('school_id', 'student_id', name='sch_id_stu_id_uk'),
)
# course_m2m_class = Table('course_m2m_class', Base.metadata,
# Column('id', Integer, primary_key=True, autoincrement=True),
# Column('course_id', Integer, ForeignKey('course.id')),
# Column('class_id', Integer, ForeignKey('class.id')),
# Column('teacher_id', Integer, ForeignKey('teacher.id')),
# UniqueConstraint('course_id', 'class_id', name='cls_id_cou_id_uk'),
# )
student_m2m_class = Table('student_m2m_class', Base.metadata,
Column('student_id', Integer, ForeignKey('student.id'), nullable=False),
Column('class_id', Integer, ForeignKey('class.id'), nullable=False),
UniqueConstraint('student_id', 'class_id', name='stu_id_cls_id_uk'),
)
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True, autoincrement=True)
username = Column(String(32), nullable=False, unique=True)
password = Column(String(64), nullable=False)
permission = Column(Integer, nullable=False)
def __repr__(self):
return "用户名:%s 权限:%s"%(self.username, self.permission) class School(Base):
__tablename__ = 'school'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(32), nullable=False, unique=True)
address = Column(String(32), nullable=False) courses = relationship('Course', secondary=school_m2m_course, backref='schools')
classes = relationship('Class', backref='school')
teachers = relationship('Teacher', secondary=school_m2m_teacher, backref='schools')
students = relationship('Student', secondary=school_m2m_student, backref='schools')
def __repr__(self):
return "校区名:%s"%self.name class Course_m2m_class(Base):
__tablename__ = 'course_m2m_class'
id = Column(Integer, primary_key=True, autoincrement=True)
course_id = Column(Integer, ForeignKey('course.id'), nullable=False)
class_id = Column(Integer, ForeignKey('class.id'), nullable=False)
teacher_id = Column(Integer, ForeignKey('teacher.id'), ) __table_args__ = (
UniqueConstraint('course_id', 'class_id', name='cls_id_cou_id_uk'), # 唯一索引
) teacher = relationship('Teacher', backref='course_m2m_classes')
glass = relationship('Class', backref='course_m2m_classes')
course = relationship('Course', backref='course_m2m_classes')
def __repr__(self):
return "班级名:%s 课程名:%s"%(self.glass.name, self.course.name) class Course(Base):
__tablename__ = 'course'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(32), nullable=False, unique=True)
price = Column(Integer, nullable=False) classes = relationship('Class', secondary=Course_m2m_class.__table__, backref='courses')
def __repr__(self):
return "课程名:%s"%self.name class Class(Base):
__tablename__ = 'class'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(32), nullable=False)
school_id = Column(Integer, ForeignKey('school.id'), nullable=False) __table_args__ = (
UniqueConstraint('name', 'school_id', name='name_school_id_uk'), # 唯一索引
)
def __repr__(self):
return "班级名:%s"%self.name class Teacher(Base):
__tablename__ = 'teacher'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(32), nullable=False)
card_id = Column(String(32), nullable=False, unique=True)
user_id = Column(Integer, ForeignKey('user.id'), unique=True, nullable=False) user = relationship('User', backref='teacher')
def __repr__(self):
return "讲师名:%s 身份证号:%s 用户名:%s"%(self.name, self.card_id, self.user.username) class Student(Base):
__tablename__ = 'student'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(32), nullable=False)
qq = Column(String(32), nullable=False, unique=True)
user_id = Column(Integer, ForeignKey('user.id'), unique=True, nullable=False) classes = relationship('Class', secondary=student_m2m_class, backref='students')
user = relationship('User', backref='student')
def __repr__(self):
return "学员名:%s QQ号:%s 用户名:%s"%(self.name, self.qq, self.user.username) class Lesson(Base):
__tablename__ = 'lesson'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(32), nullable=False, unique=True) class C2c_m2m_lesson(Base):
__tablename__ = 'c2c_m2m_lesson'
id = Column(Integer, primary_key=True, autoincrement=True)
course_m2m_class_id = Column(Integer, ForeignKey('course_m2m_class.id'), nullable=False)
lesson_id = Column(Integer, ForeignKey('lesson.id'), nullable=False) __table_args__ = (
UniqueConstraint('course_m2m_class_id', 'lesson_id', name='c2c_id_les_id_uk'), # 唯一索引
) c2c = relationship('Course_m2m_class', backref='c2c_m2m_lessons')
lesson = relationship('Lesson', backref='c2c_m2m_lessons')
def __repr__(self):
return "%s %s"%(self.c2c, self.lesson.name) class Study_record(Base):
__tablename__ = 'study_record'
id = Column(Integer, primary_key=True, autoincrement=True)
status = Column(Enum('no','yes'), nullable=False, default='no')
score = Column(String(32), nullable=False, default='未评分')
c2c_m2m_lesson_id = Column(Integer, ForeignKey('c2c_m2m_lesson.id'), nullable=False)
student_id = Column(Integer, ForeignKey('student.id'), nullable=False) __table_args__ = (
UniqueConstraint('c2c_m2m_lesson_id', 'student_id', name='c2c_m2m_les_id_stu_id_uk'), # 唯一索引
) c2c_m2m_lesson = relationship('C2c_m2m_lesson', backref='study_records')
student = relationship('Student', backref='study_records')
def __repr__(self):
return "学习记录ID:%s 签到状态:%s 成绩:%s 上课记录:%s 学员:%s"%(self.id, self.status, self.score, self.c2c_m2m_lesson, self.student.name) def create_table():
Base.metadata.create_all(engine) if __name__ == '__main__':
create_table()

  

学员管理系统(SQLAlchemy 实现)的更多相关文章

  1. Python开发程序:学员管理系统(mysql)

    主题:学员管理系统 需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图: 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节 ...

  2. python作业学员管理系统(第十二周)

    作业需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上课纪录对应多条学 ...

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

    一.作业需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图: 管理班级,可创建班级,根据学员qq号把学员加入班级      可创建指定班级的上课纪录,注意一节上 ...

  4. python项目开发:学员管理系统

    学员管理系统 #需求: 1.用户角色:讲师/学员,登陆后根据角色不同能做的事情不同 2.讲师视图 - 管理班级,可创建班级,根据学员qq号把学员加入班级 - 可创建指定班级的上课纪录,注意一节上课纪录 ...

  5. python3.6+django2.0 一小时学会开发一套学员管理系统demo

    1.在pycharm中新建project demo1 添加app01 点击create按钮完成新建 2.在demo项目目录下新建目录static,并在settings.py中追加代码: STATICF ...

  6. ORM版学员管理系统

    ORM版学员管理系统 班级表 表结构 class Class(models.Model): id = models.AutoField(primary_key=True) # 主键 cname = m ...

  7. Python学习(二十七)—— Django和pymysql搭建学员管理系统

    转载自http://www.cnblogs.com/liwenzhou/p/8270250.html 一.学员管理系统 1.项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的e ...

  8. Mysql数据库基础小实例 学员管理系统菜单

    package test; import java.sql.*; import java.util.Scanner; public class testSql002_StudentTest { /** ...

  9. ORM版学员管理系统1

    ORM版学员管理系统 班级表 表结构 class Class(models.Model): id = models.AutoField(primary_key=True) # 主键 cname = m ...

随机推荐

  1. HBase学习之路 (五)MapReduce操作Hbase

    MapReduce从HDFS读取数据存储到HBase中 现有HDFS中有一个student.txt文件,格式如下 95002,刘晨,女,19,IS 95017,王风娟,女,18,IS 95018,王一 ...

  2. c++ 浅拷贝和深拷贝 指针和引用的区别 malloc(free)和new(delete)的区别 重载重写重定义

    4.malloc(free)和new(delete)的区别 malloc()函数: 1.1 malloc的全称是memory allocation,中文叫动态内存分配. 原型:extern void ...

  3. R语法:<<-为全局变量赋值

    例:在函数内部为全局变量赋值 all_predata_time <- data.frame(pd=0.1,Row=1,preRow=0,pt=0.1,stasid='1',InitDate='1 ...

  4. Servlet 处理请求

    一:Servlet 处理请求也是通过request来进行处理,类似于python. get请求通过request.getparameter("key");key为前端传过来的key ...

  5. python 语言学入门第一课必看:编码规范

    命名 module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_VAR_NAME, ...

  6. vue2 broadcast和dispatch的理解

    /* broadcast 事件广播 @param {componentName} 组件名称 @param {eventName} 事件名 @param {params} 参数 遍历寻找所有子孙组件,假 ...

  7. HDU1753 (大正小数相加)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1753 大明A+B Time Limit: 3000/1000 MS (Java/Others)    ...

  8. java 解析http user-agent 信息

    解析http user-agent信息,使用uasparser-0.6.1.jar和jregex-1.2_01.jar两个包 import cz.mallat.uasparser.OnlineUpda ...

  9. MySQL日期时间处理函数

    -- MySQL日期时间处理函数SELECT NOW() FROM DUAL;-- 当前日期时间:2017-05-12 11:41:47-- 在MySQL里也存在和Oracle里类似的dual虚拟表: ...

  10. Hive--可执行SQL的Hadoop数据仓库管理工具

    Hive是一个基于HDFS的数据仓库软件,可理解为数据库管理工具:Hive的功能主要有: 1. 支持使用SQL对分布式存储的大型数据集进行读.写.管理,将SQL转化成MapReduce任务执行: 2. ...