选课系统
项目要求
基类:
保存
读取
管理员类:姓名、密码
创建学校
创建课程
创建老师
老师类:姓名、密码、课程列表
添加课程
修改分数
学生类:姓名、密码、学校、课程列表、分数
获取学校
选择学校
添加课程
学校类:姓名、地址、课程列表
添加课程
课程类:姓名、学生列表
添加学生
管理员视图:
注册
登录
创建学校
创建老师
创建课程
学生视图:
注册
登录
选择学校
选择课程
查看成绩
老师视图:
登录
选择课程
查看课程
查看学生
修改学生成绩
conf 文件夹
###############
# settings.py #
###############
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DB_PATH = os.path.join(BASE_DIR, 'db')
core 文件夹
#################
# admin_view.py #
#################
from interface import common_interface, admin_interface
from lib import common
user_info = {'username': None}
def login():
print('欢迎来到登录功能')
username = input('请输入你的用户名>>>')
pwd = input('请输入你的密码>>>')
flag, msg = common_interface.login_interface(username, pwd, 'admin')
print(msg)
if flag:
user_info['username'] = username
return True
def register():
print('欢迎来到注册功能')
username = input('请输入你的用户名>>>')
pwd = input('请输入你的的密码>>>')
msg = admin_interface.register_interface(username, pwd)
print(msg)
@common.login_auth('admin')
def create_school():
print('欢迎来到创建学校功能')
username = user_info['username']
school_name = input('请选择你需要创建的学校名>>>')
school_addr = input('请选择你需要创建的学校地址>>>')
msg = admin_interface.create_school_interface(username, school_name, school_addr)
print(msg)
@common.login_auth('admin')
def create_teacher():
print('欢迎来到创建老师功能')
username = user_info['username']
teacher_username = input('请选择你需要创建老师的名字>>>')
teacher_pwd = input('请选择你需要创建老师的密码>>>')
msg = admin_interface.create_teacher_interface(username, teacher_username, teacher_pwd)
print(msg)
@common.login_auth('admin')
def create_course():
print('欢迎来到创建课程功能')
username = user_info['username']
school_list = common.get_obj_list('school')
print(school_list)
school_name = input('请选择你需要选择的学校')
course_name = input('请输入你需要创建课程的名字>>>')
msg = admin_interface.create_course_interface(username, course_name, school_name)
print(msg)
def run():
print('欢迎来到管理员视图界面')
msg = '''
1. 注册
2. 登录
3. 创建学校
4. 创建老师
5. 创建课程
q. 退出
'''
func_dict = {
'1': register,
'2': login,
'3': create_school,
'4': create_teacher,
'5': create_course,
}
while True:
print(msg)
func_choice = input('请选择你需要的功能>>>')
if func_choice == 'q':
break
if func_choice not in func_dict:
print('傻逼,功能不存在')
continue
func_dict[func_choice]()
###################
# student_view.py #
###################
from lib import common
from interface import common_interface, student_interface
user_info = {'username': None}
def login():
print('欢迎来到登录功能')
username = input('请输入你的用户名>>>')
pwd = input('请输入你的密码>>>')
flag, msg = common_interface.login_interface(username, pwd, 'student')
print(msg)
if flag:
user_info['username'] = username
return True
def register():
print('欢迎来到注册功能')
username = input('请输入你的用户名>>>')
pwd = input('请输入你的密码>>>')
msg = student_interface.register_interface(username, pwd)
print(msg)
@common.login_auth('student')
def choose_school():
print('欢迎来到选择学校功能')
username = user_info['username']
school_list = common.get_obj_list('school')
for ind, school in enumerate(school_list):
print(ind, school)
school_choice = input('请输入你需要选择的学校序号>>>')
school_name = school_list[int(school_choice)]
msg = student_interface.choose_school_interface(username, school_name)
print(msg)
@common.login_auth('student')
def choose_course():
print('欢迎来到选择课程功能')
username = user_info['username']
# 1. 获取课程
course_list = student_interface.get_school_course_list(username)
for ind, course in enumerate(course_list):
print(ind, course.name)
# 2. 选择课程
course_choice = input('请输入你需要选择课程的序号>>>')
course = course_list[int(course_choice)]
msg = student_interface.choose_course_interface(username, course)
print(msg)
@common.login_auth('student')
def check_score():
print('欢迎来到查看成绩功能')
username = user_info['username']
# 1. 获取课程
course_list = student_interface.get_student_course_list(username)
for ind, course in enumerate(course_list):
print(ind, course.name)
# 2. 选择课程
course_choice = input('请输入你需要选择课程的序号>>>')
course = course_list[int(course_choice)]
score = student_interface.check_score_interface(username, course)
if score:
print(f'{username}学生课程{course.name}的成绩为{score}')
else:
print('当前课程没有成绩')
def run():
print('欢迎来到学生视图界面')
msg = '''
1. 注册
2. 登录
3. 选择学校
4. 选择课程
5. 查看成绩
q. 退出
'''
func_dict = {
'1': register,
'2': login,
'3': choose_school,
'4': choose_course,
'5': check_score,
}
while True:
print(msg)
func_choice = input('请选择你需要的功能>>>')
if func_choice == 'q':
break
if func_choice not in func_dict:
print('傻逼,功能不存在')
continue
func_dict[func_choice]()
###################
# teacher_view.py #
###################
from lib import common
from interface import common_interface, teacher_interface
user_info = {'username': None}
def login():
print('欢迎来到登录功能')
username = input('请输入你的用户名>>>')
pwd = input('请输入你的密码>>>')
flag, msg = common_interface.login_interface(username, pwd, 'teacher')
print(msg)
if flag:
user_info['username'] = username
return True
@common.login_auth('teacher')
def choose_course():
print('欢迎来到选择课程功能')
username = user_info['username']
school_list = common.get_obj_list('school')
for ind, school in enumerate(school_list):
print(ind, school)
school_choice = input('请选择你需要获取的学校的序号>>>')
school_name = school_list[int(school_choice)]
course_list = teacher_interface.get_school_course_interface(school_name)
for ind, course in enumerate(course_list):
print(ind, course.name)
course_choice = input('请选择你需要获取课程的序号>>>')
course = course_list[int(course_choice)]
msg = teacher_interface.choose_course_interface(username, course)
print(msg)
@common.login_auth('teacher')
def check_course():
print('欢迎来到查看课程功能')
username = user_info['username']
course_list = teacher_interface.get_teacher_course_interface(username)
for ind, course in enumerate(course_list):
print(ind, course.name)
@common.login_auth('teacher')
def check_student():
print('欢迎来到查看学生功能')
username = user_info['username']
course_list = teacher_interface.get_teacher_course_interface(username)
for ind, course in enumerate(course_list):
print(ind, course.name)
course_choice = input('请输入你需要选择的课程>>>')
course = course_list[int(course_choice)]
course = teacher_interface.get_course(course)
for student in course.student_list:
print(student.name)
@common.login_auth('teacher')
def modify_score():
print('欢迎来到修改成绩功能')
username = user_info['username']
course_list = teacher_interface.get_teacher_course_interface(username)
for ind, course in enumerate(course_list):
print(ind, course.name)
course_choice = input('请输入你需要选择的课程>>>')
course = course_list[int(course_choice)]
course = teacher_interface.get_course(course)
student_list = course.student_list
for ind, student in enumerate(student_list):
print(ind, student.name)
student_choice = input('请输入你需要打分的学生>>>')
student = student_list[int(student_choice)]
score = input('请输入你需要打的分数>>>')
msg = teacher_interface.modify_score_interface(username, course, student, score)
print(msg)
def run():
print('欢迎来到老师视图界面')
msg = '''
1. 登录
2. 选择课程
3. 查看课程
4. 查看学生
5. 修改学生成绩
q. 退出
'''
func_dict = {
'1': login,
'2': choose_course,
'3': check_course,
'4': check_student,
'5': modify_score,
}
while True:
print(msg)
func_choice = input('请选择你需要的功能>>>')
if func_choice == 'q':
break
if func_choice not in func_dict:
print('傻逼,功能不存在')
continue
func_dict[func_choice]()
##########
# src.py #
##########
from core import admin_view,student_view,teacher_view
def run():
print('欢迎来到主视图界面')
func_dict = {
'1': admin_view.run,
'2': teacher_view.run,
'3': student_view.run,
}
msg = '''
1. 管理员视图
2. 老师视图
3. 学生视图
q. 退出
'''
while True:
print(msg)
func_choice = input('请选择你需要的功能>>>')
if func_choice == 'q':
break
if func_choice not in func_dict:
print('傻逼,功能不存在')
continue
func_dict[func_choice]()
db 文件夹
##############
# db_handler #
##############
import os
import pickle
from conf import settings
def save_file(obj):
# 老师可能有nick,学生可能也有nick,如果都保存到db文件,则无法判断这个人为老师还是学生,所以对他们
# 分文件夹处理
# 创建文件夹
classify = obj.__class__.__name__.lower()
dir = os.path.join(settings.DB_PATH, classify)
if not os.path.exists(dir):
os.mkdir(dir)
# 创建文件
filename = os.path.join(dir, obj.name)
with open(filename, 'wb') as fw:
pickle.dump(obj, fw)
def read_file(classify, name):
# 创建文件夹
filename = os.path.join(settings.DB_PATH, classify,name)
if not os.path.exists(filename):
return False
with open(filename, 'rb') as fr:
obj = pickle.load(fr)
return obj
############
# model.py #
############
from db import db_handler
class BaseClass():
def save(self):
db_handler.save_file(self)
@classmethod
def read(cls, name):
obj = db_handler.read_file(cls.__name__.lower(), name)
return obj
class Teacher(BaseClass):
def __init__(self, name, pwd):
self.name = name
self.pwd = pwd
self.course_list = []
self.save()
def add_course(self, course):
self.course_list.append(course)
self.save()
def modify_score(self, student, course, score):
student.course_score[course.name] = score
student.save()
class Student(BaseClass):
def __init__(self, name, pwd):
self.name = name
self.pwd = pwd
self.school = ''
self.course_list = []
self.course_score = dict()
self.save()
def get_school(self):
if self.school:
return self.school
def choose_school(self, school):
self.school = school
self.save()
def add_course(self, course):
self.course_list.append(course)
self.save()
class Course(BaseClass):
def __init__(self, name):
self.name = name
self.student_list = []
self.save()
def add_student(self, student): # course
self.student_list.append(student)
self.save()
# 2. 刚开始学校绑定的课程一定是空的,然后学生添加到课程列表里,但是没有被修改
class School(BaseClass):
def __init__(self, name, addr):
self.name = name
self.addr = addr
self.course_list = []
self.save()
def add_course(self, course):
self.course_list.append(course)
self.save()
class Admin(BaseClass):
def __init__(self, name, pwd):
self.name = name
self.pwd = pwd
self.save()
def create_school(self, name, addr):
School(name, addr)
def create_course(self, name):
Course(name)
def create_teacher(self, name, pwd):
Teacher(name, pwd)
if __name__ == '__main__':
admin = Admin('nick', 123)
admin.create_course('python')
admin.create_teacher('nick', 123)
# classmethod只是让下面的代码变成下下面的代码
# nick = db_handler.read_file('teacher', 'nick') # type:Teacher
# python = db_handler.read_file('course', 'python')
nick = Teacher.read('nick')
python = Course.read('python')
print(nick)
print(nick.name)
nick.add_course(python)
print(nick.__dict__)
interface 目录
######################
# admin_interface.py #
######################
from db import model
# 健壮性/鲁棒性/robust
def register_interface(username, pwd):
obj = model.Admin.read(username)
if obj:
return '用户名已被使用'
model.Admin(username, pwd)
return '注册成功'
def create_school_interface(username,school_name,school_addr):
admin_obj = model.Admin.read(username)
school_obj = model.School.read(school_name)
if school_obj:
return f'{school_name}学校已经存在'
admin_obj.create_school(school_name,school_addr)
return f'{school_name}创建成功'
def create_teacher_interface(username,teacher_username,teacher_pwd):
admin_obj = model.Admin.read(username)
teacher_obj = model.Teacher.read(teacher_username)
if teacher_obj:
return f'{teacher_username}老师已经存在'
admin_obj.create_teacher(teacher_username,teacher_pwd)
return f'{teacher_username}创建成功'
def create_course_interface(username,course_name,school_name):
admin_obj = model.Admin.read(username)
school_obj = model.School.read(school_name)
course_obj = model.Course.read(course_name) # None
# 课程在学校里,None, [] --> [None]
if course_obj in school_obj.course_list:
return f'{course_name}课程已经存在'
admin_obj.create_course(course_name)
course_obj = model.Course.read(course_name)
school_obj.add_course(course_obj) # python对象
return f'{course_name}创建成功'
#######################
# common_interface.py #
#######################
from db import db_handler
def login_interface(username, pwd, classify='admin'):
obj = db_handler.read_file(classify, username)
if not obj:
return False, '用户不存在'
if obj.pwd == pwd:
return True, '登录成功'
return False, '登录失败'
########################
# student_interface.py #
########################
from db import model
def register_interface(username, pwd):
student_obj = model.Student.read(username)
if student_obj:
return f'{username}被注册了'
model.Student(username, pwd)
return f'{username}注册成功'
def choose_school_interface(username, school_name):
student_obj = model.Student.read(username)
school_obj = model.School.read(school_name)
if student_obj.school:
return '已经绑定学校,无法再绑定'
student_obj.choose_school(school_obj)
return f'{username}绑定学校{school_name}成功'
def get_school_course_list(username):
student_obj = model.Student.read(username)
if not student_obj.school:
return '请先选择学校'
school_obj = model.School.read(student_obj.school.name)
return school_obj.course_list
def choose_course_interface(username, course):
student_obj = model.Student.read(username)
if course in student_obj.course_list:
return '课程已经存在'
course = model.Course.read(course.name)
# 课程一直是学校的课程
student_obj.add_course(course)
course.add_student(student_obj)
return f'{username}选课{course.name}成功'
def get_student_course_list(username):
student_obj = model.Student.read(username)
return student_obj.course_list
def check_score_interface(username, course):
student_obj = model.Student.read(username)
score = student_obj.course_score.get(course.name)
return score
########################
# teacher_interface.py #
########################
from db import model
def get_school_course_interface(school_name):
school_obj = model.School.read(school_name)
return school_obj.course_list
def get_teacher_course_interface(username):
teacher_obj = model.Teacher.read(username)
return teacher_obj.course_list
def choose_course_interface(username, course):
teacher_obj = model.Teacher.read(username)
if course in teacher_obj.course_list:
return '课程已经存在'
course = model.Course.read(course.name)
teacher_obj.add_course(course)
return f'老师{username}添加课程{course.name}成功'
def modify_score_interface(username, course, student, score):
teacher_obj = model.Teacher.read(username)
teacher_obj.modify_score(student, course, score)
return f'老师{username}给学生{student.name}课程{course.name}打分{score}'
def get_course(course):
course_obj = model.Course.read(course.name)
return course_obj
lib 文件夹
#############
# commin.py #
#############
import os
from conf import settings
def login_auth(classify):
from core import admin_view, student_view, teacher_view
def outer(func):
def wrapper(*args, **kwargs):
flag = False
if classify == 'admin':
if admin_view.user_info['username']:
res = func(*args, **kwargs)
return res
flag = admin_view.login()
elif classify == 'teacher':
if teacher_view.user_info['username']:
res = func(*args, **kwargs)
return res
flag = teacher_view.login()
elif classify == 'student':
if student_view.user_info['username']:
res = func(*args, **kwargs)
return res
flag = student_view.login()
# 登录成功后运行函数func
if flag:
res = func(*args, **kwargs)
return res
else:
print('登录失败')
return wrapper
return outer
def get_obj_list(name):
obj_list = os.listdir(os.path.join(settings.DB_PATH, name))
return obj_list
run 运行文件
##########
# run.py #
##########
from core import src
if __name__ == '__main__':
src.run()