2020-02-11 23:17:21 +00:00
|
|
|
''' models for storing different kinds of Activities '''
|
2020-03-30 00:40:51 +00:00
|
|
|
from django.utils import timezone
|
2020-02-11 23:17:21 +00:00
|
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
|
|
|
from django.db import models
|
|
|
|
from model_utils.managers import InheritanceManager
|
2020-03-07 21:29:57 +00:00
|
|
|
import urllib.parse
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-02-17 03:17:11 +00:00
|
|
|
from fedireads.utils.models import FedireadsModel
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-02-17 03:17:11 +00:00
|
|
|
|
|
|
|
class Status(FedireadsModel):
|
2020-02-17 02:22:01 +00:00
|
|
|
''' any post, like a reply to a review, etc '''
|
2020-03-10 19:04:38 +00:00
|
|
|
remote_id = models.CharField(max_length=255, unique=True, null=True)
|
2020-02-15 19:13:49 +00:00
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
|
|
|
status_type = models.CharField(max_length=255, default='Note')
|
2020-02-21 01:33:50 +00:00
|
|
|
content = models.TextField(blank=True, null=True)
|
2020-02-17 02:45:25 +00:00
|
|
|
mention_users = models.ManyToManyField('User', related_name='mention_user')
|
2020-03-30 21:12:18 +00:00
|
|
|
mention_books = models.ManyToManyField('Edition', related_name='mention_book')
|
2020-02-17 02:22:01 +00:00
|
|
|
activity_type = models.CharField(max_length=255, default='Note')
|
2020-02-15 22:38:46 +00:00
|
|
|
local = models.BooleanField(default=True)
|
2020-02-17 02:22:01 +00:00
|
|
|
privacy = models.CharField(max_length=255, default='public')
|
|
|
|
sensitive = models.BooleanField(default=False)
|
2020-03-07 21:29:57 +00:00
|
|
|
# the created date can't be this, because of receiving federated posts
|
2020-03-30 00:40:51 +00:00
|
|
|
published_date = models.DateTimeField(default=timezone.now)
|
2020-02-19 07:26:42 +00:00
|
|
|
favorites = models.ManyToManyField(
|
|
|
|
'User',
|
|
|
|
symmetrical=False,
|
|
|
|
through='Favorite',
|
|
|
|
through_fields=('status', 'user'),
|
|
|
|
related_name='user_favorites'
|
|
|
|
)
|
2020-02-15 19:13:49 +00:00
|
|
|
reply_parent = models.ForeignKey(
|
|
|
|
'self',
|
|
|
|
null=True,
|
|
|
|
on_delete=models.PROTECT
|
|
|
|
)
|
|
|
|
objects = InheritanceManager()
|
|
|
|
|
2020-03-10 19:04:38 +00:00
|
|
|
@property
|
|
|
|
def absolute_id(self):
|
|
|
|
''' constructs the absolute reference to any db object '''
|
|
|
|
if self.remote_id:
|
|
|
|
return self.remote_id
|
|
|
|
base_path = self.user.absolute_id
|
|
|
|
model_name = type(self).__name__.lower()
|
|
|
|
return '%s/%s/%d' % (base_path, model_name, self.id)
|
|
|
|
|
|
|
|
|
2020-03-21 23:50:49 +00:00
|
|
|
class Comment(Status):
|
|
|
|
''' like a review but without a rating and transient '''
|
2020-03-30 21:12:18 +00:00
|
|
|
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
2020-03-21 23:50:49 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.status_type = 'Comment'
|
2020-04-01 18:22:22 +00:00
|
|
|
self.activity_type = 'Note'
|
2020-03-21 23:50:49 +00:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2020-02-15 19:13:49 +00:00
|
|
|
class Review(Status):
|
|
|
|
''' a book review '''
|
|
|
|
name = models.CharField(max_length=255)
|
2020-03-30 21:12:18 +00:00
|
|
|
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
2020-02-15 19:13:49 +00:00
|
|
|
rating = models.IntegerField(
|
2020-04-01 18:22:22 +00:00
|
|
|
default=None,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
validators=[MinValueValidator(1), MaxValueValidator(5)]
|
2020-02-15 19:13:49 +00:00
|
|
|
)
|
|
|
|
|
2020-02-11 23:17:21 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2020-02-15 19:13:49 +00:00
|
|
|
self.status_type = 'Review'
|
2020-02-19 01:30:23 +00:00
|
|
|
self.activity_type = 'Article'
|
2020-02-11 23:17:21 +00:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2020-03-07 21:29:57 +00:00
|
|
|
|
2020-02-19 07:26:42 +00:00
|
|
|
class Favorite(FedireadsModel):
|
|
|
|
''' fav'ing a post '''
|
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
|
|
|
status = models.ForeignKey('Status', on_delete=models.PROTECT)
|
2020-03-21 22:21:27 +00:00
|
|
|
remote_id = models.CharField(max_length=255, unique=True, null=True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def absolute_id(self):
|
|
|
|
''' constructs the absolute reference to any db object '''
|
|
|
|
if self.remote_id:
|
|
|
|
return self.remote_id
|
|
|
|
return super().absolute_id
|
2020-02-19 08:22:55 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('user', 'status')
|
|
|
|
|
2020-02-21 06:19:19 +00:00
|
|
|
|
2020-03-30 14:13:32 +00:00
|
|
|
class Boost(Status):
|
|
|
|
''' boost'ing a post '''
|
|
|
|
boosted_status = models.ForeignKey(
|
|
|
|
'Status',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name="boosters")
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.status_type = 'Boost'
|
|
|
|
self.activity_type = 'Announce'
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
# This constraint can't work as it would cross tables.
|
|
|
|
# class Meta:
|
|
|
|
# unique_together = ('user', 'boosted_status')
|
|
|
|
|
2020-02-21 06:19:19 +00:00
|
|
|
class Tag(FedireadsModel):
|
|
|
|
''' freeform tags for books '''
|
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
2020-03-30 21:12:18 +00:00
|
|
|
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
2020-02-21 17:10:27 +00:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
identifier = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.id:
|
|
|
|
# add identifiers to new tags
|
2020-03-07 21:29:57 +00:00
|
|
|
self.identifier = urllib.parse.quote_plus(self.name)
|
2020-02-21 17:10:27 +00:00
|
|
|
super().save(*args, **kwargs)
|
2020-02-21 06:19:19 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('user', 'book', 'name')
|
|
|
|
|
2020-03-07 22:50:29 +00:00
|
|
|
|
2020-03-13 13:38:09 +00:00
|
|
|
NotificationType = models.TextChoices(
|
2020-03-30 14:13:32 +00:00
|
|
|
'NotificationType', 'FAVORITE REPLY TAG FOLLOW FOLLOW_REQUEST BOOST')
|
2020-03-13 13:38:09 +00:00
|
|
|
|
2020-03-07 22:50:29 +00:00
|
|
|
class Notification(FedireadsModel):
|
|
|
|
''' you've been tagged, liked, followed, etc '''
|
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
|
|
|
related_book = models.ForeignKey(
|
2020-03-30 21:12:18 +00:00
|
|
|
'Edition', on_delete=models.PROTECT, null=True)
|
2020-03-07 22:50:29 +00:00
|
|
|
related_user = models.ForeignKey(
|
|
|
|
'User',
|
|
|
|
on_delete=models.PROTECT, null=True, related_name='related_user')
|
|
|
|
related_status = models.ForeignKey(
|
|
|
|
'Status', on_delete=models.PROTECT, null=True)
|
|
|
|
read = models.BooleanField(default=False)
|
2020-03-13 13:38:09 +00:00
|
|
|
notification_type = models.CharField(
|
|
|
|
max_length=255, choices=NotificationType.choices)
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.Q(notification_type__in=NotificationType.values),
|
|
|
|
name="notification_type_valid",
|
|
|
|
)
|
2020-03-07 22:50:29 +00:00
|
|
|
]
|