def add_choice(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) try: new_choice = Choice( poll = p, choice_text = request.POST['choice_text'], choice_desc = request.POST['choice_desc'], proposer = request.POST['proposer'], votes = 1 ); new_choice.save() except IntegrityError: return render(request, 'polls/detail.html', { 'poll': p, 'error_propose_message': request.POST['choice_text'] + u": 이미 존재하는 이름입니다.", }) try: if 'suggest_subscribe_check' in request.POST.keys(): subscribe_check = True else: subscribe_check = False new_voter = Voter(poll=p, choice=new_choice, email=request.POST['proposer'], subscription=subscribe_check) new_voter.save() except Exception as err: print err.message, type(err) # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. #return HttpResponseRedirect(reverse('results', args=(p.id,))) return render(request, 'polls/results.html', {'poll': p})
def vote(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) try: selected_choice = p.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): # Redisplay the poll voting form. return render(request, 'polls/detail.html', { 'poll': p, 'error_message': "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() try: if 'vote_subscribe_check' in request.POST.keys(): subscribe_check = True else: subscribe_check = False new_voter = Voter(poll=p, choice=selected_choice, email=request.POST['voter_email'], subscription=subscribe_check) new_voter.save() except Exception as err: print err.message, type(err) # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. #return HttpResponseRedirect(reverse('results', args=(p.id,))) return render(request, 'polls/results.html', {'poll': p})
def random_voters(): all_users = User.objects.all() i = 0 for u in all_users: i = i+1 username = u.username age = 40 if(i%5 == 0): gender = 'f' else: gender = 'm' v = Voter(voter_username = username, voter_gender = gender, voter_age = age) v.save()
def post(self, request): form = register_form username = request.POST.get('username', False) password = request.POST.get('password', False) gender = request.POST.get('gender', False) age = request.POST.get('age', False) if (check_age(int(age)) == False): return HttpResponse("Not eligible") else: user = User.objects.create_user(username, '*****@*****.**', password) user.save() v = Voter(voter_username = username, voter_gender = gender, voter_age = age) v.save() return HttpResponse("Created")
def poll(request, poll_slug): context_dict = {} try: current_poll = Poll.objects.get(slug=poll_slug) choice_ids = poll_video_ids(current_poll.choice_set.all().order_by('-name')) context_dict['poll'] = current_poll context_dict['choice_ids'] = choice_ids except Poll.DoesNotExist: return render(request, 'index.html', { 'error_message': "Something went wrong. Try again later.", }) try: if Voter.objects.filter(user=request.user, poll=current_poll).exists(): context_dict['error_message'] = "You have already voted" context_dict['poll_slug'] = poll_slug return render(request, 'voted.html', context_dict) except Voter.DoesNotExist: return HttpResponseRedirect('/') if request.method == 'POST': try: selected_choice = current_poll.choice_set.get(name=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'poll.html', { 'poll': current_poll, 'error_message': "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() v = Voter(user=request.user, poll=current_poll) v.save() return HttpResponseRedirect('results') return render(request, 'poll.html', context_dict)
def vote(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) if p.type in [p.SIMPLE, p.MULTI]: try: choice_ids = request.POST.getlist('choice') except (KeyError, Choice.DoesNotExist): # Redisplay the poll voting form. return render(request, 'polls/detail.html', { 'poll': p, 'error_message': "You didn't select a choice.", }) else: try: voter = Voter.objects.get(user=request.user, poll=p) except Voter.DoesNotExist: voter = Voter(poll=p, user=request.user) voter.save() for vote in voter.vote_set.all(): vote.delete() for id in choice_ids: choice = p.choice_set.get(pk=id) vote = Vote(voter=voter) vote.choice = choice vote.save() # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. return HttpResponseRedirect(reverse('polls:results', args=(p.id,))) elif p.type == p.RANKED: try: voter = Voter.objects.get(user=request.user, poll=p) except Voter.DoesNotExist: voter = Voter(poll=p, user=request.user) voter.save() for vote in voter.vote_set.all(): vote.delete() rank = request.POST.get('rank').split(',') for i,e in enumerate(rank): choice = p.choice_set.get(pk=e) vote = Vote(voter=voter) vote.choice = choice vote.ranking = i; vote.save() return HttpResponseRedirect(reverse('polls:results', args=(p.id,))) else: return render(request, 'polls/detail.html', { 'poll': p, 'error_message': "Poll not implemented :(", })