From fc599f8b9a320cd245893dea8e68a35e1bbdcba1 Mon Sep 17 00:00:00 2001 From: Ritik Ranjan Date: Sat, 28 Jan 2023 12:10:06 +0530 Subject: [PATCH 01/57] fixed singularisation/pluralisation edited two files --- bookwyrm/templates/import/import.html | 2 +- bookwyrm/views/imports/import_data.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/bookwyrm/templates/import/import.html b/bookwyrm/templates/import/import.html index f3062a447..9d278b28d 100644 --- a/bookwyrm/templates/import/import.html +++ b/bookwyrm/templates/import/import.html @@ -17,7 +17,7 @@ {% if site.imports_enabled %} {% if import_size_limit and import_limit_reset %}
-

{% blocktrans %}Currently you are allowed to import {{ import_size_limit }} books every {{ import_limit_reset }} days.{% endblocktrans %}

+

{% blocktrans %}Currently you are allowed to import {{ import_size_limit }} {{book_noun}} every {{ import_limit_reset }} {{day_noun}}.{% endblocktrans %}

{% blocktrans %}You have {{ allowed_imports }} left.{% endblocktrans %}

{% endif %} diff --git a/bookwyrm/views/imports/import_data.py b/bookwyrm/views/imports/import_data.py index 01812e1d5..d09e0257f 100644 --- a/bookwyrm/views/imports/import_data.py +++ b/bookwyrm/views/imports/import_data.py @@ -64,6 +64,9 @@ class Import(View): data["import_limit_reset"] = site_settings.import_limit_reset data["allowed_imports"] = site_settings.import_size_limit - imported_books + data["book_noun"] = "books" if site_settings.import_size_limit > 1 else 'book' + data["day_noun"] = "days" if site_settings.import_limit_reset > 1 else 'day' + return TemplateResponse(request, "import/import.html", data) def post(self, request): From e5e9e807cabf4e259bd426a08d34624c2d9f36ed Mon Sep 17 00:00:00 2001 From: Ritik Ranjan Date: Sat, 28 Jan 2023 21:11:20 +0530 Subject: [PATCH 02/57] Splited stirng into two then fixed pluralization error. --- bookwyrm/templates/import/import.html | 9 ++++++++- bookwyrm/views/imports/import_data.py | 3 --- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bookwyrm/templates/import/import.html b/bookwyrm/templates/import/import.html index 9d278b28d..06817d302 100644 --- a/bookwyrm/templates/import/import.html +++ b/bookwyrm/templates/import/import.html @@ -17,7 +17,14 @@ {% if site.imports_enabled %} {% if import_size_limit and import_limit_reset %}
-

{% blocktrans %}Currently you are allowed to import {{ import_size_limit }} {{book_noun}} every {{ import_limit_reset }} {{day_noun}}.{% endblocktrans %}

+

+ {% blocktrans count books=import_size_limit%} + Currently you are allowed to import {{ import_size_limit }} {{books}} + {% endblocktrans %} + {% blocktrans count days=import_limit_reset%} + every {{ import_limit_reset }} {{days}}. + {% endblocktrans %} +

{% blocktrans %}You have {{ allowed_imports }} left.{% endblocktrans %}

{% endif %} diff --git a/bookwyrm/views/imports/import_data.py b/bookwyrm/views/imports/import_data.py index d09e0257f..01812e1d5 100644 --- a/bookwyrm/views/imports/import_data.py +++ b/bookwyrm/views/imports/import_data.py @@ -64,9 +64,6 @@ class Import(View): data["import_limit_reset"] = site_settings.import_limit_reset data["allowed_imports"] = site_settings.import_size_limit - imported_books - data["book_noun"] = "books" if site_settings.import_size_limit > 1 else 'book' - data["day_noun"] = "days" if site_settings.import_limit_reset > 1 else 'day' - return TemplateResponse(request, "import/import.html", data) def post(self, request): From adcf9310a0850cb646af81fdbbfc53c31f3ac41d Mon Sep 17 00:00:00 2001 From: Ritik Ranjan Date: Sat, 28 Jan 2023 21:23:08 +0530 Subject: [PATCH 03/57] Fixed Syntax of pluralisation --- bookwyrm/templates/import/import.html | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templates/import/import.html b/bookwyrm/templates/import/import.html index 06817d302..9146d0715 100644 --- a/bookwyrm/templates/import/import.html +++ b/bookwyrm/templates/import/import.html @@ -19,10 +19,14 @@

{% blocktrans count books=import_size_limit%} - Currently you are allowed to import {{ import_size_limit }} {{books}} + Currently you are allowed to import {{ import_size_limit }} book + {% plural %} + Currently you are allowed to import {{ import_size_limit }} books {% endblocktrans %} {% blocktrans count days=import_limit_reset%} - every {{ import_limit_reset }} {{days}}. + every {{ import_limit_reset }} day. + {% plural %} + every {{ import_limit_reset }} days. {% endblocktrans %}

{% blocktrans %}You have {{ allowed_imports }} left.{% endblocktrans %}

From a94a4732ec726cc09bb9b810e4f5d9313fe84dba Mon Sep 17 00:00:00 2001 From: Zach Flanders Date: Mon, 24 Apr 2023 23:29:55 -0500 Subject: [PATCH 04/57] add support for title sort to ignore initial article --- bookwyrm/forms/lists.py | 2 +- .../migrations/0179_populate_sort_title.py | 41 +++++++++++++++++++ bookwyrm/models/book.py | 12 ++++++ bookwyrm/settings.py | 3 ++ bookwyrm/templates/shelf/shelf.html | 2 +- 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 bookwyrm/migrations/0179_populate_sort_title.py diff --git a/bookwyrm/forms/lists.py b/bookwyrm/forms/lists.py index 647db3bfe..f5008baa3 100644 --- a/bookwyrm/forms/lists.py +++ b/bookwyrm/forms/lists.py @@ -24,7 +24,7 @@ class SortListForm(forms.Form): sort_by = ChoiceField( choices=( ("order", _("List Order")), - ("title", _("Book Title")), + ("sort_title", _("Book Title")), ("rating", _("Rating")), ), label=_("Sort By"), diff --git a/bookwyrm/migrations/0179_populate_sort_title.py b/bookwyrm/migrations/0179_populate_sort_title.py new file mode 100644 index 000000000..5c9ae39a3 --- /dev/null +++ b/bookwyrm/migrations/0179_populate_sort_title.py @@ -0,0 +1,41 @@ +import re +from itertools import chain + +from django.db import migrations, transaction +from django.db.models import Q + +from bookwyrm.settings import LANGUAGE_ARTICLES + + +@transaction.atomic +def populate_sort_title(apps, schema_editor): + Edition = apps.get_model("bookwyrm", "Edition") + db_alias = schema_editor.connection.alias + editions_wo_sort_title = Edition.objects.using(db_alias).filter( + Q(sort_title__isnull=True) | Q(sort_title__exact="") + ) + for edition in editions_wo_sort_title: + 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() + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0178_auto_20230328_2132"), + ] + + operations = [ + migrations.RunPython(populate_sort_title), + ] diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 4e7ffcad3..2bad36a50 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -1,4 +1,5 @@ """ database schema for books and shelves """ +from itertools import chain import re from django.contrib.postgres.search import SearchVectorField @@ -17,6 +18,7 @@ from bookwyrm.preview_images import generate_edition_preview_image_task from bookwyrm.settings import ( DOMAIN, DEFAULT_LANGUAGE, + LANGUAGE_ARTICLES, ENABLE_PREVIEW_IMAGES, ENABLE_THUMBNAIL_GENERATION, ) @@ -363,6 +365,16 @@ class Edition(Book): for author_id in self.authors.values_list("id", flat=True): 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) + return super().save(*args, **kwargs) @classmethod diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index 8dcf90fcb..f9940bcd5 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -312,6 +312,9 @@ LANGUAGES = [ ("zh-hant", _("繁體中文 (Traditional Chinese)")), ] +LANGUAGE_ARTICLES = { + "English": {"The", "A", "An"}, +} TIME_ZONE = "UTC" diff --git a/bookwyrm/templates/shelf/shelf.html b/bookwyrm/templates/shelf/shelf.html index 79ec241be..7d0035ed3 100644 --- a/bookwyrm/templates/shelf/shelf.html +++ b/bookwyrm/templates/shelf/shelf.html @@ -145,7 +145,7 @@ {% trans "Cover"%} - {% trans "Title" as text %}{% include 'snippets/table-sort-header.html' with field="title" sort=sort text=text %} + {% trans "Title" as text %}{% include 'snippets/table-sort-header.html' with field="sort_title" sort=sort text=text %} {% trans "Author" as text %}{% include 'snippets/table-sort-header.html' with field="author" sort=sort text=text %} {% if request.user.is_authenticated %} {% if is_self %} From 21d9cb5fe56c5f37429610abbbdb894940b4ec87 Mon Sep 17 00:00:00 2001 From: Zach Flanders Date: Tue, 25 Apr 2023 00:15:58 -0500 Subject: [PATCH 05/57] updating shelf view --- bookwyrm/views/shelf/shelf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/views/shelf/shelf.py b/bookwyrm/views/shelf/shelf.py index 0c3074902..dbbcc2d3a 100644 --- a/bookwyrm/views/shelf/shelf.py +++ b/bookwyrm/views/shelf/shelf.py @@ -128,7 +128,7 @@ class Shelf(View): def sort_books(books, sort): """Books in shelf sorting""" sort_fields = [ - "title", + "sort_title", "author", "shelved_date", "start_date", From a3013c6224e91dbc6d836a6b4f660fdbefe6663f Mon Sep 17 00:00:00 2001 From: Zach Flanders Date: Tue, 25 Apr 2023 00:20:54 -0500 Subject: [PATCH 06/57] updating list view --- bookwyrm/views/list/list.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/views/list/list.py b/bookwyrm/views/list/list.py index 30d6f970a..660bd1a62 100644 --- a/bookwyrm/views/list/list.py +++ b/bookwyrm/views/list/list.py @@ -129,7 +129,7 @@ def sort_list(request, items): """helper to handle the surprisingly involved sorting""" # sort_by shall be "order" unless a valid alternative is given sort_by = request.GET.get("sort_by", "order") - if sort_by not in ("order", "title", "rating"): + if sort_by not in ("order", "sort_title", "rating"): sort_by = "order" # direction shall be "ascending" unless a valid alternative is given @@ -139,7 +139,7 @@ def sort_list(request, items): directional_sort_by = { "order": "order", - "title": "book__title", + "sort_title": "book__sort_title", "rating": "average_rating", }[sort_by] if direction == "descending": From 6b39052fcca4aebba6f9d9e4d049e4afd1b0d66d Mon Sep 17 00:00:00 2001 From: Zach Flanders Date: Tue, 25 Apr 2023 07:17:23 -0500 Subject: [PATCH 07/57] Adding test for sort_title population --- bookwyrm/tests/models/test_book_model.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py index 33854b3d6..50ff8c7e1 100644 --- a/bookwyrm/tests/models/test_book_model.py +++ b/bookwyrm/tests/models/test_book_model.py @@ -132,3 +132,16 @@ class Book(TestCase): self.assertIsNotNone(book.cover_bw_book_xlarge_jpg.url) self.assertIsNotNone(book.cover_bw_book_xxlarge_webp.url) self.assertIsNotNone(book.cover_bw_book_xxlarge_jpg.url) + + def test_populate_sort_title(self): + """The sort title should remove the initial article on save""" + books = ( + models.Edition.objects.create( + 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]) + ) From 575e1bac4cd6837639ed849cb19040a53502e6d9 Mon Sep 17 00:00:00 2001 From: Zach Flanders Date: Tue, 25 Apr 2023 19:46:38 -0500 Subject: [PATCH 08/57] 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)) From f43d7f8c70bc7f5b60a66ff0854e1e13e48ac352 Mon Sep 17 00:00:00 2001 From: Zach Flanders Date: Tue, 25 Apr 2023 21:00:16 -0500 Subject: [PATCH 09/57] fixing test and other checks --- bookwyrm/models/book.py | 5 ++++- bookwyrm/tests/models/test_book_model.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 3cc89a45d..c25f8fee2 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -369,7 +369,10 @@ class Edition(Book): if self.sort_title in [None, ""]: if self.sort_title in [None, ""]: articles = chain( - *(LANGUAGE_ARTICLES.get(language, ()) for language in self.languages) + *( + LANGUAGE_ARTICLES.get(language, ()) + for language in tuple(self.languages) + ) ) self.sort_title = re.sub( f'^{" |^".join(articles)} ', "", str(self.title).lower() diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py index fda74cb38..825f42b87 100644 --- a/bookwyrm/tests/models/test_book_model.py +++ b/bookwyrm/tests/models/test_book_model.py @@ -142,4 +142,4 @@ class Book(TestCase): for langauge, articles in settings.LANGUAGE_ARTICLES.items() for article in articles ) - self.assertTrue(all(book.sort_title == "Test Edition" for book in books)) + self.assertTrue(all(book.sort_title == "test edition" for book in books)) From 858a93e98acdecf80264c6b04174bea5710a3bd6 Mon Sep 17 00:00:00 2001 From: Zach Flanders Date: Tue, 25 Apr 2023 21:05:11 -0500 Subject: [PATCH 10/57] fixing migration --- bookwyrm/migrations/0179_populate_sort_title.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bookwyrm/migrations/0179_populate_sort_title.py b/bookwyrm/migrations/0179_populate_sort_title.py index 7f79fb321..a5055c5e1 100644 --- a/bookwyrm/migrations/0179_populate_sort_title.py +++ b/bookwyrm/migrations/0179_populate_sort_title.py @@ -16,7 +16,10 @@ def populate_sort_title(apps, schema_editor): ) for edition in editions_wo_sort_title: articles = chain( - *(LANGUAGE_ARTICLES.get(language, ()) for language in edition.languages) + *( + LANGUAGE_ARTICLES.get(language, ()) + for language in tuple(edition.languages) + ) ) edition.sort_title = re.sub( f'^{" |^".join(articles)} ', "", str(edition.title).lower() From a6e5939ad2e37d64993392149080024eb0ae4add Mon Sep 17 00:00:00 2001 From: Zach Flanders Date: Wed, 26 Apr 2023 23:05:03 -0500 Subject: [PATCH 11/57] adding sort title to edit book form --- bookwyrm/forms/books.py | 4 ++++ bookwyrm/templates/book/edit/edit_book_form.html | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/bookwyrm/forms/books.py b/bookwyrm/forms/books.py index 623beaa04..3a3979e2c 100644 --- a/bookwyrm/forms/books.py +++ b/bookwyrm/forms/books.py @@ -20,6 +20,7 @@ class EditionForm(CustomForm): model = models.Edition fields = [ "title", + "sort_title", "subtitle", "description", "series", @@ -45,6 +46,9 @@ class EditionForm(CustomForm): ] widgets = { "title": forms.TextInput(attrs={"aria-describedby": "desc_title"}), + "sort_title": forms.TextInput( + attrs={"aria-describedby": "desc_sort_title"} + ), "subtitle": forms.TextInput(attrs={"aria-describedby": "desc_subtitle"}), "description": forms.Textarea( attrs={"aria-describedby": "desc_description"} diff --git a/bookwyrm/templates/book/edit/edit_book_form.html b/bookwyrm/templates/book/edit/edit_book_form.html index e85164444..72d80e9cf 100644 --- a/bookwyrm/templates/book/edit/edit_book_form.html +++ b/bookwyrm/templates/book/edit/edit_book_form.html @@ -28,6 +28,15 @@ {% include 'snippets/form_errors.html' with errors_list=form.title.errors id="desc_title" %}
+
+ + + + {% include 'snippets/form_errors.html' with errors_list=form.sort_title.errors id="desc_sort_title" %} +
+