forked from mirrors/bookwyrm
Formats code changes
This commit is contained in:
parent
01a2a77cbb
commit
acbebbe947
5 changed files with 75 additions and 81 deletions
|
@ -138,7 +138,6 @@ class EditionForm(CustomForm):
|
|||
class Meta:
|
||||
model = models.Edition
|
||||
exclude = [
|
||||
|
||||
"remote_id",
|
||||
"origin_id",
|
||||
"created_date",
|
||||
|
|
|
@ -85,14 +85,14 @@ class BookViews(TestCase):
|
|||
self.assertEqual(self.book.title, "New Title")
|
||||
|
||||
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()
|
||||
self.local_user.groups.add(self.group)
|
||||
form = forms.EditionForm(instance=self.book)
|
||||
form.data['title'] = 'New Title'
|
||||
form.data['last_edited_by'] = self.local_user.id
|
||||
form.data['add_author'] = 'Sappho'
|
||||
request = self.factory.post('', form.data)
|
||||
form.data["title"] = "New Title"
|
||||
form.data["last_edited_by"] = self.local_user.id
|
||||
form.data["add_author"] = "Sappho"
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
|
||||
result = view(request, self.book.id)
|
||||
|
@ -100,47 +100,46 @@ class BookViews(TestCase):
|
|||
|
||||
# the changes haven't been saved yet
|
||||
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):
|
||||
''' 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()
|
||||
self.local_user.groups.add(self.group)
|
||||
form = forms.EditionForm(instance=self.book)
|
||||
form.data['title'] = 'New Title'
|
||||
form.data['last_edited_by'] = self.local_user.id
|
||||
form.data['add_author'] = 'Sappho'
|
||||
request = self.factory.post('', form.data)
|
||||
form.data["title"] = "New Title"
|
||||
form.data["last_edited_by"] = self.local_user.id
|
||||
form.data["add_author"] = "Sappho"
|
||||
request = self.factory.post("", form.data)
|
||||
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)
|
||||
|
||||
self.book.refresh_from_db()
|
||||
self.assertEqual(self.book.title, 'New Title')
|
||||
self.assertEqual(self.book.authors.first().name, 'Sappho')
|
||||
self.assertEqual(self.book.title, "New Title")
|
||||
self.assertEqual(self.book.authors.first().name, "Sappho")
|
||||
|
||||
def test_edit_book_remove_author(self):
|
||||
''' remove an author from a book '''
|
||||
author = models.Author.objects.create(name='Sappho')
|
||||
""" remove an author from a book """
|
||||
author = models.Author.objects.create(name="Sappho")
|
||||
self.book.authors.add(author)
|
||||
form = forms.EditionForm(instance=self.book)
|
||||
view = views.EditBook.as_view()
|
||||
self.local_user.groups.add(self.group)
|
||||
form = forms.EditionForm(instance=self.book)
|
||||
form.data['title'] = 'New Title'
|
||||
form.data['last_edited_by'] = self.local_user.id
|
||||
form.data['remove_authors'] = [author.id]
|
||||
request = self.factory.post('', form.data)
|
||||
form.data["title"] = "New Title"
|
||||
form.data["last_edited_by"] = self.local_user.id
|
||||
form.data["remove_authors"] = [author.id]
|
||||
request = self.factory.post("", form.data)
|
||||
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)
|
||||
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())
|
||||
|
||||
|
||||
def test_switch_edition(self):
|
||||
""" updates user's relationships to a book """
|
||||
work = models.Work.objects.create(title="test work")
|
||||
|
|
|
@ -117,17 +117,16 @@ urlpatterns = [
|
|||
re_path(r"^boost/(?P<status_id>\d+)/?$", views.Boost.as_view()),
|
||||
re_path(r"^unboost/(?P<status_id>\d+)/?$", views.Unboost.as_view()),
|
||||
# books
|
||||
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/confirm/?$' % book_path, views.ConfirmEditBook.as_view()),
|
||||
re_path(r'^create-book/?$', views.EditBook.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'^upload-cover/(?P<book_id>\d+)/?$', views.upload_cover),
|
||||
re_path(r'^add-description/(?P<book_id>\d+)/?$', views.add_description),
|
||||
re_path(r'^resolve-book/?$', views.resolve_book),
|
||||
re_path(r'^switch-edition/?$', views.switch_edition),
|
||||
|
||||
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/confirm/?$" % book_path, views.ConfirmEditBook.as_view()),
|
||||
re_path(r"^create-book/?$", views.EditBook.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"^upload-cover/(?P<book_id>\d+)/?$", views.upload_cover),
|
||||
re_path(r"^add-description/(?P<book_id>\d+)/?$", views.add_description),
|
||||
re_path(r"^resolve-book/?$", views.resolve_book),
|
||||
re_path(r"^switch-edition/?$", views.switch_edition),
|
||||
# isbn
|
||||
re_path(r"^isbn/(?P<isbn>\d+)(.json)?/?$", views.Isbn.as_view()),
|
||||
# author
|
||||
|
|
|
@ -106,107 +106,104 @@ class Book(View):
|
|||
permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"
|
||||
)
|
||||
class EditBook(View):
|
||||
''' edit a book '''
|
||||
""" edit a book """
|
||||
|
||||
def get(self, request, book_id=None):
|
||||
''' info about a book '''
|
||||
""" info about a book """
|
||||
book = None
|
||||
if book_id:
|
||||
book = get_edition(book_id)
|
||||
if not book.description:
|
||||
book.description = book.parent_work.description
|
||||
data = {
|
||||
'book': book,
|
||||
'form': forms.EditionForm(instance=book)
|
||||
}
|
||||
return TemplateResponse(request, 'edit_book.html', data)
|
||||
data = {"book": book, "form": forms.EditionForm(instance=book)}
|
||||
return TemplateResponse(request, "edit_book.html", data)
|
||||
|
||||
def post(self, request, book_id=None):
|
||||
''' edit a book cool '''
|
||||
""" edit a book cool """
|
||||
# returns None if no match is found
|
||||
book = models.Edition.objects.filter(id=book_id).first()
|
||||
form = forms.EditionForm(request.POST, request.FILES, instance=book)
|
||||
|
||||
data = {
|
||||
'book': book,
|
||||
'form': form
|
||||
}
|
||||
data = {"book": book, "form": form}
|
||||
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
|
||||
if add_author:
|
||||
data['add_author'] = add_author
|
||||
data["add_author"] = add_author
|
||||
# check for existing authors
|
||||
vector = SearchVector('name', weight='A') +\
|
||||
SearchVector('aliases', weight='B')
|
||||
vector = SearchVector("name", weight="A") + SearchVector(
|
||||
"aliases", weight="B"
|
||||
)
|
||||
|
||||
data['author_matches'] = models.Author.objects.annotate(
|
||||
search=vector
|
||||
).annotate(
|
||||
rank=SearchRank(vector, add_author)
|
||||
).filter(rank__gt=0.4).order_by('-rank')[:5]
|
||||
data["author_matches"] = (
|
||||
models.Author.objects.annotate(search=vector)
|
||||
.annotate(rank=SearchRank(vector, add_author))
|
||||
.filter(rank__gt=0.4)
|
||||
.order_by("-rank")[:5]
|
||||
)
|
||||
|
||||
# we're creating a new book
|
||||
if not book:
|
||||
# check if this is an edition of an existing work
|
||||
author_text = book.author_text if book else add_author
|
||||
data['book_matches'] = connector_manager.local_search(
|
||||
'%s %s' % (form.cleaned_data.get('title'), author_text),
|
||||
data["book_matches"] = connector_manager.local_search(
|
||||
"%s %s" % (form.cleaned_data.get("title"), author_text),
|
||||
min_confidence=0.5,
|
||||
raw=True
|
||||
raw=True,
|
||||
)[:5]
|
||||
|
||||
# either of the above cases requires additional confirmation
|
||||
if add_author or not book:
|
||||
# creting a book or adding an author to a book needs another step
|
||||
data['confirm_mode'] = True
|
||||
return TemplateResponse(request, 'edit_book.html', data)
|
||||
data["confirm_mode"] = True
|
||||
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:
|
||||
book.authors.remove(author_id)
|
||||
|
||||
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(
|
||||
permission_required('bookwyrm.edit_book', raise_exception=True),
|
||||
name='dispatch')
|
||||
permission_required("bookwyrm.edit_book", raise_exception=True), name="dispatch"
|
||||
)
|
||||
class ConfirmEditBook(View):
|
||||
''' confirm edits to a book '''
|
||||
""" confirm edits to a book """
|
||||
|
||||
def post(self, request, book_id=None):
|
||||
''' edit a book cool '''
|
||||
""" edit a book cool """
|
||||
# returns None if no match is found
|
||||
book = models.Edition.objects.filter(id=book_id).first()
|
||||
form = forms.EditionForm(request.POST, request.FILES, instance=book)
|
||||
|
||||
data = {
|
||||
'book': book,
|
||||
'form': form
|
||||
}
|
||||
data = {"book": book, "form": form}
|
||||
if not form.is_valid():
|
||||
return TemplateResponse(request, 'edit_book.html', data)
|
||||
return TemplateResponse(request, "edit_book.html", data)
|
||||
|
||||
with transaction.atomic():
|
||||
# save book
|
||||
book = form.save()
|
||||
|
||||
# get or create author as needed
|
||||
if request.POST.get('add_author'):
|
||||
if request.POST.get('author_match'):
|
||||
if request.POST.get("add_author"):
|
||||
if request.POST.get("author_match"):
|
||||
author = get_object_or_404(
|
||||
models.Author, id=request.POST['author_match'])
|
||||
models.Author, id=request.POST["author_match"]
|
||||
)
|
||||
else:
|
||||
author = models.Author.objects.create(
|
||||
name=request.POST.get('add_author'))
|
||||
name=request.POST.get("add_author")
|
||||
)
|
||||
book.authors.add(author)
|
||||
|
||||
# create work, if needed
|
||||
if not book_id:
|
||||
work_match = request.POST.get('parent_work')
|
||||
work_match = request.POST.get("parent_work")
|
||||
if work_match:
|
||||
work = get_object_or_404(models.Work, id=work_match)
|
||||
else:
|
||||
|
@ -215,7 +212,7 @@ class ConfirmEditBook(View):
|
|||
book.parent_work = work
|
||||
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)
|
||||
|
||||
return redirect("/book/%s" % book.id)
|
||||
|
|
2
bw-dev
2
bw-dev
|
@ -36,7 +36,7 @@ function initdb {
|
|||
}
|
||||
|
||||
function makeitblack {
|
||||
runweb black celerywyrm bookwyrm
|
||||
docker-compose run --rm web black celerywyrm bookwyrm
|
||||
}
|
||||
|
||||
CMD=$1
|
||||
|
|
Loading…
Reference in a new issue