Formats code changes

This commit is contained in:
Mouse Reeve 2021-03-08 10:10:30 -08:00
parent 01a2a77cbb
commit acbebbe947
5 changed files with 75 additions and 81 deletions

View file

@ -138,7 +138,6 @@ class EditionForm(CustomForm):
class Meta: class Meta:
model = models.Edition model = models.Edition
exclude = [ exclude = [
"remote_id", "remote_id",
"origin_id", "origin_id",
"created_date", "created_date",

View file

@ -85,14 +85,14 @@ class BookViews(TestCase):
self.assertEqual(self.book.title, "New Title") self.assertEqual(self.book.title, "New Title")
def test_edit_book_add_author(self): def test_edit_book_add_author(self):
''' lets a user edit a book with new authors ''' """ lets a user edit a book with new authors """
view = views.EditBook.as_view() view = views.EditBook.as_view()
self.local_user.groups.add(self.group) self.local_user.groups.add(self.group)
form = forms.EditionForm(instance=self.book) form = forms.EditionForm(instance=self.book)
form.data['title'] = 'New Title' form.data["title"] = "New Title"
form.data['last_edited_by'] = self.local_user.id form.data["last_edited_by"] = self.local_user.id
form.data['add_author'] = 'Sappho' form.data["add_author"] = "Sappho"
request = self.factory.post('', form.data) request = self.factory.post("", form.data)
request.user = self.local_user request.user = self.local_user
result = view(request, self.book.id) result = view(request, self.book.id)
@ -100,47 +100,46 @@ class BookViews(TestCase):
# the changes haven't been saved yet # the changes haven't been saved yet
self.book.refresh_from_db() self.book.refresh_from_db()
self.assertEqual(self.book.title, 'Example Edition') self.assertEqual(self.book.title, "Example Edition")
def test_edit_book_add_new_author_confirm(self): def test_edit_book_add_new_author_confirm(self):
''' lets a user edit a book confirmed with new authors ''' """ lets a user edit a book confirmed with new authors """
view = views.ConfirmEditBook.as_view() view = views.ConfirmEditBook.as_view()
self.local_user.groups.add(self.group) self.local_user.groups.add(self.group)
form = forms.EditionForm(instance=self.book) form = forms.EditionForm(instance=self.book)
form.data['title'] = 'New Title' form.data["title"] = "New Title"
form.data['last_edited_by'] = self.local_user.id form.data["last_edited_by"] = self.local_user.id
form.data['add_author'] = 'Sappho' form.data["add_author"] = "Sappho"
request = self.factory.post('', form.data) request = self.factory.post("", form.data)
request.user = self.local_user request.user = self.local_user
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
view(request, self.book.id) view(request, self.book.id)
self.book.refresh_from_db() self.book.refresh_from_db()
self.assertEqual(self.book.title, 'New Title') self.assertEqual(self.book.title, "New Title")
self.assertEqual(self.book.authors.first().name, 'Sappho') self.assertEqual(self.book.authors.first().name, "Sappho")
def test_edit_book_remove_author(self): def test_edit_book_remove_author(self):
''' remove an author from a book ''' """ remove an author from a book """
author = models.Author.objects.create(name='Sappho') author = models.Author.objects.create(name="Sappho")
self.book.authors.add(author) self.book.authors.add(author)
form = forms.EditionForm(instance=self.book) form = forms.EditionForm(instance=self.book)
view = views.EditBook.as_view() view = views.EditBook.as_view()
self.local_user.groups.add(self.group) self.local_user.groups.add(self.group)
form = forms.EditionForm(instance=self.book) form = forms.EditionForm(instance=self.book)
form.data['title'] = 'New Title' form.data["title"] = "New Title"
form.data['last_edited_by'] = self.local_user.id form.data["last_edited_by"] = self.local_user.id
form.data['remove_authors'] = [author.id] form.data["remove_authors"] = [author.id]
request = self.factory.post('', form.data) request = self.factory.post("", form.data)
request.user = self.local_user request.user = self.local_user
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
view(request, self.book.id) view(request, self.book.id)
self.book.refresh_from_db() self.book.refresh_from_db()
self.assertEqual(self.book.title, 'New Title') self.assertEqual(self.book.title, "New Title")
self.assertFalse(self.book.authors.exists()) self.assertFalse(self.book.authors.exists())
def test_switch_edition(self): def test_switch_edition(self):
""" updates user's relationships to a book """ """ updates user's relationships to a book """
work = models.Work.objects.create(title="test work") work = models.Work.objects.create(title="test work")

View file

@ -117,17 +117,16 @@ urlpatterns = [
re_path(r"^boost/(?P<status_id>\d+)/?$", views.Boost.as_view()), re_path(r"^boost/(?P<status_id>\d+)/?$", views.Boost.as_view()),
re_path(r"^unboost/(?P<status_id>\d+)/?$", views.Unboost.as_view()), re_path(r"^unboost/(?P<status_id>\d+)/?$", views.Unboost.as_view()),
# books # books
re_path(r'%s(.json)?/?$' % book_path, views.Book.as_view()), re_path(r"%s(.json)?/?$" % book_path, views.Book.as_view()),
re_path(r'%s/edit/?$' % book_path, views.EditBook.as_view()), re_path(r"%s/edit/?$" % book_path, views.EditBook.as_view()),
re_path(r'%s/confirm/?$' % book_path, views.ConfirmEditBook.as_view()), re_path(r"%s/confirm/?$" % book_path, views.ConfirmEditBook.as_view()),
re_path(r'^create-book/?$', views.EditBook.as_view()), re_path(r"^create-book/?$", views.EditBook.as_view()),
re_path(r'^create-book/confirm?$', views.ConfirmEditBook.as_view()), re_path(r"^create-book/confirm?$", views.ConfirmEditBook.as_view()),
re_path(r'%s/editions(.json)?/?$' % book_path, views.Editions.as_view()), re_path(r"%s/editions(.json)?/?$" % book_path, views.Editions.as_view()),
re_path(r'^upload-cover/(?P<book_id>\d+)/?$', views.upload_cover), re_path(r"^upload-cover/(?P<book_id>\d+)/?$", views.upload_cover),
re_path(r'^add-description/(?P<book_id>\d+)/?$', views.add_description), re_path(r"^add-description/(?P<book_id>\d+)/?$", views.add_description),
re_path(r'^resolve-book/?$', views.resolve_book), re_path(r"^resolve-book/?$", views.resolve_book),
re_path(r'^switch-edition/?$', views.switch_edition), re_path(r"^switch-edition/?$", views.switch_edition),
# isbn # isbn
re_path(r"^isbn/(?P<isbn>\d+)(.json)?/?$", views.Isbn.as_view()), re_path(r"^isbn/(?P<isbn>\d+)(.json)?/?$", views.Isbn.as_view()),
# author # author

View file

@ -106,107 +106,104 @@ class Book(View):
permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch" permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"
) )
class EditBook(View): class EditBook(View):
''' edit a book ''' """ edit a book """
def get(self, request, book_id=None): def get(self, request, book_id=None):
''' info about a book ''' """ info about a book """
book = None book = None
if book_id: if book_id:
book = get_edition(book_id) book = get_edition(book_id)
if not book.description: if not book.description:
book.description = book.parent_work.description book.description = book.parent_work.description
data = { data = {"book": book, "form": forms.EditionForm(instance=book)}
'book': book, return TemplateResponse(request, "edit_book.html", data)
'form': forms.EditionForm(instance=book)
}
return TemplateResponse(request, 'edit_book.html', data)
def post(self, request, book_id=None): def post(self, request, book_id=None):
''' edit a book cool ''' """ edit a book cool """
# returns None if no match is found # returns None if no match is found
book = models.Edition.objects.filter(id=book_id).first() book = models.Edition.objects.filter(id=book_id).first()
form = forms.EditionForm(request.POST, request.FILES, instance=book) form = forms.EditionForm(request.POST, request.FILES, instance=book)
data = { data = {"book": book, "form": form}
'book': book,
'form': form
}
if not form.is_valid(): if not form.is_valid():
return TemplateResponse(request, 'edit_book.html', data) return TemplateResponse(request, "edit_book.html", data)
add_author = request.POST.get('add_author') add_author = request.POST.get("add_author")
# we're adding an author through a free text field # we're adding an author through a free text field
if add_author: if add_author:
data['add_author'] = add_author data["add_author"] = add_author
# check for existing authors # check for existing authors
vector = SearchVector('name', weight='A') +\ vector = SearchVector("name", weight="A") + SearchVector(
SearchVector('aliases', weight='B') "aliases", weight="B"
)
data['author_matches'] = models.Author.objects.annotate( data["author_matches"] = (
search=vector models.Author.objects.annotate(search=vector)
).annotate( .annotate(rank=SearchRank(vector, add_author))
rank=SearchRank(vector, add_author) .filter(rank__gt=0.4)
).filter(rank__gt=0.4).order_by('-rank')[:5] .order_by("-rank")[:5]
)
# we're creating a new book # we're creating a new book
if not book: if not book:
# check if this is an edition of an existing work # check if this is an edition of an existing work
author_text = book.author_text if book else add_author author_text = book.author_text if book else add_author
data['book_matches'] = connector_manager.local_search( data["book_matches"] = connector_manager.local_search(
'%s %s' % (form.cleaned_data.get('title'), author_text), "%s %s" % (form.cleaned_data.get("title"), author_text),
min_confidence=0.5, min_confidence=0.5,
raw=True raw=True,
)[:5] )[:5]
# either of the above cases requires additional confirmation # either of the above cases requires additional confirmation
if add_author or not book: if add_author or not book:
# creting a book or adding an author to a book needs another step # creting a book or adding an author to a book needs another step
data['confirm_mode'] = True data["confirm_mode"] = True
return TemplateResponse(request, 'edit_book.html', data) return TemplateResponse(request, "edit_book.html", data)
remove_authors = request.POST.getlist('remove_authors') remove_authors = request.POST.getlist("remove_authors")
for author_id in remove_authors: for author_id in remove_authors:
book.authors.remove(author_id) book.authors.remove(author_id)
book = form.save() book = form.save()
return redirect('/book/%s' % book.id) return redirect("/book/%s" % book.id)
@method_decorator(login_required, name='dispatch') @method_decorator(login_required, name="dispatch")
@method_decorator( @method_decorator(
permission_required('bookwyrm.edit_book', raise_exception=True), permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"
name='dispatch') )
class ConfirmEditBook(View): class ConfirmEditBook(View):
''' confirm edits to a book ''' """ confirm edits to a book """
def post(self, request, book_id=None): def post(self, request, book_id=None):
''' edit a book cool ''' """ edit a book cool """
# returns None if no match is found # returns None if no match is found
book = models.Edition.objects.filter(id=book_id).first() book = models.Edition.objects.filter(id=book_id).first()
form = forms.EditionForm(request.POST, request.FILES, instance=book) form = forms.EditionForm(request.POST, request.FILES, instance=book)
data = { data = {"book": book, "form": form}
'book': book,
'form': form
}
if not form.is_valid(): if not form.is_valid():
return TemplateResponse(request, 'edit_book.html', data) return TemplateResponse(request, "edit_book.html", data)
with transaction.atomic(): with transaction.atomic():
# save book # save book
book = form.save() book = form.save()
# get or create author as needed # get or create author as needed
if request.POST.get('add_author'): if request.POST.get("add_author"):
if request.POST.get('author_match'): if request.POST.get("author_match"):
author = get_object_or_404( author = get_object_or_404(
models.Author, id=request.POST['author_match']) models.Author, id=request.POST["author_match"]
)
else: else:
author = models.Author.objects.create( author = models.Author.objects.create(
name=request.POST.get('add_author')) name=request.POST.get("add_author")
)
book.authors.add(author) book.authors.add(author)
# create work, if needed # create work, if needed
if not book_id: if not book_id:
work_match = request.POST.get('parent_work') work_match = request.POST.get("parent_work")
if work_match: if work_match:
work = get_object_or_404(models.Work, id=work_match) work = get_object_or_404(models.Work, id=work_match)
else: else:
@ -215,7 +212,7 @@ class ConfirmEditBook(View):
book.parent_work = work book.parent_work = work
book.save() book.save()
for author_id in request.POST.getlist('remove_authors'): for author_id in request.POST.getlist("remove_authors"):
book.authors.remove(author_id) book.authors.remove(author_id)
return redirect("/book/%s" % book.id) return redirect("/book/%s" % book.id)

2
bw-dev
View file

@ -36,7 +36,7 @@ function initdb {
} }
function makeitblack { function makeitblack {
runweb black celerywyrm bookwyrm docker-compose run --rm web black celerywyrm bookwyrm
} }
CMD=$1 CMD=$1