From ffc0051ab0348563222c4dd6dac14c8e50d4423e Mon Sep 17 00:00:00 2001 From: SvyatoslavArtymovych Date: Tue, 2 May 2023 09:48:10 +0300 Subject: [PATCH] move validators to form --- app/forms/book.py | 33 +++++++++++++++++++++++++++++++- app/templates/book/settings.html | 1 + app/views/book.py | 16 ++-------------- tests/test_book.py | 7 +++++-- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/app/forms/book.py b/app/forms/book.py index 37be3eb..62e9de7 100644 --- a/app/forms/book.py +++ b/app/forms/book.py @@ -1,6 +1,14 @@ from flask_wtf import FlaskForm -from wtforms import StringField, SubmitField +from wtforms import StringField, SubmitField, ValidationError from wtforms.validators import DataRequired, Length +from flask import ( + flash, + redirect, + url_for, +) + +from app import models as m, db +from app.logger import log class BaseBookForm(FlaskForm): @@ -12,4 +20,27 @@ class CreateBookForm(BaseBookForm): class EditBookForm(BaseBookForm): + book_id = StringField("User ID", [DataRequired()]) submit = SubmitField("Edit book") + + def validate_book_id(self, field): + book_id = field.data + book: m.Book = db.session.get(m.Book, book_id) + if not book or book.is_deleted: + log(log.WARNING, "Book with id [%s] not found", book_id) + raise ValidationError("Book not found") + + def validate_label(self, field): + label = field.data + book_id = self.book_id.data + + existing_book: m.Book = m.Book.query.filter_by( + is_deleted=False, + label=label, + ).first() + if existing_book: + log( + log.WARNING, "Book with label [%s] already exists: [%s]", label, book_id + ) + flash("Book label must be unique!", "danger") + raise ValidationError("Book label must be unique!") diff --git a/app/templates/book/settings.html b/app/templates/book/settings.html index 0652f22..a7c686f 100644 --- a/app/templates/book/settings.html +++ b/app/templates/book/settings.html @@ -14,6 +14,7 @@
{{ form_hidden_tag() }} +
diff --git a/app/views/book.py b/app/views/book.py index 6a5f620..68ef74e 100644 --- a/app/views/book.py +++ b/app/views/book.py @@ -82,32 +82,20 @@ def edit(book_id: int): form = f.EditBookForm() if form.validate_on_submit(): book: m.Book = db.session.get(m.Book, book_id) - if not book or book.is_deleted: - log(log.WARNING, "Book with id [%s] not found", book_id) - flash("Book not found", "danger") - return redirect(url_for("book.my_books")) label = form.label.data - existing_book: m.Book = m.Book.query.filter_by( - is_deleted=False, - label=label, - ).first() - if existing_book: - log(log.WARNING, "Book with id [%s] not found", book_id) - flash("Book label must be unique!", "danger") - return redirect(url_for("book.my_books")) book.label = label log(log.INFO, "Update Book: [%s]", book) book.save() flash("Success!", "success") - return redirect(url_for("book.my_books")) + return redirect(url_for("book.collection_view", book_id=book_id)) else: log(log.ERROR, "Book create errors: [%s]", form.errors) for field, errors in form.errors.items(): field_label = form._fields[field].label.text for error in errors: flash(error.replace("Field", field_label), "danger") - return redirect(url_for("book.collection_view", book_id=book_id)) + return redirect(url_for("book.settings", book_id=book_id)) @bp.route("//collections", methods=["GET"]) diff --git a/tests/test_book.py b/tests/test_book.py index 9acac07..3b27d29 100644 --- a/tests/test_book.py +++ b/tests/test_book.py @@ -62,9 +62,10 @@ def test_create_edit_book(client: FlaskClient): assert len(book.versions) == 1 response: Response = client.post( - "/book/9999/edit", + "/book/999/edit", data=dict( - label=BOOK_NAME, + book_id=999, + label="Book 1", ), follow_redirects=True, ) @@ -75,6 +76,7 @@ def test_create_edit_book(client: FlaskClient): response: Response = client.post( f"/book/{book.id}/edit", data=dict( + book_id=book.id, label=BOOK_NAME, ), follow_redirects=True, @@ -86,6 +88,7 @@ def test_create_edit_book(client: FlaskClient): response: Response = client.post( f"/book/{book.id}/edit", data=dict( + book_id=book.id, label=BOOK_NAME + " EDITED", ), follow_redirects=True,