moviewyrm/fedireads/models/status.py

105 lines
3.5 KiB
Python
Raw Normal View History

2020-02-11 23:17:21 +00:00
''' models for storing different kinds of Activities '''
from datetime import datetime
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
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):
''' any post, like a reply to a review, etc '''
user = models.ForeignKey('User', on_delete=models.PROTECT)
status_type = models.CharField(max_length=255, default='Note')
content = models.TextField(blank=True, null=True)
2020-02-17 02:45:25 +00:00
mention_users = models.ManyToManyField('User', related_name='mention_user')
mention_books = models.ManyToManyField('Book', related_name='mention_book')
activity_type = models.CharField(max_length=255, default='Note')
2020-02-15 22:38:46 +00:00
local = models.BooleanField(default=True)
privacy = models.CharField(max_length=255, default='public')
sensitive = models.BooleanField(default=False)
# the created date can't be this, because of receiving federated posts
published_date = models.DateTimeField(default=datetime.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'
)
reply_parent = models.ForeignKey(
'self',
null=True,
on_delete=models.PROTECT
)
objects = InheritanceManager()
class Review(Status):
''' a book review '''
name = models.CharField(max_length=255)
book = models.ForeignKey('Book', on_delete=models.PROTECT)
rating = models.IntegerField(
default=0,
validators=[MinValueValidator(0), MaxValueValidator(5)]
)
2020-02-11 23:17:21 +00:00
def save(self, *args, **kwargs):
self.status_type = 'Review'
self.activity_type = 'Article'
2020-02-11 23:17:21 +00:00
super().save(*args, **kwargs)
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)
class Meta:
unique_together = ('user', 'status')
2020-02-21 06:19:19 +00:00
class Tag(FedireadsModel):
''' freeform tags for books '''
user = models.ForeignKey('User', on_delete=models.PROTECT)
book = models.ForeignKey('Book', 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
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
class Notification(FedireadsModel):
''' you've been tagged, liked, followed, etc '''
user = models.ForeignKey('User', on_delete=models.PROTECT)
related_book = models.ForeignKey(
'Book', on_delete=models.PROTECT, null=True)
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)
notification_type = models.CharField(max_length=255)
def save(self, *args, **kwargs):
# TODO: there's probably a real way to do enums
types = [
'FAVORITE',
'REPLY',
'TAG',
'FOLLOW'
]
if not self.notification_type in types:
raise ValueError('Invalid notitication type')
super().save(*args, **kwargs)