moviewyrm/bookwyrm/models/list.py

92 lines
2.9 KiB
Python
Raw Normal View History

2021-01-31 05:00:36 +00:00
''' make a list of books!! '''
from django.db import models
from bookwyrm import activitypub
2021-01-31 16:08:52 +00:00
from bookwyrm.settings import DOMAIN
2021-01-31 05:00:36 +00:00
from .base_model import ActivitypubMixin, BookWyrmModel
from .base_model import OrderedCollectionMixin
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'),
)
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 '''
return self.books.all().order_by('listitem')
2021-02-01 01:34:06 +00:00
class Meta:
''' default sorting '''
ordering = ('-updated_date',)
2021-01-31 05:00:36 +00:00
class ListItem(ActivitypubMixin, BookWyrmModel):
''' ok '''
book = fields.ForeignKey(
'Edition', on_delete=models.PROTECT, activitypub_field='object')
book_list = fields.ForeignKey(
'List', on_delete=models.CASCADE, activitypub_field='target')
added_by = fields.ForeignKey(
'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')
activity_serializer = activitypub.AddBook
def to_add_activity(self, user):
''' AP for shelving a book'''
return activitypub.Add(
id='%s#add' % self.remote_id,
actor=user.remote_id,
object=self.book.to_activity(),
target=self.book_list.remote_id,
).serialize()
def to_remove_activity(self, user):
''' AP for un-shelving a book'''
return activitypub.Remove(
id='%s#remove' % self.remote_id,
actor=user.remote_id,
object=self.book.to_activity(),
2021-01-31 21:31:49 +00:00
target=self.book_list.remote_id
2021-01-31 05:00:36 +00:00
).serialize()
class Meta:
''' an opinionated constraint! you can't put a book on a list twice '''
unique_together = ('book', 'book_list')
ordering = ('-created_date',)