2020-02-15 22:38:46 +00:00
|
|
|
''' database schema for books and shelves '''
|
2020-03-30 00:40:51 +00:00
|
|
|
from django.utils import timezone
|
2020-02-11 23:17:21 +00:00
|
|
|
from django.db import models
|
2020-03-27 22:42:44 +00:00
|
|
|
from model_utils.managers import InheritanceManager
|
2020-02-17 03:17:11 +00:00
|
|
|
|
2020-05-10 04:52:13 +00:00
|
|
|
from fedireads import activitypub
|
2020-02-17 03:17:11 +00:00
|
|
|
from fedireads.settings import DOMAIN
|
2020-03-07 06:56:44 +00:00
|
|
|
from fedireads.utils.fields import JSONField, ArrayField
|
2020-05-10 20:38:47 +00:00
|
|
|
from .base_model import FedireadsModel
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-03-27 23:36:52 +00:00
|
|
|
from fedireads.connectors.settings import CONNECTORS
|
2020-02-15 22:38:46 +00:00
|
|
|
|
2020-03-27 23:36:52 +00:00
|
|
|
|
|
|
|
ConnectorFiles = models.TextChoices('ConnectorFiles', CONNECTORS)
|
2020-03-27 21:14:28 +00:00
|
|
|
class Connector(FedireadsModel):
|
|
|
|
''' book data source connectors '''
|
2020-03-27 23:36:52 +00:00
|
|
|
identifier = models.CharField(max_length=255, unique=True)
|
2020-05-03 19:59:06 +00:00
|
|
|
priority = models.IntegerField(default=2)
|
|
|
|
name = models.CharField(max_length=255, null=True)
|
2020-05-03 20:12:42 +00:00
|
|
|
local = models.BooleanField(default=False)
|
2020-03-27 23:36:52 +00:00
|
|
|
connector_file = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
choices=ConnectorFiles.choices
|
|
|
|
)
|
2020-03-27 21:14:28 +00:00
|
|
|
api_key = models.CharField(max_length=255, null=True)
|
|
|
|
|
|
|
|
base_url = models.CharField(max_length=255)
|
2020-05-03 19:59:06 +00:00
|
|
|
books_url = models.CharField(max_length=255)
|
2020-03-27 21:14:28 +00:00
|
|
|
covers_url = models.CharField(max_length=255)
|
|
|
|
search_url = models.CharField(max_length=255, null=True)
|
|
|
|
|
|
|
|
politeness_delay = models.IntegerField(null=True) #seconds
|
|
|
|
max_query_count = models.IntegerField(null=True)
|
|
|
|
# how many queries executed in a unit of time, like a day
|
|
|
|
query_count = models.IntegerField(default=0)
|
|
|
|
# when to reset the query count back to 0 (ie, after 1 day)
|
|
|
|
query_count_expiry = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
2020-03-27 23:36:52 +00:00
|
|
|
class Meta:
|
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.Q(connector_file__in=ConnectorFiles),
|
|
|
|
name='connector_file_valid'
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2020-03-27 21:14:28 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
class Book(FedireadsModel):
|
|
|
|
''' a generic book, which can mean either an edition or a work '''
|
2020-05-04 01:56:29 +00:00
|
|
|
remote_id = models.CharField(max_length=255, null=True)
|
2020-03-07 06:56:44 +00:00
|
|
|
# these identifiers apply to both works and editions
|
2020-03-28 22:06:16 +00:00
|
|
|
openlibrary_key = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
librarything_key = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
goodreads_key = models.CharField(max_length=255, blank=True, null=True)
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
# info about where the data comes from and where/if to sync
|
|
|
|
sync = models.BooleanField(default=True)
|
2020-03-28 22:06:16 +00:00
|
|
|
sync_cover = models.BooleanField(default=True)
|
2020-03-30 00:40:51 +00:00
|
|
|
last_sync_date = models.DateTimeField(default=timezone.now)
|
2020-03-27 21:14:28 +00:00
|
|
|
connector = models.ForeignKey(
|
|
|
|
'Connector', on_delete=models.PROTECT, null=True)
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
# TODO: edit history
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
# book/work metadata
|
|
|
|
title = models.CharField(max_length=255)
|
2020-03-28 22:06:16 +00:00
|
|
|
sort_title = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
subtitle = models.CharField(max_length=255, blank=True, null=True)
|
2020-03-07 06:56:44 +00:00
|
|
|
description = models.TextField(blank=True, null=True)
|
2020-03-30 20:15:49 +00:00
|
|
|
languages = ArrayField(
|
|
|
|
models.CharField(max_length=255), blank=True, default=list
|
|
|
|
)
|
2020-03-07 06:56:44 +00:00
|
|
|
series = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
series_number = models.CharField(max_length=255, blank=True, null=True)
|
2020-03-28 04:28:52 +00:00
|
|
|
subjects = ArrayField(
|
|
|
|
models.CharField(max_length=255), blank=True, default=list
|
|
|
|
)
|
|
|
|
subject_places = ArrayField(
|
|
|
|
models.CharField(max_length=255), blank=True, default=list
|
|
|
|
)
|
2020-03-07 06:56:44 +00:00
|
|
|
# TODO: include an annotation about the type of authorship (ie, translator)
|
2020-02-11 23:17:21 +00:00
|
|
|
authors = models.ManyToManyField('Author')
|
2020-04-29 17:09:14 +00:00
|
|
|
# preformatted authorship string for search and easier display
|
|
|
|
author_text = models.CharField(max_length=255, blank=True, null=True)
|
2020-02-11 23:17:21 +00:00
|
|
|
cover = models.ImageField(upload_to='covers/', blank=True, null=True)
|
2020-03-28 22:06:16 +00:00
|
|
|
first_published_date = models.DateTimeField(blank=True, null=True)
|
|
|
|
published_date = models.DateTimeField(blank=True, null=True)
|
2020-03-27 22:42:44 +00:00
|
|
|
objects = InheritanceManager()
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2020-03-07 21:17:48 +00:00
|
|
|
@property
|
|
|
|
def absolute_id(self):
|
|
|
|
''' constructs the absolute reference to any db object '''
|
2020-05-04 01:07:06 +00:00
|
|
|
if self.sync and self.remote_id:
|
|
|
|
return self.remote_id
|
2020-03-07 21:17:48 +00:00
|
|
|
base_path = 'https://%s' % DOMAIN
|
2020-05-04 01:07:06 +00:00
|
|
|
return '%s/book/%d' % (base_path, self.id)
|
2020-03-07 21:17:48 +00:00
|
|
|
|
2020-03-30 21:12:18 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
''' can't be abstract for query reasons, but you shouldn't USE it '''
|
|
|
|
if not isinstance(self, Edition) and not isinstance(self, Work):
|
|
|
|
raise ValueError('Books should be added as Editions or Works')
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2020-03-24 16:56:53 +00:00
|
|
|
def __repr__(self):
|
2020-03-28 04:28:52 +00:00
|
|
|
return "<{} key={!r} title={!r}>".format(
|
2020-03-27 21:14:28 +00:00
|
|
|
self.__class__,
|
|
|
|
self.openlibrary_key,
|
|
|
|
self.title,
|
|
|
|
)
|
2020-03-24 16:56:53 +00:00
|
|
|
|
2020-05-10 04:52:13 +00:00
|
|
|
@property
|
|
|
|
def activitypub_serialize(self):
|
|
|
|
return activitypub.get_book(self)
|
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
|
|
|
|
class Work(Book):
|
|
|
|
''' a work (an abstract concept of a book that manifests in an edition) '''
|
|
|
|
# library of congress catalog control number
|
2020-03-28 22:06:16 +00:00
|
|
|
lccn = models.CharField(max_length=255, blank=True, null=True)
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2020-03-30 22:03:21 +00:00
|
|
|
@property
|
|
|
|
def default_edition(self):
|
|
|
|
ed = Edition.objects.filter(parent_work=self, default=True).first()
|
|
|
|
if not ed:
|
|
|
|
ed = Edition.objects.filter(parent_work=self).first()
|
|
|
|
return ed
|
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
|
|
|
|
class Edition(Book):
|
|
|
|
''' an edition of a book '''
|
2020-04-29 17:09:14 +00:00
|
|
|
# default -> this is what gets displayed for a work
|
2020-03-30 20:15:49 +00:00
|
|
|
default = models.BooleanField(default=False)
|
2020-05-04 01:56:29 +00:00
|
|
|
|
2020-04-29 17:09:14 +00:00
|
|
|
# these identifiers only apply to editions, not works
|
|
|
|
isbn_10 = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
isbn_13 = models.CharField(max_length=255, blank=True, null=True)
|
2020-03-28 22:06:16 +00:00
|
|
|
oclc_number = models.CharField(max_length=255, blank=True, null=True)
|
2020-04-29 17:09:14 +00:00
|
|
|
asin = models.CharField(max_length=255, blank=True, null=True)
|
2020-03-28 22:06:16 +00:00
|
|
|
pages = models.IntegerField(blank=True, null=True)
|
|
|
|
physical_format = models.CharField(max_length=255, blank=True, null=True)
|
2020-03-28 04:28:52 +00:00
|
|
|
publishers = ArrayField(
|
|
|
|
models.CharField(max_length=255), blank=True, default=list
|
|
|
|
)
|
2020-03-30 21:12:18 +00:00
|
|
|
shelves = models.ManyToManyField(
|
|
|
|
'Shelf',
|
|
|
|
symmetrical=False,
|
|
|
|
through='ShelfBook',
|
|
|
|
through_fields=('book', 'shelf')
|
|
|
|
)
|
|
|
|
parent_work = models.ForeignKey('Work', on_delete=models.PROTECT, null=True)
|
2020-02-17 03:17:11 +00:00
|
|
|
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-02-17 03:17:11 +00:00
|
|
|
class Author(FedireadsModel):
|
2020-02-15 22:38:46 +00:00
|
|
|
''' copy of an author from OL '''
|
2020-05-09 19:29:23 +00:00
|
|
|
remote_id = models.CharField(max_length=255, null=True)
|
2020-03-28 22:06:16 +00:00
|
|
|
openlibrary_key = models.CharField(max_length=255, blank=True, null=True)
|
2020-05-04 01:56:29 +00:00
|
|
|
sync = models.BooleanField(default=True)
|
|
|
|
last_sync_date = models.DateTimeField(default=timezone.now)
|
2020-03-07 06:56:44 +00:00
|
|
|
wikipedia_link = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
# idk probably other keys would be useful here?
|
2020-03-28 22:06:16 +00:00
|
|
|
born = models.DateTimeField(blank=True, null=True)
|
|
|
|
died = models.DateTimeField(blank=True, null=True)
|
2020-03-07 06:56:44 +00:00
|
|
|
name = models.CharField(max_length=255)
|
2020-03-28 22:06:16 +00:00
|
|
|
last_name = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
first_name = models.CharField(max_length=255, blank=True, null=True)
|
2020-03-27 21:14:28 +00:00
|
|
|
aliases = ArrayField(
|
|
|
|
models.CharField(max_length=255), blank=True, default=list
|
|
|
|
)
|
2020-03-07 06:56:44 +00:00
|
|
|
bio = models.TextField(null=True, blank=True)
|
2020-05-10 04:52:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def activitypub_serialize(self):
|
|
|
|
return activitypub.get_author(self)
|