modifies edit book code to allow creation as well

This commit is contained in:
Mouse Reeve 2021-03-02 09:01:31 -08:00
parent 2c2d7c4694
commit 2faf5cea2a
3 changed files with 73 additions and 42 deletions

View file

@ -2,12 +2,16 @@
{% load i18n %}
{% load humanize %}
{% block title %}{% trans "Edit Book" %}{% endblock %}
{% block title %}{% if book %}{% blocktrans with book_title=book.title %}Edit "{{ book_title }}"{% endblocktrans %}{% else %}{% trans "Add Book" %}{% endif %}{% endblock %}
{% block content %}
<header class="block">
<h1 class="title level-left">
Edit "{{ book.title }}"
{% if book %}
{% blocktrans with book_title=book.title %}Edit "{{ book.title }}"{% endblocktrans %}
{% else %}
{% trans "Add Book" %}
{% endif %}
</h1>
<div>
<p>{% trans "Added:" %} {{ book.created_date | naturaltime }}</p>
@ -27,6 +31,7 @@
<input type="hidden" name="last_edited_by" value="{{ request.user.id }}">
<div class="columns">
<div class="column">
<section class="block">
<h2 class="title is-4">{% trans "Metadata" %}</h2>
<p class="fields is-grouped"><label class="label" for="id_title">{% trans "Title:" %}</label> {{ form.title }} </p>
{% for error in form.title.errors %}
@ -56,6 +61,19 @@
{% for error in form.published_date.errors %}
<p class="help is-danger">{{ error | escape }}</p>
{% endfor %}
</section>
<section class="block">
<h2 class="title is-4">{% trans "Authors" %}</h2>
{% for author in book.authors.all %}
<p><a href="{{ author.local_path }}">{{ author.name }}</a>
<label class="label">
<input type="checkbox" name="remove-author" value="{{ author.id }}"> {% trans "Remove this author" %}
</label>
{% endfor %}
<label class="label" for="id_add_author">{% trans "Add Author:" %}</label>
<input class="input" type="text" name="author" id="id_add_author" placeholder="John Doe">
</section>
</div>
<div class="column">

View file

@ -134,6 +134,7 @@ urlpatterns = [
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()),
# author
re_path(r'^author/(?P<author_id>\d+)(.json)?/?$', views.Author.as_view()),

View file

@ -106,8 +106,10 @@ class Book(View):
name='dispatch')
class EditBook(View):
''' edit a book '''
def get(self, request, book_id):
def get(self, request, book_id=None):
''' info about a book '''
book = None
if book_id:
book = get_edition(book_id)
if not book.description:
book.description = book.parent_work.description
@ -117,17 +119,27 @@ class EditBook(View):
}
return TemplateResponse(request, 'edit_book.html', data)
def post(self, request, book_id):
def post(self, request, book_id=None):
''' edit a book cool '''
book = get_object_or_404(models.Edition, id=book_id)
book = get_object_or_404(models.Edition, id=book_id) if book_id \
else None
form = forms.EditionForm(request.POST, request.FILES, instance=book)
if not form.is_valid():
data = {
'book': book,
'form': form
}
if not form.is_valid():
return TemplateResponse(request, 'edit_book.html', data)
if not book or form.author:
# creting a book or adding an author to a book needs another step
return TemplateResponse(request, 'confirm_book.html', data)
# remove authors
if request.POST.get('remove-author'):
import pdb;pdb.set_trace()
author = get_object_or_404(id=author_id)
book = form.save()
return redirect('/book/%s' % book.id)