open-law/app/forms/interpretation.py

63 lines
2.0 KiB
Python
Raw Normal View History

2023-05-04 12:14:17 +00:00
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, ValidationError
from wtforms.validators import DataRequired, Length
2023-05-04 13:32:56 +00:00
from app import models as m, db
2023-05-04 12:14:17 +00:00
from app.logger import log
class BaseInterpretationForm(FlaskForm):
label = StringField("Label", [DataRequired(), Length(3, 256)])
about = StringField("About")
class CreateInterpretationForm(BaseInterpretationForm):
section_id = StringField("Interpretation ID", [DataRequired()])
text = StringField("Text")
submit = SubmitField("Create")
def validate_label(self, field):
label = field.data
section_id = self.section_id.data
interpretation: m.Interpretation = m.Interpretation.query.filter_by(
is_deleted=False, label=label, section_id=section_id
).first()
if interpretation:
log(
log.WARNING,
"Interpretation with label [%s] already exists: [%s]",
label,
interpretation,
)
raise ValidationError("Interpretation label must be unique!")
2023-05-04 13:32:56 +00:00
class EditInterpretationForm(BaseInterpretationForm):
interpretation_id = StringField("Interpretation ID", [DataRequired()])
text = StringField("Text")
submit = SubmitField("Edit")
def validate_label(self, field):
label = field.data
interpretation_id = self.interpretation_id.data
section_id = db.session.get(m.Interpretation, interpretation_id).section_id
interpretation: m.Interpretation = (
m.Interpretation.query.filter_by(
is_deleted=False, label=label, section_id=section_id
)
.filter(m.Interpretation.id != interpretation_id)
.first()
)
if interpretation:
log(
log.WARNING,
"Interpretation with label [%s] already exists: [%s]",
label,
interpretation,
)
raise ValidationError("Interpretation label must be unique!")