From: tilly-Q Date: Wed, 14 May 2014 15:51:13 +0000 (-0400) Subject: Tweaked the metadata edit screen to run jsonschema validators against the data. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0d6550fb05c25e230706c719e3a476d1b1e670b9;p=mediagoblin.git Tweaked the metadata edit screen to run jsonschema validators against the data. --- diff --git a/mediagoblin/edit/forms.py b/mediagoblin/edit/forms.py index c2355980..7c390a3f 100644 --- a/mediagoblin/edit/forms.py +++ b/mediagoblin/edit/forms.py @@ -15,10 +15,12 @@ # along with this program. If not, see . 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 @@ -123,11 +125,37 @@ class ChangeEmailForm(wtforms.Form): 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')) + identifier = wtforms.TextField(_(u'Identifier'),[MetaDataValidator()]) value = wtforms.TextField(_(u'Value')) class EditMetaDataForm(wtforms.Form): media_metadata = wtforms.FieldList( - wtforms.FormField(MetaDataForm, label="") + wtforms.FormField(MetaDataForm, ""), ) diff --git a/mediagoblin/edit/views.py b/mediagoblin/edit/views.py index 34021257..cfbaf2fa 100644 --- a/mediagoblin/edit/views.py +++ b/mediagoblin/edit/views.py @@ -20,6 +20,7 @@ from itsdangerous import BadSignature from pyld import jsonld from werkzeug.exceptions import Forbidden from werkzeug.utils import secure_filename +from jsonschema import ValidationError, Draft4Validator from mediagoblin import messages from mediagoblin import mg_globals @@ -33,7 +34,8 @@ from mediagoblin.decorators import (require_active_login, active_user_from_url, get_user_collection, user_has_privilege, user_not_banned) from mediagoblin.tools.crypto import get_timed_signer_url -from mediagoblin.tools.metadata import compact_and_validate +from mediagoblin.tools.metadata import (compact_and_validate, DEFAULT_CHECKER, + DEFAULT_SCHEMA) from mediagoblin.tools.mail import email_debug_message from mediagoblin.tools.response import (render_to_response, redirect, redirect_obj, render_404) @@ -444,24 +446,19 @@ def edit_metadata(request, media): if request.method == "POST" and form.validate(): metadata_dict = dict([(row['identifier'],row['value']) for row in form.media_metadata.data]) + json_ld_metadata = None json_ld_metadata = compact_and_validate(metadata_dict) media.media_metadata = json_ld_metadata media.save() return redirect_obj(request, media) - if media.media_metadata: + if media.media_metadata and len(form.media_metadata) == 0: for identifier, value in media.media_metadata.iteritems(): if identifier == "@context": continue form.media_metadata.append_entry({ 'identifier':identifier, 'value':value}) - else: - form.media_metadata.append_entry({ - 'identifier':"", - 'value':""}) - form.media_metadata.append_entry({ - 'identifier':"", - 'value':""}) + return render_to_response( request, 'mediagoblin/edit/metadata.html', diff --git a/mediagoblin/static/css/base.css b/mediagoblin/static/css/base.css index a3b564ea..9087034b 100644 --- a/mediagoblin/static/css/base.css +++ b/mediagoblin/static/css/base.css @@ -940,18 +940,13 @@ p.verifier { /* for the media metadata editing table */ table.metadata_editor { - margin: 10px auto; - width: 1000px; -} - -table.metadata_editor tr th { - width:100px; + width: 800px; } table.metadata_editor tr td { - width:300px; + width:350px; } table.metadata_editor tr td.form_field_input input { - width:300px; + width:350px; } diff --git a/mediagoblin/templates/mediagoblin/edit/metadata.html b/mediagoblin/templates/mediagoblin/edit/metadata.html index b5a52e5f..21eb27b1 100644 --- a/mediagoblin/templates/mediagoblin/edit/metadata.html +++ b/mediagoblin/templates/mediagoblin/edit/metadata.html @@ -69,7 +69,7 @@
-

{% trans %}Data{% endtrans %}

+

{% trans %}MetaData{% endtrans %}

{{ wtforms_util.render_fieldlist_as_table_rows(form.media_metadata) }} @@ -77,16 +77,13 @@ - - - diff --git a/mediagoblin/templates/mediagoblin/utils/wtforms.html b/mediagoblin/templates/mediagoblin/utils/wtforms.html index c83d53f1..7e16708c 100644 --- a/mediagoblin/templates/mediagoblin/utils/wtforms.html +++ b/mediagoblin/templates/mediagoblin/utils/wtforms.html @@ -77,20 +77,21 @@ {% macro render_form_as_table_row(form) %} {%- for field in form %} - {%- endfor %} + + {%- for field in form %} + {% for error in field.errors %} + + + + {%- endfor %} + {%- endfor %} {%- endmacro %} {% macro render_field_as_table_row(field) %} @@ -98,16 +99,15 @@ + {% for error in field.errors %} + + + + {%- endfor %} {% endmacro %} {% macro render_fieldlist_as_table_rows(fieldlist) %}