2020-03-07 06:56:44 +00:00
|
|
|
''' puttin' books on shelves '''
|
|
|
|
from django.db import models
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import activitypub
|
2020-09-21 15:16:34 +00:00
|
|
|
from .base_model import BookWyrmModel, OrderedCollectionMixin
|
2020-03-07 06:56:44 +00:00
|
|
|
|
|
|
|
|
2020-09-21 15:16:34 +00:00
|
|
|
class Shelf(OrderedCollectionMixin, BookWyrmModel):
|
2020-09-17 20:02:52 +00:00
|
|
|
''' a list of books owned by a user '''
|
2020-03-07 06:56:44 +00:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
identifier = models.CharField(max_length=100)
|
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
|
|
|
editable = models.BooleanField(default=True)
|
|
|
|
books = models.ManyToManyField(
|
2020-03-30 21:12:18 +00:00
|
|
|
'Edition',
|
2020-03-07 06:56:44 +00:00
|
|
|
symmetrical=False,
|
|
|
|
through='ShelfBook',
|
|
|
|
through_fields=('shelf', 'book')
|
|
|
|
)
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
@property
|
|
|
|
def collection_queryset(self):
|
|
|
|
''' list of books for this shelf, overrides OrderedCollectionMixin '''
|
|
|
|
return self.books
|
|
|
|
|
2020-05-13 01:56:28 +00:00
|
|
|
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:
|
2020-09-17 20:02:52 +00:00
|
|
|
''' user/shelf unqiueness '''
|
2020-03-07 06:56:44 +00:00
|
|
|
unique_together = ('user', 'identifier')
|
|
|
|
|
|
|
|
|
2020-09-21 15:16:34 +00:00
|
|
|
class ShelfBook(BookWyrmModel):
|
2020-09-17 20:02:52 +00:00
|
|
|
''' many to many join table for books and shelves '''
|
2020-03-30 21:12:18 +00:00
|
|
|
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
2020-03-07 06:56:44 +00:00
|
|
|
shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT)
|
|
|
|
added_by = models.ForeignKey(
|
|
|
|
'User',
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
on_delete=models.PROTECT
|
|
|
|
)
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
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.to_activity()
|
|
|
|
).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:
|
2020-09-17 20:02:52 +00:00
|
|
|
''' an opinionated constraint!
|
|
|
|
you can't put a book on shelf twice '''
|
2020-03-07 06:56:44 +00:00
|
|
|
unique_together = ('book', 'shelf')
|