moviewyrm/bookwyrm/models/shelf.py

92 lines
2.9 KiB
Python
Raw Normal View History

2020-03-07 06:56:44 +00:00
''' puttin' books on shelves '''
2020-11-10 22:52:04 +00:00
import re
2020-03-07 06:56:44 +00:00
from django.db import models
from bookwyrm import activitypub
2020-12-16 02:57:17 +00:00
from .base_model import ActivitypubMixin, BookWyrmModel
from .base_model import OrderedCollectionMixin
from . import fields
2020-03-07 06:56:44 +00:00
2020-09-21 15:16:34 +00:00
class Shelf(OrderedCollectionMixin, BookWyrmModel):
''' a list of books owned by a user '''
name = fields.CharField(max_length=100)
2020-03-07 06:56:44 +00:00
identifier = models.CharField(max_length=100)
user = fields.ForeignKey(
'User', on_delete=models.PROTECT, activitypub_field='owner')
2020-03-07 06:56:44 +00:00
editable = models.BooleanField(default=True)
2021-02-02 17:37:46 +00:00
privacy = fields.PrivacyField()
2020-03-07 06:56:44 +00:00
books = models.ManyToManyField(
'Edition',
2020-03-07 06:56:44 +00:00
symmetrical=False,
through='ShelfBook',
through_fields=('shelf', 'book')
)
activity_serializer = activitypub.Shelf
2020-11-10 22:52:04 +00:00
def save(self, *args, **kwargs):
''' set the identifier '''
saved = super().save(*args, **kwargs)
if not self.identifier:
2020-11-10 22:56:53 +00:00
slug = re.sub(r'[^\w]', '', self.name).lower()
2020-11-10 22:52:04 +00:00
self.identifier = '%s-%d' % (slug, self.id)
return super().save(*args, **kwargs)
return saved
@property
def collection_queryset(self):
''' list of books for this shelf, overrides OrderedCollectionMixin '''
return self.books.all().order_by('shelfbook')
def get_remote_id(self):
''' shelf identifier instead of id '''
base_path = self.user.remote_id
return '%s/shelf/%s' % (base_path, self.identifier)
2020-03-07 06:56:44 +00:00
class Meta:
''' user/shelf unqiueness '''
2020-03-07 06:56:44 +00:00
unique_together = ('user', 'identifier')
2020-12-16 02:57:17 +00:00
class ShelfBook(ActivitypubMixin, BookWyrmModel):
''' many to many join table for books and shelves '''
book = fields.ForeignKey(
'Edition', on_delete=models.PROTECT, activitypub_field='object')
shelf = fields.ForeignKey(
'Shelf', on_delete=models.PROTECT, activitypub_field='target')
added_by = fields.ForeignKey(
2020-03-07 06:56:44 +00:00
'User',
blank=True,
null=True,
on_delete=models.PROTECT,
activitypub_field='actor'
2020-03-07 06:56:44 +00:00
)
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,
2020-11-08 00:11:12 +00:00
object=self.book.to_activity(),
2020-11-02 23:10:41 +00:00
target=self.shelf.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(),
target=self.shelf.to_activity()
).serialize()
2020-03-07 06:56:44 +00:00
class Meta:
''' an opinionated constraint!
you can't put a book on shelf twice '''
2020-03-07 06:56:44 +00:00
unique_together = ('book', 'shelf')
ordering = ('-created_date',)