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 i18n %}
{% load humanize %} {% 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 %} {% block content %}
<header class="block"> <header class="block">
<h1 class="title level-left"> <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> </h1>
<div> <div>
<p>{% trans "Added:" %} {{ book.created_date | naturaltime }}</p> <p>{% trans "Added:" %} {{ book.created_date | naturaltime }}</p>
@ -27,35 +31,49 @@
<input type="hidden" name="last_edited_by" value="{{ request.user.id }}"> <input type="hidden" name="last_edited_by" value="{{ request.user.id }}">
<div class="columns"> <div class="columns">
<div class="column"> <div class="column">
<h2 class="title is-4">{% trans "Metadata" %}</h2> <section class="block">
<p class="fields is-grouped"><label class="label" for="id_title">{% trans "Title:" %}</label> {{ form.title }} </p> <h2 class="title is-4">{% trans "Metadata" %}</h2>
{% for error in form.title.errors %} <p class="fields is-grouped"><label class="label" for="id_title">{% trans "Title:" %}</label> {{ form.title }} </p>
<p class="help is-danger">{{ error | escape }}</p> {% for error in form.title.errors %}
{% endfor %} <p class="help is-danger">{{ error | escape }}</p>
<p class="fields is-grouped"><label class="label" for="id_subtitle">{% trans "Subtitle:" %}</label> {{ form.subtitle }} </p> {% endfor %}
{% for error in form.subtitle.errors %} <p class="fields is-grouped"><label class="label" for="id_subtitle">{% trans "Subtitle:" %}</label> {{ form.subtitle }} </p>
<p class="help is-danger">{{ error | escape }}</p> {% for error in form.subtitle.errors %}
{% endfor %} <p class="help is-danger">{{ error | escape }}</p>
<p class="fields is-grouped"><label class="label" for="id_description">{% trans "Description:" %}</label> {{ form.description }} </p> {% endfor %}
{% for error in form.description.errors %} <p class="fields is-grouped"><label class="label" for="id_description">{% trans "Description:" %}</label> {{ form.description }} </p>
<p class="help is-danger">{{ error | escape }}</p> {% for error in form.description.errors %}
{% endfor %} <p class="help is-danger">{{ error | escape }}</p>
<p class="fields is-grouped"><label class="label" for="id_series">{% trans "Series:" %}</label> {{ form.series }} </p> {% endfor %}
{% for error in form.series.errors %} <p class="fields is-grouped"><label class="label" for="id_series">{% trans "Series:" %}</label> {{ form.series }} </p>
<p class="help is-danger">{{ error | escape }}</p> {% for error in form.series.errors %}
{% endfor %} <p class="help is-danger">{{ error | escape }}</p>
<p class="fields is-grouped"><label class="label" for="id_series_number">{% trans "Series number:" %}</label> {{ form.series_number }} </p> {% endfor %}
{% for error in form.series_number.errors %} <p class="fields is-grouped"><label class="label" for="id_series_number">{% trans "Series number:" %}</label> {{ form.series_number }} </p>
<p class="help is-danger">{{ error | escape }}</p> {% for error in form.series_number.errors %}
{% endfor %} <p class="help is-danger">{{ error | escape }}</p>
<p class="fields is-grouped"><label class="label" for="id_first_published_date">{% trans "First published date:" %}</label> {{ form.first_published_date }} </p> {% endfor %}
{% for error in form.first_published_date.errors %} <p class="fields is-grouped"><label class="label" for="id_first_published_date">{% trans "First published date:" %}</label> {{ form.first_published_date }} </p>
<p class="help is-danger">{{ error | escape }}</p> {% for error in form.first_published_date.errors %}
{% endfor %} <p class="help is-danger">{{ error | escape }}</p>
<p class="fields is-grouped"><label class="label" for="id_published_date">{% trans "Published date:" %}</label> {{ form.published_date }} </p> {% endfor %}
{% for error in form.published_date.errors %} <p class="fields is-grouped"><label class="label" for="id_published_date">{% trans "Published date:" %}</label> {{ form.published_date }} </p>
<p class="help is-danger">{{ error | escape }}</p> {% for error in form.published_date.errors %}
{% endfor %} <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>
<div class="column"> <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'^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),
re_path(r'^create-book/?$', views.EditBook.as_view()),
# author # author
re_path(r'^author/(?P<author_id>\d+)(.json)?/?$', views.Author.as_view()), re_path(r'^author/(?P<author_id>\d+)(.json)?/?$', views.Author.as_view()),

View File

@ -106,28 +106,40 @@ class Book(View):
name='dispatch') name='dispatch')
class EditBook(View): class EditBook(View):
''' edit a book ''' ''' edit a book '''
def get(self, request, book_id): def get(self, request, book_id=None):
''' info about a book ''' ''' info about a book '''
book = get_edition(book_id) book = None
if not book.description: if book_id:
book.description = book.parent_work.description book = get_edition(book_id)
if not book.description:
book.description = book.parent_work.description
data = { data = {
'book': book, 'book': book,
'form': forms.EditionForm(instance=book) 'form': forms.EditionForm(instance=book)
} }
return TemplateResponse(request, 'edit_book.html', data) return TemplateResponse(request, 'edit_book.html', data)
def post(self, request, book_id): def post(self, request, book_id=None):
''' edit a book cool ''' ''' 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) form = forms.EditionForm(request.POST, request.FILES, instance=book)
data = {
'book': book,
'form': form
}
if not form.is_valid(): if not form.is_valid():
data = {
'book': book,
'form': form
}
return TemplateResponse(request, 'edit_book.html', data) 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() book = form.save()
return redirect('/book/%s' % book.id) return redirect('/book/%s' % book.id)