一、start.py
import sys, os
path = os.path.dirname(__file__)
sys.path.append(path)
from core import src
if __name__ == '__main__':
src.run()
二、src.py
from core.admin import admin_view
from core.teacher import teacher_view
from core.student import student_view
func_dic={
'1':student_view,
'2':teacher_view,
'3':admin_view
}
def run():
while True:
print('''
1 学生视图
2 老师视图
3 管理员视图
''')
choice = input('please choice(q to quit)>>')
if choice == 'q': break
if choice not in func_dic:continue
func_dic[choice]()
1、admin.py
from interface import admin_interface, common_interface
from lib.common import login_auth
admin_info = {'name': None}
def admin_register():
name = input('please input your name>>:')
password = input('please input your password>>:')
re_password = input('please confirm your password>>:')
if password == re_password:
_, msg = admin_interface.admin_register(name, password)
print(msg)
else:
print('两次密码不一致')
def admin_login():
print('管理员登录')
while True:
name = input('please input your name>>:')
password = input('please input your password>>:')
flag, msg = common_interface.common_login(name, password, 'admin')
if flag:
admin_info['name'] = name
print(msg)
break
else:
print(msg)
@login_auth(type_str='admin')
def create_school():
print('创建学校')
while True:
school_name = input('请输入学校名字:')
school_addr = input('请输入学校地址:')
flag, msg = admin_interface.create_school(school_name, school_addr)
if flag:
print(msg)
break
else:
print(msg)
@login_auth(type_str='admin')
def create_teacher():
print('创建老师')
while True:
name = input('请输入老师名字:')
flag, msg = admin_interface.create_teacher(name)
if flag:
print(msg)
break
else:
print(msg)
@login_auth(type_str='admin')
def create_course():
print('创建课程')
while True:
school_list = admin_interface.check_all_school()
for i, school in enumerate(school_list):
print('%s : %s' % (i, school))
choice = input('请选择学校(按数字):')
if choice.isdigit():
if 0 <= int(choice) < len(school_list):
name = input('请输入课程名字:')
school_name = school_list[int(choice)]
flag, msg = admin_interface.create_course(school_name, name)
if flag:
print(msg)
break
else:
print(msg)
func_dic = {
'1': admin_register,
'2': admin_login,
'3': create_school,
'4': create_teacher,
'5': create_course
}
def admin_view():
while True:
print('''
1、注册
2、登录
3、创建学校
4、创建老师
5、创建课程
''')
choice = input('please choice(q to quit)>>')
if choice == 'q': break
if choice not in func_dic: continue
func_dic[choice]()
2、teacher.py
from lib.common import login_auth
from interface import common_interface, teacher_interface
teacher_info = {'name': None}
def teacher_login():
print('老师登录')
while True:
name = input('请输入用户名:')
password = input('请输入密码:')
flag, msg = common_interface.common_login(name, password, 'teacher')
if flag:
teacher_info['name'] = name
print(msg)
break
else:
print(msg)
@login_auth(type_str='teacher')
def check_course():
print('查看教授课程')
course_list = teacher_interface.get_course_list(teacher_info['name'])
for i, course in enumerate(course_list):
print('%s:%s' % (i, course))
@login_auth(type_str='teacher')
def choose_course():
course_list = teacher_interface.get_all_course()
for i, course in enumerate(course_list):
print('%s : %s ' % (i, course))
choice = input('请选择课程(按数字):')
if choice.isdigit():
if 0 <= int(choice) < len(course_list):
course_name = course_list[int(choice)]
msg = teacher_interface.choose_course(teacher_info['name'], course_name)
print(msg)
@login_auth(type_str='teacher')
def check_student():
print('查看学生')
while True:
course_list = teacher_interface.get_course_list(teacher_info['name'])
for i, course in enumerate(course_list):
print('%s:%s' % (i, course))
choice = input('请选择要查看的课程:')
if choice.isdigit():
if 0 <= int(choice) < len(course_list):
course_name = course_list[int(choice)]
student_list = teacher_interface.check_students_by_course(course_name)
for i, student in enumerate(student_list):
print('%s : %s ' % (i, student))
break
else:
print('输入有误,请重新输入')
else:
print('请输入数字')
@login_auth(type_str='teacher')
def modify_score():
'''
1 打印老师教授的所有课程
2 打印选择课程下的学生
3 修改成绩
'''
print('修改成绩')
while True:
course_list = teacher_interface.get_course_list(teacher_info['name'])
for i, course in enumerate(course_list):
print('%s:%s' % (i, course))
choice = input('请选择要查看的课程:')
if choice.isdigit():
if 0 <= int(choice) < len(course_list):
course_name = course_list[int(choice)]
student_list = teacher_interface.check_students_by_course(course_name)
for i, student in enumerate(student_list):
print('%s : %s ' % (i, student))
choice_stu = input('请输入要修改的学生(按数字):')
if choice_stu.isdigit():
if 0 <= int(choice_stu) < len(student_list):
score = input('请输入分数:')
msg = teacher_interface.change_score(student_list[int(choice_stu)], score, course_name,
teacher_info['name'])
print(msg)
break
else:
print('数字应该在范围内')
else:
print('请输入数字')
else:
print('输入有误,请重新输入')
else:
print('请输入数字')
func_dic = {
'1': teacher_login,
'2': check_course,
'3': choose_course,
'4': check_student,
'5': modify_score
}
def teacher_view():
while True:
print('''
1、登录
2、查看教授课程
3、选择教授课程
4、查看课程下学生
5、修改学生成绩
''')
choice = input('please choice(q to quit)>>')
if choice == 'q': break
func_dic[choice]()
3、student.py
from interface import student_interface, common_interface,admin_interface
from lib.common import login_auth
student_info = {'name': None}
def student_register():
print('学生注册')
while True:
name = input('please input your name>>:')
password = input('please input your password>>:')
re_password = input('please confirm your password>>:')
if password == re_password:
_, msg = student_interface.student_register(name, password)
print(msg)
break
else:
print('两次密码不一致')
def student_login():
print('学生登录')
while True:
name = input('请输入用户名:')
password = input('请输入密码:')
flag, msg = common_interface.common_login(name, password, 'student')
if flag:
student_info['name'] = name
print(msg)
break
else:
print(msg)
@login_auth(type_str='student')
def choose_school():
print('选择学校')
while True:
school_list = admin_interface.check_all_school()
for i, school in enumerate(school_list):
print('%s : %s' % (i, school))
choice = input('请选择学校(按数字):')
if choice.isdigit():
if 0 <= int(choice) < len(school_list):
flag,msg=student_interface.choose_school(student_info['name'],school_list[int(choice)])
if flag:
print(msg)
break
else:
print(msg)
@login_auth(type_str='student')
def choose_course():
print('选择课程')
flag,course_list=student_interface.get_can_choose_course(student_info['name'])
if flag:
if course_list:
for i,course in enumerate(course_list):
print('%s : %s'%(i,course))
choice=input('请选择课程:')
if choice.isdigit():
if 0<=int(choice)<len(course_list):
msg=student_interface.choose_course(student_info['name'],course_list[int(choice)])
print(msg)
else:
print(course_list)
@login_auth(type_str='student')
def check_score():
print('查看成绩')
score=student_interface.get_score(student_info['name'])
print(score)
func_dic = {
'1': student_register,
'2': student_login,
'3': choose_school,
'4': choose_course,
'5': check_score
}
def student_view():
while True:
print('''
1、注册
2、登录
3、选择校区
4、选择课程
5、查看成绩
''')
choice = input('please choice(q to quit)>>')
if choice == 'q': break
func_dic[choice]()
三、common.py
import os
def login_auth(type_str):
'''
:param type_str: admin,student,teacher
:return:
'''
def wrapper(func):
def inner(*args, **kwargs):
from core import admin, student, teacher
if type_str == 'admin':
if admin.admin_info['name']:
return func(*args, **kwargs)
else:
print('您没有登录')
admin.admin_login()
elif type_str == 'teacher':
if teacher.teacher_info['name']:
return func(*args, **kwargs)
else:
print('您没有登录')
teacher.teacher_login()
else:
if student.student_info['name']:
return func(*args, **kwargs)
else:
print('您没有登录')
student.student_login()
return inner
return wrapper
def get_all_file_name(path):
'''
获取当前路径下所有文件的名字,返回名字列表
:param path:
:return:
'''
return os.listdir(path)
四、settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DB = os.path.join(BASE_DIR, 'db')
五、db_handler.py
import pickle
import os
from conf import settings
def save(obj):
name = obj.__class__.__name__.lower()
path = os.path.join(settings.BASE_DB, name)
if not os.path.isdir(path):
os.mkdir(path)
path_obj = os.path.join(path, obj.name)
with open(path_obj, 'wb') as f:
pickle.dump(obj, f)
f.flush()
def select(name, type_str):
path = os.path.join(settings.BASE_DB, type_str)
if not os.path.isdir(path):
os.mkdir(path)
path_obj = os.path.join(path, name)
if os.path.exists(path_obj):
with open(path_obj, 'rb') as f:
return pickle.load(f)
else:
return False
六、models.py
from db import db_handler
class BaseClass():
def save(self):
db_handler.save(self)
@classmethod
def get_obj_by_name(cls, name):
type_str = cls.__name__.lower()
return db_handler.select(name, type_str)
class Admin(BaseClass):
def register(self, name, password):
self.name = name
self.password = password
self.save()
class School(BaseClass):
def __init__(self, school_name, school_addr):
self.name = school_name
self.addr = school_addr
self.course_list = []
def add_course(self, course):
self.course_list.append(course)
self.save()
class Teacher(BaseClass):
def __init__(self, name, password):
self.name = name
self.password = password
self.course_list = []
def choose_course(self, course_name):
self.course_list.append(course_name)
self.save()
def change_score(self, stu_obj, course_name, score):
stu_obj.score[course_name] = score
stu_obj.save()
class Course(BaseClass):
def __init__(self, name):
self.name = name
self.student_list = []
class Student(BaseClass):
def __init__(self, name, password):
self.name = name
self.password = password
self.score = {}
self.school = None
self.course_list=[]
def choose_school(self, school_name):
self.school = school_name
self.save()
七、interface
1、common_interface.py
from db import models
def common_login(name, password,type_str):
if type_str=='admin':
obj = models.Admin.get_obj_by_name(name)
elif type_str=='student':
obj = models.Student.get_obj_by_name(name)
else:
obj = models.Teacher.get_obj_by_name(name)
if obj:
if password == obj.password:
return True, '登录成功'
else:
return False, '密码错误'
else:
return False, '账号不存在,请先注册'
2、admin_interface.py
from db import models
from conf import settings
import os
from lib import common
def admin_register(name, password):
admin_obj = models.Admin.get_obj_by_name(name)
if admin_obj:
return False, '用户存在'
admin_obj = models.Admin()
admin_obj.register(name, password)
return True, '注册成功'
def create_school(school_name, school_addr):
obj = models.School.get_obj_by_name(school_name)
if obj:
return False, '学校已经存在'
else:
obj = models.School(school_name, school_addr)
obj.save()
return True, '创建成功'
def create_teacher(name, password='1234'):
obj = models.Teacher.get_obj_by_name(name)
if obj:
return False, '老师已经存在'
else:
obj = models.Teacher(name, password)
obj.save()
return True, '创建成功'
def create_course(school_name, course_name):
obj = models.Course.get_obj_by_name(course_name)
if obj:
return False, '课程已经存在'
else:
obj = models.Course(course_name)
obj.save()
school_obj = models.School.get_obj_by_name(school_name)
school_obj.add_course(course_name)
return True, '课程创建成功'
def check_all_school():
path = os.path.join(settings.BASE_DB, 'school')
return common.get_all_file_name(path)
3、teacher_interface.py
from db import models
import os
from conf import settings
from lib import common
def get_course_list(name):
obj = models.Teacher.get_obj_by_name(name)
if obj:
return obj.course_list
else:
return []
def choose_course(teacher_name, course_name):
obj=models.Teacher.get_obj_by_name(teacher_name)
if obj:
obj.choose_course(course_name)
return '选课成功'
return '老师不存在'
def get_all_course():
path = os.path.join(settings.BASE_DB, 'course')
return common.get_all_file_name(path)
def check_students_by_course(course_name):
'''
通过课程名字,获取所有学生
:param course_name:
:return:
'''
obj=models.Course.get_obj_by_name(course_name)
return obj.student_list
def change_score(stu_name,score,course_name,teacher_name):
obj=models.Teacher.get_obj_by_name(teacher_name)
stu_obj=models.Student.get_obj_by_name(stu_name)
obj.change_score(stu_obj,course_name,score)
return '修改成功'
4、student_interface.py
from db import models
from conf import settings
import os
from lib import common
def student_register(name, password):
obj = models.Student.get_obj_by_name(name)
if obj:
return False, '用户存在'
obj = models.Student(name, password)
obj.save()
return True, '注册成功'
def choose_school(student_name, school_name):
obj = models.Student.get_obj_by_name(student_name)
if obj.school:
return False, '已经选择过学校了'
else:
obj.choose_school(school_name)
return True, '选择学校成功'
def get_score(name):
obj = models.Student.get_obj_by_name(name)
return obj.score
def get_can_choose_course(name):
obj = models.Student.get_obj_by_name(name)
if obj.school:
school_obj = models.School.get_obj_by_name(obj.school)
return True, school_obj.course_list
else:
return False, '请先选择学校'
def choose_course(stu_name, course_name):
stu_obj = models.Student.get_obj_by_name(stu_name)
stu_obj.course_list.append(course_name)
stu_obj.save()
course_obj = models.Course.get_obj_by_name(course_name)
course_obj.student_list.append(stu_name)
course_obj.save()
return '选课成功'