在遇到一个项目之前呢,需要做以下步骤
1,分析项目需要完成那些功能需求
2,抽象出模型,写到models,基于model中的对象,分析有哪些属性,写到__init__方法中,需要打印某些对象的信息的时候,可以写在__str__,某些不依赖于具体实例的方法,如查看新建了多少个实例,可以写为如下:
class BaseModel:
def save(self):
"""
使用pickle将用户对象保存到文件
:return:
"""
nid = str(self.nid)
file_path = os.path.join(self.db_path,nid)
print("step5")
print("file_path:"+file_path)
pickle.dump(self,open(file_path,'wb'))
class Admin(BaseModel):
db_path =settings.ADMIN_DB
def __init__(self,username,password):
self.nid =identifier.AdminNid(Admin.db_path)
self.username = username
self.password = password
self.create_time = time.strftime('%Y-%m-%d')
@staticmethod
def login(user,pwd):
"""
管理员登陆,去遍历/db/admin/,pickle.load()
for item 循环/db/admin/所有文件:
obj = pickle.load(open(os.path.join(/db/admin/, item)))
if user == obj.username and pwd == obj.password:
return obj
return None
:param user: 管理员用户名
:param pwd: 管理员密码
:return: 如果登陆成功,获取管理员对象,否则 None
"""
class School(BaseModel):
db_path = settings.SCHOOL_DB
def __init__(self,name):
self.nid = identifier.SchoolNid(School.db_path)
self.schoolName = name
self.income = 0
print("step4")
def __str__(self):
return self.schoolName
@staticmethod
def get_all_list():
ret=[]
for item in os.listdir(os.path.join(School.db_path)):
obj = pickle.load(open(os.path.join(School.db_path,item),'rb'))
ret.append(obj)
return ret
class Teacher(BaseModel):
db_path = settings.TEACHER_DB
def __init__(self,name,level):
"""
:param name:老师姓名
:param level:老师级别
"""
self.nid = identifier.TeacherNid(Teacher.db_path)
self.name = name
self.level = level
def __str__(self):
return "老师的名字为:"+self.name+",老师的等级为:"+self.level
@staticmethod
def get_all_list():
ret=[]
for item in os.listdir(os.path.join(Teacher.db_path)):
obj = pickle.load(open(os.path.join(Teacher.db_path,item),'rb'))
ret.append(obj)
return ret
class Course(BaseModel):
db_path = settings.COURSE_DB
def __init__(self,name,price,priod,school_id):
'''
:param name:课程名
:param price: 课程价格
:param priod: 课程周期
:param school_id:关联学校Id,学校ID具有get_obj_by_uuid方法,以此获取学校对象(其中包含学校信息)
'''
self.nid = identifier.CourseNid(Course.db_path)
self.courseName = name
self.coursePrice = price
self.coursePeriod = priod
self.schoolId = school_id
def __str__(self):
return "课程名:%s;课程价格:%s;课程周期:%s;所属学校:%s"%(self.courseName,self.coursePrice,self.coursePeriod,self.schoolId.get_obj_by_uuid().schoolName)
@staticmethod
def get_all_list():
"""
获取所有课程对象
:return:
"""
ret=[]
for item in os.listdir(os.path.join(Course.db_path)):
obj = pickle.load(open(os.path.join(Course.db_path,item),'rb'))
ret.append(obj)
return ret
class CourseToTeacher(BaseModel):
db_path = settings.COURSE_TO_TEACHER_DB
def __init__(self,course_id,teacher_id):
self.nid = identifier.CourseToTeacherNid(CourseToTeacher.db_path)
self.courseId = course_id
self.teacherId = teacher_id
@staticmethod
def course_teacher_list():
ret=[]
for item in os.listdir(os.path.join(CourseToTeacher.db_path)):
obj = pickle.load(open(os.path.join(CourseToTeacher.db_path,item),'rb'))
ret.append(obj)
return ret
class Classes(BaseModel):
db_path = settings.CLASSES_DB
def __init__(self,name,tuition,school_id,course_to_teacher_list):
"""
班级
:param name:班级名
:param tuition: 学费
:param school_id: 学校NID
:param course_to_teacher_list:[CourseToTeacher,CourseToTeacher,]
"""
self.nid = identifier.ClassesNid(Classes.db_path)
self.name = name
self.tuition = tuition
self.schoolId = school_id
self.courseToTeacherList = course_to_teacher_list
模型层完成之后,编写service层,通过service层创建对象实例,如下:
from XuanKe.src.models import School
from XuanKe.src.models import Teacher
from XuanKe.src.models import Course
from XuanKe.src.models import CourseToTeacher
from XuanKe.src.models import Classes
def show_choice():
show ="""
0,查看菜单
1,创建学校
2,查看学校
3,创建老师
4,查看老师
5,创建课程
6,查看课程
7,创建老师课程对应关系
8,查看老师课程对应关系
9,创建班级
"""
print(show)
def create_school():
name = input('请输入学校名称')
obj = School(name)
obj.save()
def show_school():
print("=======学校=======")
school_list = School.get_all_list()
for obj in school_list:
print(obj.schoolName)
def create_teacher():
name = input("请输入老师姓名")
level = input("请输入老师职级")
teacher_list = Teacher.get_all_list()
for obj in teacher_list:
if name == obj.name and level == obj.level:
print('老师已存在,请重新输入')
name=''
level=''
break
if len(name)>0 and len(level)>0:
obj = Teacher(name, level)
obj.save()
def show_teacher():
print("======老师=======")
teacher_list=Teacher.get_all_list()
for obj in teacher_list:
print("测试执行")
print('老师的姓名为:'+obj.name+','+'老师的职级为:'+obj.level)
def create_course():
print('==========创建课程==============')
school_list = School.get_all_list()
for k,obj in enumerate(school_list,1):
print(k,obj)
sid = input('请选择学校:')
sid = int(sid)
school_obj = school_list[sid - 1]
name = input('请输入课程名称:')
price = input('请输入课程价格:')
period = input('请输入课程周期:')
obj = Course(name,price,period,school_obj.nid)
obj.save()
print('课程【%s】创建成功' % name)
def show_course():
print("====查看课程========")
course_list = Course.get_all_list()
for item in course_list:
print(item.courseName,item.coursePrice,item.coursePeriod,item.schoolId.get_obj_by_uuid().schoolName)
def create_course_teacher():
"""
为课程分配老师
:return:
"""
print('============创建老师和课程对应关系')
course_list = Course.get_all_list()
for k,obj in enumerate(course_list,1):
print(k,obj)
cid = input('请选择课程:')
cid = int(cid)
course_id = course_list[cid-1]
teacher_list = Teacher.get_all_list()
for k,obj in enumerate(teacher_list,1):
print(k,obj)
tid = input('请选择老师姓名:')
tid = int(tid)
teacher_id =teacher_list[tid-1]
obj = CourseToTeacher(course_id,teacher_id)
obj.save()
def create_classes():
print('============创建班级==============')
"""
班级
:param name:班级名
:param tuition: 学费
:param school_id: 学校NID
:param course_to_teacher_list:[CourseToTeacher,CourseToTeacher,]
"""
name = input('请输入班级名称:')
tuition = input("请输入学费:")
school_list = School.get_all_list()
for k, obj in enumerate(school_list, 1):
print(k, obj)
sid = input('请选择学校')
school_id = int(sid)
CourseToTeacher_list = CourseToTeacher.course_teacher_list()
for k,obj in enumerate(CourseToTeacher_list,1):
print(k,obj)
inp = input("请选择课程以及对应的老师:")
inp = int(inp)
courseToTeacherList=[]
courseToTeacherList.append(inp)
obj = Classes(name,tuition,school_id,courseToTeacherList)
obj.save()
def show_course_teacher():
print("==============查看课程和老师的关系==========")
course_teacher_list = CourseToTeacher.course_teacher_list()
for item in course_teacher_list:
print("课程的名称为:"+item.courseId.courseName+",对应的老师的姓名为:"+item.teacherId.name)
def main():
print("step2")
choice_dict={
'0':show_choice,
'1':create_school,
'2':show_school,
'3':create_teacher,
'4':show_teacher,
'5':create_course,
'6':show_course,
'7':create_course_teacher,
'8':show_course_teacher,
'9':create_classes
}
show_choice()
while True:
inp = input('请输入选项:')
if inp not in choice_dict:
print('选项错误,请重新选择')
continue
func = choice_dict[inp]
result = func()
最后编写bin目录的代码进行项目的启动,作为整个系统的启动入口:
from XuanKe.src.service import admin_service
def execute():
admin_service.main()
if __name__ == '__main__':
print("step1")
execute()
编写完成之后,项目结构如下:
更多测试交流欢迎访问:http://www.bcbxhome.com