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")
示例#2
0
def manage_paper(request):
    postjson = jh.post2json(request)
    action = postjson['action']
    ret = {'code': 404, 'info': 'unknown action ' + action}
    ph = PaperHelper()

    if action == 'create':
        # TODO(LOW): verify if the specified paper name is used
        ###
        # get paper name and initialize the new paper with a time id
        database = Paper(pid=time.strftime('%Y%m%d%H%M%S',
                                           time.localtime(time.time())),
                         pname=postjson['papername'],
                         teaname=request.session['login_name'],
                         penabled='no',
                         stulist=json.dumps(ph.CreateStuList()),
                         prolist=json.dumps(ph.CreateProList()))
        database.save()
        ret = {'code': 200, 'info': 'ok', 'papername': postjson['papername']}

    elif action == 'delete':
        # get the paper id and delete it from database
        Paper.objects.filter(pid=postjson['paperid']).delete()
        ###
        ret = {'code': 200, 'info': 'ok', 'paperid': postjson['paperid']}

    elif action == 'enable':
        # turn the status of paper to yes
        paperdb = Paper.objects.get(pid=postjson['paperid'])
        paperdb.penabled = 'yes'
        paperdb.save()
        ###
        ret = {'code': 200, 'info': 'ok', 'paperid': postjson['paperid']}
    elif action == 'disable':
        # turn the status of paper to no
        paperdb = Paper.objects.get(pid=postjson['paperid'])
        paperdb.penabled = 'no'
        paperdb.save()
        ###
        ret = {'code': 200, 'info': 'ok', 'paperid': postjson['paperid']}

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