diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2020-07-30 17:26:49 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2020-07-30 23:45:30 -0700 |
commit | 81a3d98f8bf647ded6923eb77d3df791888e0a2a (patch) | |
tree | 044b93f2308790da36bb9eb625add00d486ea73f /python/fatcat_web/forms.py | |
parent | 69e281deaf601b39e8ef51d603e3e5e16dc71777 (diff) | |
download | fatcat-81a3d98f8bf647ded6923eb77d3df791888e0a2a.tar.gz fatcat-81a3d98f8bf647ded6923eb77d3df791888e0a2a.zip |
generic helpers for TOML editing routes
Diffstat (limited to 'python/fatcat_web/forms.py')
-rw-r--r-- | python/fatcat_web/forms.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/python/fatcat_web/forms.py b/python/fatcat_web/forms.py index 15585bf6..b06788b8 100644 --- a/python/fatcat_web/forms.py +++ b/python/fatcat_web/forms.py @@ -4,10 +4,12 @@ Note: in thoery could use, eg, https://github.com/christabor/swagger_wtforms, but can't find one that is actually maintained. """ +import toml from flask_wtf import FlaskForm from wtforms import SelectField, DateField, StringField, IntegerField, \ - HiddenField, FormField, FieldList, validators + HiddenField, FormField, FieldList, validators, ValidationError, TextAreaField +from fatcat_tools import entity_to_toml from fatcat_openapi_client import ContainerEntity, FileEntity, \ ReleaseEntity, ReleaseContrib, FileUrl, ReleaseExtIds @@ -413,3 +415,27 @@ class SavePaperNowForm(FlaskForm): ingest_request['link_source'] = 'arxiv' ingest_request['link_source_id'] = release.ext_ids.arxiv return ingest_request + +def valid_toml(form, field): + try: + toml.loads(field.data) + except toml.TomlDecodeError as tpe: + raise ValidationError(tpe) + +class EntityTomlForm(EntityEditForm): + + toml = TextAreaField( + "TOML", + [validators.DataRequired(), + valid_toml, + ], + ) + + @staticmethod + def from_entity(entity): + """ + Initializes form with TOML version of existing entity + """ + etf = EntityTomlForm() + etf.toml.data = entity_to_toml(entity) + return etf |