示例#1
0
文件: views.py 项目: BingyZhang/DEMOS
def client(request, eid = 0):
    try:
	e = Election.objects.get(EID=eid)
    except Election.DoesNotExist:
	return HttpResponse('The election ID is invalid!')
    if request.method == 'POST':
        #response = HttpResponse("invalid code")
        #response['Access-Control-Allow-Origin'] = "*"
        #return response	
	#if request.is_ajax():#ajax post
	    #check election is running?
	    running = 0
	    if e.was_started():
		running = 1
	    if e.was_ended():
		running = 2
		if not e.request:
		    send_request(e)
		    e.request = True
		    e.save()
		    pass
	    #if e.pause:
		#running = 10
	    if running != 1:
		response = HttpResponse("invalid code")
		response['Access-Control-Allow-Origin'] = "*"
		return response
	    feedback = []
            # maximum 50 options
            for i in range(1,51):
                temp = request.POST.get('feedback'+str(i),'')
                if temp != '':
                    feedback.append(temp)
                else:
                    break
            code = request.POST["code"].upper()
            serial = request.POST["serial"].upper()
            codelist,receipt = verify_code(e,serial,code)
            if receipt != "":
                #add the code to DB
                new_entry = Vbb(election = e, serial = serial, votecode = code)
                new_entry.save()
                #store the dual ballot feedback
                for x in feedback:
                    feed = x.split(",")
                    if feed[0] in codelist:
                        balls = Dballot(vbb = new_entry, serial = serial, code = feed[0], checked = True, value = feed[1])
                        balls.save()
		response = HttpResponse(receipt)
                response['Access-Control-Allow-Origin'] = "*"
                return response
            else:
                response = HttpResponse("invalid code")
                response['Access-Control-Allow-Origin'] = "*"
                return response
	#404 if not ajax for security
	#return render_to_response('404.html')        
    else:
        return render_to_response('404.html')
示例#2
0
文件: views.py 项目: BingyZhang/finer
def client(request, eid=0):
    try:
        e = Election.objects.get(EID=eid)
    except Election.DoesNotExist:
        return HttpResponse('The election ID is invalid!')
    if request.method == 'POST':
        if request.is_ajax():  #ajax post
            #check election is running?
            running = 0
            if e.was_started():
                running = 1
            if e.was_ended():
                running = 2
                if not e.request:
                    #send_request(e)
                    #e.request = True
                    #e.save()
                    pass
            if e.pause:
                running = 10
            if running != 1:
                return HttpResponse("invalid code")
            feedback = []
            # maximum 50 options
            for i in range(1, 51):
                temp = request.POST.get('feedback' + str(i), '')
                if temp != '':
                    feedback.append(temp)
                else:
                    break
            code = request.POST["code"].upper()
            serial = request.POST["serial"].upper()
            codelist, receipt = verify_code(e, serial, code)
            if receipt != "":
                #add the code to DB
                new_entry = Vbb(election=e, serial=serial, votecode=code)
                new_entry.save()
                #store the dual ballot feedback
                for x in feedback:
                    feed = x.split(",")
                    if feed[0] in codelist:
                        balls = Dballot(vbb=new_entry,
                                        serial=serial,
                                        code=feed[0],
                                        checked=True,
                                        value=feed[1])
                        balls.save()
                return HttpResponse(receipt)
            else:
                return HttpResponse('invalid code')
        #404 if not ajax for security
        return render_to_response('404.html')
    else:
        return render_to_response('404.html')
示例#3
0
文件: views.py 项目: BingyZhang/finer
def index(request, eid = 0):
	try:
		e = Election.objects.get(EID=eid)
	except Election.DoesNotExist:
		return HttpResponse('The election ID is invalid!')
	time = 0
	options = e.choice_set.all()
	#short party names only sorted
	short_opts = [[x.votes, x.text.split(";")[0]] for x in options ]
	sorted_opts = sorted(short_opts,reverse=True)
	table_data = []
	checkcode = "invalid code"
	running = 0
	if e.was_started():
		running = 1
		time = int((e.end - timezone.now()).total_seconds())
	if e.was_ended():
		running = 2
		if not e.request:
			#send_request(e)
			#e.request = True
			#e.save()
			pass
		else:
			if e.tally:
				running = 3
	if e.pause:
                running = 10
	if request.method == 'POST':#there are two posts
		if running != 1:
			return HttpResponse("invalid code")
		if request.is_ajax():#ajax post
			form = VoteForm(request.POST) # A form bound to the POST data
			if form.is_valid(): # All validation rules pass
				s = form.cleaned_data.get('serial').upper()#request.POST['serial']
				c = form.cleaned_data.get('code').upper()#request.POST['code']
				if len(s) == 0 or len(c) ==0:
					return HttpResponse("invalid code")
				codelist,receipt = verify_code(e,s,c)
				if receipt != "":
					#add the code to DB
					new_entry = Vbb(election = e, serial = s, votecode = c)
					new_entry.save()
					#store the dual ballot
					for i in range(len(codelist)):
						balls = Dballot(vbb = new_entry, serial = s, code = codelist[i])
						balls.save()
					#return HttpResponse(receipt)
                                        return render_to_response('feedback.html', {'codes': codelist,'options':short_opts,'rec':receipt}, context_instance=RequestContext(request))
                                else:
                                        return HttpResponse("invalid code")
			else:
				return HttpResponse("invalid code")
		else:
			form = FeedbackForm(request.POST) # A form bound to the POST data
			if form.is_valid(): # All validation rules pass
				ic = form.cleaned_data.get('checkcode')				
				io = form.cleaned_data.get('checkoption') #request.POST['checkoption']
				if "Select" not in io and "Select" not in ic:# good feedback
                                        ball = Dballot.objects.get(code = ic)
                                        ball.value = io
					ball.checked = True
                                        ball.save()
				return render_to_response('thanks.html')
			else:
				return HttpResponse("Wrong Form")
	else:# no post
		data = e.vbb_set.all().order_by('-date')
		#prepare the table_data
		for item in data:
			temp_row = []
			temp_row.append(item.serial)
			temp_row.append(item.votecode)
			temp_row.append(item.date)
			unused = item.dballot_set.filter(checked = True)
			l = len(unused)
			if l==0:
			    temp_row.append("")
			    temp_row.append("")			

			else:
			#randomly display one
			    if l > 1:
				x = random.randrange(l)
				temp_row.append(unused[x].code)
                                temp_row.append(unused[x].value)
			    else: 
                                temp_row.append(unused[0].code)
			        temp_row.append(unused[0].value)
			table_data.append(temp_row)
		progress = int(e.vbb_set.count()*100/e.total+0.5)

		return render_to_response('vbb.html', {'data':table_data, 'options':sorted_opts, 'time':time, 'running':running, 'election':e, 'progress':progress}, context_instance=RequestContext(request))
示例#4
0
文件: views.py 项目: BingyZhang/finer
def index(request, eid=0):
    try:
        e = Election.objects.get(EID=eid)
    except Election.DoesNotExist:
        return HttpResponse('The election ID is invalid!')
    time = 0
    options = e.choice_set.all()
    #short party names only sorted
    short_opts = [[x.votes, x.text.split(";")[0]] for x in options]
    sorted_opts = sorted(short_opts, reverse=True)
    table_data = []
    checkcode = "invalid code"
    running = 0
    if e.was_started():
        running = 1
        time = int((e.end - timezone.now()).total_seconds())
    if e.was_ended():
        running = 2
        if not e.request:
            #send_request(e)
            #e.request = True
            #e.save()
            pass
        else:
            if e.tally:
                running = 3
    if e.pause:
        running = 10
    if request.method == 'POST':  #there are two posts
        if running != 1:
            return HttpResponse("invalid code")
        if request.is_ajax():  #ajax post
            form = VoteForm(request.POST)  # A form bound to the POST data
            if form.is_valid():  # All validation rules pass
                s = form.cleaned_data.get(
                    'serial').upper()  #request.POST['serial']
                c = form.cleaned_data.get(
                    'code').upper()  #request.POST['code']
                if len(s) == 0 or len(c) == 0:
                    return HttpResponse("invalid code")
                codelist, receipt = verify_code(e, s, c)
                if receipt != "":
                    #add the code to DB
                    new_entry = Vbb(election=e, serial=s, votecode=c)
                    new_entry.save()
                    #store the dual ballot
                    for i in range(len(codelist)):
                        balls = Dballot(vbb=new_entry,
                                        serial=s,
                                        code=codelist[i])
                        balls.save()
                    #return HttpResponse(receipt)
                    return render_to_response(
                        'feedback.html', {
                            'codes': codelist,
                            'options': short_opts,
                            'rec': receipt
                        },
                        context_instance=RequestContext(request))
                else:
                    return HttpResponse("invalid code")
            else:
                return HttpResponse("invalid code")
        else:
            form = FeedbackForm(request.POST)  # A form bound to the POST data
            if form.is_valid():  # All validation rules pass
                ic = form.cleaned_data.get('checkcode')
                io = form.cleaned_data.get(
                    'checkoption')  #request.POST['checkoption']
                if "Select" not in io and "Select" not in ic:  # good feedback
                    ball = Dballot.objects.get(code=ic)
                    ball.value = io
                    ball.checked = True
                    ball.save()
                return render_to_response('thanks.html')
            else:
                return HttpResponse("Wrong Form")
    else:  # no post
        data = e.vbb_set.all().order_by('-date')
        #prepare the table_data
        for item in data:
            temp_row = []
            temp_row.append(item.serial)
            temp_row.append(item.votecode)
            temp_row.append(item.date)
            unused = item.dballot_set.filter(checked=True)
            l = len(unused)
            if l == 0:
                temp_row.append("")
                temp_row.append("")

            else:
                #randomly display one
                if l > 1:
                    x = random.randrange(l)
                    temp_row.append(unused[x].code)
                    temp_row.append(unused[x].value)
                else:
                    temp_row.append(unused[0].code)
                    temp_row.append(unused[0].value)
            table_data.append(temp_row)
        progress = int(e.vbb_set.count() * 100 / e.total + 0.5)

        return render_to_response('vbb.html', {
            'data': table_data,
            'options': sorted_opts,
            'time': time,
            'running': running,
            'election': e,
            'progress': progress
        },
                                  context_instance=RequestContext(request))