open-law/app/forms/version.py

34 lines
1.1 KiB
Python
Raw Normal View History

2023-06-12 15:18:21 +03:00
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, IntegerField, ValidationError
from wtforms.validators import DataRequired
from app.controllers import clean_html
from app.logger import log
from app import models as m, db
class BaseVersionForm(FlaskForm):
version_id = IntegerField("Version ID")
def validate_version_id(self, field):
version: m.BookVersion = db.session.get(m.BookVersion, field.data)
if not version:
raise ValidationError("Version not found")
class EditVersionForm(BaseVersionForm):
semver = StringField("Semver")
submit = SubmitField("Edit")
def validate_semver(self, field):
version: m.BookVersion = db.session.get(m.BookVersion, self.version_id.data)
if not version:
raise ValidationError("Version not found")
version_semvers = [version.semver.lower() for version in version.book.versions]
if field.data.lower() in version_semvers:
raise ValidationError("Version name must me unique")
class DeleteVersionForm(BaseVersionForm):
submit = SubmitField("Delete")