mirror of
https://github.com/logos-co/open-law.git
synced 2025-01-28 07:35:07 +00:00
move validators to form
This commit is contained in:
parent
0a6c5d3544
commit
ffc0051ab0
@ -1,6 +1,14 @@
|
|||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import StringField, SubmitField
|
from wtforms import StringField, SubmitField, ValidationError
|
||||||
from wtforms.validators import DataRequired, Length
|
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):
|
class BaseBookForm(FlaskForm):
|
||||||
@ -12,4 +20,27 @@ class CreateBookForm(BaseBookForm):
|
|||||||
|
|
||||||
|
|
||||||
class EditBookForm(BaseBookForm):
|
class EditBookForm(BaseBookForm):
|
||||||
|
book_id = StringField("User ID", [DataRequired()])
|
||||||
submit = SubmitField("Edit book")
|
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!")
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
<form action="{{ url_for('book.edit', book_id=book.id) }}" method="post" class="mb-0 flex flex-col space-y-2 w-1/2">
|
<form action="{{ url_for('book.edit', book_id=book.id) }}" method="post" class="mb-0 flex flex-col space-y-2 w-1/2">
|
||||||
{{ form_hidden_tag() }}
|
{{ form_hidden_tag() }}
|
||||||
|
<input value="{{book.id}}" type="text" name="book_id" id="book_id" class="hidden" placeholder="Book id" required>
|
||||||
<div>
|
<div>
|
||||||
<label for="label" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Label</label>
|
<label for="label" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Label</label>
|
||||||
<input value="{{book.label}}" type="text" name="label" id="label" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="My Book" required>
|
<input value="{{book.label}}" type="text" name="label" id="label" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="My Book" required>
|
||||||
|
@ -82,32 +82,20 @@ def edit(book_id: int):
|
|||||||
form = f.EditBookForm()
|
form = f.EditBookForm()
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
book: m.Book = db.session.get(m.Book, book_id)
|
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
|
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
|
book.label = label
|
||||||
log(log.INFO, "Update Book: [%s]", book)
|
log(log.INFO, "Update Book: [%s]", book)
|
||||||
book.save()
|
book.save()
|
||||||
flash("Success!", "success")
|
flash("Success!", "success")
|
||||||
return redirect(url_for("book.my_books"))
|
return redirect(url_for("book.collection_view", book_id=book_id))
|
||||||
else:
|
else:
|
||||||
log(log.ERROR, "Book create errors: [%s]", form.errors)
|
log(log.ERROR, "Book create errors: [%s]", form.errors)
|
||||||
for field, errors in form.errors.items():
|
for field, errors in form.errors.items():
|
||||||
field_label = form._fields[field].label.text
|
field_label = form._fields[field].label.text
|
||||||
for error in errors:
|
for error in errors:
|
||||||
flash(error.replace("Field", field_label), "danger")
|
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("/<int:book_id>/collections", methods=["GET"])
|
@bp.route("/<int:book_id>/collections", methods=["GET"])
|
||||||
|
@ -62,9 +62,10 @@ def test_create_edit_book(client: FlaskClient):
|
|||||||
assert len(book.versions) == 1
|
assert len(book.versions) == 1
|
||||||
|
|
||||||
response: Response = client.post(
|
response: Response = client.post(
|
||||||
"/book/9999/edit",
|
"/book/999/edit",
|
||||||
data=dict(
|
data=dict(
|
||||||
label=BOOK_NAME,
|
book_id=999,
|
||||||
|
label="Book 1",
|
||||||
),
|
),
|
||||||
follow_redirects=True,
|
follow_redirects=True,
|
||||||
)
|
)
|
||||||
@ -75,6 +76,7 @@ def test_create_edit_book(client: FlaskClient):
|
|||||||
response: Response = client.post(
|
response: Response = client.post(
|
||||||
f"/book/{book.id}/edit",
|
f"/book/{book.id}/edit",
|
||||||
data=dict(
|
data=dict(
|
||||||
|
book_id=book.id,
|
||||||
label=BOOK_NAME,
|
label=BOOK_NAME,
|
||||||
),
|
),
|
||||||
follow_redirects=True,
|
follow_redirects=True,
|
||||||
@ -86,6 +88,7 @@ def test_create_edit_book(client: FlaskClient):
|
|||||||
response: Response = client.post(
|
response: Response = client.post(
|
||||||
f"/book/{book.id}/edit",
|
f"/book/{book.id}/edit",
|
||||||
data=dict(
|
data=dict(
|
||||||
|
book_id=book.id,
|
||||||
label=BOOK_NAME + " EDITED",
|
label=BOOK_NAME + " EDITED",
|
||||||
),
|
),
|
||||||
follow_redirects=True,
|
follow_redirects=True,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user