forked from mirrors/bookwyrm
Merge pull request #283 from mouse-reeve/default-edition
Migrate default edition field to work model
This commit is contained in:
commit
a44edd8eab
5 changed files with 43 additions and 15 deletions
|
@ -128,9 +128,10 @@ class AbstractConnector(ABC):
|
||||||
if not edition:
|
if not edition:
|
||||||
ed_key = self.get_remote_id_from_data(edition_data)
|
ed_key = self.get_remote_id_from_data(edition_data)
|
||||||
edition = self.create_book(ed_key, edition_data, models.Edition)
|
edition = self.create_book(ed_key, edition_data, models.Edition)
|
||||||
edition.default = True
|
|
||||||
edition.parent_work = work
|
edition.parent_work = work
|
||||||
edition.save()
|
edition.save()
|
||||||
|
work.default_edition = edition
|
||||||
|
work.save()
|
||||||
|
|
||||||
# now's our change to fill in author gaps
|
# now's our change to fill in author gaps
|
||||||
if not edition.authors and work.authors:
|
if not edition.authors and work.authors:
|
||||||
|
|
|
@ -72,7 +72,6 @@ class Connector(AbstractConnector):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_remote_id_from_data(self, data):
|
def get_remote_id_from_data(self, data):
|
||||||
try:
|
try:
|
||||||
key = data['key']
|
key = data['key']
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
''' using a bookwyrm instance as a source of book data '''
|
''' using a bookwyrm instance as a source of book data '''
|
||||||
from django.contrib.postgres.search import SearchRank, SearchVector
|
from django.contrib.postgres.search import SearchRank, SearchVector
|
||||||
|
from django.db.models import F
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
from .abstract_connector import AbstractConnector, SearchResult
|
from .abstract_connector import AbstractConnector, SearchResult
|
||||||
|
@ -30,7 +31,10 @@ class Connector(AbstractConnector):
|
||||||
).filter(
|
).filter(
|
||||||
rank__gt=min_confidence
|
rank__gt=min_confidence
|
||||||
).order_by('-rank')
|
).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 = []
|
search_results = []
|
||||||
for book in results[:10]:
|
for book in results[:10]:
|
||||||
|
|
35
bookwyrm/migrations/0008_work_default_edition.py
Normal file
35
bookwyrm/migrations/0008_work_default_edition.py
Normal 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',
|
||||||
|
),
|
||||||
|
]
|
|
@ -139,29 +139,18 @@ class Work(Book):
|
||||||
''' a work (an abstract concept of a book that manifests in an edition) '''
|
''' a work (an abstract concept of a book that manifests in an edition) '''
|
||||||
# library of congress catalog control number
|
# library of congress catalog control number
|
||||||
lccn = models.CharField(max_length=255, blank=True, null=True)
|
lccn = models.CharField(max_length=255, blank=True, null=True)
|
||||||
|
default_edition = models.ForeignKey('Edition', on_delete=models.PROTECT, null=True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def editions_path(self):
|
def editions_path(self):
|
||||||
''' it'd be nice to serialize the edition instead but, recursion '''
|
''' it'd be nice to serialize the edition instead but, recursion '''
|
||||||
return [e.remote_id for e in self.edition_set.all()]
|
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
|
activity_serializer = activitypub.Work
|
||||||
|
|
||||||
|
|
||||||
class Edition(Book):
|
class Edition(Book):
|
||||||
''' an edition of a 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
|
# these identifiers only apply to editions, not works
|
||||||
isbn_10 = models.CharField(max_length=255, blank=True, null=True)
|
isbn_10 = models.CharField(max_length=255, blank=True, null=True)
|
||||||
isbn_13 = models.CharField(max_length=255, blank=True, null=True)
|
isbn_13 = models.CharField(max_length=255, blank=True, null=True)
|
||||||
|
|
Loading…
Reference in a new issue