def modify_paper_stulist(request): postjson = jh.post2json(request) paperid = postjson['paperid'] action = postjson['action'] ph = PaperHelper() ret = {'code': 404, 'info': 'unknown action' + action} # TODO(LOW): verify paperid whether existing ### print(paperid) paperdb = Paper.objects.get(pid=paperid) if (action == 'addstu'): stulist = postjson['stulist'] stuarray = stulist.split(';') original_stulist = json.loads(paperdb.stulist) count = 0 for var in stuarray: # TODO(LOW): verify var(stuid) whether existing # result = UserList.objects.filter(username=var) if not result.exists(): continue if (var == ''): continue ph.AddStu(original_stulist, var) count += 1 paperdb.stulist = json.dumps(original_stulist) paperdb.save() ret = {'code': 200, 'info': 'ok', 'count': count} elif (action == 'delstu'): stu_to_del = postjson['stu_to_del'] original_stulist = json.loads(paperdb.stulist) ph.DelStu(original_stulist, stu_to_del) paperdb.stulist = json.dumps(original_stulist) paperdb.save() ret = {'code': 200, 'info': 'ok', 'deleted': stu_to_del} elif (action == 'cleanstu'): original_stulist = json.loads(paperdb.stulist) gradelist = json.loads(paperdb.stulist) count = original_stulist['count'] empty_list = ph.CreateStuList() paperdb.stulist = json.dumps(empty_list) paperdb.save() ret = {'code': 200, 'info': 'ok', 'count': count} return HttpResponse(json.dumps(ret), content_type="application/json")
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")