Bump babel from 2.9.1 to 2.10.3
[mygpo.git] / mygpo / votes / models.py
blob9f800adc0a94f0d31ff0eb90e85363a893f5e0cc
1 from django.db import models
2 from django.conf import settings
3 from django.contrib.contenttypes.models import ContentType
4 from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey
6 from mygpo.core.models import UpdateInfoModel
9 class Vote(UpdateInfoModel):
10 """A vote by a user for some object"""
12 # the user who voted
13 user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
15 # the object that was voted for
16 content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
17 # this should suit UUID and integer primary keys
18 object_id = models.UUIDField()
19 content_object = GenericForeignKey("content_type", "object_id")
21 class Meta:
22 unique_together = [
23 # a user can only vote once per object
24 ("user", "content_type", "object_id")
28 class VoteMixin(models.Model):
30 votes = GenericRelation(Vote, related_query_name="votes")
32 class Meta:
33 abstract = True
35 def vote(self, user):
36 """Register a vote from the user for the current object"""
37 Vote.objects.get_or_create(
38 user=user,
39 content_type=ContentType.objects.get_for_model(self),
40 object_id=obj.pk,
43 def vote_count(self):
44 return self.votes.count()