def validator(): ctx = { 'conversion_formats': FORMATS_LIST } if request.method == 'GET' and 'url' not in request.args: return render_template('validator.html', **ctx) else: try: doc_content = _load_document() except FetchError as e: ctx['fetch_error'] = unicode(e) return render_template('validator.html', **ctx) try: if not isinstance(doc_content, unicode): doc_content = doc_content.decode('utf8') doc, doc_format = deserialize(doc_content) except Exception as e: ctx.update(doc_content=doc_content, deserialize_error=unicode(e)) return render_template('validator.html', **ctx) if doc_format == 'json': json_doc = doc try: xml_doc = json_doc_to_xml(json_doc, custom_namespace='http://validator.open511.com/custom-field') except Exception as e: ctx.update(error=unicode(e), doc_content=doc_content) return render_template('validator.html', **ctx) elif doc_format == 'xml': xml_doc = doc try: json_doc = xml_to_json(xml_doc) except: json_doc = 'Error generating JSON' else: raise NotImplementedError try: validate(xml_doc) success = True except Open511ValidationError as e: success = False error = unicode(e) ctx.update( success=success, error=None if success else error, xml_string=serialize(xml_doc), json_string=serialize(json_doc), doc_format=doc_format ) return render_template('validator.html', **ctx)
def render_json(self, request, result): xml_doc = self.get_xml_doc(request, result) json_obj = xml_to_json(xml_doc) resp = HttpResponse(content_type='application/json') callback = '' if self.allow_jsonp and 'callback' in request.GET: callback = re.sub(r'[^a-zA-Z0-9_]', '', request.GET['callback']) resp.write(callback + '(') json.dump(json_obj, resp, indent=4 if request.pretty_print else None) if callback: resp.write(');') return resp
def validator(): ctx = {'conversion_formats': FORMATS_LIST} if request.method == 'GET' and 'url' not in request.args: return render_template('validator.html', **ctx) else: try: doc_content = _load_document() except FetchError as e: ctx['fetch_error'] = unicode(e) return render_template('validator.html', **ctx) try: if not isinstance(doc_content, unicode): doc_content = doc_content.decode('utf8') doc, doc_format = deserialize(doc_content) except Exception as e: ctx.update(doc_content=doc_content, deserialize_error=unicode(e)) return render_template('validator.html', **ctx) if doc_format == 'json': json_doc = doc try: xml_doc = json_doc_to_xml( json_doc, custom_namespace='http://validator.open511.com/custom-field' ) except Exception as e: ctx.update(error=unicode(e), doc_content=doc_content) return render_template('validator.html', **ctx) elif doc_format == 'xml': xml_doc = doc try: json_doc = xml_to_json(xml_doc) except: json_doc = 'Error generating JSON' else: raise NotImplementedError try: validate(xml_doc) success = True except Open511ValidationError as e: success = False error = unicode(e) ctx.update(success=success, error=None if success else error, xml_string=serialize(xml_doc), json_string=serialize(json_doc), doc_format=doc_format) return render_template('validator.html', **ctx)