0

I've recently started learning Django so it's still a bit confusing for me.

I'll be really happy if someone can guide me to a link or tutorial or help me to figure out the following.

-Allow user to only vote once a day

This is from my models.py

class YoungArtistShortlisted(models.Model):
    image = models.ImageField(upload_to=upload_file_path, blank=True, null=True)
    artist = models.CharField(max_length=200)
    age = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True, db_index=True)
    modified = models.DateTimeField(auto_now=True, db_index=True)
    location = models.CharField(max_length=3, choices=LOCATION_CHOICES)
    likes = models.IntegerField(default=0)

    def __unicode__(self):
        return self.artist

and this is my views.py

def vote(request, youngartistshortlisted_id):
    p = get_object_or_404(YoungArtistShortlisted, pk=youngartistshortlisted_id)
    p.likes += 1
    p.save()

    return HttpResponseRedirect(reverse_lazy('youngartist:submission_vote', args=(p.id,)))

I'm currently working an app to create a user automatically when the user logs in with Facebook. I have absolutely no idea how to limit voting to once a day so I'd really appreciate any help given as I can't seem to find anything on google. Thanks!

I'm using Django 1.8.2

Lim Damien
  • 11
  • 3
  • Since you mentioned "create a user", I suppose there is a user table in database. I would add a "last_vote_time" field to the table. Then when a user votes, I can check the last time the user voted: if it was less that 24h ago, deny the vote. And of course, I would also create a user model. – Meng Wang Aug 11 '15 at 04:53
  • Hi Meng Wang, thanks for replying! I already have a user model and i added a last_time_vote field and also a function to check if last_time_vote happened within the last day. my question is, how to I change last_time_vote everytime when the user votes? @_@ – Lim Damien Aug 11 '15 at 05:23
  • Ahhh I managed to figure it out thanks to your guidance!! – Lim Damien Aug 11 '15 at 06:58

1 Answers1

1

Just add a field to keep track of the last time the user voted.

Example,

last_vote_time = models.DateTimeField()

and in views.py, check if last_vote_time has a 24 hour difference from the current time.

This should help. Tell me if you need some code. But, I think you'll be able to do it.

Abhyudit Jain
  • 3,640
  • 2
  • 24
  • 32