bookwyrm/fedireads/models/book.py

80 lines
2.3 KiB
Python
Raw Normal View History

2020-02-15 22:38:46 +00:00
''' database schema for books and shelves '''
2020-02-11 23:17:21 +00:00
from django.db import models
2020-02-17 03:17:11 +00:00
from fedireads.settings import DOMAIN
2020-02-15 22:19:36 +00:00
from fedireads.utils.fields import JSONField
2020-02-17 03:17:11 +00:00
from fedireads.utils.models import FedireadsModel
2020-02-11 23:17:21 +00:00
2020-02-15 22:38:46 +00:00
2020-02-17 03:17:11 +00:00
class Shelf(FedireadsModel):
2020-02-11 23:17:21 +00:00
name = models.CharField(max_length=100)
2020-02-15 22:38:46 +00:00
identifier = models.CharField(max_length=100)
2020-02-11 23:17:21 +00:00
user = models.ForeignKey('User', on_delete=models.PROTECT)
editable = models.BooleanField(default=True)
books = models.ManyToManyField(
'Book',
symmetrical=False,
through='ShelfBook',
through_fields=('shelf', 'book')
)
2020-02-18 01:53:40 +00:00
@property
def absolute_id(self):
''' use shelf identifier as absolute id '''
base_path = self.user.absolute_id
model_name = type(self).__name__.lower()
return '%s/%s/%s' % (base_path, model_name, self.identifier)
2020-02-11 23:17:21 +00:00
class Meta:
2020-02-15 22:38:46 +00:00
unique_together = ('user', 'identifier')
2020-02-11 23:17:21 +00:00
2020-02-17 03:17:11 +00:00
class ShelfBook(FedireadsModel):
2020-02-11 23:17:21 +00:00
# many to many join table for books and shelves
book = models.ForeignKey('Book', on_delete=models.PROTECT)
shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT)
added_by = models.ForeignKey(
'User',
blank=True,
null=True,
on_delete=models.PROTECT
)
class Meta:
unique_together = ('book', 'shelf')
2020-02-17 03:17:11 +00:00
class Book(FedireadsModel):
2020-02-11 23:17:21 +00:00
''' a non-canonical copy of a work (not book) from open library '''
openlibrary_key = models.CharField(max_length=255, unique=True)
data = JSONField()
authors = models.ManyToManyField('Author')
# TODO: also store cover thumbnail
cover = models.ImageField(upload_to='covers/', blank=True, null=True)
shelves = models.ManyToManyField(
'Shelf',
symmetrical=False,
through='ShelfBook',
through_fields=('book', 'shelf')
)
added_by = models.ForeignKey(
'User',
blank=True,
null=True,
on_delete=models.PROTECT
)
2020-02-17 03:17:11 +00:00
@property
def absolute_id(self):
''' constructs the absolute reference to any db object '''
base_path = 'https://%s' % DOMAIN
2020-02-18 01:53:40 +00:00
model_name = type(self).__name__.lower()
return '%s/%s/%s' % (base_path, model_name, self.openlibrary_key)
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-02-11 23:17:21 +00:00
openlibrary_key = models.CharField(max_length=255)
data = JSONField()