From 502f29082c04cc652e94090a7d8a0720576b585c Mon Sep 17 00:00:00 2001 From: n2no1 <7995366-n2no1@users.noreply.gitlab.com> Date: Tue, 6 Apr 2021 19:57:57 -0400 Subject: [PATCH 1/7] check the form for initial date values if the book has none --- bookwyrm/templates/book/edit_book.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templates/book/edit_book.html b/bookwyrm/templates/book/edit_book.html index a9ce651e7..8cf6f31c9 100644 --- a/bookwyrm/templates/book/edit_book.html +++ b/bookwyrm/templates/book/edit_book.html @@ -124,7 +124,7 @@
- +
{% for error in form.first_published_date.errors %}{{ error | escape }}
@@ -132,7 +132,7 @@- +
{% for error in form.published_date.errors %}{{ error | escape }}
From 0941c50c6913d94430d03525396df1b9dca75d3d Mon Sep 17 00:00:00 2001 From: n2no1 <7995366-n2no1@users.noreply.gitlab.com> Date: Tue, 6 Apr 2021 20:43:37 -0400 Subject: [PATCH 2/7] ensure that the book edit confirmation receives initial date data as a datetime --- bookwyrm/templates/book/edit_book.html | 4 ++-- bookwyrm/views/books.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templates/book/edit_book.html b/bookwyrm/templates/book/edit_book.html index 8cf6f31c9..0cde8dff2 100644 --- a/bookwyrm/templates/book/edit_book.html +++ b/bookwyrm/templates/book/edit_book.html @@ -124,7 +124,7 @@- +
{% for error in form.first_published_date.errors %}{{ error | escape }}
@@ -132,7 +132,7 @@- +
{% for error in form.published_date.errors %}{{ error | escape }}
diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index 58886cade..e982a7d3c 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -1,4 +1,5 @@ """ the good stuff! the books! """ +from datetime import datetime from uuid import uuid4 from django.contrib.auth.decorators import login_required, permission_required @@ -172,6 +173,12 @@ class EditBook(View): data["confirm_mode"] = True # this isn't preserved because it isn't part of the form obj data["remove_authors"] = request.POST.getlist("remove_authors") + # we have to make sure the dates are passed in as datetime, they're currently a string + # QueryDicts are immutable, we need to copy + formcopy = data["form"].data.copy() + formcopy["first_published_date"] = datetime.strptime(formcopy["first_published_date"], "%Y-%m-%d") + formcopy["published_date"] = datetime.strptime(formcopy["published_date"], "%Y-%m-%d") + data["form"].data = formcopy return TemplateResponse(request, "book/edit_book.html", data) remove_authors = request.POST.getlist("remove_authors") From 51e16fba97b459eb110294628653db298aa8489c Mon Sep 17 00:00:00 2001 From: n2no1 <7995366-n2no1@users.noreply.gitlab.com> Date: Tue, 6 Apr 2021 20:49:19 -0400 Subject: [PATCH 3/7] run black, add a try/catch around the formcopy fix --- bookwyrm/views/books.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index e982a7d3c..02f61ea77 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -176,8 +176,18 @@ class EditBook(View): # we have to make sure the dates are passed in as datetime, they're currently a string # QueryDicts are immutable, we need to copy formcopy = data["form"].data.copy() - formcopy["first_published_date"] = datetime.strptime(formcopy["first_published_date"], "%Y-%m-%d") - formcopy["published_date"] = datetime.strptime(formcopy["published_date"], "%Y-%m-%d") + try: + formcopy["first_published_date"] = datetime.strptime( + formcopy["first_published_date"], "%Y-%m-%d" + ) + except MultiValueDictKeyError: + pass + try: + formcopy["published_date"] = datetime.strptime( + formcopy["published_date"], "%Y-%m-%d" + ) + except MultiValueDictKeyError: + pass data["form"].data = formcopy return TemplateResponse(request, "book/edit_book.html", data) From 31146b00e47052e450eb93ca2559aaa33cfbdc5c Mon Sep 17 00:00:00 2001 From: n2no1 <7995366-n2no1@users.noreply.gitlab.com> Date: Tue, 6 Apr 2021 21:40:11 -0400 Subject: [PATCH 4/7] import MultiValueDictKeyError to catch with formcopy --- bookwyrm/views/books.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index 02f61ea77..2e48942b2 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -11,6 +11,7 @@ from django.db.models import Avg, Q from django.http import HttpResponseBadRequest, HttpResponseNotFound 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 import View from django.views.decorators.http import require_POST From ac86c194d4550903e73414f4ad7ad9d93b21c5a8 Mon Sep 17 00:00:00 2001 From: n2no1 <7995366-n2no1@users.noreply.gitlab.com> Date: Wed, 7 Apr 2021 14:11:13 -0400 Subject: [PATCH 5/7] move from strptime to dateutil for parsing date inputs --- bookwyrm/views/books.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index 2e48942b2..b1b2d0656 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -2,6 +2,7 @@ from datetime import datetime from uuid import uuid4 +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.core.files.base import ContentFile @@ -178,15 +179,13 @@ class EditBook(View): # QueryDicts are immutable, we need to copy formcopy = data["form"].data.copy() try: - formcopy["first_published_date"] = datetime.strptime( - formcopy["first_published_date"], "%Y-%m-%d" + formcopy["first_published_date"] = dateparse( + formcopy["first_published_date"] ) except MultiValueDictKeyError: pass try: - formcopy["published_date"] = datetime.strptime( - formcopy["published_date"], "%Y-%m-%d" - ) + formcopy["published_date"] = dateparse(formcopy["published_date"]) except MultiValueDictKeyError: pass data["form"].data = formcopy From 89af144105c74d413bf1b87aeced7b5f34bb442a Mon Sep 17 00:00:00 2001 From: Mouse Reeve- +
{% for error in form.title.errors %}{{ error | escape }}
@@ -98,7 +98,7 @@- +
{% for error in form.subtitle.errors %}{{ error | escape }}
From 779377bd0cf1bdf92dd74bafd08f00b252a7a812 Mon Sep 17 00:00:00 2001 From: Mouse Reeve