diff --git a/bookwyrm/forms/books.py b/bookwyrm/forms/books.py index 20f307fa..9b3c8401 100644 --- a/bookwyrm/forms/books.py +++ b/bookwyrm/forms/books.py @@ -4,6 +4,7 @@ from django import forms from bookwyrm import models from bookwyrm.models.fields import ClearableFileInputWithWarning from .custom_form import CustomForm +from .widgets import ArrayWidget, SelectDateWidget, Select # pylint: disable=missing-class-docstring @@ -14,14 +15,6 @@ class CoverForm(CustomForm): help_texts = {f: None for f in fields} -class ArrayWidget(forms.widgets.TextInput): - # pylint: disable=unused-argument - # pylint: disable=no-self-use - def value_from_datadict(self, data, files, name): - """get all values for this name""" - return [i for i in data.getlist(name) if i] - - class EditionForm(CustomForm): class Meta: model = models.Edition @@ -56,16 +49,16 @@ class EditionForm(CustomForm): "publishers": forms.TextInput( attrs={"aria-describedby": "desc_publishers_help desc_publishers"} ), - "first_published_date": forms.SelectDateWidget( + "first_published_date": SelectDateWidget( attrs={"aria-describedby": "desc_first_published_date"} ), - "published_date": forms.SelectDateWidget( + "published_date": SelectDateWidget( attrs={"aria-describedby": "desc_published_date"} ), "cover": ClearableFileInputWithWarning( attrs={"aria-describedby": "desc_cover"} ), - "physical_format": forms.Select( + "physical_format": Select( attrs={"aria-describedby": "desc_physical_format"} ), "physical_format_detail": forms.TextInput( diff --git a/bookwyrm/templates/book/edit/edit_book_form.html b/bookwyrm/templates/book/edit/edit_book_form.html index 06035e33..bf5404be 100644 --- a/bookwyrm/templates/book/edit/edit_book_form.html +++ b/bookwyrm/templates/book/edit/edit_book_form.html @@ -155,8 +155,7 @@ - - + {{ form.first_published_date }} {% include 'snippets/form_errors.html' with errors_list=form.first_published_date.errors id="desc_first_published_date" %} @@ -164,7 +163,7 @@ - + {{ form.published_date }} {% include 'snippets/form_errors.html' with errors_list=form.published_date.errors id="desc_published_date" %} @@ -259,9 +258,7 @@ -
- {{ form.physical_format }} -
+ {{ form.physical_format }} {% include 'snippets/form_errors.html' with errors_list=form.physical_format.errors id="desc_physical_format" %} diff --git a/bookwyrm/templates/widgets/addon_multiwidget.html b/bookwyrm/templates/widgets/addon_multiwidget.html new file mode 100644 index 00000000..5c8616b8 --- /dev/null +++ b/bookwyrm/templates/widgets/addon_multiwidget.html @@ -0,0 +1,9 @@ +{% spaceless %} +
+{% for widget in widget.subwidgets %} +
+ {% include widget.template_name %} +
+{% endfor %} +
+{% endspaceless %} diff --git a/bookwyrm/templates/widgets/select.html b/bookwyrm/templates/widgets/select.html new file mode 100644 index 00000000..eb59ebd3 --- /dev/null +++ b/bookwyrm/templates/widgets/select.html @@ -0,0 +1,10 @@ +
+ +
diff --git a/bookwyrm/views/books/edit_book.py b/bookwyrm/views/books/edit_book.py index 40cc44ca..2315cfce 100644 --- a/bookwyrm/views/books/edit_book.py +++ b/bookwyrm/views/books/edit_book.py @@ -1,13 +1,11 @@ """ the good stuff! the books! """ from re import sub, findall -from dateutil.parser import parse as dateparse from django.contrib.auth.decorators import login_required, permission_required from django.contrib.postgres.search import SearchRank, SearchVector from django.db import transaction from django.http import HttpResponseBadRequest from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse -from django.utils.datastructures import MultiValueDictKeyError from django.utils.decorators import method_decorator from django.views.decorators.http import require_POST from django.views import View @@ -52,7 +50,6 @@ class EditBook(View): # either of the above cases requires additional confirmation if data.get("add_author"): - data = copy_form(data) return TemplateResponse(request, "book/edit/edit_book.html", data) remove_authors = request.POST.getlist("remove_authors") @@ -118,7 +115,6 @@ class CreateBook(View): # go to confirm mode if not parent_work_id or data.get("add_author"): - data = copy_form(data) return TemplateResponse(request, "book/edit/edit_book.html", data) with transaction.atomic(): @@ -139,21 +135,6 @@ class CreateBook(View): return redirect(f"/book/{book.id}") -def copy_form(data): - """helper to re-create the date fields in the form""" - formcopy = data["form"].data.copy() - try: - formcopy["first_published_date"] = dateparse(formcopy["first_published_date"]) - except (MultiValueDictKeyError, ValueError): - pass - try: - formcopy["published_date"] = dateparse(formcopy["published_date"]) - except (MultiValueDictKeyError, ValueError): - pass - data["form"].data = formcopy - return data - - def add_authors(request, data): """helper for adding authors""" add_author = [author for author in request.POST.getlist("add_author") if author]