def modify_paper_prolist(request):
    postjson = jh.post2json(request)
    action = postjson['action']
    ret = {'code': 404, 'info': 'unknown action ' + action}
    ph = PaperHelper()
    sh = StoreHelper()

    if action == 'addpro':
        # add problem given in POST packet to paper
        paperid = postjson['paperid']
        problem = postjson['problem']
        # fetch original problem list from database
        paperdb = Paper.objects.get(pid=paperid)
        original_prolist = json.loads(paperdb.prolist)
        ph.AddPro(original_prolist, problem["problem"], problem["ptype"], problem["point"],
                  problem["right"], problem["wrong1"], problem["wrong2"], problem["wrong3"])
        paperdb.prolist = json.dumps(original_prolist)
        paperdb.save()
        ret = {'code': 200, 'info': 'ok'}

    elif action == 'add_from_store':
        # add problem given in POST packet to paper
        paperid = postjson['paperid']
        problem = postjson['problem']
        store = postjson['storeid']
        newpro = postjson['newpro']
        # fetch original problem list from database
        paperdb = Paper.objects.get(pid=paperid)
        storedb = Teststore.objects.get(storeid=store)
        original_prolist = json.loads(paperdb.prolist)
        storeprolist = json.loads(storedb.prolist)
        question = sh.GetPro(storeprolist, problem)
        ph.AddPro(original_prolist, question["problem"], question["type"], newpro["point"],
                  question["right"], question["wrong1"], question["wrong2"], question["wrong3"])
        paperdb.prolist = json.dumps(original_prolist)
        paperdb.save()
        ret = {'code': 200, 'info': 'ok'}

    elif action == 'delpro':
        # delete problem given in POST packet from paper
        paperid = postjson['paperid']
        problem = postjson['problem']
        paperdb = Paper.objects.get(pid=paperid)
        original_prolist = json.loads(paperdb.prolist)
        ph.DelPro(original_prolist, problem)
        paperdb.prolist = json.dumps(original_prolist)
        paperdb.save()
        ret = {'code': 200, 'paper': 'ok'}

    elif action == 'delall':
        paperid = postjson['paperid']
        paperdb = Paper.objects.get(pid=paperid)
        paperdb.prolist = json.dumps(ph.CreateProList())
        paperdb.save()
        ret = {'code': 200, 'paper': 'ok'}

    return HttpResponse(json.dumps(ret), content_type="application/json")
Exemple #2
0
def upload_prolist(request):
    ret = {'code': 403, 'info': 'denied method ' + request.method}
    ph = PaperHelper()

    if request.method == 'POST':
        # acquire paperid from form
        paperid = request.POST.get('paperid')
        obj = request.FILES.get('file')
        paperdb = Paper.objects.get(pid=paperid)
        original_prolist = json.loads(paperdb.prolist)

        # acquire file from form
        obj = request.FILES.get('file')
        save_path = os.path.join(settings.BASE_DIR, 'upload.xls')
        #print(save_path)
        f = open(save_path, 'wb')
        for chunk in obj.chunks():
            f.write(chunk)
        f.close()

        # read the xls file and load problems
        x1 = xlrd.open_workbook(save_path)
        sheet1 = x1.sheet_by_name("Sheet1")
        line = 3
        while line <= 50 and line < sheet1.nrows:
            if sheet1.cell_value(line, 0) == "":
                break
            #print(sheet1.cell_value(line, 0))
            problem = str(sheet1.cell_value(line, 0))
            ptype = str(sheet1.cell_value(line, 1))
            if ptype == '主观题':
                ptype = 'zhuguan'
            else:
                ptype = 'keguan'
            point = int(sheet1.cell_value(line, 2))
            right = str(sheet1.cell_value(line, 3))
            wrong1 = str(sheet1.cell_value(line, 4))
            wrong2 = str(sheet1.cell_value(line, 5))
            wrong3 = str(sheet1.cell_value(line, 6))
            ph.AddPro(original_prolist, problem, ptype, point, right, wrong1,
                      wrong2, wrong3)
            paperdb.prolist = json.dumps(original_prolist)
            line += 1

        paperdb.save()
        '''
    paperdb = Paper.objects.get(pid = paperid)
    original_prolist = json.loads(paperdb.prolist)
    ph.AddPro(original_prolist, problem["problem"], problem["ptype"], problem["point"],
     problem["right"], problem["wrong1"], problem["wrong2"], problem["wrong3"])
    paperdb.prolist = json.dumps(original_prolist)
    paperdb.save()
    '''

        # delete file after used
        os.remove(save_path)
        ret = {'code': 200, 'info': 'ok'}
        pass

    return HttpResponse(json.dumps(ret), content_type="application/json")
def auto_save(request):
    ret = {'code': 403, 'info': 'denied method ' + request.method}
    ph = PaperHelper()

    if request.method == 'POST':
        # acquire paperid from form
        postjson = jh.post2json(request)
        print(postjson)
        paperid = postjson['paperid']
        storeid = postjson['storeid']
        print(paperid)
        paperdb = Paper.objects.get(pid=paperid)
        original_prolist = json.loads(paperdb.prolist)
        prolist = postjson['prolist']



        for pro in prolist:
            if pro['valid'] == 'false':
                continue
            problem = pro['problem']
            ptype = pro['type']
            if ptype == '主观题':
                ptype = 'zhuguan'
            else:
                ptype = 'keguan'
            point = pro['point']
            right = pro['right']
            wrong1 = pro['wrong1']
            wrong2 = pro['wrong2']
            wrong3 = pro['wrong3']
            ph.AddPro(original_prolist, problem, ptype, point, right, wrong1, wrong2, wrong3)
            paperdb.prolist = json.dumps(original_prolist)

        paperdb.save()
        '''
    paperdb = Paper.objects.get(pid = paperid)
    original_prolist = json.loads(paperdb.prolist)
    ph.AddPro(original_prolist, problem["problem"], problem["ptype"], problem["point"],
     problem["right"], problem["wrong1"], problem["wrong2"], problem["wrong3"])
    paperdb.prolist = json.dumps(original_prolist)
    paperdb.save()
    '''

        # delete file after used
        ret = {'code': 200, 'info': 'ok'}
        pass

    return HttpResponse(json.dumps(ret), content_type="application/json")