From c4b9b82000b9e91875229803b39584dd5c8e5a39 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 20 Dec 2020 13:31:11 -0800 Subject: [PATCH 1/7] Use author name as a deduplication field I feel iffy about this but openlibrary has hella duplicates --- bookwyrm/models/author.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/models/author.py b/bookwyrm/models/author.py index 911cc109d..a2eac507b 100644 --- a/bookwyrm/models/author.py +++ b/bookwyrm/models/author.py @@ -21,7 +21,7 @@ class Author(ActivitypubMixin, BookWyrmModel): # idk probably other keys would be useful here? born = fields.DateTimeField(blank=True, null=True) died = fields.DateTimeField(blank=True, null=True) - name = fields.CharField(max_length=255) + name = fields.CharField(max_length=255, deduplication_field=True) aliases = fields.ArrayField( models.CharField(max_length=255), blank=True, default=list ) From c3d0e8e7f70e9c6f687163a0541802e4f79cd55b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 21 Dec 2020 11:47:47 -0800 Subject: [PATCH 2/7] Fixes openlibrary import to prefer editions with covers --- bookwyrm/connectors/abstract_connector.py | 2 +- bookwyrm/connectors/openlibrary.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index 5afd10897..86ac74353 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -140,7 +140,7 @@ class AbstractConnector(AbstractMinimalConnector): for author in self.get_authors_from_data(edition_data): edition.authors.add(author) if not edition.authors.exists() and work.authors.exists(): - edition.authors.add(work.authors.all()) + edition.authors.set(work.authors.all()) return edition diff --git a/bookwyrm/connectors/openlibrary.py b/bookwyrm/connectors/openlibrary.py index 74f76668c..3b60c3073 100644 --- a/bookwyrm/connectors/openlibrary.py +++ b/bookwyrm/connectors/openlibrary.py @@ -174,7 +174,7 @@ def pick_default_edition(options): if len(options) == 1: return options[0] - options = [e for e in options if e.get('cover')] or options + options = [e for e in options if e.get('covers')] or options options = [e for e in options if \ '/languages/eng' in str(e.get('languages'))] or options formats = ['paperback', 'hardcover', 'mass market paperback'] From aac264c998d5b896f26e2f5f87509241c563a7da Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 21 Dec 2020 11:57:45 -0800 Subject: [PATCH 3/7] Generate author_text field dynamically --- bookwyrm/connectors/self_connector.py | 2 +- .../migrations/0028_remove_book_author_text.py | 17 +++++++++++++++++ bookwyrm/models/book.py | 6 +++++- .../tests/connectors/test_self_connector.py | 14 +++++++++----- 4 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 bookwyrm/migrations/0028_remove_book_author_text.py diff --git a/bookwyrm/connectors/self_connector.py b/bookwyrm/connectors/self_connector.py index 80d3a67d9..8d31c8a1a 100644 --- a/bookwyrm/connectors/self_connector.py +++ b/bookwyrm/connectors/self_connector.py @@ -13,7 +13,7 @@ class Connector(AbstractConnector): that gets implemented it will totally rule ''' vector = SearchVector('title', weight='A') +\ SearchVector('subtitle', weight='B') +\ - SearchVector('author_text', weight='C') +\ + SearchVector('authors__name', weight='C') +\ SearchVector('isbn_13', weight='A') +\ SearchVector('isbn_10', weight='A') +\ SearchVector('openlibrary_key', weight='C') +\ diff --git a/bookwyrm/migrations/0028_remove_book_author_text.py b/bookwyrm/migrations/0028_remove_book_author_text.py new file mode 100644 index 000000000..8743c910d --- /dev/null +++ b/bookwyrm/migrations/0028_remove_book_author_text.py @@ -0,0 +1,17 @@ +# Generated by Django 3.0.7 on 2020-12-21 19:57 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0027_auto_20201220_2007'), + ] + + operations = [ + migrations.RemoveField( + model_name='book', + name='author_text', + ), + ] diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 0a3265915..21311d6c4 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -51,7 +51,6 @@ class Book(ActivitypubMixin, BookWyrmModel): # TODO: include an annotation about the type of authorship (ie, translator) authors = fields.ManyToManyField('Author') # preformatted authorship string for search and easier display - author_text = models.CharField(max_length=255, blank=True, null=True) cover = fields.ImageField( upload_to='covers/', blank=True, null=True, alt_field='alt_text') first_published_date = fields.DateTimeField(blank=True, null=True) @@ -59,6 +58,11 @@ class Book(ActivitypubMixin, BookWyrmModel): objects = InheritanceManager() + @property + def author_text(self): + ''' format a list of authors ''' + return ', '.join(a.name for a in self.authors.all()) + @property def edition_info(self): ''' properties of this edition, as a string ''' diff --git a/bookwyrm/tests/connectors/test_self_connector.py b/bookwyrm/tests/connectors/test_self_connector.py index 4627bc8c8..0a925d796 100644 --- a/bookwyrm/tests/connectors/test_self_connector.py +++ b/bookwyrm/tests/connectors/test_self_connector.py @@ -22,30 +22,34 @@ class SelfConnector(TestCase): priority=1, ) self.connector = Connector(DOMAIN) + author = models.Author.objects.create(name='Anonymouse') self.work = models.Work.objects.create( title='Example Work', ) + self.work.add(author) self.edition = models.Edition.objects.create( title='Edition of Example Work', - author_text='Anonymous', published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc), parent_work=self.work, ) - models.Edition.objects.create( + self.edition.add(author) + edition = models.Edition.objects.create( title='Another Edition', parent_work=self.work, series='Anonymous' ) - models.Edition.objects.create( + edition.add(author) + edition = models.Edition.objects.create( title='More Editions', subtitle='The Anonymous Edition', parent_work=self.work, ) - models.Edition.objects.create( + edition.add(author) + edition = models.Edition.objects.create( title='An Edition', - author_text='Fish', parent_work=self.work ) + edition.add(author) def test_format_search_result(self): From 862f1d2580eda909db10acf68218a84ee3c08220 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 21 Dec 2020 12:22:47 -0800 Subject: [PATCH 4/7] Fixes cover unit test --- bookwyrm/tests/connectors/test_openlibrary_connector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/tests/connectors/test_openlibrary_connector.py b/bookwyrm/tests/connectors/test_openlibrary_connector.py index b3d97ba31..e2d54cd36 100644 --- a/bookwyrm/tests/connectors/test_openlibrary_connector.py +++ b/bookwyrm/tests/connectors/test_openlibrary_connector.py @@ -44,7 +44,7 @@ class Openlibrary(TestCase): def test_pick_default_edition(self): edition = pick_default_edition(self.edition_list_data['entries']) - self.assertEqual(edition['key'], '/books/OL9952943M') + self.assertEqual(edition['key'], '/books/OL9788823M') def test_format_search_result(self): From bc64ae0504daafa292f4d6d10bacd796d91dc7c7 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 21 Dec 2020 12:49:00 -0800 Subject: [PATCH 5/7] Fixes assigning authors in test --- bookwyrm/tests/connectors/test_self_connector.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bookwyrm/tests/connectors/test_self_connector.py b/bookwyrm/tests/connectors/test_self_connector.py index 0a925d796..d747748fb 100644 --- a/bookwyrm/tests/connectors/test_self_connector.py +++ b/bookwyrm/tests/connectors/test_self_connector.py @@ -26,30 +26,30 @@ class SelfConnector(TestCase): self.work = models.Work.objects.create( title='Example Work', ) - self.work.add(author) + self.work.authors.add(author) self.edition = models.Edition.objects.create( title='Edition of Example Work', published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc), parent_work=self.work, ) - self.edition.add(author) + self.edition.authors.add(author) edition = models.Edition.objects.create( title='Another Edition', parent_work=self.work, series='Anonymous' ) - edition.add(author) + edition.authors.add(author) edition = models.Edition.objects.create( title='More Editions', subtitle='The Anonymous Edition', parent_work=self.work, ) - edition.add(author) + edition.authors.add(author) edition = models.Edition.objects.create( title='An Edition', parent_work=self.work ) - edition.add(author) + edition.authors.add(author) def test_format_search_result(self): From adfb1e696ae6b8e8b56de7275dc8ae903bb04a57 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 21 Dec 2020 12:49:33 -0800 Subject: [PATCH 6/7] typo in test --- bookwyrm/tests/connectors/test_self_connector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/tests/connectors/test_self_connector.py b/bookwyrm/tests/connectors/test_self_connector.py index d747748fb..d80725d7f 100644 --- a/bookwyrm/tests/connectors/test_self_connector.py +++ b/bookwyrm/tests/connectors/test_self_connector.py @@ -22,7 +22,7 @@ class SelfConnector(TestCase): priority=1, ) self.connector = Connector(DOMAIN) - author = models.Author.objects.create(name='Anonymouse') + author = models.Author.objects.create(name='Anonymous') self.work = models.Work.objects.create( title='Example Work', ) From 0de479a89f41969ad1ecca33f227e5f08b835862 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 21 Dec 2020 13:03:48 -0800 Subject: [PATCH 7/7] Fixes authors set in tests --- bookwyrm/tests/connectors/test_self_connector.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/bookwyrm/tests/connectors/test_self_connector.py b/bookwyrm/tests/connectors/test_self_connector.py index d80725d7f..91857def0 100644 --- a/bookwyrm/tests/connectors/test_self_connector.py +++ b/bookwyrm/tests/connectors/test_self_connector.py @@ -22,34 +22,32 @@ class SelfConnector(TestCase): priority=1, ) self.connector = Connector(DOMAIN) - author = models.Author.objects.create(name='Anonymous') self.work = models.Work.objects.create( title='Example Work', ) - self.work.authors.add(author) + author = models.Author.objects.create(name='Anonymous') self.edition = models.Edition.objects.create( title='Edition of Example Work', published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc), parent_work=self.work, ) self.edition.authors.add(author) - edition = models.Edition.objects.create( + models.Edition.objects.create( title='Another Edition', parent_work=self.work, series='Anonymous' ) - edition.authors.add(author) - edition = models.Edition.objects.create( + models.Edition.objects.create( title='More Editions', subtitle='The Anonymous Edition', parent_work=self.work, ) - edition.authors.add(author) + edition = models.Edition.objects.create( title='An Edition', parent_work=self.work ) - edition.authors.add(author) + edition.authors.add(models.Author.objects.create(name='Fish')) def test_format_search_result(self):