Re: Mark user as 'already voted' in polls app

2006-06-26 Thread Guillermo Fernandez Castellanos

Ok, I tried it and it works.

The idea is to put a cookie in the computer of the other person, where
it stocks all the polls it has participated to. That way it does not
depend of the IP.

The code:
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
voted_polls=request.session.get('has_voted_poll',[])
if poll_id in voted_polls:
return render_to_response('sysvortex/polls/poll_results.html', {
'object': p,
'error_message': "You have already voted.",
})
if p.is_open == False :
# Display the poll results.
return render_to_response('sysvortex/polls/poll_results.html', {
'object': p,
'error_message': "The poll is closed.",
})
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the poll voting form.
return render_to_response('sysvortex/polls/poll_detail.html', {
'object': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
request.session['has_voted_poll']=voted_polls+[poll_id]
# 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('/polls/%s/results/' % p.id)

Any comment and improvement welcomed.

Enjoy,

G

On 6/26/06, bahund <[EMAIL PROTECTED]> wrote:
>
> I'm in the same boat as well, where I'll have logged in users as well
> as anonymous users, and each user will only be allowed to submit each
> poll once.  I haven't figured out how to handle the anonymous users yet
> (I'll stay tuned to this thread), but I may be able to suggest a way to
> handle it in the model.
>
> What I did was create an additional model for submitted polls.  It has:
>
> class SubmittedPoll(models.Model):
>poll = models.ForeignKey(Poll)
>user = models.ForeignKey(User)
>choice = models.ForeignKey(Choice)
>submit_date = models.DateTimeField()
>
> As I said, I still have not been able to handle the anonymous user, so
> I'm hoping someone can shed some light on a way to do with with
> sessions/cookies.
>
> Thanks,
> Andy
>
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: Mark user as 'already voted' in polls app

2006-06-26 Thread bahund

I'm in the same boat as well, where I'll have logged in users as well
as anonymous users, and each user will only be allowed to submit each
poll once.  I haven't figured out how to handle the anonymous users yet
(I'll stay tuned to this thread), but I may be able to suggest a way to
handle it in the model.

What I did was create an additional model for submitted polls.  It has:

class SubmittedPoll(models.Model):
poll = models.ForeignKey(Poll)
user = models.ForeignKey(User)
choice = models.ForeignKey(Choice)
submit_date = models.DateTimeField()

As I said, I still have not been able to handle the anonymous user, so
I'm hoping someone can shed some light on a way to do with with
sessions/cookies.

Thanks,
Andy


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: Mark user as 'already voted' in polls app

2006-06-25 Thread Guillermo Fernandez Castellanos

Hi,

I am in the same situation actually. Or almost, as I'll have only
anonymous users.

I though about restricting votes based on IP addresses, but today,
with NATs and all, that would be unfair for a lot of people.

What I was planning to do is use the session framework. I will have in
the sessions each poll the user has answered.

Of course, a user could erase the cookies and vote again, but the idea
is to avoid accidental voting and the temptation of easy cheating :-)

But I am sure there must be a better way.

Enjoy,

G

On 6/25/06, Vladimir Pouzanov <[EMAIL PROTECTED]> wrote:
>
> Hello all,
>
> I'm trying to extend polls app from tutorial and add a 'user already
> voted' check. Can somebody suggest me a way to do that? My current
> idea is to add some BLOB filed to polls model and add each voted user
> id to list that will be pickled into that field. For anonymous users a
> cookie
> with poll id should be added.
>
> Not sure if the pickle/unpickle way is the best in terms of used
> system resources and speed. Any other suggestions?
>
> --
> Sincerely,
> Vladimir "Farcaller" Pouzanov
> http://www.hackndev.com
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---