From 575e1bac4cd6837639ed849cb19040a53502e6d9 Mon Sep 17 00:00:00 2001 From: Zach Flanders Date: Tue, 25 Apr 2023 19:46:38 -0500 Subject: [PATCH] responding to review comments --- bookwyrm/migrations/0179_populate_sort_title.py | 14 ++++---------- bookwyrm/models/book.py | 16 ++++++++-------- bookwyrm/settings.py | 2 +- bookwyrm/tests/models/test_book_model.py | 6 ++---- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/bookwyrm/migrations/0179_populate_sort_title.py b/bookwyrm/migrations/0179_populate_sort_title.py index 5c9ae39a3..7f79fb321 100644 --- a/bookwyrm/migrations/0179_populate_sort_title.py +++ b/bookwyrm/migrations/0179_populate_sort_title.py @@ -18,16 +18,10 @@ def populate_sort_title(apps, schema_editor): articles = chain( *(LANGUAGE_ARTICLES.get(language, ()) for language in edition.languages) ) - if articles: - icase_articles = ( - f"[{a[0].capitalize()}{a[0].lower()}]{a[1:]}" for a in articles - ) - edition.sort_title = re.sub( - f'^{" |^".join(icase_articles)} ', "", edition.title - ) - else: - edition.sort_title = edition.title - edition.save() + edition.sort_title = re.sub( + f'^{" |^".join(articles)} ', "", str(edition.title).lower() + ) + Edition.objects.bulk_update(editions_wo_sort_title, ["sort_title"]) class Migration(migrations.Migration): diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 2bad36a50..3cc89a45d 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -366,14 +366,14 @@ class Edition(Book): cache.delete(f"author-books-{author_id}") # Create sort title by removing articles from title - if self.sort_title is None: - articles = chain( - *(LANGUAGE_ARTICLES[language] for language in self.languages) - ) - icase_articles = ( - f"[{a[0].capitalize()}{a[0].lower()}]{a[1:]}" for a in articles - ) - self.sort_title = re.sub(f'^{" |^".join(icase_articles)} ', "", self.title) + if self.sort_title in [None, ""]: + if self.sort_title in [None, ""]: + articles = chain( + *(LANGUAGE_ARTICLES.get(language, ()) for language in self.languages) + ) + self.sort_title = re.sub( + f'^{" |^".join(articles)} ', "", str(self.title).lower() + ) return super().save(*args, **kwargs) diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index f9940bcd5..23fa3b064 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -313,7 +313,7 @@ LANGUAGES = [ ] LANGUAGE_ARTICLES = { - "English": {"The", "A", "An"}, + "English": {"the", "a", "an"}, } TIME_ZONE = "UTC" diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py index 50ff8c7e1..fda74cb38 100644 --- a/bookwyrm/tests/models/test_book_model.py +++ b/bookwyrm/tests/models/test_book_model.py @@ -140,8 +140,6 @@ class Book(TestCase): title=f"{article} Test Edition", languages=[langauge] ) for langauge, articles in settings.LANGUAGE_ARTICLES.items() - for article in article - ) - self.assertEqual( - all([book.sort_title == "Test Edition" for book in books]) + for article in articles ) + self.assertTrue(all(book.sort_title == "Test Edition" for book in books))