def teacher(request): #登录功能用装饰器实现一下 #teacher_list = get_list('select id,name from teacher') obj = SqlHeper() sql = 'SELECT teacher.id as tid,teacher.name,classes.title from teacher ' \ 'left join teacher2class on teacher.id=teacher2class.teacher_id ' \ 'left join classes on classes.id = teacher2class.class_id' teacher_list = obj.get_list(sql) result = {} for row in teacher_list: tid = row['tid'] if tid in result: result[tid]['titles'].append(row['title']) else: result[tid] = { 'tid': row['tid'], 'name': row['name'], 'titles': [ row['title'], ] } return render(request, "teacher.html", {'teacher_list': result.values()})
def classes(request): sqlhepr = SqlHeper() result = sqlhepr.select_all_data("select * from classes", []) sqlhepr.close_data() context = {} context['classes'] = result return render(request, 'classes.html', context)
def del_class(request, args): obj = SqlHeper() #nid = request.GET.get('nid') nid = args[0] print(nid) obj.modify("delete from classes where id=%s", nid) return redirect('/classes/')
def tearchs(request): sqlhepr = SqlHeper() tearchs_list = sqlhepr.select_all_data( """ select tearch2class.id, tearchs.id as tid ,tearchs.tname,classes.title from tearch2class left join tearchs on tearch2class.tearch_id=tearchs.id left join classes on tearch2class.class_id=classes.id """, []) sqlhepr.close_data() res = {} for data in tearchs_list: tid = data['tid'] if tid in res: res[tid]['titles'].append(data['title']) else: res[tid] = { "tid": data['tid'], 'name': data['tname'], 'titles': [ data['title'], ] } context = {} context['tearchs_list'] = res return render(request, 'tearchs.html', context)
def modal_add_class(request): obj = SqlHeper() title = request.POST.get('title') if len(title): obj.modify('insert into classes(title) value(%s)', [title]) return HttpResponse('OK') else: return HttpResponse('标题不能为空')
def classes(request): #从cookies中获取凭证 #tk=request.COOKIES.get('ticket') tk = request.get_signed_cookie('ticket', salt='9999') if not tk: return redirect('/login/') obj = SqlHeper() class_list = obj.get_list("select id,title from classes") return render(request, 'classes.html', {'class_list': class_list})
def add_tearch(request): sqlhepr = SqlHeper() if request.method == "POST": tearch_name = request.POST.get('add_tearchname') class_ids = request.POST.getlist('class_id') tearch_id = sqlhepr.lastrowid("insert into tearchs (tname) value(%s)", [ tearch_name, ]) class_many_id = map(lambda x: (tearch_id, x), class_ids) sqlhepr.insert_many_data( "insert into tearch2class (tearch_id,class_id) value(%s,%s)", class_many_id) sqlhepr.commit_data() sqlhepr.close_data() return redirect('/tearchs/') else: import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='******', passwd='root', db='test', charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("select * from classes") class_list = cursor.fetchall() context = {} context['class_list'] = class_list return render(request, 'add_tearch.html', context)
def add_class(request): obj = SqlHeper() if request.method == 'GET': return render(request, 'add_class.html') else: v = request.POST.get('title') if len(v) > 0: obj.modify("insert into classes(title) value(%s)", v) return redirect('/classes/') else: return render(request, 'add_class.html', {'msg': '班级名称不能为空'})
def student(request): #登录功能用装饰器实现一下 obj = SqlHeper() sql = 'select student.id,student.name,student.class_id,classes.title from student left join classes on student.class_id=classes.id' student_list = obj.get_list(sql) class_list = obj.get_list('select id,title from classes') return render(request, "student.html", { 'student_list': student_list, 'class_list': class_list })
def add_student(request): obj = SqlHeper() if request.method == 'GET': class_list = obj.get_list("select id,title from classes") return render(request, 'add_student.html', {'class_list': class_list}) else: name = request.POST.get('name') title = request.POST.get('class_id') obj.modify("insert into student(name,class_id) value(%s,%s)", [name, title]) return redirect('/student/')
def modal_add_student(request): obj = SqlHeper() ret = {'status': True, 'msg': None} try: name = request.POST.get('name') class_id = request.POST.get('class_id') obj.modify('insert into student(name,class_id) value(%s,%s)', [name, class_id]) except Exception as e: ret['status'] = False ret['msg'] = str(e) import json return HttpResponse(json.dumps(ret))
def modal_edit_student(request): obj = SqlHeper() ret = {'status': True, 'msg': None} try: name = request.POST.get('name') id = request.POST.get('id') class_id = request.POST.get('class_id') obj.modify('update student set name=%s,class_id=%s where id=%s', [name, class_id, id]) except Exception as e: ret['status'] = False ret['msg'] = str(e) import json ret = json.dumps(ret) return HttpResponse(ret)
def deleteclass(request): nid = request.GET.get('nid') sqlhepr = SqlHeper() sqlhepr.delete_data("delete from classes where id=%s", [ nid, ]) sqlhepr.commit_data() sqlhepr.close_data() return redirect('/classes/')
def modal_edit_class(request): obj = SqlHeper() ret = {'status': True, 'msg': None} try: nid = request.POST.get('nid') title = request.POST.get('title') obj.modify('update classes set title=%s where id=%s', [ title, nid, ]) except Exception as e: ret['status'] = False ret['msg'] = str(e) finally: import json return HttpResponse(json.dumps(ret))
def edit_class(request, *args): obj = SqlHeper() if request.method == 'GET': #nid = request.GET.get('nid') nid = args[0] result = obj.get_one("select id,title from classes where id=%s", [nid]) title = result['title'] return render(request, "edit_class.html", {'nid': nid, 'title': title}) else: #nid = request.POST.get('nid') nid = args[0] title = request.POST.get('title') obj.modify("update classes set title=%s where id=%s", [ title, nid, ]) return redirect('/classes/')
def add_teacher(request): obj = SqlHeper() if request.method == 'GET': class_list = obj.get_list('select id,title from classes') return render(request, 'add_teacher.html', {'class_list': class_list}) else: obj = SqlHeper() name = request.POST.get('name') teacher_id = obj.create('insert into teacher(name) value(%s)', name) class_list = request.POST.getlist('class_ids') lst = [] for cls in class_list: lst.append((teacher_id, cls)) obj.multiple_modify( 'insert into teacher2class(teacher_id,class_id) value(%s,%s)', lst) obj.close() return redirect('/teacher/')
def addclass(request): sqlhepr = SqlHeper() if request.method == 'POST': data = request.POST.get('addclass') sqlhepr.insert_data("insert into classes (title) value(%s)", [data]) sqlhepr.commit_data() sqlhepr.close_data() return redirect('/classes/') else: context = {} return render(request, 'addclass.html', context)
def modal_deleteclass(request): sqlhepr = SqlHeper() nid = request.POST.get('nid') sqlhepr.delete_data("delete from classes where id=%s", [ nid, ]) sqlhepr.commit_data() sqlhepr.close_data() data = {} data['status'] = 'success' return JsonResponse(data)
def deletestudent(request): nid = request.GET.get('nid') sqlhepr = SqlHeper() sqlhepr.delete_data("delete from students where id=%s", [ nid, ]) sqlhepr.commit_data() return redirect('/students/')
def modal_addclass(request): sqlhepr = SqlHeper() data = {} if request.method == 'POST': new_class = request.POST.get('modal_addclass') if new_class.strip() == '': data["status"] = 'error' data['message'] = '输入错误,请重新输入' return JsonResponse(data) else: sqlhepr.insert_data("insert into classes (title) value(%s)", [new_class]) sqlhepr.commit_data() sqlhepr.close_data() data["status"] = 'ok' return JsonResponse(data)
def modal_editorstudent(request): sqlhepr = SqlHeper() try: student_id = request.POST.get('nid') student_name = request.POST.get('student_name') class_id = request.POST.get('class_id') sqlhepr.updata_data( 'update students set name=%s,class_id=%s where id=%s', [student_name, class_id, student_id]) sqlhepr.commit_data() sqlhepr.close_data() context = {} context['status'] = 'success' except Exception as e: context['status'] = 'error' context['message'] = str(e) return JsonResponse(context)
def students(request): sqlhepr = SqlHeper() result = sqlhepr.select_all_data( "select students.id, students.name,students.class_id, classes.title from students left JOIN classes on students.class_id = classes.id", []) class_list = sqlhepr.select_all_data("select id ,title from classes ", []) sqlhepr.close_data() context = {} context['students'] = result context['class_list'] = class_list return render(request, 'students.html', context)
def modal_editorclass(request): sqlhepr = SqlHeper() data = {} data['status'] = 'success' data['message'] = None try: nid = request.GET.get('nid') title = request.GET.get('title') if title.strip() != '': sqlhepr.updata_data("update classes set title=%s where id=%s", [title, nid]) sqlhepr.commit_data() sqlhepr.close_data() else: data['status'] = 'error' data['message'] = '输出的内容不能为空' except Exception as e: data['message'] = e return JsonResponse(data)
def modal_addstudent(request): sqlhepr = SqlHeper() context = {} try: studentname = request.POST.get('studentname') if studentname.strip() == '': context['status'] = 'error' context['message'] = '输入的学生姓名不能为空' return JsonResponse(context) class_id = request.POST.get('class_id') sqlhepr.insert_data("insert into students(name,class_id) value(%s,%s)", [ studentname, class_id, ]) sqlhepr.commit_data() sqlhepr.close_data() context['status'] = 'success' except Exception as e: context['status'] = 'error' context['message'] = str(e) return JsonResponse(context)
def modal_add_teacher(request): ret = {'status': True, 'msg': None} try: teacher_name = request.POST.get('teacher_name') class_id_list = request.POST.getlist('class_id_list') print(teacher_name, class_id_list) obj = SqlHeper() teacher_id = obj.create('insert into teacher(name) value(%s)', [teacher_name]) lst = [] for cls in class_id_list: lst.append((teacher_id, cls)) obj.multiple_modify( 'insert into teacher2class(teacher_id,class_id) value(%s,%s)', lst) obj.close() except Exception as e: ret['status'] = False ret['msg'] = str(e) import json return HttpResponse(json.dumps(ret))
def edit_student(request): obj = SqlHeper() if request.method == 'GET': nid = request.GET.get('nid') sql = 'select id,title from classes' class_list = obj.get_list(sql) sql = 'select id,name,class_id from student where id=%s' result = obj.get_list(sql, [ nid, ], all=False) return render(request, "edit_student.html", { 'class_list': class_list, 'student_info': result }) else: nid = request.GET.get('nid') name = request.POST.get('name') class_id = request.POST.get('class_id') sql = 'update student set name=%s,class_Id=%s where id=%s' obj.modify(sql, [name, class_id, nid]) return redirect('/student/')
def editor(request): nid = request.GET.get('nid') sqlhepr = SqlHeper() if request.method == "POST": name = request.POST.get('editor') class_id = request.POST.get('class_name') sqlhepr.updata_data( "update students set name=%s,class_id=%s where id=%s", [name, class_id, nid]) sqlhepr.commit_data() sqlhepr.close_data() return redirect('/students/') else: result = sqlhepr.select_one_data( "select id,name ,class_id from students where id=%s", [ nid, ]) class_list = sqlhepr.select_all_data("select * from classes", []) sqlhepr.close_data() context = {} context['class_list'] = class_list context['result'] = result return render(request, "editorstudent.html", context)
def modal_add_tearch(request): context = {} if request.method == 'GET': import time time.sleep(1) sqlheper = SqlHeper() class_list = sqlheper.select_all_data("select id,title from classes", []) sqlheper.close_data() context['class_list'] = class_list else: try: t_name = request.POST.get('t_name') class_id_list = request.POST.getlist('class_id_list') sqlheper = SqlHeper() t_id = sqlheper.lastrowid("insert into tearchs (tname)value(%s)", [ t_name, ]) t_c_id = list(map(lambda x: (t_id, x), class_id_list)) sqlheper.insert_many_data( "insert into tearch2class (tearch_id,class_id)value(%s,%s)", t_c_id) sqlheper.commit_data() sqlheper.close_data() context = {} context['status'] = 'success' except Exception as e: context['status'] = 'error' context['message'] = str(e) return JsonResponse(context)
def editor_tearch(request): sqlhepr = SqlHeper() context = {} if request.method == 'POST': t_id = request.GET.get('t_id') #的到的是字符串 t2c_id = request.POST.get('t_c_ids') # 转化为列表 import re compile = re.compile('\d+') t2c_id = compile.findall(t2c_id) editor_class_ids = request.POST.getlist('editor_class_ids') t_name = request.POST.get('t_name') t_c = list(map(lambda x: (t_id, x), editor_class_ids)) sqlhepr.updata_data("update tearchs set tname=%s where id=%s", [t_name, t_id]) for i in t2c_id: sqlhepr.delete_data("delete from tearch2class where id=%s ", [i]) sqlhepr.commit_data() sqlhepr.updata_many_data( "insert into tearch2class (tearch_id,class_id)value(%s,%s)", t_c) sqlhepr.commit_data() sqlhepr.close_data() return redirect('/tearchs/') else: tid = request.GET.get('tid') tearch_name = sqlhepr.select_one_data( "select tname from tearchs where id=%s ", [tid]) current_class_list = sqlhepr.select_all_data( "select id ,class_id from tearch2class where tearch_id=%s", [tid]) tearch_class_ids = list(map(lambda x: x['id'], current_class_list)) current_class_ids = list( map(lambda x: x.get('class_id'), current_class_list)) class_list = sqlhepr.select_all_data("select id,title from classes ", []) sqlhepr.close_data() context['tearch_name'] = tearch_name context['current_class_ids'] = current_class_ids context['class_list'] = class_list context['tearch_class_ids'] = tearch_class_ids context['t_id'] = tid return render(request, 'editor_tearch.html', context)
def addstudent(request): sqlhepr = SqlHeper() if request.method == 'POST': student_name = request.POST.get('addstudent') class_id = request.POST.get('class_name') sqlhepr.insert_data( "insert into students (name,class_id) value(%s,%s)", [student_name, class_id]) sqlhepr.commit_data() sqlhepr.close_data() return redirect('/students/') else: class_list = sqlhepr.select_all_data("select * from classes", []) context = {} context['class_list'] = class_list sqlhepr.close_data() return render(request, 'addstudent.html', context)