def test_create_template_same_name(self): with open(join(RESOURCES_PATH, 'a.xsd'), 'r') as data_file: data_content = data_file.read() create_template(data_content, "a", "a.xsd") self.assertEquals(len(Template.objects()), 1) with self.assertRaises(Exception): create_template(data_content, "a", "a.xsd")
def execute_test(self, file_id): filename_inc = '{}_inc.xsd'.format(file_id) # load XSD xsd_file_path_inc = join(self.resources_path, filename_inc) xsd_file_inc = open(xsd_file_path_inc, 'r') xsd_file_content_inc = xsd_file_inc.read() # create template template_inc = create_template(xsd_file_content_inc, filename_inc, filename_inc) template_version = TemplateVersion.objects().get( pk=template_inc.templateVersion) # remove the dependency template template_version.deletedVersions.append(str(template_inc.id)) template_version.isDeleted = True template_version.save() filename = '{}.xsd'.format(file_id) # load XSD xsd_file_path = join(self.resources_path, filename) xsd_file = open(xsd_file_path, 'r') xsd_file_content = xsd_file.read() # create template xsd_tree = etree.fromstring(xsd_file_content) update_dependencies(xsd_tree, {filename_inc: str(template_inc.id)}) xsd_file_content = etree.tostring(xsd_tree) template = create_template(xsd_file_content, filename, filename, [str(template_inc.id)]) # load the form load_new_form(self.driver, self.base_url, file_id) time.sleep(TIMEOUT) # download XML self.driver.execute_script("downloadCurrentXML();") # wait a bit more to let time to save the file time.sleep(TIMEOUT * 5) # load expected result exp_result_path = join(self.resources_path, "{0}.xml".format(file_id)) exp_result_file = open(exp_result_path, 'r') exp_result_content = exp_result_file.read() # load result generated by the form result_path = join(self.results_path, "{0}.xml".format(file_id)) result_file = open(result_path, 'r') result_content = result_file.read() expected = etree.fromstring(exp_result_content) result = etree.fromstring(result_content) if not are_equals(expected, result): raise Exception('NOT EQUALS TO EXPECTED: {}'.format(file_id))
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 execute_test(self, file_id): filename_inc = '{}_inc.xsd'.format(file_id) # load XSD xsd_file_path_inc = join(self.resources_path, filename_inc) xsd_file_inc = open(xsd_file_path_inc, 'r') xsd_file_content_inc = xsd_file_inc.read() # create template template_inc = create_template(xsd_file_content_inc, filename_inc, filename_inc) template_version = TemplateVersion.objects().get(pk=template_inc.templateVersion) # remove the dependency template template_version.deletedVersions.append(str(template_inc.id)) template_version.isDeleted = True template_version.save() filename = '{}.xsd'.format(file_id) # load XSD xsd_file_path = join(self.resources_path, filename) xsd_file = open(xsd_file_path, 'r') xsd_file_content = xsd_file.read() # create template xsd_tree = etree.fromstring(xsd_file_content) update_dependencies(xsd_tree, {filename_inc: str(template_inc.id)}) xsd_file_content = etree.tostring(xsd_tree) template = create_template(xsd_file_content, filename, filename, [str(template_inc.id)]) # load the form load_new_form(self.driver, self.base_url, file_id) time.sleep(TIMEOUT) # download XML self.driver.execute_script("downloadCurrentXML();") # wait a bit more to let time to save the file time.sleep(TIMEOUT * 5) # load expected result exp_result_path = join(self.resources_path, "{0}.xml".format(file_id)) exp_result_file = open(exp_result_path, 'r') exp_result_content = exp_result_file.read() # load result generated by the form result_path = join(self.results_path, "{0}.xml".format(file_id)) result_file = open(result_path, 'r') result_content = result_file.read() expected = etree.fromstring(exp_result_content) result = etree.fromstring(result_content) if not are_equals(expected, result): raise Exception('NOT EQUALS TO EXPECTED: {}'.format(file_id))
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)
def execute_test(self, file_id): filename = '{}.xsd'.format(file_id) # load XSD xsd_file_path = join(self.resources_path, filename) xsd_file = open(xsd_file_path, 'r') xsd_file_content = xsd_file.read() # create template template = create_template(xsd_file_content, filename, filename) # load the form load_new_form(self.driver, self.base_url, file_id) time.sleep(TIMEOUT) # download XML self.driver.execute_script("downloadCurrentXML();") # wait a bit more to let time to save the file time.sleep(TIMEOUT * 5) # load expected result exp_result_path = join(self.resources_path, "{0}.xml".format(file_id)) exp_result_file = open(exp_result_path, 'r') exp_result_content = exp_result_file.read() # load result generated by the form result_path = join(self.results_path, "{0}.xml".format(file_id)) result_file = open(result_path, 'r') result_content = result_file.read() expected = etree.fromstring(exp_result_content) result = etree.fromstring(result_content) if not are_equals(expected, result): raise Exception('NOT EQUALS TO EXPECTED: {}'.format(file_id))
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 add_template(self, file_id): filename = '{}.xsd'.format(file_id) # load XSD xsd_file_path = join(self.resources_path, filename) xsd_file = open(xsd_file_path, 'r') xsd_file_content = xsd_file.read() # create template template = create_template(xsd_file_content, filename, filename)
def load_template(template_path): """ Load the template to search on :param template_path: :return: """ # Open the the file with open(template_path, 'r') as template_file: # read the file content template_content = template_file.read() return create_template(template_content, template_path, template_path)
# validate the schema error = validate_xml_schema(xml_tree) if error is not None: response_dict['errors'] = 'This is not a valid XML schema.' + error.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_template(content, template_name, template_name, dependencies, user=str(request.user.id)) return HttpResponse(json.dumps(response_dict), content_type='application/javascript') ################################################################################ # # Function Name: save_type(request) # Inputs: request - HTTP request # Outputs: JSON # Exceptions: None # Description: save the current type in the database # ################################################################################ def save_type(request): type_name = request.POST['typeName']
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'] = 'This is not a valid XML schema.' + error.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_template(content, template_name, template_name, dependencies, user=str(request.user.id)) return HttpResponse(json.dumps(response_dict), content_type='application/javascript') ################################################################################ # # Function Name: save_type(request) # Inputs: request - HTTP request # Outputs: JSON # Exceptions: None # Description: save the current type in the database # ################################################################################ def save_type(request): type_name = request.POST['typeName']
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))
def load_templates(): """ Loads templates/xslt for NMRR the first time """ # if templates are already present, initialization already happened existing_templates = Template.objects() if len(existing_templates) == 0: vocab_template_filename = 'mse_vocab.xsd' templates = { 'all': 'res-md.xsd', 'organization': 'res-md.xsd', 'datacollection': 'res-md.xsd', 'repository': 'res-md.xsd', 'projectarchive': 'res-md.xsd', 'database': 'res-md.xsd', 'dataset': 'res-md.xsd', 'service': 'res-md.xsd', 'informational': 'res-md.xsd', 'software': 'res-md.xsd', } template_ids = [] template_results = { 'full': 'nmrr-full_demo.xsl', 'detail': 'nmrr-detail_demo.xsl', } template_results_id = { 'full': None, 'detail': None, } # connect to mongo client = MongoClient(MONGODB_URI) # connect to the db 'mgi' db = client[MGI_DB] # add vocab vocab_file = open( os.path.join(SITE_ROOT, 'static', 'resources', 'xsd', vocab_template_filename), 'r') vocab_content = vocab_file.read() vocab_template = create_template(vocab_content, 'vocab', vocab_template_filename) # Add the templates for template_name, template_path in templates.iteritems(): template_file = open( os.path.join(SITE_ROOT, 'static', 'resources', 'xsd', template_path), 'r') template_content = template_file.read() # add local vocab to template xml_tree = etree.parse(BytesIO(template_content.encode('utf-8'))) update_dependencies(xml_tree, {'mse_vocab.xsd': str(vocab_template.id)}) template_content = etree.tostring(xml_tree) # create template resource_template = create_template( template_content, template_name, template_path, dependencies=[str(vocab_template.id)], user=None, validation=False) template_ids.append(str(resource_template.id)) # Add xslt xsl_col = db['result_xslt'] for xsl_name, xsl_path in template_results.iteritems(): file = open( os.path.join(SITE_ROOT, 'static', 'resources', 'xsl', xsl_path), 'r') fileContent = file.read() xsl = {} xsl['name'] = xsl_name xsl['filename'] = xsl_path xsl['content'] = fileContent xsl_id = xsl_col.insert(xsl) template_results_id[xsl_name] = str(xsl_id) templates = db['template'] results_xslt = { 'ResultXsltList': template_results_id['full'], 'ResultXsltDetailed': template_results_id['detail'] } templates.update({}, {"$set": results_xslt}, upsert=False, multi=True) 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")