def test_create_type_same_name(self): with open(join(RESOURCES_PATH, 'bType.xsd'), 'r') as data_file: data_content = data_file.read() create_type(data_content, "b", "bType.xsd") self.assertEquals(len(Type.objects()), 1) with self.assertRaises(Exception): create_type(data_content, "b", "bType.xsd")
def resolve_dependencies(request): print 'BEGIN def resolveDependencies(request)' schema_locations = request.POST.getlist('schemaLocations[]') dependencies = request.POST.getlist('dependencies[]') if ('uploadObjectName' in request.session and request.session['uploadObjectName'] is not None and 'uploadObjectFilename' in request.session and request.session['uploadObjectFilename'] is not None and 'uploadObjectContent' in request.session and request.session['uploadObjectContent'] is not None and 'uploadObjectType' in request.session and request.session['uploadObjectType'] is not None): object_content = request.session['uploadObjectContent'] name = request.session['uploadObjectName'] filename = request.session['uploadObjectFilename'] object_type = request.session['uploadObjectType'] # Load a parser able to clean the XML from blanks, comments and processing instructions clean_parser = etree.XMLParser(remove_blank_text=True, remove_comments=True, remove_pis=True) # set the parser etree.set_default_parser(parser=clean_parser) xsd_tree = etree.XML(str(object_content.encode('utf-8'))) # replace includes/imports by API calls (get dependencies starting by the imports) update_dependencies(xsd_tree, dict(zip(schema_locations, dependencies))) # validate the schema error = validate_xml_schema(xsd_tree) if error is None: object_content = etree.tostring(xsd_tree) # create a new version if 'uploadVersion' in request.session and request.session['uploadVersion'] is not None: object_versions_id = request.session['uploadVersion'] if object_type == 'Template': new_template = create_template_version(object_content, filename, object_versions_id) redirect = '/admin/manage_versions?type={0}&id={1}'.format(object_type, str(new_template.id)) elif object_type == 'Type': new_type = create_type_version(object_content, filename, object_versions_id) redirect = '/admin/manage_versions?type={0}&id={1}'.format(object_type, str(new_type.id)) # create new object else: # save the object if object_type == "Template": create_template(object_content, name, filename, dependencies) redirect = '/admin/xml-schemas/manage-schemas' elif object_type == "Type": if 'uploadBuckets' in request.session and request.session['uploadBuckets'] is not None: buckets = request.session['uploadBuckets'] create_type(object_content, name, filename, buckets, dependencies) redirect = '/admin/xml-schemas/manage-types' response_dict = {'redirect': redirect} messages.add_message(request, messages.INFO, '{} uploaded with success.'.format(object_type)) return HttpResponse(json.dumps(response_dict), content_type='application/javascript') else: response_dict = {'errorDependencies': error.replace("'", "")} return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
def scan_static_resources(): """ Dynamically load templates/types in resource folder :return: """ try: # if templates are already present, initialization already happened existing_templates = Template.objects() existing_types = Type.objects() if len(existing_templates) == 0 and len(existing_types) == 0: templates_dir = os.path.join(STATIC_DIR, 'templates') if os.path.exists(templates_dir): for filename in os.listdir(templates_dir): if filename.endswith(".xsd"): file_path = os.path.join(templates_dir, filename) file = open(file_path, 'r') file_content = file.read() try: name_no_extension = filename.split(".xsd")[0] create_template(file_content, name_no_extension, name_no_extension) except Exception, e: print "ERROR: Unable to load {0} ({1})".format( filename, e.message) else: print "WARNING: {0} not loaded because extension is not {1}".format( filename, ".xsd") types_dir = os.path.join(STATIC_DIR, 'types') if os.path.exists(types_dir): for filename in os.listdir(types_dir): if filename.endswith(".xsd"): file_path = os.path.join(types_dir, filename) file = open(file_path, 'r') file_content = file.read() try: name_no_extension = filename.split(".xsd")[0] create_type(file_content, name_no_extension, name_no_extension) except Exception, e: print "ERROR: Unable to load {0} ({1})".format( filename, e.message) else: print "WARNING: {0} not loaded because extension is not {1}".format( filename, ".xsd")
def load_type(self, type_path): """ Load the type to use in validation :param type_path: :return: """ # Open the the file with open(type_path, 'r') as type_file: # read the file content type_content = type_file.read() # add the type in database type_object = create_type(type_content, 'type_name', type_path) return type_object
def load_type(self, type_path): """ Load the type to use in validation :param type_path: :return: """ # Open the the file with open(type_path, 'r') as type_file: # read the file content type_content = type_file.read() # add the type in database type_object = create_type(type_content, 'type_name', type_path) return type_object
def load_type(self, type_path): """ Load the type to use in the composer :param type_path: :return: """ # Open the the file with open(type_path, 'r') as type_file: # read the file content type_content = type_file.read() # add the type in database type_object = create_type(type_content, type_file.name, type_path) # set the type self.request.POST['typeID'] = type_object.id self.request.POST['typeName'] = 'type_name'
def load_type(self, type_path): """ Load the type to use in the composer :param type_path: :return: """ # Open the the file with open(type_path, 'r') as type_file: # read the file content type_content = type_file.read() # add the type in database type_object = create_type(type_content, 'type_name', type_path) # set the type self.request.POST['typeID'] = type_object.id self.request.POST['typeName'] = 'type_name'
def test_create_template_dependency(self): with open(join(RESOURCES_PATH, 'bType.xsd'), 'r') as data_file: data_content = data_file.read() dependency = create_type(data_content, "b", "bType.xsd") self.assertEquals(len(Type.objects()), 1) with open(join(RESOURCES_PATH, 'a.xsd'), 'r') as data_file: data_content = data_file.read() xml_tree = etree.parse(BytesIO(data_content.encode('utf-8'))) update_dependencies(xml_tree, {'bType.xsd': str(dependency.id)}) data_content = etree.tostring(xml_tree) create_template(data_content, "a", "a.xsd", dependencies=[str(dependency.id)]) self.assertEquals(len(Template.objects()), 1)
# validate the schema error = validate_xml_schema(xmlTree) if error is not None: response_dict['errors'] = "Not a valid XML document." response_dict['message'] = error.message.replace("'", "") return HttpResponse(json.dumps(response_dict), content_type='application/javascript') dependencies = [] for uri in request.session["includedTypesCompose"]: url = urlparse(uri) id = url.query.split("=")[1] dependencies.append(id) create_type(content, type_name, type_name, [], dependencies, user=str(request.user.id)) return HttpResponse(json.dumps(response_dict), content_type='application/javascript') ################################################################################ # # Function Name: get_occurrences(request) # Inputs: request - HTTP request # Outputs: JSON # Exceptions: None # Description: Get the occurrences of the selected element # ################################################################################ def get_occurrences(request): xpath = request.POST['xpath']
def resolve_dependencies(request): print 'BEGIN def resolveDependencies(request)' schema_locations = request.POST.getlist('schemaLocations[]') dependencies = request.POST.getlist('dependencies[]') if ('uploadObjectName' in request.session and request.session['uploadObjectName'] is not None and 'uploadObjectFilename' in request.session and request.session['uploadObjectFilename'] is not None and 'uploadObjectContent' in request.session and request.session['uploadObjectContent'] is not None and 'uploadObjectType' in request.session and request.session['uploadObjectType'] is not None): object_content = request.session['uploadObjectContent'] name = request.session['uploadObjectName'] filename = request.session['uploadObjectFilename'] object_type = request.session['uploadObjectType'] # Load a parser able to clean the XML from blanks, comments and processing instructions clean_parser = etree.XMLParser(remove_blank_text=True, remove_comments=True, remove_pis=True) # set the parser etree.set_default_parser(parser=clean_parser) xsd_tree = etree.XML(str(object_content.encode('utf-8'))) # replace includes/imports by API calls (get dependencies starting by the imports) update_dependencies(xsd_tree, dict(zip(schema_locations, dependencies))) # validate the schema error = validate_xml_schema(xsd_tree) if error is None: object_content = etree.tostring(xsd_tree) # create a new version if 'uploadVersion' in request.session and request.session[ 'uploadVersion'] is not None: object_versions_id = request.session['uploadVersion'] if object_type == 'Template': new_template = create_template_version(object_content, filename, object_versions_id) redirect = '/admin/manage_versions?type={0}&id={1}'.format( object_type, str(new_template.id)) elif object_type == 'Type': new_type = create_type_version(object_content, filename, object_versions_id) redirect = '/admin/manage_versions?type={0}&id={1}'.format( object_type, str(new_type.id)) # create new object else: # save the object if object_type == "Template": create_template(object_content, name, filename, dependencies) redirect = '/admin/xml-schemas/manage-schemas' elif object_type == "Type": if 'uploadBuckets' in request.session and request.session[ 'uploadBuckets'] is not None: buckets = request.session['uploadBuckets'] create_type(object_content, name, filename, buckets, dependencies) redirect = '/admin/xml-schemas/manage-types' response_dict = {'redirect': redirect} messages.add_message(request, messages.INFO, '{} uploaded with success.'.format(object_type)) return HttpResponse(json.dumps(response_dict), content_type='application/javascript') else: response_dict = {'errorDependencies': error.replace("'", "")} return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
response_dict['errors'] = "Not a valid XML document." response_dict['message'] = error.message.replace("'","") return HttpResponse(json.dumps(response_dict), content_type='application/javascript') dependencies = [] for uri in request.session["includedTypesCompose"]: try: url = urlparse(uri) id = url.query.split("=")[1] # add dependency if it matches a type id Type.objects().get(pk=id) dependencies.append(id) except: pass create_type(content, type_name, type_name, [], dependencies, user=str(request.user.id)) return HttpResponse(json.dumps(response_dict), content_type='application/javascript') ################################################################################ # # Function Name: get_occurrences(request) # Inputs: request - HTTP request # Outputs: JSON # Exceptions: None # Description: Get the occurrences of the selected element # ################################################################################ def get_occurrences(request): xpath = request.POST['xpath']
def upload_xsd(request): # get the type of object in param: Template or Type object_type = request.GET['type'] if 'type' in request.GET else None request.session['uploadObjectContent'] = None request.session['uploadObjectName'] = None request.session['uploadObjectFilename'] = None request.session['uploadObjectType'] = None request.session['uploadVersion'] = None # check if object type is set if object_type is not None: # load the html template to upload xsd template = loader.get_template('admin/upload_xsd.html') # check the parameters are correct if object_type in ['Template', 'Type']: # method is POST if request.method == 'POST': if object_type == 'Template': form = UploadTemplateForm(request.POST, request.FILES) elif object_type == 'Type': form = UploadTypeForm(request.POST, request.FILES) if form.is_valid(): # get the schema name name = request.POST['name'] # get the file from the form xsd_file = request.FILES['xsd_file'] # put the cursor at the beginning of the file xsd_file.seek(0) # read the content of the file xsd_data = xsd_file.read() try: if object_type == 'Template': create_template(xsd_data, name, xsd_file.name) # XML schema loaded with success messages.add_message(request, messages.INFO, '{} uploaded with success.'.format(object_type)) return redirect('/admin/xml-schemas/manage-schemas') elif object_type == 'Type': buckets = request.POST.getlist('buckets') create_type(xsd_data, name, xsd_file.name, buckets) # XML schema loaded with success messages.add_message(request, messages.INFO, '{} uploaded with success.'.format(object_type)) return redirect('/admin/xml-schemas/manage-types') except XSDError, e: error = e.message try: xsd_tree = etree.parse(BytesIO(xsd_data.encode('utf-8'))) except: xsd_tree = etree.parse(BytesIO(xsd_data)) # a problem with includes/imports has been detected # get the imports imports = xsd_tree.findall("{}import".format(LXML_SCHEMA_NAMESPACE)) # get the includes includes = xsd_tree.findall("{}include".format(LXML_SCHEMA_NAMESPACE)) if len(includes) > 0 or len(imports) > 0: # build the list of dependencies list_dependencies_template = loader.get_template('admin/list_dependencies.html') context = RequestContext(request, { 'templates': template_list_current(), 'types': type_list_current(), }) list_dependencies_html = list_dependencies_template.render(context) imports = xsd_tree.findall("{}import".format(LXML_SCHEMA_NAMESPACE)) includes = xsd_tree.findall("{}include".format(LXML_SCHEMA_NAMESPACE)) # build the dependency resolver form dependency_resolver_template = loader.get_template('admin/dependency_resolver.html') context = RequestContext(request, { 'imports': imports, 'includes': includes, 'xsd_content': utils.html.escape(xsd_data), 'dependencies': list_dependencies_html, }) dependency_resolver_html = dependency_resolver_template.render(context) context = RequestContext(request, { 'upload_form': form, 'object_type': object_type, 'dependency_resolver': dependency_resolver_html, }) # TODO: use a better method to store schema information # TODO: can create an entry in db request.session['uploadObjectName'] = name request.session['uploadObjectFilename'] = xsd_file.name request.session['uploadObjectContent'] = xsd_data request.session['uploadObjectType'] = object_type if object_type == 'Type': request.session['uploadBuckets'] = request.POST.getlist('buckets') return HttpResponse(template.render(context)) else: context = RequestContext(request, { 'upload_form': form, 'object_type': object_type, 'errors': error.replace('"', '\''), }) return HttpResponse(template.render(context)) except Exception, e: error = e.message context = RequestContext(request, { 'upload_form': form, 'object_type': object_type, 'errors': error.replace('"', '\''), }) return HttpResponse(template.render(context)) else: context = RequestContext(request, { 'upload_form': form, 'object_type': object_type, }) return HttpResponse(template.render(context)) # method is GET else: # if the param is Template if object_type == 'Template': # render the form to upload a template context = RequestContext(request, { 'upload_form': UploadTemplateForm(), 'object_type': object_type, }) return HttpResponse(template.render(context)) # if the param is Type elif object_type == 'Type': # render the form to upload a type context = RequestContext(request, { 'upload_form': UploadTypeForm(), 'object_type': object_type, }) return HttpResponse(template.render(context))