open-law/app/forms/book.py

40 lines
1.1 KiB
Python
Raw Normal View History

2023-04-24 06:57:38 +00:00
from flask_wtf import FlaskForm
2023-05-02 06:48:10 +00:00
from wtforms import StringField, SubmitField, ValidationError
2023-04-24 06:57:38 +00:00
from wtforms.validators import DataRequired, Length
2023-05-02 06:48:10 +00:00
2023-05-11 11:28:45 +00:00
from app import models as m
2023-05-02 06:48:10 +00:00
from app.logger import log
2023-04-24 06:57:38 +00:00
2023-05-01 15:17:46 +00:00
class BaseBookForm(FlaskForm):
2023-04-27 13:17:25 +00:00
label = StringField("Label", [DataRequired(), Length(6, 256)])
about = StringField("About")
2023-05-01 15:17:46 +00:00
class CreateBookForm(BaseBookForm):
2023-04-24 06:57:38 +00:00
submit = SubmitField("Add new book")
2023-05-01 15:17:46 +00:00
class EditBookForm(BaseBookForm):
2023-05-02 06:48:10 +00:00
book_id = StringField("User ID", [DataRequired()])
2023-05-15 20:29:10 +00:00
tags = StringField("Tags")
2023-05-01 15:17:46 +00:00
submit = SubmitField("Edit book")
2023-05-02 06:48:10 +00:00
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,
)
.filter(m.Book.id != book_id)
.first()
)
2023-05-02 06:48:10 +00:00
if existing_book:
log(
log.WARNING, "Book with label [%s] already exists: [%s]", label, book_id
)
raise ValidationError("Book label must be unique!")