2021-01-31 05:00:36 +00:00
|
|
|
''' make a list of books!! '''
|
2021-02-10 21:46:56 +00:00
|
|
|
from django.apps import apps
|
2021-01-31 05:00:36 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
from bookwyrm import activitypub
|
2021-01-31 16:08:52 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
2021-02-04 20:25:07 +00:00
|
|
|
from .activitypub_mixin import CollectionItemMixin, OrderedCollectionMixin
|
2021-02-04 18:47:03 +00:00
|
|
|
from .base_model import BookWyrmModel
|
2021-01-31 05:00:36 +00:00
|
|
|
from . import fields
|
|
|
|
|
|
|
|
|
|
|
|
CurationType = models.TextChoices('Curation', [
|
|
|
|
'closed',
|
|
|
|
'open',
|
2021-01-31 16:41:11 +00:00
|
|
|
'curated',
|
2021-01-31 05:00:36 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
class List(OrderedCollectionMixin, BookWyrmModel):
|
|
|
|
''' a list of books '''
|
|
|
|
name = fields.CharField(max_length=100)
|
|
|
|
user = fields.ForeignKey(
|
|
|
|
'User', on_delete=models.PROTECT, activitypub_field='owner')
|
2021-02-02 17:37:46 +00:00
|
|
|
description = fields.TextField(
|
|
|
|
blank=True, null=True, activitypub_field='summary')
|
|
|
|
privacy = fields.PrivacyField()
|
2021-01-31 05:00:36 +00:00
|
|
|
curation = fields.CharField(
|
|
|
|
max_length=255,
|
|
|
|
default='closed',
|
|
|
|
choices=CurationType.choices
|
|
|
|
)
|
|
|
|
books = models.ManyToManyField(
|
|
|
|
'Edition',
|
|
|
|
symmetrical=False,
|
|
|
|
through='ListItem',
|
|
|
|
through_fields=('book_list', 'book'),
|
|
|
|
)
|
2021-02-02 19:05:47 +00:00
|
|
|
activity_serializer = activitypub.BookList
|
2021-01-31 16:08:52 +00:00
|
|
|
|
|
|
|
def get_remote_id(self):
|
|
|
|
''' don't want the user to be in there in this case '''
|
|
|
|
return 'https://%s/list/%d' % (DOMAIN, self.id)
|
|
|
|
|
2021-01-31 05:00:36 +00:00
|
|
|
@property
|
|
|
|
def collection_queryset(self):
|
|
|
|
''' list of books for this shelf, overrides OrderedCollectionMixin '''
|
2021-02-09 19:40:35 +00:00
|
|
|
return self.books.filter(
|
|
|
|
listitem__approved=True
|
|
|
|
).all().order_by('listitem')
|
2021-01-31 05:00:36 +00:00
|
|
|
|
2021-02-01 01:34:06 +00:00
|
|
|
class Meta:
|
|
|
|
''' default sorting '''
|
|
|
|
ordering = ('-updated_date',)
|
|
|
|
|
2021-01-31 05:00:36 +00:00
|
|
|
|
2021-02-04 20:25:07 +00:00
|
|
|
class ListItem(CollectionItemMixin, BookWyrmModel):
|
2021-01-31 05:00:36 +00:00
|
|
|
''' ok '''
|
|
|
|
book = fields.ForeignKey(
|
|
|
|
'Edition', on_delete=models.PROTECT, activitypub_field='object')
|
|
|
|
book_list = fields.ForeignKey(
|
|
|
|
'List', on_delete=models.CASCADE, activitypub_field='target')
|
2021-02-04 20:25:07 +00:00
|
|
|
user = fields.ForeignKey(
|
2021-01-31 05:00:36 +00:00
|
|
|
'User',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
activitypub_field='actor'
|
|
|
|
)
|
|
|
|
notes = fields.TextField(blank=True, null=True)
|
|
|
|
approved = models.BooleanField(default=True)
|
|
|
|
order = fields.IntegerField(blank=True, null=True)
|
|
|
|
endorsement = models.ManyToManyField('User', related_name='endorsers')
|
|
|
|
|
2021-02-11 03:17:01 +00:00
|
|
|
activity_serializer = activitypub.AddListItem
|
2021-02-04 20:25:07 +00:00
|
|
|
object_field = 'book'
|
|
|
|
collection_field = 'book_list'
|
2021-01-31 05:00:36 +00:00
|
|
|
|
2021-02-10 21:46:56 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
''' create a notification too '''
|
|
|
|
created = not bool(self.id)
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
list_owner = self.book_list.user
|
|
|
|
# create a notification if somoene ELSE added to a local user's list
|
|
|
|
if created and list_owner.local and list_owner != self.user:
|
|
|
|
model = apps.get_model('bookwyrm.Notification', require_ready=True)
|
|
|
|
model.objects.create(
|
|
|
|
user=list_owner,
|
|
|
|
related_user=self.user,
|
|
|
|
related_list_item=self,
|
|
|
|
notification_type='ADD',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-01-31 05:00:36 +00:00
|
|
|
class Meta:
|
|
|
|
''' an opinionated constraint! you can't put a book on a list twice '''
|
|
|
|
unique_together = ('book', 'book_list')
|
|
|
|
ordering = ('-created_date',)
|