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 Date: Wed, 7 Apr 2021 18:38:26 -0700 Subject: [PATCH 6/7] Avoid showing "None" for title and subtitle fields --- 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 963b2b75f..1da7c3f70 100644 --- a/bookwyrm/templates/book/edit_book.html +++ b/bookwyrm/templates/book/edit_book.html @@ -90,7 +90,7 @@

{% trans "Metadata" %}

- +

{% 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 Date: Thu, 8 Apr 2021 08:59:07 -0700 Subject: [PATCH 7/7] Makes localname case insensitive --- .../migrations/0063_auto_20210408_1556.py | 27 +++++++++++++++++++ bookwyrm/models/user.py | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 bookwyrm/migrations/0063_auto_20210408_1556.py diff --git a/bookwyrm/migrations/0063_auto_20210408_1556.py b/bookwyrm/migrations/0063_auto_20210408_1556.py new file mode 100644 index 000000000..750997fb6 --- /dev/null +++ b/bookwyrm/migrations/0063_auto_20210408_1556.py @@ -0,0 +1,27 @@ +# Generated by Django 3.1.6 on 2021-04-08 15:56 + +import bookwyrm.models.fields +import django.contrib.postgres.fields.citext +import django.contrib.postgres.operations +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0062_auto_20210407_1545"), + ] + + operations = [ + django.contrib.postgres.operations.CITextExtension(), + migrations.AlterField( + model_name="user", + name="localname", + field=django.contrib.postgres.fields.citext.CICharField( + max_length=255, + null=True, + unique=True, + validators=[bookwyrm.models.fields.validate_localname], + ), + ), + ] diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index dcc4162eb..c519f76c9 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -4,6 +4,7 @@ from urllib.parse import urlparse from django.apps import apps from django.contrib.auth.models import AbstractUser, Group +from django.contrib.postgres.fields import CICharField from django.core.validators import MinValueValidator from django.db import models from django.utils import timezone @@ -54,7 +55,7 @@ class User(OrderedCollectionPageMixin, AbstractUser): summary = fields.HtmlField(null=True, blank=True) local = models.BooleanField(default=False) bookwyrm_user = fields.BooleanField(default=True) - localname = models.CharField( + localname = CICharField( max_length=255, null=True, unique=True,