2022-11-17 19:17:45 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.db.models import CASCADE
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-17 21:21:32 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2023-08-03 09:04:39 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2022-11-17 19:17:45 +00:00
|
|
|
|
2023-02-20 23:15:13 +00:00
|
|
|
from judge.models.profile import Profile
|
2022-11-17 19:17:45 +00:00
|
|
|
|
|
|
|
__all__ = ["BookMark"]
|
|
|
|
|
|
|
|
|
|
|
|
class BookMark(models.Model):
|
|
|
|
page = models.CharField(
|
|
|
|
max_length=30,
|
|
|
|
verbose_name=_("associated page"),
|
|
|
|
db_index=True,
|
2023-08-03 09:04:39 +00:00
|
|
|
) # deprecated
|
|
|
|
score = models.IntegerField(verbose_name=_("votes"), default=0)
|
|
|
|
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
|
|
|
object_id = models.PositiveIntegerField()
|
|
|
|
linked_object = GenericForeignKey("content_type", "object_id")
|
2022-11-17 19:17:45 +00:00
|
|
|
|
|
|
|
def get_bookmark(self, user):
|
|
|
|
userqueryset = MakeBookMark.objects.filter(bookmark=self, user=user)
|
|
|
|
if userqueryset.exists():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("bookmark")
|
|
|
|
verbose_name_plural = _("bookmarks")
|
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 19:17:45 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-08-03 09:04:39 +00:00
|
|
|
return f"bookmark for {self.linked_object}"
|
2022-11-17 19:17:45 +00:00
|
|
|
|
2022-11-17 22:11:47 +00:00
|
|
|
|
2022-11-17 19:17:45 +00:00
|
|
|
class MakeBookMark(models.Model):
|
|
|
|
bookmark = models.ForeignKey(BookMark, related_name="bookmark", on_delete=CASCADE)
|
2022-11-17 22:11:47 +00:00
|
|
|
user = models.ForeignKey(
|
|
|
|
Profile, related_name="user_bookmark", on_delete=CASCADE, db_index=True
|
|
|
|
)
|
2022-11-17 19:17:45 +00:00
|
|
|
|
|
|
|
class Meta:
|
2023-08-03 09:04:39 +00:00
|
|
|
indexes = [
|
|
|
|
models.Index(fields=["user", "bookmark"]),
|
|
|
|
]
|
2022-11-17 19:17:45 +00:00
|
|
|
unique_together = ["user", "bookmark"]
|
|
|
|
verbose_name = _("make bookmark")
|
|
|
|
verbose_name_plural = _("make bookmarks")
|
2023-08-03 16:19:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Bookmarkable:
|
|
|
|
def get_or_create_bookmark(self):
|
|
|
|
if self.bookmark.count():
|
|
|
|
return self.bookmark.first()
|
|
|
|
new_bookmark = BookMark()
|
|
|
|
new_bookmark.linked_object = self
|
|
|
|
new_bookmark.save()
|
|
|
|
return new_bookmark
|