def add_student(request): """ 添加学生 """ ret = {'status': True, 'message': None} try: name = request.POST.get('name') s = StudentList() s.add_check(request.POST) # 校验 print(request.POST) add_list = list(request.POST.values()) # 待添加的数据 sqlheper.modify("insert into student(name,sex,major_name,class_id) values(%s,%s,%s,%s)", add_list) # 自动生成学号 sid = sqlheper.get_list("select id from student where name=%s order by id DESC", [name, ]) sid = sid[0].get('id') year = ['2017', '2018', '2019', '2020'] # 年份 job = ['320', '321', '322'] # 工号 student_id = str(sid).zfill(3) # 序号,三位数 student_id = year[-1] + job[2] + student_id sqlheper.modify("update student set student_id=%s where id=%s", [student_id, sid]) operationlogs.operationlogs(request.COOKIES.get('name'), 'add', 'student') except Exception as e: # 添加失败返回错误信息 ret['status'] = False ret['message'] = str(e) return HttpResponse(json.dumps(ret))
def edit_student(request): """ 编辑学生信息 """ if request.method == "GET": nid = request.GET.get('nid') sql = "select * from student where id=%s" result = sqlheper.get_list(sql, [nid, ]) class_list = sqlheper.get_list("select id,title from class", []) major = sqlheper.get_list('select major_name from major', []) return render(request, 'edit_student.html', {'result': result[0], 'class_list': class_list, 'major': major}) else: ret = {'status': True, 'message': None} try: s = StudentList() s.edit_check(request.POST) stu_dict = list(request.POST.values()) sql = "update student set name=%s,sex=%s,nation=%s,native_place=%s,birth_date=%s,telephone=%s,major_name=%s,class_id=%s where id=%s" operationlogs.operationlogs(request.COOKIES.get('name'), 'edit', 'student', stu_dict[-1], json.dumps(request.POST)) sqlheper.modify(sql, stu_dict) except Exception as e: ret['status'] = False ret['message'] = str(e) return HttpResponse(json.dumps(ret))
def edit_course(request): """ 编辑课程信息 """ nid = request.GET.get('nid') if request.method == "GET": c = CourseList() sql = "select * from course where id=%s" result = sqlheper.get_list(sql, [nid, ]) return render(request, 'edit_course.html', {'result': result[0], 'teacher_list': c.teacher_list}) else: ret = {'status': True, 'message': None} try: c = CourseList() c.edit_check(request.POST) edit_list = list(request.POST.values()) sql = "update course set course_name=%s,credit=%s,teacher_number=%s where id=%s" operationlogs.operationlogs(request.COOKIES.get('name'), 'edit', 'course', edit_list[-1], json.dumps(request.POST)) sqlheper.modify(sql, edit_list) except Exception as e: ret['status'] = False ret['message'] = str(e) print(e.__traceback__.tb_lineno) return HttpResponse(json.dumps(ret))
def del_student(request): """ 删除学生 """ student_id = request.POST.get('sid') sqlheper.modify('delete from student where id=%s', [student_id, ]) operationlogs.operationlogs(request.COOKIES.get('name'), 'del', 'student', student_id) return HttpResponse('ok')
def del_course(request): """ 删除课程 """ course_id = request.POST.get('cid') sqlheper.modify('delete from course where id=%s', [course_id, ]) operationlogs.operationlogs(request.COOKIES.get('name'), 'del', 'course', course_id) return HttpResponse('ok')
def close_course(request): """ 关闭选课 """ ret = {'status': True, 'message': '关闭成功'} open_list = request.POST.get('check_val') open_list = json.loads(open_list) try: for i in open_list: sql = "update course set status=%s where id=%s" operationlogs.operationlogs(request.COOKIES.get('name'), 'edit', 'course', i['id'], '关闭选课') sqlheper.modify(sql, ['不可选', i['id']]) except Exception as e: ret['status'] = False ret['message'] = str(e) return HttpResponse(json.dumps(ret))
def add_course(request): """ 添加课程 """ ret = {'status': True, 'message': None} try: cname = request.POST.get('course_name') c = CourseList() c.add_check(request.POST) # 校验 add_list = list(request.POST.values()) # 待添加的数据 sqlheper.modify("insert into course(course_name,teacher_number,credit) values(%s,%s,%s)", add_list) cid = sqlheper.get_list("select id from course where course_name=%s order by id", [cname, ]) cid = cid[0].get('id') course_number = 'c' + str(cid) sqlheper.modify("update course set course_number=%s where id=%s", [course_number, cid]) operationlogs.operationlogs(request.COOKIES.get('name'), 'add', 'course') except Exception as e: # 添加失败返回错误信息 ret['status'] = False ret['message'] = str(e) return HttpResponse(json.dumps(ret))