mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-23 16:40:36 +00:00
Create new books
This commit is contained in:
parent
d3162e12db
commit
a5baa1f5c6
5 changed files with 39 additions and 23 deletions
|
@ -134,7 +134,7 @@ class EditionForm(CustomForm):
|
|||
'updated_date',
|
||||
'edition_rank',
|
||||
|
||||
'authors',# TODO
|
||||
'authors',
|
||||
'parent_work',
|
||||
'shelves',
|
||||
|
||||
|
|
|
@ -13,11 +13,13 @@
|
|||
{% trans "Add Book" %}
|
||||
{% endif %}
|
||||
</h1>
|
||||
{% if book %}
|
||||
<div>
|
||||
<p>{% trans "Added:" %} {{ book.created_date | naturaltime }}</p>
|
||||
<p>{% trans "Updated:" %} {{ book.updated_date | naturaltime }}</p>
|
||||
<p>{% trans "Last edited by:" %} <a href="{{ book.last_edited_by.remote_id }}">{{ book.last_edited_by.display_name }}</a></p>
|
||||
</div>
|
||||
{% endif %}
|
||||
</header>
|
||||
|
||||
{% if form.non_field_errors %}
|
||||
|
@ -26,7 +28,12 @@
|
|||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if book %}
|
||||
<form class="block" name="edit-book" action="{{ book.local_path }}/{% if confirm_mode %}confirm{% else %}edit{% endif %}" method="post" enctype="multipart/form-data">
|
||||
{% else %}
|
||||
<form class="block" name="create-book" action="/create-book{% if confirm_mode %}/confirm{% endif %}" method="post" enctype="multipart/form-data">
|
||||
{% endif %}
|
||||
|
||||
{% csrf_token %}
|
||||
{% if confirm_mode %}
|
||||
<div class="box">
|
||||
|
@ -111,12 +118,8 @@
|
|||
</label>
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
{% if confirm_mode %}
|
||||
<input type="hidden" name="add_author" value="{{ add_author }}">
|
||||
{% else %}
|
||||
<label class="label" for="id_add_author">{% trans "Add Author:" %}</label>
|
||||
<input class="input" type="text" name="add_author" id="id_add_author" placeholder="{% trans 'John Doe' %}" value="{{ add_author }}">
|
||||
{% endif %}
|
||||
</section>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -68,6 +68,10 @@
|
|||
{% include 'snippets/toggle/close_button.html' with text=button_text small=True controls_text="more-results" %}
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="block">
|
||||
<a href="/create-book">Manually add book</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="column">
|
||||
|
|
|
@ -130,12 +130,13 @@ urlpatterns = [
|
|||
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'^create-book/?$', views.EditBook.as_view()),
|
||||
|
||||
# isbn
|
||||
re_path(r'^isbn/(?P<isbn>\d+)(.json)?/?$', views.Isbn.as_view()),
|
||||
|
|
|
@ -145,7 +145,7 @@ class EditBook(View):
|
|||
search=vector
|
||||
).annotate(
|
||||
rank=SearchRank(vector, add_author)
|
||||
).filter(rank__gt=0.8).order_by('-rank')[:5]
|
||||
).filter(rank__gt=0.4).order_by('-rank')[:5]
|
||||
|
||||
# we're creating a new book
|
||||
if not book:
|
||||
|
@ -190,25 +190,33 @@ class ConfirmEditBook(View):
|
|||
if not form.is_valid():
|
||||
return TemplateResponse(request, 'edit_book.html', data)
|
||||
|
||||
# create work, if needed
|
||||
# TODO
|
||||
with transaction.atomic():
|
||||
# save book
|
||||
book = form.save()
|
||||
|
||||
# save book
|
||||
book = form.save()
|
||||
# get or create author as needed
|
||||
if request.POST.get('add_author'):
|
||||
if request.POST.get('author_match'):
|
||||
author = get_object_or_404(
|
||||
models.Author, id=request.POST['author_match'])
|
||||
else:
|
||||
author = models.Author.objects.create(
|
||||
name=request.POST.get('add_author'))
|
||||
book.authors.add(author)
|
||||
|
||||
# get or create author as needed
|
||||
if request.POST.get('add_author'):
|
||||
if request.POST.get('author_match'):
|
||||
author = get_object_or_404(
|
||||
models.Author, id=request.POST['author_match'])
|
||||
else:
|
||||
author = models.Author.objects.create(
|
||||
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')
|
||||
if work_match:
|
||||
work = get_object_or_404(models.Work, id=work_match)
|
||||
else:
|
||||
work = models.Work.objects.create(title=form.cleaned_data.title)
|
||||
work.authors.set(book.authors.all())
|
||||
book.parent_work = work
|
||||
book.save()
|
||||
|
||||
remove_authors = request.POST.getlist('remove_authors')
|
||||
for author_id in remove_authors:
|
||||
book.authors.remove(author_id)
|
||||
for author_id in request.POST.getlist('remove_authors'):
|
||||
book.authors.remove(author_id)
|
||||
|
||||
return redirect('/book/%s' % book.id)
|
||||
|
||||
|
|
Loading…
Reference in a new issue