Created mixin for ordered collection items

This commit is contained in:
Mouse Reeve
2021-02-04 12:25:07 -08:00
parent 12e0e6a1f0
commit feb8190d8f
4 changed files with 76 additions and 72 deletions

View File

@ -3,7 +3,7 @@ import re
from django.db import models
from bookwyrm import activitypub
from .activitypub_mixin import ActivitypubMixin, OrderedCollectionMixin
from .activitypub_mixin import CollectionItemMixin, OrderedCollectionMixin
from .base_model import BookWyrmModel
from . import fields
@ -49,13 +49,13 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
unique_together = ('user', 'identifier')
class ShelfBook(ActivitypubMixin, BookWyrmModel):
class ShelfBook(CollectionItemMixin, 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(
user = fields.ForeignKey(
'User',
blank=True,
null=True,
@ -64,24 +64,8 @@ class ShelfBook(ActivitypubMixin, BookWyrmModel):
)
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.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()
object_field = 'book'
collection_field = 'shelf'
class Meta: