diff options
Diffstat (limited to 'mediagoblin/edit/forms.py')
-rw-r--r-- | mediagoblin/edit/forms.py | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/mediagoblin/edit/forms.py b/mediagoblin/edit/forms.py index 2c9b5e99..c0bece8b 100644 --- a/mediagoblin/edit/forms.py +++ b/mediagoblin/edit/forms.py @@ -15,10 +15,12 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import wtforms +from jsonschema import Draft4Validator from mediagoblin.tools.text import tag_length_validator from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ from mediagoblin.tools.licenses import licenses_as_choices +from mediagoblin.tools.metadata import DEFAULT_SCHEMA, DEFAULT_CHECKER from mediagoblin.auth.tools import normalize_user_or_email_field @@ -38,7 +40,7 @@ class EditForm(wtforms.Form): "Separate tags by commas.")) slug = wtforms.TextField( _('Slug'), - [wtforms.validators.Required(message=_("The slug can't be empty"))], + [wtforms.validators.InputRequired(message=_("The slug can't be empty"))], description=_( "The title part of this media's address. " "You usually don't need to change this.")) @@ -85,7 +87,7 @@ class EditAttachmentsForm(wtforms.Form): class EditCollectionForm(wtforms.Form): title = wtforms.TextField( _('Title'), - [wtforms.validators.Length(min=0, max=500), wtforms.validators.Required(message=_("The title can't be empty"))]) + [wtforms.validators.Length(min=0, max=500), wtforms.validators.InputRequired(message=_("The title can't be empty"))]) description = wtforms.TextAreaField( _('Description of this collection'), description=_("""You can use @@ -93,7 +95,7 @@ class EditCollectionForm(wtforms.Form): Markdown</a> for formatting.""")) slug = wtforms.TextField( _('Slug'), - [wtforms.validators.Required(message=_("The slug can't be empty"))], + [wtforms.validators.InputRequired(message=_("The slug can't be empty"))], description=_( "The title part of this collection's address. " "You usually don't need to change this.")) @@ -102,12 +104,12 @@ class EditCollectionForm(wtforms.Form): class ChangePassForm(wtforms.Form): old_password = wtforms.PasswordField( _('Old password'), - [wtforms.validators.Required()], + [wtforms.validators.InputRequired()], description=_( "Enter your old password to prove you own this account.")) new_password = wtforms.PasswordField( _('New password'), - [wtforms.validators.Required(), + [wtforms.validators.InputRequired(), wtforms.validators.Length(min=6, max=30)], id="password") @@ -115,10 +117,45 @@ class ChangePassForm(wtforms.Form): class ChangeEmailForm(wtforms.Form): new_email = wtforms.TextField( _('New email address'), - [wtforms.validators.Required(), + [wtforms.validators.InputRequired(), normalize_user_or_email_field(allow_user=False)]) password = wtforms.PasswordField( _('Password'), - [wtforms.validators.Required()], + [wtforms.validators.InputRequired()], description=_( "Enter your password to prove you own this account.")) + +class MetaDataValidator(object): + """ + Custom validator which runs form data in a MetaDataForm through a jsonschema + validator and passes errors recieved in jsonschema to wtforms. + + :param schema The json schema to validate the data against. By + default this uses the DEFAULT_SCHEMA from + mediagoblin.tools.metadata. + :param format_checker The FormatChecker object that limits which types + jsonschema can recognize. By default this uses + DEFAULT_CHECKER from mediagoblin.tools.metadata. + """ + def __init__(self, schema=DEFAULT_SCHEMA, format_checker=DEFAULT_CHECKER): + self.schema = schema + self.format_checker = format_checker + + def __call__(self, form, field): + metadata_dict = {field.data:form.value.data} + validator = Draft4Validator(self.schema, + format_checker=self.format_checker) + errors = [e.message + for e in validator.iter_errors(metadata_dict)] + if len(errors) >= 1: + raise wtforms.validators.ValidationError( + errors.pop()) + +class MetaDataForm(wtforms.Form): + identifier = wtforms.TextField(_(u'Identifier'),[MetaDataValidator()]) + value = wtforms.TextField(_(u'Value')) + +class EditMetaDataForm(wtforms.Form): + media_metadata = wtforms.FieldList( + wtforms.FormField(MetaDataForm, ""), + ) |