def login(request): try: params = json.loads(request.body) username = params['teacher'] password = params['password'] except: response = JsonResponse({"error_code": 1, "msg": "params error"}) return ret(response) md5 = hashlib.md5() md5.update(password.encode("utf-8")) password = md5.hexdigest() if username == 'admin': if password == 'f5db5f3b5378097c93d8e99633165fdc': request.session['isteacher'] = 'yes' response = JsonResponse({"error_code": 0, "msg": "success"}) return ret(response) else: response = JsonResponse({"error_code": 1, "msg": "wrong password"}) return ret(response) else: response = JsonResponse({"error_code": 1, "msg": "wrong username"}) return ret(response)
def delete(request): try: sign = request.session['isteacher'] if sign != 'yes': response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) try: voteid = request.GET['voteId'] except: response = JsonResponse({"error_code": 1, "msg": "ID wrong"}) return ret(response) try: info = models.vote_info.objects.get(id=voteid) except: response = JsonResponse({"error_code": 1, "msg": "voteId not exit"}) return ret(response) info.delete() try: result = models.vote_result.objects.filter(voteid=voteid) for i in result: i.delete() except: pass response = JsonResponse({"error_code": 0, "msg": "success"}) return ret(response)
def showResult(request): try: sign = request.session['isteacher'] if sign != 'yes': response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) try: voteid = request.GET['voteId'] except: response = JsonResponse({"error_code": 1, "msg": "ID wrong"}) return ret(response) data = [] info = {} for i in range(1,12): res = models.vote_result.objects.filter(voteid=voteid,tno=i).values() dist = [] for j in res: print(j) name = j.get('sno') + j.get('sname') + '第' + j.get('stno') + '组' dist.append(name) info = {"tno": i,"count": len(res), "list": dist} data.append(info) response = JsonResponse({"error_code": 0, "data": data}) return ret(response)
def getVoteList(request): try: sign = request.session['isteacher'] if sign != 'yes': response = JsonResponse({ "error_code": 1, "msg": "please login first" }) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) data = [] info = {} qs = models.vote_info.objects.values() for i in qs: info = { "voteId": i.get('id'), "voteName": i.get('votename'), "intro": i.get('voteintro') } data.append(info) response = JsonResponse({'error_code': 0, 'data': data}) return ret(response)
def newvote(request): try: sign = request.session['isteacher'] if sign != 'yes': response = JsonResponse({ "error_code": 1, "msg": "please login first" }) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) try: params = json.loads(request.body) votename = params['voteName'] voteintro = params['intro'] votecontent = str(params['voteContent']) votetype = params['type'] record = models.vote_info.objects.create(votename=votename, voteintro=voteintro, votetype=votetype, votecontent=votecontent) response = JsonResponse({"error_code": 0, "msg": "success"}) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "params error"}) return ret(response)
def getVoteInfo(request): try: sign = request.session['sign'] if sign != 'yes': response = JsonResponse({ "error_code": 1, "msg": "please login first" }) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) try: voteid = request.GET['voteId'] except: response = JsonResponse({"error_code": 1, "msg": "ID wrong"}) return ret(response) try: # 根据 id 从数据库中找到相应的客户记录 info = models.vote_info.objects.get(id=voteid) response = JsonResponse({ "error_code": 0, "voteName": info.votename.decode('UTF-8'), "intro": info.voteintro.decode('UTF-8'), "voteContent": info.votecontent.decode('UTF-8'), "type": info.votetype.decode('UTF-8') }) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "ID wrong"}) return ret(response)
def register(request): try: params = json.loads(request.body) username = params['sno'] password = params['password'] name = params['name'] except: response = JsonResponse({"error_code": 1, "msg": "params error"}) return ret(response) has_user = 0 try: has_user = models.Student.objects.get(sno=username) except: pass if has_user != 0: response = JsonResponse({"error_code": 1, "msg": "registed"}) return ret(response) else: md5 = hashlib.md5() md5.update(password.encode("utf-8")) passwd = md5.hexdigest() models.Student.objects.create(sno=username, password=passwd, name=name) response = JsonResponse({"error_code": 0, "msg": "success"}) return ret(response)
def showResult(request): try: sign = request.session['sign'] if sign != 'yes': response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) try: voteid = request.GET['voteId'] except: response = JsonResponse({"error_code": 1, "msg": "params error"}) return ret(response) res = models.vote_result.objects.filter(voteid=voteid).values() if(len(res)==0): response = JsonResponse({"error_code": 1, "msg": "voteId does not exist"}) return ret(response) data = [] info = {} for i in range(1,12): res = models.vote_result.objects.filter(voteid=voteid,tno=i).values() info = {"tno": i,"count": len(res)} data.append(info) response = JsonResponse({"error_code": 0, "data": data}) return ret(response)
def getvoteList(request): try: sign = request.session['sign'] if sign != 'yes': response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) data = [] info = {} qs = models.vote_info.objects.filter() for i in qs: info = {"voteId": i.id, "voteName": i.votename, "intro": i.voteintro } data.append(info) response = JsonResponse({'error_code': 0, 'data': data},content_type="application/json,charset=utf-8") return ret(response)
def signin(request): try: sign = request.session['sign'] if sign == 'yes': response = JsonResponse({"error_code": 1, "msg": "landed"}) return ret(response) except: pass try: params = json.loads(request.body) username = params['sno'] tno = params['tno'] name = params['name'] request.session['sign'] = 'yes' request.session['sno'] = username request.session['name'] = name request.session['tno'] = tno response = JsonResponse({"error_code": 0, "msg": {"sno": username, "name": name, "tno": tno}}) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "params error"}) return ret(response)
def vote(request): try: sign = request.session['sign'] if sign != 'yes': response = JsonResponse({ "error_code": 1, "msg": "please login first" }) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "please login first"}) return ret(response) try: params = json.loads(request.body) voteid = params['voteId'] sno = request.session['sno'] sname = request.session['name'] tno = request.session['tno'] data = params['data'] res = models.vote_info.objects.filter(id=voteid).values() if (len(res) == 0): response = JsonResponse({ "error_code": 1, "msg": "voteId does not exist" }) return ret(response) res = models.vote_result.objects.filter(voteid=voteid, sno=sno).values() if (len(res) != 0): response = JsonResponse({ "error_code": 1, "msg": "you have already voted before" }) return ret(response) for i in data: record = models.vote_result.objects.create(voteid=voteid, sno=sno, sname=sname, stno=tno, tno=i) response = JsonResponse({"error_code": 0, "msg": "success"}) return ret(response) except: response = JsonResponse({"error_code": 1, "msg": "params error"}) return ret(response)
def signout(request): request.session.flush() response = JsonResponse({"error_code": 0, "msg": "success"}) return ret(response)