def is_schema_valid(object_type, content, name=None): # is the name unique? if name is not None: if object_type.lower() == 'template': names = Template.objects.all().values_list('title') elif object_type.lower() == 'type': names = Type.objects.all().values_list('title') if name in names: raise MDCSError( 'A {} with the same name already exists'.format(object_type)) # is it a valid XML document? try: try: xsd_tree = etree.parse(BytesIO(content.encode('utf-8'))) except Exception: xsd_tree = etree.parse(BytesIO(content)) except Exception: raise XMLError('Uploaded file is not well formatted XML.') # is it supported by the MDCS? errors = common.getValidityErrorsForMDCS(xsd_tree, object_type) if len(errors) > 0: errors_str = ", ".join(errors) raise MDCSError(errors_str) # is it a valid XML schema? error = validate_xml_schema(xsd_tree) if error is not None: raise XSDError(error)
def upload_version(request): object_version_id = request.POST['objectVersionID'] object_type = request.POST['objectType'] response_dict = {} versionContent = None if ('uploadVersionFilename' in request.session and request.session['uploadVersionFilename'] is not None and 'uploadVersionContent' in request.session and request.session['uploadVersionContent'] is not None): request.session['uploadVersionID'] = object_version_id request.session['uploadVersionType'] = object_type versionContent = request.session['uploadVersionContent'] xmlTree = None # is it a valid XML document ? try: xmlTree = etree.parse(BytesIO(versionContent.encode('utf-8'))) except Exception, e: response_dict['errors'] = "Not a valid XML document." response_dict['message'] = e.message.replace("'", "") return HttpResponse(json.dumps(response_dict), content_type='application/javascript') # is it supported by the MDCS ? errors = common.getValidityErrorsForMDCS(xmlTree, object_type) if len(errors) > 0: errorsStr = "" for error in errors: errorsStr += error + "<br/>" response_dict['errors'] = errorsStr return HttpResponse(json.dumps(response_dict), content_type='application/javascript') # get the imports imports = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}import") # get the includes includes = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}include") if len(imports) != 0 or len(includes) != 0: # Display array to resolve dependencies htmlString = generateHtmlDependencyResolver(imports, includes) response_dict['errors'] = htmlString return HttpResponse(json.dumps(response_dict), content_type='application/javascript') else: try: # is it a valid XML schema ? xmlSchema = etree.XMLSchema(xmlTree) except Exception, e: response_dict['errors'] = "Not a valid XML schema." response_dict['message'] = e.message.replace("'", "") return HttpResponse(json.dumps(response_dict), content_type='application/javascript') request.session['uploadVersionValid'] = True return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
def upload_version(request): object_version_id = request.POST['objectVersionID'] object_type = request.POST['objectType'] response_dict = {} versionContent = None if ('uploadVersionFilename' in request.session and request.session['uploadVersionFilename'] is not None and 'uploadVersionContent' in request.session and request.session['uploadVersionContent'] is not None): request.session['uploadVersionID'] = object_version_id request.session['uploadVersionType'] = object_type versionContent = request.session['uploadVersionContent'] xmlTree = None # is it a valid XML document ? try: xmlTree = etree.parse(BytesIO(versionContent.encode('utf-8'))) except Exception, e: response_dict['errors'] = "Not a valid XML document." response_dict['message'] = e.message.replace("'","") return HttpResponse(json.dumps(response_dict), content_type='application/javascript') # is it supported by the MDCS ? errors = common.getValidityErrorsForMDCS(xmlTree, object_type) if len(errors) > 0: errorsStr = "" for error in errors: errorsStr += error + "<br/>" response_dict['errors'] = errorsStr return HttpResponse(json.dumps(response_dict), content_type='application/javascript') # get the imports imports = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}import") # get the includes includes = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}include") if len(imports) != 0 or len(includes) != 0: # Display array to resolve dependencies htmlString = generateHtmlDependencyResolver(imports, includes) response_dict['errors'] = htmlString return HttpResponse(json.dumps(response_dict), content_type='application/javascript') else: try: # is it a valid XML schema ? xmlSchema = etree.XMLSchema(xmlTree) except Exception, e: response_dict['errors'] = "Not a valid XML schema." response_dict['message'] = e.message.replace("'","") return HttpResponse(json.dumps(response_dict), content_type='application/javascript') request.session['uploadVersionValid'] = True return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
request.session['uploadObjectType'] = object_type request.session['uploadObjectValid'] = False xmlTree = None response_dict = {} # is it a valid XML document ? try: xmlTree = etree.parse(BytesIO(object_content.encode('utf-8'))) except Exception, e: response_dict['errors'] = "Not a valid XML document." response_dict['message'] = e.message.replace("'","") return HttpResponse(json.dumps(response_dict), content_type='application/javascript') # is it supported by the MDCS ? errors = common.getValidityErrorsForMDCS(xmlTree, object_type) if len(errors) > 0: errorsStr = "" for error in errors: errorsStr += error + "<br/>" response_dict['errors'] = errorsStr return HttpResponse(json.dumps(response_dict), content_type='application/javascript') # get the imports imports = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}import") # get the includes includes = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}include") if len(imports) != 0 or len(includes) != 0: # Display array to resolve dependencies htmlString = generateHtmlDependencyResolver(imports, includes)
request.session['uploadObjectValid'] = False xmlTree = None response_dict = {} # is it a valid XML document ? try: xmlTree = etree.parse(BytesIO(object_content.encode('utf-8'))) except Exception, e: response_dict['errors'] = "Not a valid XML document." response_dict['message'] = e.message.replace("'", "") return HttpResponse(json.dumps(response_dict), content_type='application/javascript') # is it supported by the MDCS ? errors = common.getValidityErrorsForMDCS(xmlTree, object_type) if len(errors) > 0: errorsStr = "" for error in errors: errorsStr += error + "<br/>" response_dict['errors'] = errorsStr return HttpResponse(json.dumps(response_dict), content_type='application/javascript') # get the imports imports = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}import") # get the includes includes = xmlTree.findall("{http://www.w3.org/2001/XMLSchema}include") if len(imports) != 0 or len(includes) != 0: # Display array to resolve dependencies