Merge pull request #2948 from bookwyrm-social/populate-sort-title

Pre-populate sort title in edit book form if not provided
This commit is contained in:
Mouse Reeve 2023-09-01 17:01:41 -07:00 committed by GitHub
commit c2196fb704
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 10 deletions

View file

@ -217,6 +217,13 @@ class Book(BookDataModel):
"""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(
@ -374,16 +381,7 @@ class Edition(Book):
# Create sort title by removing articles from title
if self.sort_title in [None, ""]:
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)

View file

@ -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)}
@ -40,6 +43,7 @@ class EditBook(View):
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}