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