2022-11-16 15:43:03 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.db.models import CASCADE
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-08-03 09:04:39 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2022-11-16 15:43:03 +00:00
|
|
|
|
2023-02-20 23:15:13 +00:00
|
|
|
from judge.models.profile import Profile
|
2023-10-10 22:38:48 +00:00
|
|
|
from judge.caching import cache_wrapper
|
2022-11-16 15:43:03 +00:00
|
|
|
|
|
|
|
__all__ = ["PageVote", "PageVoteVoter"]
|
|
|
|
|
|
|
|
|
|
|
|
class PageVote(models.Model):
|
|
|
|
page = models.CharField(
|
|
|
|
max_length=30,
|
|
|
|
verbose_name=_("associated page"),
|
|
|
|
db_index=True,
|
2023-08-03 09:04:39 +00:00
|
|
|
) # deprecated
|
2022-11-16 15:43:03 +00:00
|
|
|
score = models.IntegerField(verbose_name=_("votes"), default=0)
|
2023-08-03 09:04:39 +00:00
|
|
|
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
|
|
|
object_id = models.PositiveIntegerField()
|
|
|
|
linked_object = GenericForeignKey("content_type", "object_id")
|
2022-11-16 15:43:03 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("pagevote")
|
|
|
|
verbose_name_plural = _("pagevotes")
|
2023-08-03 09:04:39 +00:00
|
|
|
indexes = [
|
|
|
|
models.Index(fields=["content_type", "object_id"]),
|
|
|
|
]
|
|
|
|
unique_together = ("content_type", "object_id")
|
2022-11-17 00:48:32 +00:00
|
|
|
|
2023-10-10 22:38:48 +00:00
|
|
|
@cache_wrapper(prefix="PVvs")
|
2022-11-16 15:43:03 +00:00
|
|
|
def vote_score(self, user):
|
2024-04-13 22:02:54 +00:00
|
|
|
page_vote = PageVoteVoter.objects.filter(pagevote=self, voter=user).first()
|
|
|
|
return page_vote.score if page_vote else 0
|
2022-11-16 15:43:03 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-08-03 09:04:39 +00:00
|
|
|
return f"pagevote for {self.linked_object}"
|
2022-11-16 15:43:03 +00:00
|
|
|
|
2022-11-17 00:48:32 +00:00
|
|
|
|
2022-11-16 15:43:03 +00:00
|
|
|
class PageVoteVoter(models.Model):
|
|
|
|
voter = models.ForeignKey(Profile, related_name="voted_page", on_delete=CASCADE)
|
|
|
|
pagevote = models.ForeignKey(PageVote, related_name="votes", on_delete=CASCADE)
|
|
|
|
score = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ["voter", "pagevote"]
|
|
|
|
verbose_name = _("pagevote vote")
|
2022-11-17 00:48:32 +00:00
|
|
|
verbose_name_plural = _("pagevote votes")
|
2023-08-03 16:19:45 +00:00
|
|
|
|
|
|
|
|
2024-05-08 15:15:55 +00:00
|
|
|
@cache_wrapper(prefix="gocp", expected_type=PageVote)
|
2024-04-13 22:02:54 +00:00
|
|
|
def _get_or_create_pagevote(content_type, object_id):
|
|
|
|
pagevote, created = PageVote.objects.get_or_create(
|
|
|
|
content_type=content_type,
|
|
|
|
object_id=object_id,
|
|
|
|
)
|
|
|
|
return pagevote
|
|
|
|
|
|
|
|
|
2023-08-03 16:19:45 +00:00
|
|
|
class PageVotable:
|
|
|
|
def get_or_create_pagevote(self):
|
2024-04-13 22:02:54 +00:00
|
|
|
content_type = ContentType.objects.get_for_model(self)
|
|
|
|
object_id = self.pk
|
|
|
|
return _get_or_create_pagevote(content_type, object_id)
|
|
|
|
|
|
|
|
|
|
|
|
def dirty_pagevote(pagevote, profile):
|
|
|
|
pagevote.vote_score.dirty(pagevote, profile)
|
|
|
|
_get_or_create_pagevote.dirty(pagevote.content_type, pagevote.object_id)
|