2021-03-08 16:49:10 +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
|
2021-07-14 04:02:34 +00:00
|
|
|
from django.utils import timezone
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import activitypub
|
2021-02-04 20:25:07 +00:00
|
|
|
from .activitypub_mixin import CollectionItemMixin, OrderedCollectionMixin
|
2021-02-04 18:47:03 +00:00
|
|
|
from .base_model import BookWyrmModel
|
2020-11-30 22:54:45 +00:00
|
|
|
from . import fields
|
2020-03-07 06:56:44 +00:00
|
|
|
|
|
|
|
|
2020-09-21 15:16:34 +00:00
|
|
|
class Shelf(OrderedCollectionMixin, BookWyrmModel):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""a list of books owned by a user"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-03-25 23:57:20 +00:00
|
|
|
TO_READ = "to-read"
|
|
|
|
READING = "reading"
|
|
|
|
READ_FINISHED = "read"
|
|
|
|
|
|
|
|
READ_STATUS_IDENTIFIERS = (TO_READ, READING, READ_FINISHED)
|
|
|
|
|
2020-11-30 22:54:45 +00:00
|
|
|
name = fields.CharField(max_length=100)
|
2020-03-07 06:56:44 +00:00
|
|
|
identifier = models.CharField(max_length=100)
|
2020-11-30 22:54:45 +00:00
|
|
|
user = fields.ForeignKey(
|
2021-03-08 16:49:10 +00:00
|
|
|
"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(
|
2021-03-08 16:49:10 +00:00
|
|
|
"Edition",
|
2020-03-07 06:56:44 +00:00
|
|
|
symmetrical=False,
|
2021-03-08 16:49:10 +00:00
|
|
|
through="ShelfBook",
|
|
|
|
through_fields=("shelf", "book"),
|
2020-03-07 06:56:44 +00:00
|
|
|
)
|
|
|
|
|
2021-02-02 19:17:31 +00:00
|
|
|
activity_serializer = activitypub.Shelf
|
|
|
|
|
2020-11-10 22:52:04 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""set the identifier"""
|
2021-02-07 05:00:08 +00:00
|
|
|
super().save(*args, **kwargs)
|
2020-11-10 22:52:04 +00:00
|
|
|
if not self.identifier:
|
2021-03-31 18:04:20 +00:00
|
|
|
self.identifier = self.get_identifier()
|
|
|
|
super().save(*args, **kwargs, broadcast=False)
|
|
|
|
|
|
|
|
def get_identifier(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""custom-shelf-123 for the url"""
|
2021-03-31 18:04:20 +00:00
|
|
|
slug = re.sub(r"[^\w]", "", self.name).lower()
|
|
|
|
return "{:s}-{:d}".format(slug, self.id)
|
2020-11-10 22:52:04 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
@property
|
|
|
|
def collection_queryset(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""list of books for this shelf, overrides OrderedCollectionMixin"""
|
2021-04-19 21:47:59 +00:00
|
|
|
return self.books.order_by("shelfbook")
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2020-05-13 01:56:28 +00:00
|
|
|
def get_remote_id(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""shelf identifier instead of id"""
|
2020-05-13 01:56:28 +00:00
|
|
|
base_path = self.user.remote_id
|
2021-03-31 18:04:20 +00:00
|
|
|
identifier = self.identifier or self.get_identifier()
|
|
|
|
return "%s/books/%s" % (base_path, identifier)
|
2020-03-07 06:56:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
2021-04-26 16:15:42 +00:00
|
|
|
"""user/shelf unqiueness"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
unique_together = ("user", "identifier")
|
2020-03-07 06:56:44 +00:00
|
|
|
|
|
|
|
|
2021-02-04 20:25:07 +00:00
|
|
|
class ShelfBook(CollectionItemMixin, BookWyrmModel):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""many to many join table for books and shelves"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-11-30 22:54:45 +00:00
|
|
|
book = fields.ForeignKey(
|
2021-04-08 21:47:38 +00:00
|
|
|
"Edition", on_delete=models.PROTECT, activitypub_field="book"
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-04-08 21:53:45 +00:00
|
|
|
shelf = models.ForeignKey("Shelf", on_delete=models.PROTECT)
|
2021-07-14 04:02:34 +00:00
|
|
|
shelved_date = models.DateTimeField(default=timezone.now)
|
2021-02-04 20:25:07 +00:00
|
|
|
user = fields.ForeignKey(
|
2021-03-08 16:49:10 +00:00
|
|
|
"User", on_delete=models.PROTECT, activitypub_field="actor"
|
|
|
|
)
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2021-04-08 21:17:29 +00:00
|
|
|
activity_serializer = activitypub.ShelfItem
|
2021-04-08 21:47:38 +00:00
|
|
|
collection_field = "shelf"
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2021-03-16 20:37:22 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.user:
|
|
|
|
self.user = self.shelf.user
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
class Meta:
|
2021-03-08 16:49:10 +00:00
|
|
|
"""an opinionated constraint!
|
|
|
|
you can't put a book on shelf twice"""
|
|
|
|
|
|
|
|
unique_together = ("book", "shelf")
|
2021-07-14 04:02:34 +00:00
|
|
|
ordering = ("-shelved_date", "-created_date", "-updated_date")
|