[API] handle missing URLs in episode action upload
[mygpo.git] / mygpo / votes / models.py
blob2966309784e7392068ef92e1dca76fbbd15c0a87
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,
5 GenericForeignKey)
7 from mygpo.core.models import UpdateInfoModel
10 class Vote(UpdateInfoModel):
11 """ A vote by a user for some object """
13 # the user who voted
14 user = models.ForeignKey(settings.AUTH_USER_MODEL,
15 on_delete=models.CASCADE)
17 # the object that was voted for
18 content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
19 # this should suit UUID and integer primary keys
20 object_id = models.UUIDField()
21 content_object = GenericForeignKey('content_type', 'object_id')
23 class Meta:
24 unique_together = [
25 # a user can only vote once per object
26 ('user', 'content_type', 'object_id'),
30 class VoteMixin(models.Model):
32 votes = GenericRelation('Vote', related_query_name='votes')
34 class Meta:
35 abstract = True
37 def vote(self, user):
38 """ Register a vote from the user for the current object """
39 Vote.objects.get_or_create(
40 user=user,
41 content_type=ContentType.objects.get_for_model(self),
42 object_id=obj.pk,
45 def vote_count(self):
46 return self.votes.count()