Django limit voting to once a day

362 Views Asked by At

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

1

There are 1 best solutions below

0
Abhyudit Jain On

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.