Merge pull request #283 from mouse-reeve/default-edition

Migrate default edition field to work model
This commit is contained in:
Mouse Reeve 2020-11-04 11:29:51 -08:00 committed by GitHub
commit a44edd8eab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 15 deletions

View file

@ -128,9 +128,10 @@ class AbstractConnector(ABC):
if not edition:
ed_key = self.get_remote_id_from_data(edition_data)
edition = self.create_book(ed_key, edition_data, models.Edition)
edition.default = True
edition.parent_work = work
edition.save()
work.default_edition = edition
work.save()
# now's our change to fill in author gaps
if not edition.authors and work.authors:

View file

@ -72,7 +72,6 @@ class Connector(AbstractConnector):
]
def get_remote_id_from_data(self, data):
try:
key = data['key']

View file

@ -1,5 +1,6 @@
''' using a bookwyrm instance as a source of book data '''
from django.contrib.postgres.search import SearchRank, SearchVector
from django.db.models import F
from bookwyrm import models
from .abstract_connector import AbstractConnector, SearchResult
@ -30,7 +31,10 @@ class Connector(AbstractConnector):
).filter(
rank__gt=min_confidence
).order_by('-rank')
results = results.filter(default=True) or results
# remove non-default editions, if possible
results = results.filter(parent_work__default_edition__id=F('id')) \
or results
search_results = []
for book in results[:10]:

View file

@ -0,0 +1,35 @@
# Generated by Django 3.0.7 on 2020-11-04 18:15
from django.db import migrations, models
import django.db.models.deletion
def set_default_edition(app_registry, schema_editor):
db_alias = schema_editor.connection.alias
works = app_registry.get_model('bookwyrm', 'Work').objects.using(db_alias)
editions = app_registry.get_model('bookwyrm', 'Edition').objects.using(db_alias)
for work in works:
ed = editions.filter(parent_work=work, default=True).first()
if not ed:
ed = editions.filter(parent_work=work).first()
work.default_edition = ed
work.save()
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0007_auto_20201103_0014'),
]
operations = [
migrations.AddField(
model_name='work',
name='default_edition',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='bookwyrm.Edition'),
),
migrations.RunPython(set_default_edition),
migrations.RemoveField(
model_name='edition',
name='default',
),
]

View file

@ -139,29 +139,18 @@ class Work(Book):
''' a work (an abstract concept of a book that manifests in an edition) '''
# library of congress catalog control number
lccn = models.CharField(max_length=255, blank=True, null=True)
default_edition = models.ForeignKey('Edition', on_delete=models.PROTECT, null=True)
@property
def editions_path(self):
''' it'd be nice to serialize the edition instead but, recursion '''
return [e.remote_id for e in self.edition_set.all()]
@property
def default_edition(self):
''' best-guess attempt at picking the default edition for this work '''
ed = Edition.objects.filter(parent_work=self, default=True).first()
if not ed:
ed = Edition.objects.filter(parent_work=self).first()
return ed
activity_serializer = activitypub.Work
class Edition(Book):
''' an edition of a book '''
# default -> this is what gets displayed for a work
default = models.BooleanField(default=False)
# 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)