2021-03-08 16:49:10 +00:00
|
|
|
""" make a list of books!! """
|
2021-02-10 21:46:56 +00:00
|
|
|
from django.apps import apps
|
2021-01-31 05:00:36 +00:00
|
|
|
from django.db import models
|
2021-03-30 17:28:50 +00:00
|
|
|
from django.utils import timezone
|
2021-01-31 05:00:36 +00:00
|
|
|
|
|
|
|
from bookwyrm import activitypub
|
2021-01-31 16:08:52 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
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
|
2021-01-31 05:00:36 +00:00
|
|
|
from . import fields
|
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
CurationType = models.TextChoices(
|
|
|
|
"Curation",
|
|
|
|
[
|
|
|
|
"closed",
|
|
|
|
"open",
|
|
|
|
"curated",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-01-31 05:00:36 +00:00
|
|
|
|
|
|
|
class List(OrderedCollectionMixin, BookWyrmModel):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""a list of books"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-31 05:00:36 +00:00
|
|
|
name = fields.CharField(max_length=100)
|
|
|
|
user = fields.ForeignKey(
|
2021-03-08 16:49:10 +00:00
|
|
|
"User", on_delete=models.PROTECT, activitypub_field="owner"
|
|
|
|
)
|
|
|
|
description = fields.TextField(blank=True, null=True, activitypub_field="summary")
|
2021-02-02 17:37:46 +00:00
|
|
|
privacy = fields.PrivacyField()
|
2021-01-31 05:00:36 +00:00
|
|
|
curation = fields.CharField(
|
2021-03-08 16:49:10 +00:00
|
|
|
max_length=255, default="closed", choices=CurationType.choices
|
2021-01-31 05:00:36 +00:00
|
|
|
)
|
|
|
|
books = models.ManyToManyField(
|
2021-03-08 16:49:10 +00:00
|
|
|
"Edition",
|
2021-01-31 05:00:36 +00:00
|
|
|
symmetrical=False,
|
2021-03-08 16:49:10 +00:00
|
|
|
through="ListItem",
|
|
|
|
through_fields=("book_list", "book"),
|
2021-01-31 05:00:36 +00:00
|
|
|
)
|
2021-02-02 19:05:47 +00:00
|
|
|
activity_serializer = activitypub.BookList
|
2021-01-31 16:08:52 +00:00
|
|
|
|
|
|
|
def get_remote_id(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""don't want the user to be in there in this case"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return "https://%s/list/%d" % (DOMAIN, self.id)
|
2021-01-31 16:08:52 +00:00
|
|
|
|
2021-01-31 05:00:36 +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.filter(listitem__approved=True).order_by("listitem")
|
2021-01-31 05:00:36 +00:00
|
|
|
|
2021-02-01 01:34:06 +00:00
|
|
|
class Meta:
|
2021-04-26 16:15:42 +00:00
|
|
|
"""default sorting"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
ordering = ("-updated_date",)
|
2021-02-01 01:34:06 +00:00
|
|
|
|
2021-01-31 05:00:36 +00:00
|
|
|
|
2021-02-04 20:25:07 +00:00
|
|
|
class ListItem(CollectionItemMixin, BookWyrmModel):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""ok"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-31 05:00:36 +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
|
|
|
book_list = models.ForeignKey("List", on_delete=models.CASCADE)
|
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"
|
2021-01-31 05:00:36 +00:00
|
|
|
)
|
|
|
|
notes = fields.TextField(blank=True, null=True)
|
|
|
|
approved = models.BooleanField(default=True)
|
2021-04-08 16:05:21 +00:00
|
|
|
order = fields.IntegerField()
|
2021-03-08 16:49:10 +00:00
|
|
|
endorsement = models.ManyToManyField("User", related_name="endorsers")
|
2021-01-31 05:00:36 +00:00
|
|
|
|
2021-04-08 21:17:29 +00:00
|
|
|
activity_serializer = activitypub.ListItem
|
2021-03-08 16:49:10 +00:00
|
|
|
collection_field = "book_list"
|
2021-01-31 05:00:36 +00:00
|
|
|
|
2021-02-10 21:46:56 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""create a notification too"""
|
2021-02-10 21:46:56 +00:00
|
|
|
created = not bool(self.id)
|
|
|
|
super().save(*args, **kwargs)
|
2021-03-30 17:28:50 +00:00
|
|
|
# tick the updated date on the parent list
|
|
|
|
self.book_list.updated_date = timezone.now()
|
|
|
|
self.book_list.save(broadcast=False)
|
|
|
|
|
2021-02-10 21:46:56 +00:00
|
|
|
list_owner = self.book_list.user
|
|
|
|
# create a notification if somoene ELSE added to a local user's list
|
|
|
|
if created and list_owner.local and list_owner != self.user:
|
2021-03-08 16:49:10 +00:00
|
|
|
model = apps.get_model("bookwyrm.Notification", require_ready=True)
|
2021-02-10 21:46:56 +00:00
|
|
|
model.objects.create(
|
|
|
|
user=list_owner,
|
|
|
|
related_user=self.user,
|
|
|
|
related_list_item=self,
|
2021-03-08 16:49:10 +00:00
|
|
|
notification_type="ADD",
|
2021-02-10 21:46:56 +00:00
|
|
|
)
|
|
|
|
|
2021-01-31 05:00:36 +00:00
|
|
|
class Meta:
|
2021-04-08 16:05:21 +00:00
|
|
|
# A book may only be placed into a list once, and each order in the list may be used only
|
|
|
|
# once
|
|
|
|
unique_together = (("book", "book_list"), ("order", "book_list"))
|
2021-03-08 16:49:10 +00:00
|
|
|
ordering = ("-created_date",)
|