forked from mirrors/bookwyrm
870d0b9697
Works on #55
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
''' models for storing different kinds of Activities '''
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
|
from django.db import models
|
|
from django.dispatch import receiver
|
|
from model_utils.managers import InheritanceManager
|
|
|
|
from fedireads.utils.models import FedireadsModel
|
|
|
|
|
|
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)
|
|
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')
|
|
local = models.BooleanField(default=True)
|
|
privacy = models.CharField(max_length=255, default='public')
|
|
sensitive = models.BooleanField(default=False)
|
|
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)]
|
|
)
|
|
|
|
def save(self, *args, **kwargs):
|
|
self.status_type = 'Review'
|
|
self.activity_type = 'Article'
|
|
super().save(*args, **kwargs)
|
|
|
|
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')
|
|
|
|
|
|
class Tag(FedireadsModel):
|
|
''' freeform tags for books '''
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
|
book = models.ForeignKey('Book', on_delete=models.PROTECT)
|
|
name = models.CharField(max_length=140)
|
|
|
|
class Meta:
|
|
unique_together = ('user', 'book', 'name')
|
|
|