Exemplo n.º 1
0
def tearchers(request):
    obj = SQLHelper()
    tearcher_list = obj.get_list(
        '''
        SELECT tearcher.tid,tearcher.tname,tearch2class.cid,class.name from tearcher  
        LEFT JOIN tearch2class on tearcher.tid = tearch2class.tid    
        LEFT JOIN class ON class.id = tearch2class.cid;
    ''', [])
    result = {}
    for tearcher in tearcher_list:
        tid = tearcher['tid']
        if tid in result:
            result[tid]['classes'].append({
                'cid': tearcher['cid'],
                'name': tearcher['name']
            })
        else:
            result[tid] = {
                'tid': tearcher['tid'],
                'tname': tearcher['tname'],
                'classes': [{
                    'cid': tearcher['cid'],
                    'name': tearcher['name']
                }],
            }
    class_list = obj.get_list('select * from class')
    obj.close()
    return render(request, 'tearchers.html', {
        'result': result.values(),
        "class_list": class_list
    })
Exemplo n.º 2
0
def students(request):
    obj = SQLHelper()
    student_list = obj.get_list(
        "select student.id,student.sname,class.name,student.cid from student left join class on student.cid = class.id",
        [])
    class_list = obj.get_list('select id,name from class', [])
    obj.close()
    return render(request, "students.html", {
        "student_list": student_list,
        'class_list': class_list
    })
Exemplo n.º 3
0
def edit_student(request):
    if request.method == "GET":
        id = request.GET.get("id")
        obj = SQLHelper()
        class_list = obj.get_list("select id,name from class", [])
        student = obj.get_one(
            "select student.id,student.sname,student.cid,class.name from student left join class on  student.cid = class.id where student.id = %s",
            id)
        obj.close()
        return render(request, "edit_student.html", {
            "student": student,
            "class_list": class_list
        })
    else:
        id = request.POST.get("id")
        sname = request.POST.get("sname")
        cid = request.POST.get("class_id")
        obj = SQLHelper()
        obj.modify("update student set sname = %s,cid = %s where id = %s", [
            sname,
            cid,
            id,
        ])
        obj.close()
        return redirect("/students/")
Exemplo n.º 4
0
def editTearcher(request):

    if request.method == 'GET':
        tid = request.GET.get('tid')
        obj = SQLHelper()
        tearcher_info = obj.get_one(
            'select tid,tname from tearcher where tid = %s', [tid])
        tearcher_class_list = obj.get_list(
            'select cid from tearch2class where tearch2class.tid = %s', [
                tid,
            ])
        class_list = obj.get_list('select id,name from class')
        obj.close()
        temp = []
        for i in tearcher_class_list:
            temp.append(i['cid'])
        return render(
            request, 'edit_tearcher.html', {
                'info': tearcher_info,
                'tearcher_class_list': temp,
                'class_list': class_list
            })
    else:
        tid = request.GET.get('tid')
        class_ids = request.POST.getlist('class_ids')
        tname = request.POST.get('tname')
        #更新老师表
        obj = SQLHelper()
        obj.modify('update tearcher set tname=%s where tid = %s', [tname, tid])
        #更细老师班级关系表
        # 方案一:先删除老师和班级的对应关系删除,之后再做添加
        obj.modify('delete from tearch2class where tid = %s', [tid])
        sql_args = []
        for cid in class_ids:
            temp = (tid, cid)
            sql_args.append(temp)
        obj.multiple_modify(
            'insert into tearch2class(tid, cid) values (%s,%s)', sql_args)
        obj.close()
        # 方案二:把新的班级数据和老的数据做对比进行删除和增加
        return redirect('/tearchers/')
Exemplo n.º 5
0
def addStudent(request):
    if request.method == "GET":
        obj = SQLHelper()
        classes = obj.get_list("select id,name from class")
        obj.close()
        return render(request, "addStudent.html", {'classes': classes})
    else:

        class_id = request.POST.get("class_id")
        sname = request.POST.get("sname")

        obj = SQLHelper()
        obj.modify("insert into student( sname, cid) value (%s,%s)",
                   [sname, class_id])
        obj.close()
        return redirect("/students/")
Exemplo n.º 6
0
def addtearchers(request):
    if request.method == 'GET':
        obj = SQLHelper()
        class_list = obj.get_list('select * from class')
        obj.close()
        return render(request, 'addTearcher.html', {"class_list": class_list})
    else:
        tname = request.POST.get('tname')
        clds = request.POST.getlist('class_ids')
        obj = SQLHelper()
        #老师表中添加数据 #获取老师id
        tearcher_id = obj.create('insert into tearcher(tname) values (%s)',
                                 [tname])
        #老师班级表中插入数据
        sql_args = []
        for cid in clds:
            temp = (tearcher_id, cid)
            sql_args.append(temp)
        obj.multiple_modify(
            'insert into tearch2class(tid, cid) values (%s,%s)', sql_args)
        obj.close()

        return redirect('/tearchers/')
Exemplo n.º 7
0
def classes(request):
    obj = SQLHelper()
    class_list = obj.get_list("select id,name from class")
    obj.close()
    return render(request, 'classes.html', {"class_list": class_list})