2020-12-09 13:47:00 +01:00
|
|
|
import re
|
2020-05-01 22:46:15 +08:00
|
|
|
from decimal import *
|
2020-12-09 13:47:00 +01:00
|
|
|
from markdown import markdown
|
2020-05-01 22:46:15 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-05-05 23:50:48 +08:00
|
|
|
from django.db import models, IntegrityError
|
2020-05-01 22:46:15 +08:00
|
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
2020-05-05 23:50:48 +08:00
|
|
|
from django.db.models import Q
|
2020-05-01 22:46:15 +08:00
|
|
|
from markdownx.models import MarkdownxField
|
|
|
|
from users.models import User
|
2020-10-22 21:45:05 +02:00
|
|
|
from mastodon.api import get_relationships, get_cross_site_id
|
2021-06-14 22:18:39 +02:00
|
|
|
from django.utils import timezone
|
2021-09-10 20:24:22 -04:00
|
|
|
from django.conf import settings
|
2020-05-01 22:46:15 +08:00
|
|
|
|
|
|
|
|
2020-12-09 13:47:00 +01:00
|
|
|
RE_HTML_TAG = re.compile(r"<[^>]*>")
|
|
|
|
|
|
|
|
|
2020-05-01 22:46:15 +08:00
|
|
|
# abstract base classes
|
|
|
|
###################################
|
2020-11-28 16:25:27 +01:00
|
|
|
class SourceSiteEnum(models.TextChoices):
|
2021-09-10 20:24:22 -04:00
|
|
|
IN_SITE = "in-site", settings.CLIENT_NAME
|
2021-09-21 23:09:09 -04:00
|
|
|
DOUBAN = "douban", _("豆瓣")
|
2021-02-15 21:27:50 +01:00
|
|
|
SPOTIFY = "spotify", _("Spotify")
|
2021-02-18 15:44:10 +01:00
|
|
|
IMDB = "imdb", _("IMDb")
|
2021-02-25 19:43:43 +01:00
|
|
|
STEAM = "steam", _("STEAM")
|
2021-02-26 16:36:44 +01:00
|
|
|
BANGUMI = 'bangumi', _("bangumi")
|
2021-09-21 23:09:09 -04:00
|
|
|
GOODREADS = "goodreads", _("goodreads")
|
2021-10-06 21:21:24 -04:00
|
|
|
TMDB = "tmdb", _("The Movie Database")
|
2021-10-17 22:43:56 -04:00
|
|
|
GOOGLEBOOKS = "googlebooks", _("Google Books")
|
2021-11-26 23:41:47 -05:00
|
|
|
BANDCAMP = "bandcamp", _("BandCamp")
|
2020-11-22 14:11:59 +01:00
|
|
|
|
|
|
|
|
2020-11-23 23:18:14 +01:00
|
|
|
class Entity(models.Model):
|
2020-05-01 22:46:15 +08:00
|
|
|
|
|
|
|
rating_total_score = models.PositiveIntegerField(null=True, blank=True)
|
|
|
|
rating_number = models.PositiveIntegerField(null=True, blank=True)
|
2020-10-22 21:45:05 +02:00
|
|
|
rating = models.DecimalField(
|
|
|
|
null=True, blank=True, max_digits=3, decimal_places=1)
|
2020-05-01 22:46:15 +08:00
|
|
|
created_time = models.DateTimeField(auto_now_add=True)
|
2021-06-14 22:18:39 +02:00
|
|
|
edited_time = models.DateTimeField(auto_now=True)
|
2020-10-22 21:45:05 +02:00
|
|
|
last_editor = models.ForeignKey(
|
|
|
|
User, on_delete=models.SET_NULL, related_name='%(class)s_last_editor', null=True, blank=False)
|
2021-02-12 19:23:23 +01:00
|
|
|
brief = models.TextField(_("简介"), blank=True, default="")
|
2021-08-01 12:39:19 +02:00
|
|
|
other_info = models.JSONField(_("其他信息"),
|
2020-10-22 21:45:05 +02:00
|
|
|
blank=True, null=True, encoder=DjangoJSONEncoder, default=dict)
|
2020-11-23 23:18:14 +01:00
|
|
|
# source_url should include shceme, which is normally https://
|
|
|
|
source_url = models.URLField(_("URL"), max_length=500, unique=True)
|
2020-11-28 16:25:27 +01:00
|
|
|
source_site = models.CharField(_("源网站"), choices=SourceSiteEnum.choices, max_length=50)
|
2020-05-01 22:46:15 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
constraints = [
|
2020-10-22 21:45:05 +02:00
|
|
|
models.CheckConstraint(check=models.Q(
|
|
|
|
rating__gte=0), name='%(class)s_rating_lowerbound'),
|
|
|
|
models.CheckConstraint(check=models.Q(
|
|
|
|
rating__lte=10), name='%(class)s_rating_upperbound'),
|
|
|
|
]
|
2020-05-01 22:46:15 +08:00
|
|
|
|
2020-11-23 23:18:14 +01:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
raise NotImplementedError("Subclass should implement this method")
|
|
|
|
|
2020-05-01 22:46:15 +08:00
|
|
|
def save(self, *args, **kwargs):
|
2020-11-22 14:11:59 +01:00
|
|
|
""" update rating and strip source url scheme & querystring before save to db """
|
2020-05-01 22:46:15 +08:00
|
|
|
if self.rating_number and self.rating_total_score:
|
2020-10-22 21:45:05 +02:00
|
|
|
self.rating = Decimal(
|
|
|
|
str(round(self.rating_total_score / self.rating_number, 1)))
|
2020-05-05 23:50:48 +08:00
|
|
|
elif self.rating_number is None and self.rating_total_score is None:
|
|
|
|
self.rating = None
|
|
|
|
else:
|
|
|
|
raise IntegrityError()
|
2020-05-01 22:46:15 +08:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2020-05-05 23:50:48 +08:00
|
|
|
def calculate_rating(self, old_rating, new_rating):
|
2020-10-22 21:45:05 +02:00
|
|
|
if (not (self.rating and self.rating_total_score and self.rating_number)
|
|
|
|
and (self.rating or self.rating_total_score or self.rating_number))\
|
|
|
|
or (not (self.rating or self.rating_number or self.rating_total_score) and old_rating is not None):
|
2020-05-05 23:50:48 +08:00
|
|
|
raise IntegrityError("Rating integiry error.")
|
|
|
|
if old_rating:
|
|
|
|
if new_rating:
|
|
|
|
# old -> new
|
|
|
|
self.rating_total_score += (new_rating - old_rating)
|
|
|
|
else:
|
|
|
|
# old -> none
|
|
|
|
if self.rating_number >= 2:
|
|
|
|
self.rating_total_score -= old_rating
|
|
|
|
self.rating_number -= 1
|
|
|
|
else:
|
|
|
|
# only one rating record
|
|
|
|
self.rating_number = None
|
|
|
|
self.rating_total_score = None
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if new_rating:
|
|
|
|
# none -> new
|
|
|
|
if self.rating_number and self.rating_number >= 1:
|
|
|
|
self.rating_total_score += new_rating
|
|
|
|
self.rating_number += 1
|
|
|
|
else:
|
|
|
|
# no rating record before
|
|
|
|
self.rating_number = 1
|
|
|
|
self.rating_total_score = new_rating
|
|
|
|
else:
|
|
|
|
# none -> none
|
|
|
|
pass
|
2020-10-22 21:45:05 +02:00
|
|
|
|
2020-05-05 23:50:48 +08:00
|
|
|
def update_rating(self, old_rating, new_rating):
|
2021-06-14 22:18:39 +02:00
|
|
|
"""
|
|
|
|
@param old_rating: the old mark rating
|
|
|
|
@param new_rating: the new mark rating
|
|
|
|
"""
|
2020-05-05 23:50:48 +08:00
|
|
|
self.calculate_rating(old_rating, new_rating)
|
|
|
|
self.save()
|
|
|
|
|
2020-07-10 21:28:09 +08:00
|
|
|
def get_tags_manager(self):
|
|
|
|
"""
|
2020-11-23 23:18:14 +01:00
|
|
|
Since relation between tag and entity is foreign key, and related name has to be unique,
|
2020-07-10 21:28:09 +08:00
|
|
|
this method works like interface.
|
|
|
|
"""
|
2020-10-04 16:16:50 +02:00
|
|
|
raise NotImplementedError("Subclass should implement this method.")
|
2020-07-10 21:28:09 +08:00
|
|
|
|
2020-09-28 11:21:40 +02:00
|
|
|
def get_marks_manager(self):
|
|
|
|
"""
|
|
|
|
Normally this won't be used.
|
|
|
|
There is no ocassion where visitor can simply view all the marks.
|
|
|
|
"""
|
2020-10-04 16:16:50 +02:00
|
|
|
raise NotImplementedError("Subclass should implement this method.")
|
2020-10-22 21:45:05 +02:00
|
|
|
|
2021-02-12 19:23:23 +01:00
|
|
|
def get_reviews_manager(self):
|
2020-09-28 11:21:40 +02:00
|
|
|
"""
|
|
|
|
Normally this won't be used.
|
|
|
|
There is no ocassion where visitor can simply view all the reviews.
|
|
|
|
"""
|
2020-10-04 16:16:50 +02:00
|
|
|
raise NotImplementedError("Subclass should implement this method.")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_category_mapping_dict(cls):
|
|
|
|
category_mapping_dict = {}
|
|
|
|
for subclass in cls.__subclasses__():
|
|
|
|
category_mapping_dict[subclass.__name__.lower()] = subclass
|
|
|
|
return category_mapping_dict
|
|
|
|
|
|
|
|
@property
|
|
|
|
def category_name(self):
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def verbose_category_name(self):
|
|
|
|
raise NotImplementedError("Subclass should implement this.")
|
|
|
|
|
2020-05-01 22:46:15 +08:00
|
|
|
|
|
|
|
class UserOwnedEntity(models.Model):
|
|
|
|
is_private = models.BooleanField()
|
2020-10-22 21:45:05 +02:00
|
|
|
owner = models.ForeignKey(
|
|
|
|
User, on_delete=models.CASCADE, related_name='user_%(class)ss')
|
2021-06-14 22:18:39 +02:00
|
|
|
created_time = models.DateTimeField(default=timezone.now)
|
|
|
|
edited_time = models.DateTimeField(default=timezone.now)
|
2020-05-01 22:46:15 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2021-12-15 00:16:48 -05:00
|
|
|
def is_visible_to(self, viewer):
|
|
|
|
owner = self.owner
|
|
|
|
if owner == viewer:
|
|
|
|
return True
|
|
|
|
if viewer.is_blocking(owner) or owner.is_blocking(viewer) or viewer.is_muting(owner):
|
|
|
|
return False
|
|
|
|
if self.is_private:
|
|
|
|
return viewer.is_following(owner)
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2020-05-05 23:50:48 +08:00
|
|
|
@classmethod
|
2021-12-15 21:54:24 -05:00
|
|
|
def get_available(cls, entity, request_user, following_only=False):
|
2021-12-15 00:16:48 -05:00
|
|
|
# e.g. SongMark.get_available(song, request.user, request.session['oauth_token'])
|
2020-11-23 23:18:14 +01:00
|
|
|
query_kwargs = {entity.__class__.__name__.lower(): entity}
|
2021-12-15 00:16:48 -05:00
|
|
|
all_entities = cls.objects.filter(**query_kwargs).order_by("-edited_time") # get all marks for song
|
2021-12-15 21:54:24 -05:00
|
|
|
visible_entities = list(filter(lambda _entity: _entity.is_visible_to(request_user) and (_entity.owner.mastodon_username in request_user.mastodon_following if following_only else True), all_entities))
|
2021-12-15 00:16:48 -05:00
|
|
|
return visible_entities
|
2020-05-05 23:50:48 +08:00
|
|
|
|
|
|
|
@classmethod
|
2021-12-15 00:16:48 -05:00
|
|
|
def get_available_by_user(cls, owner, is_following): # FIXME
|
2020-05-05 23:50:48 +08:00
|
|
|
"""
|
2020-11-28 16:25:27 +01:00
|
|
|
Returns all avaliable owner's entities.
|
|
|
|
Mute/Block relation is not handled in this method.
|
2020-05-05 23:50:48 +08:00
|
|
|
|
|
|
|
:param owner: visited user
|
|
|
|
:param is_following: if the current user is following the owner
|
|
|
|
"""
|
|
|
|
user_owned_entities = cls.objects.filter(owner=owner)
|
|
|
|
if not is_following:
|
|
|
|
user_owned_entities = user_owned_entities.exclude(is_private=True)
|
|
|
|
return user_owned_entities
|
|
|
|
|
2020-05-01 22:46:15 +08:00
|
|
|
|
|
|
|
# commonly used entity classes
|
|
|
|
###################################
|
2020-11-28 16:25:27 +01:00
|
|
|
class MarkStatusEnum(models.TextChoices):
|
|
|
|
WISH = 'wish', _('Wish')
|
|
|
|
DO = 'do', _('Do')
|
|
|
|
COLLECT = 'collect', _('Collect')
|
2020-05-01 22:46:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Mark(UserOwnedEntity):
|
2020-11-28 16:25:27 +01:00
|
|
|
status = models.CharField(choices=MarkStatusEnum.choices, max_length=20)
|
2020-05-01 22:46:15 +08:00
|
|
|
rating = models.PositiveSmallIntegerField(blank=True, null=True)
|
2021-10-11 23:24:16 -04:00
|
|
|
text = models.CharField(max_length=5000, blank=True, default='')
|
2020-05-01 22:46:15 +08:00
|
|
|
|
2020-07-10 21:28:09 +08:00
|
|
|
def __str__(self):
|
2021-12-12 18:15:42 -05:00
|
|
|
return f"Mark({self.id} {self.owner} {self.status.upper()})"
|
2020-07-10 21:28:09 +08:00
|
|
|
|
2020-05-01 22:46:15 +08:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
constraints = [
|
2020-10-22 21:45:05 +02:00
|
|
|
models.CheckConstraint(check=models.Q(
|
|
|
|
rating__gte=0), name='mark_rating_lowerbound'),
|
|
|
|
models.CheckConstraint(check=models.Q(
|
|
|
|
rating__lte=10), name='mark_rating_upperbound'),
|
2020-05-01 22:46:15 +08:00
|
|
|
]
|
2021-06-14 22:18:39 +02:00
|
|
|
|
|
|
|
# TODO update entity rating when save
|
|
|
|
# TODO update tags
|
2020-05-01 22:46:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Review(UserOwnedEntity):
|
|
|
|
title = models.CharField(max_length=120)
|
|
|
|
content = MarkdownxField()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
2020-12-09 13:47:00 +01:00
|
|
|
def get_plain_content(self):
|
|
|
|
"""
|
|
|
|
Get plain text format content
|
|
|
|
"""
|
|
|
|
html = markdown(self.content)
|
|
|
|
return RE_HTML_TAG.sub(' ', html)
|
|
|
|
|
2020-05-01 22:46:15 +08:00
|
|
|
class Meta:
|
2020-05-05 23:50:48 +08:00
|
|
|
abstract = True
|
2020-07-10 21:28:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Tag(models.Model):
|
|
|
|
content = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.content
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|