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
,
7 from mygpo
.core
.models
import UpdateInfoModel
10 class Vote(UpdateInfoModel
):
11 """ A vote by a user for some object """
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')
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')
38 """ Register a vote from the user for the current object """
39 Vote
.objects
.get_or_create(
41 content_type
=ContentType
.objects
.get_for_model(self
),
46 return self
.votes
.count()