From a701bfcf8e9b1250ad77b645ba37d073ea4707ee Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 19 Mar 2022 08:44:30 -0700 Subject: [PATCH 1/2] Uses custom date select widget for publication dates --- bookwyrm/forms/books.py | 15 ++++----------- .../templates/book/edit/edit_book_form.html | 9 +++------ .../templates/widgets/addon_multiwidget.html | 9 +++++++++ bookwyrm/templates/widgets/select.html | 10 ++++++++++ bookwyrm/views/books/edit_book.py | 19 ------------------- 5 files changed, 26 insertions(+), 36 deletions(-) create mode 100644 bookwyrm/templates/widgets/addon_multiwidget.html create mode 100644 bookwyrm/templates/widgets/select.html diff --git a/bookwyrm/forms/books.py b/bookwyrm/forms/books.py index 20f307fae..9b3c84010 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 06035e333..bf5404be5 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 000000000..5c8616b88 --- /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 000000000..eb59ebd33 --- /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 40cc44cab..2315cfce2 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] From 45672c2b7077936c62a04895d3bd274e6f411d85 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 19 Mar 2022 09:04:50 -0700 Subject: [PATCH 2/2] Adds missing widgets file --- bookwyrm/forms/widgets.py | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 bookwyrm/forms/widgets.py diff --git a/bookwyrm/forms/widgets.py b/bookwyrm/forms/widgets.py new file mode 100644 index 000000000..ee9345aa0 --- /dev/null +++ b/bookwyrm/forms/widgets.py @@ -0,0 +1,70 @@ +""" using django model forms """ +from django import forms + + +class ArrayWidget(forms.widgets.TextInput): + """Inputs for postgres array fields""" + + # 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 Select(forms.Select): + """custom template for select widget""" + + template_name = "widgets/select.html" + + +class SelectDateWidget(forms.SelectDateWidget): + """ + A widget that splits date input into two