Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-populate sort title in edit book form if not provided #2948

Merged
merged 2 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions bookwyrm/models/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ def get_remote_id(self):
"""editions and works both use "book" instead of model_name"""
return f"https://{DOMAIN}/book/{self.id}"

def guess_sort_title(self):
"""Get a best-guess sort title for the current book"""
articles = chain(
*(LANGUAGE_ARTICLES.get(language, ()) for language in tuple(self.languages))
)
return re.sub(f'^{" |^".join(articles)} ', "", str(self.title).lower())

def __repr__(self):
# pylint: disable=consider-using-f-string
return "<{} key={!r} title={!r}>".format(
Expand Down Expand Up @@ -375,15 +382,7 @@ def save(self, *args: Any, **kwargs: Any) -> None:
# Create sort title by removing articles from title
if self.sort_title in [None, ""]:
mouse-reeve marked this conversation as resolved.
Show resolved Hide resolved
if self.sort_title in [None, ""]:
articles = chain(
*(
LANGUAGE_ARTICLES.get(language, ())
for language in tuple(self.languages)
)
)
self.sort_title = re.sub(
f'^{" |^".join(articles)} ', "", str(self.title).lower()
)
self.sort_title = self.guess_sort_title()

return super().save(*args, **kwargs)

Expand Down
4 changes: 4 additions & 0 deletions bookwyrm/views/books/edit_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class EditBook(View):
def get(self, request, book_id):
"""info about a book"""
book = get_edition(book_id)
# This doesn't update the sort title, just pre-populates it in the form
if book.sort_title in ["", None]:
book.sort_title = book.guess_sort_title()
if not book.description:
book.description = book.parent_work.description
data = {"book": book, "form": forms.EditionForm(instance=book)}
Expand All @@ -40,6 +43,7 @@ def get(self, request, book_id):
def post(self, request, book_id):
"""edit a book cool"""
book = get_object_or_404(models.Edition, id=book_id)

form = forms.EditionForm(request.POST, request.FILES, instance=book)

data = {"book": book, "form": form}
Expand Down