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
import generic
5 from django
.contrib
.contenttypes
.fields
import GenericRelation
7 from uuidfield
import UUIDField
9 from mygpo
.core
.models
import UpdateInfoModel
12 class Vote(UpdateInfoModel
):
13 """ A vote by a user for some object """
16 user
= models
.ForeignKey(settings
.AUTH_USER_MODEL
,
17 on_delete
=models
.CASCADE
)
19 # the object that was voted for
20 content_type
= models
.ForeignKey(ContentType
, on_delete
=models
.PROTECT
)
21 # this should suit UUID and integer primary keys
22 object_id
= UUIDField()
23 content_object
= generic
.GenericForeignKey('content_type', 'object_id')
27 # a user can only vote once per object
28 ('user', 'content_type', 'object_id'),
32 class VoteMixin(models
.Model
):
34 votes
= GenericRelation('Vote', related_query_name
='votes')
40 """ Register a vote from the user for the current object """
41 Vote
.objects
.get_or_create(
43 content_type
=ContentType
.objects
.get_for_model(self
),
48 return self
.votes
.count()