def _add_template(): """Add the registry template. Returns: """ xsd_filepath = REGISTRY_XSD_FILEPATH xsd_filename = REGISTRY_XSD_FILENAME if xsd_filename == "": raise Exception( "Please configure the REGISTRY_XSD_FILENAME setting in your project." ) if xsd_filepath == "": raise Exception( "Please configure the REGISTRY_XSD_FILEPATH setting in your project." ) try: version_manager_api.get_active_global_version_manager_by_title( xsd_filename) except exceptions.DoesNotExist: default_xsd_path = finders.find(xsd_filepath) xsd_data = read_file_content(default_xsd_path) template = Template(filename=xsd_filename, content=xsd_data) template_version_manager = TemplateVersionManager(title=xsd_filename) template_version_manager_api.insert(template_version_manager, template) except Exception as e: logger.error("Impossible to add the template: {0}".format(str(e)))
def _get_or_create_xslt(filename): """ Get or create an xslt. Args: filename: XSLT filename. Returns: XSLT. """ try: return xslt_transformation_api.get_by_name(filename) except exceptions.ApiError: # Get XSLT. list_xslt_path = finders.find(join(XSL_FOLDER_PATH, filename)) # Read content. list_xsl_data = read_file_content(list_xslt_path) # Create the XSLT. list_xslt = XslTransformation(name=filename, filename=filename, content=list_xsl_data) return xslt_transformation_api.upsert(list_xslt) except Exception, e: raise Exception("Impossible to add the xslt {0} : {1} ".format( filename, e.message))
def _render_xml_as_html( xml_string, template_id=None, template_hash=None, xslt_type=XSLType.type_list, xsl_transform_id=None, ): """Render an XML to HTML according to an xslt type (list or detail). Args: xml_string: template_id: template_hash: xslt_type: Returns: HTML """ try: try: if xslt_type not in (XSLType.type_list, XSLType.type_detail): raise Exception("XSLT Type unknown. Default xslt will be used.") if xsl_transform_id: xsl_transformation = xsl_transformation_api.get_by_id(xsl_transform_id) elif template_id or template_hash: if template_id: template_xsl_rendering = ( template_xsl_rendering_api.get_by_template_id(template_id) ) else: template_xsl_rendering = ( template_xsl_rendering_api.get_by_template_hash(template_hash) ) if xslt_type == XSLType.type_list: xsl_transformation = template_xsl_rendering.list_xslt else: xsl_transformation = template_xsl_rendering.default_detail_xslt else: raise Exception( "No template information provided. Default xslt will be used." ) xslt_string = xsl_transformation.content except (Exception, exceptions.DoesNotExist): default_xslt_path = finders.find(DEFAULT_DATA_RENDERING_XSLT) xslt_string = read_file_content(default_xslt_path) return xsl_transform(xml_string, xslt_string) except Exception: return xml_string
def build_template(request, template_id): """View that allows to build the Template. Args: request: template_id: Returns: """ if template_id == "new": base_template_path = finders.find(join('core_composer_app', 'user', 'xsd', 'new_base_template.xsd')) xsd_string = read_file_content(base_template_path) else: template = template_api.get(template_id) xsd_string = template.content request.session['newXmlTemplateCompose'] = xsd_string request.session['includedTypesCompose'] = [] # store the current includes/imports xsd_tree = XSDTree.build_tree(xsd_string) includes = xsd_tree.findall("{}include".format(LXML_SCHEMA_NAMESPACE)) for el_include in includes: if 'schemaLocation' in el_include.attrib: request.session['includedTypesCompose'].append(el_include.attrib['schemaLocation']) imports = xsd_tree.findall("{}import".format(LXML_SCHEMA_NAMESPACE)) for el_import in imports: if 'schemaLocation' in el_import.attrib: request.session['includedTypesCompose'].append(el_import.attrib['schemaLocation']) # remove annotations from the tree remove_annotations(xsd_tree) xsd_string = XSDTree.tostring(xsd_tree) # loads XSLT xslt_path = finders.find(join('core_composer_app', 'user', 'xsl', 'xsd2html.xsl')) # reads XSLT xslt_string = read_file_content(xslt_path) # transform XML to HTML xsd_to_html_string = xsl_transform(xsd_string, xslt_string) # 1) Get user defined types user_types = type_version_manager_api.get_version_managers_by_user(str(request.user.id)) # 2) Get buckets buckets = bucket_api.get_all() # 3) no_buckets_types: list of types that are not assigned to a specific bucket no_buckets_types = type_version_manager_api.get_no_buckets_types() # 4) Build list of built-in types built_in_types = [] for built_in_type in get_xsd_types(): built_in_types.append({'current': 'built_in_type', 'title': built_in_type}) assets = { "js": [ { "path": 'core_composer_app/user/js/build_template.js', "is_raw": False }, { "path": 'core_composer_app/user/js/build_template.raw.js', "is_raw": True }, { "path": 'core_composer_app/user/js/xpath.js', "is_raw": False }, { "path": 'core_composer_app/user/js/menus.js', "is_raw": False }, { "path": 'core_composer_app/user/js/xsd_tree.js', "is_raw": False }, ], "css": ['core_main_app/common/css/XMLTree.css', 'core_composer_app/common/css/bucket.css', 'core_composer_app/user/css/menu.css', 'core_composer_app/user/css/style.css', 'core_composer_app/user/css/xsd_tree.css'] } context = { 'buckets': buckets, 'built_in_types': built_in_types, 'no_buckets_types': no_buckets_types, 'user_types': user_types, 'xsd_form': xsd_to_html_string, 'template_id': template_id, } modals = [ 'core_composer_app/user/builder/menus/sequence.html', 'core_composer_app/user/builder/menus/element.html', 'core_composer_app/user/builder/menus/element_root.html', 'core_composer_app/user/builder/modals/root_type_name.html', 'core_composer_app/user/builder/modals/element_name.html', 'core_composer_app/user/builder/modals/insert_element.html', 'core_composer_app/user/builder/modals/delete_element.html', 'core_composer_app/user/builder/modals/change_type.html', 'core_composer_app/user/builder/modals/save_template.html', 'core_composer_app/user/builder/modals/save_type.html', 'core_composer_app/user/builder/modals/save_success.html', 'core_composer_app/user/builder/modals/occurrences.html', 'core_composer_app/user/builder/modals/errors.html', ] return render(request, 'core_composer_app/user/build_template.html', assets=assets, context=context, modals=modals)