def ajaxvote(request): # TODO Refactor / Clean up. """ This view handles voting using asynchronous javascript. View gets posted a voteform and returns succes / failure through http status codes and JSON. """ if request.method == 'POST': form = CastVoteFormFull(request.POST) if form.is_valid(): issue_no = int(form.cleaned_data["issue_no"]) try: issue = Issue.objects.get(pk = issue_no) except: reply = simplejson.dumps({'msg' : _('Issue object does not exist')}, ensure_ascii = False) return HttpResponseServerError(reply, mimetype = 'application/javascript') # The following line adapt the output from the vote form (old style) # to the new style of votes. direction = form.cleaned_data["vote"] + form.cleaned_data["motivation"] if direction == 1: css_class = u'for' elif direction == -1: css_class = u'against' else: css_class = u'blank' if request.user.is_authenticated(): gamelogic.actions.vote(request.user, issue, direction, form.cleaned_data['keep_private']) else: anonymous_actions.vote(request, issue, direction) data = { 'issue_no' : issue_no, 'vote_text' : votes_to_description[direction], 'css_class' : css_class, 'score' : issue.score, 'votes' : issue.votes, } reply = simplejson.dumps(data, ensure_ascii = False) return HttpResponse(reply, mimetype = 'application/json') else: reply = simplejson.dumps({'msg' : 'Failed to vote.'}, ensure_ascii = False) return HttpResponseServerError(reply, mimetype = 'application/json') else: reply = simplejson.dumps({'msg' : 'View needs to be posted to.'}, ensure_ascii = False) return HttpResponseServerError(reply, mimetype = 'application/json')
def handle_voteblankform(self, request, issue): if request.method == 'POST': qd = self.clean_GET_parameters(request) form = BlankVoteForm(request.POST) if form.is_valid(): # Register the vote in the DB and assign score if request.user.is_authenticated(): gamelogic.actions.vote(request.user, issue, form.cleaned_data['motivation'], form.cleaned_data["keep_private"]) else: anonymous_actions.vote(request, issue, form.cleaned_data['motivation']) # Succesful submit of a motivation for a blank vote. Redirect to # a formless version of the same page. del qd['form_type'] return None, HttpResponseRedirect(request.path + '?' + qd.urlencode()) else: form = BlankVoteForm() return form, None
def handle_voteform(self, request, issue): if request.method == 'POST': qd = self.clean_GET_parameters(request) form = NormalVoteForm(request.POST) if form.is_valid(): if form.cleaned_data['vote'] == 0: # Succesful submit of a blank vote, now redirect to a form # that lets the user give a motivation. qd['form_type'] = 'voteblankform' return None, HttpResponseRedirect(request.path + '?' + qd.urlencode()) else: # Register the votes in the DB and assign score: if request.user.is_authenticated(): gamelogic.actions.vote(request.user, issue, form.cleaned_data['vote'], form.cleaned_data["keep_private"]) else: anonymous_actions.vote(request, issue, form.cleaned_data['vote']) # Succesful submit of a normal (For or Against) vote, # redirect to a formless version of the same page. (i.e. # remove formtype query parameter). del qd['form_type'] return None, HttpResponseRedirect(request.path + '?' + qd.urlencode()) else: form = NormalVoteForm() return form, None