mirror of
https://github.com/logos-co/open-law.git
synced 2025-01-27 07:05:06 +00:00
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
|
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")
|