def import_views(views_node): log.info('Importing views') nsmap = get_ns_map(views_node.getroot()) for view_node in views_node.findall('view'): view_uri = get_uri(view_node, nsmap) try: view = View.objects.get(uri=view_uri) except View.DoesNotExist: view = View() log.info('View not in db. Created with uri ' + view_uri) else: log.info('View does exist. Loaded from uri ' + view_uri) view.uri_prefix = view_uri.split('/views/')[0] view.key = view_uri.split('/')[-1] for element in view_node.findall('title'): setattr(view, 'title_' + element.attrib['lang'], element.text) for element in view_node.findall('help'): setattr(view, 'help_' + element.attrib['lang'], element.text) try: ViewUniqueKeyValidator(view).validate() except ValidationError: log.info('View not saving "' + str(view.key) + '" due to validation error') pass else: log.info('Optionset saving to "' + str(view.key) + '"') view.template = view_node.find('template').text view.save()
def import_catalog(catalog_node): nsmap = get_ns_map(catalog_node.getroot()) catalog_uri = get_uri(catalog_node, nsmap) log.info('Importing catalog ' + catalog_uri) try: catalog = Catalog.objects.get(uri=catalog_uri) except Catalog.DoesNotExist: catalog = Catalog() log.info('Catalog not in db. Created with uri ' + str(catalog_uri)) else: log.info('Catalog does exist. Loaded from uri ' + str(catalog_uri)) catalog.uri_prefix = catalog_uri.split('/questions/')[0] catalog.key = catalog_uri.split('/')[-1] catalog.comment = get_value_from_treenode(catalog_node, get_ns_tag('dc:comment', nsmap)) catalog.order = get_value_from_treenode(catalog_node, 'order') for element in catalog_node.findall('title'): setattr(catalog, 'title_' + element.attrib['lang'], element.text) try: CatalogUniqueKeyValidator(catalog).validate() except ValidationError: log.info('Catalog not saving "' + str(catalog_uri) + '" due to validation error') pass else: log.info('Catalog saving to "' + str(catalog_uri) + '"') catalog.save() for section_node in catalog_node.find('sections').findall('section'): import_section(section_node, nsmap, catalog=catalog)
def import_attribute_entity(entity_node, nsmap, parent=None): uri = get_uri(entity_node, nsmap) try: entity = AttributeEntity.objects.get(uri=uri, parent=parent) except AttributeEntity.DoesNotExist: entity = AttributeEntity() log.info('Entity not in db. Created with uri ' + str(uri)) else: log.info('Entity does exist. Loaded from uri ' + str(uri)) entity.parent = parent entity.uri_prefix = uri.split('/domain/')[0] entity.key = uri.split('/')[-1] entity.comment = get_value_from_treenode(entity_node, get_ns_tag('dc:comment', nsmap)) entity.is_collection = make_bool(entity_node.find('is_collection').text) try: AttributeEntityUniquePathValidator(entity).validate() except ValidationError: log.info('Entity not saving "' + str(uri) + '" due to validation error') pass else: log.info('Entity saving to "' + str(uri) + '", parent "' + str(parent) + '"') entity.save() if entity_node.find('verbosename').text is not None: import_verbose_name(get_value_from_treenode(entity_node, 'verbosename'), entity) if entity_node.find('conditions') is not None: for condition_node in entity_node.find('conditions').findall('condition'): try: condition_uri = get_uri(condition_node, nsmap, 'plain') condition = Condition.objects.get(uri=condition_uri) entity.conditions.add(condition) except Condition.DoesNotExist: log.info('Condition import failed: ' + str(Condition.DoesNotExist)) pass for child_node in entity_node.find('children').findall('entity'): import_attribute_entity(child_node, nsmap, parent=entity) for child_node in entity_node.find('children').findall('attribute'): import_attribute(child_node, nsmap, parent=entity)
def import_conditions(conditions_node): log.info('Importing conditions') nsmap = get_ns_map(conditions_node.getroot()) for condition_node in conditions_node.findall('condition'): condition_uri = get_uri(condition_node, nsmap) try: condition = Condition.objects.get(uri=condition_uri) except Condition.DoesNotExist: condition = Condition() log.info('Condition not in db. Created with uri ' + condition_uri) else: log.info('Condition does exist. Loaded from uri ' + condition_uri) condition.uri_prefix = condition_uri.split('/conditions/')[0] condition.key = condition_uri.split('/')[-1] condition.comment = get_value_from_treenode( condition_node, get_ns_tag('dc:comment', nsmap)) condition.relation = get_value_from_treenode(condition_node, 'relation') try: # condition_source = get_value_from_treenode(condition_node, 'source', 'attrib') # source_uri = str(condition_source[get_ns_tag('dc:uri', nsmap)]) source_node = condition_node.find('source') source_uri = source_node.get(get_ns_tag('dc:uri', nsmap)) condition.source = Attribute.objects.get(uri=source_uri) except (AttributeError, Attribute.DoesNotExist): condition.source = None if get_value_from_treenode(condition_node, 'target_text') != '': condition.target_text = get_value_from_treenode( condition_node, 'target_text') else: condition.target_text = None try: target_option_node = condition_node.find('target_option') target_option_uri = target_option_node.get( get_ns_tag('dc:uri', nsmap)) condition.target_option = Option.objects.get(uri=target_option_uri) except (AttributeError, Option.DoesNotExist): condition.target_option = None try: ConditionUniqueKeyValidator(condition).validate() except ValidationError: log.info('Condition not saving "' + str(condition_uri) + '" due to validation error') pass else: log.info('Condition saving to "' + str(condition_uri) + '"') condition.save()
def import_attribute(attribute_node, nsmap, parent=None): attribute_uri = get_uri(attribute_node, nsmap) try: attribute = Attribute.objects.get(uri=attribute_uri) except Attribute.DoesNotExist: log.info('Attribute not in db. Created with uri ' + str(attribute_uri)) attribute = Attribute() pass else: log.info('Attribute does exist. Loaded from uri ' + str(attribute_uri)) attribute.uri = attribute_uri attribute.parent = parent attribute.uri_prefix = attribute_uri.split('/domain/')[0] attribute.key = attribute_uri.split('/')[-1] attribute.comment = get_value_from_treenode(attribute_node, get_ns_tag('dc:comment', nsmap)) attribute.is_collection = make_bool(attribute_node.find('is_collection').text) attribute.value_type = get_value_from_treenode(attribute_node, 'value_type') attribute.unit = get_value_from_treenode(attribute_node, 'unit') try: AttributeEntityUniquePathValidator(attribute).validate() except ValidationError: log.info('Attribute not saving "' + str(attribute_uri) + '" due to validation error') pass else: log.info('Attribute saving to "' + str(attribute_uri) + '", parent "' + str(parent) + '"') attribute.save() if hasattr(attribute_node, 'range'): import_verbose_name(attribute_node.range, attribute) if hasattr(attribute_node, 'verbosename'): import_verbose_name(attribute_node.verbosename, attribute) if hasattr(attribute_node, 'optionsets'): for optionset_node in attribute_node.optionsets.iterchildren(): try: optionset_uri = optionset_node.get(get_ns_tag('dc:uri', nsmap)) optionset = OptionSet.objects.get(uri=optionset_uri) attribute.optionsets.add(optionset) except OptionSet.DoesNotExist: pass if hasattr(attribute_node, 'conditions'): for condition_node in attribute_node.conditions.iterchildren(): try: condition_uri = condition_node.get(get_ns_tag('dc:uri', nsmap)) condition = Condition.objects.get(uri=condition_uri) attribute.conditions.add(condition) except Condition.DoesNotExist: pass
def import_questionset(questionset_node, nsmap, subsection=None): questionset_uri = get_uri(questionset_node, nsmap) log.info('Importing questionset ' + questionset_uri) try: questionset = QuestionEntity.objects.get(uri=questionset_uri) except QuestionEntity.DoesNotExist: questionset = QuestionEntity() log.info('Questionset not in db. Created with uri ' + str(questionset_uri)) else: log.info('Questionset does exist. Loaded from uri ' + str(questionset_uri)) questionset.uri_prefix = questionset_uri.split('/questions/')[0] questionset.key = questionset_uri.split('/')[-1] questionset.comment = get_value_from_treenode( questionset_node, get_ns_tag('dc:comment', nsmap)) questionset.subsection = subsection questionset.order = get_value_from_treenode(questionset_node, 'order') for element in questionset_node.findall('help'): setattr(questionset, 'help_' + element.attrib['lang'], element.text) try: urimap = questionset_node.find('attribute_entity').attrib nstag = get_ns_tag('dc:uri', nsmap) attribute_entity_uri = urimap[nstag] questionset.attribute_entity = AttributeEntity.objects.get( uri=attribute_entity_uri) except (AttributeError, AttributeEntity.DoesNotExist): questionset.attribute_entity = None try: QuestionEntityUniquePathValidator(questionset).validate() except ValidationError: log.info('Questionset not saving "' + str(questionset_uri) + '" due to validation error') pass else: log.info('Questionset saving to "' + str(questionset_uri) + '"') questionset.save() for question_node in questionset_node.find('questions').findall( 'question'): import_question(question_node, nsmap, subsection=subsection, parent=questionset)
def import_subsection(subsection_node, nsmap, section=None): subsection_uri = get_uri(subsection_node, nsmap) log.info('Importing subsection ' + subsection_uri) try: subsection = Subsection.objects.get(uri=subsection_uri) except Subsection.DoesNotExist: subsection = Subsection() log.info('Subsection not in db. Created with uri ' + str(subsection_uri)) else: log.info('Subsection does exist. Loaded from uri ' + str(subsection_uri)) subsection.uri_prefix = subsection_uri.split('/questions/')[0] subsection.key = subsection_uri.split('/')[-1] subsection.comment = get_value_from_treenode( subsection_node, get_ns_tag('dc:comment', nsmap)) subsection.section = section subsection.order = get_value_from_treenode(subsection_node, 'order') for element in subsection_node.findall('title'): setattr(subsection, 'title_' + element.attrib['lang'], element.text) try: SubsectionUniquePathValidator(subsection).validate() except ValidationError: log.info('Subsection not saving "' + str(subsection_uri) + '" due to validation error') pass else: log.info('Subsection saving to "' + str(subsection_uri) + '"') subsection.save() for questionset_node in subsection_node.find('entities').findall( 'questionset'): import_questionset(questionset_node, nsmap, subsection=subsection) for question_node in subsection_node.find('entities').findall('question'): import_question(question_node, nsmap, subsection=subsection)
def import_options(optionsets_node): log.info('Importing options') nsmap = get_ns_map(optionsets_node.getroot()) for optionset_node in optionsets_node.findall('optionset'): uri = get_uri(optionset_node, nsmap) try: optionset = OptionSet.objects.get(uri=uri) except OptionSet.DoesNotExist: optionset = OptionSet() log.info('Optionset not in db. Created with uri ' + str(uri)) else: log.info('Optionset does exist. Loaded from uri ' + str(uri)) optionset.uri_prefix = uri.split('/options/')[0] optionset.key = uri.split('/')[-1] optionset.comment = get_value_from_treenode( optionset_node, get_ns_tag('dc:comment', nsmap)) optionset.order = get_value_from_treenode(optionset_node, 'order') try: OptionSetUniqueKeyValidator(optionset).validate() except ValidationError: log.info('Optionset not saving "' + str(uri) + '" due to validation error') pass else: log.info('Optionset saving to "' + str(uri) + '"') optionset.save() for options_node in optionset_node.findall('options'): for option_node in options_node.findall('option'): uri = get_uri(option_node, nsmap) try: option = Option.objects.get(uri=uri) except Option.DoesNotExist: log.info(Option.DoesNotExist) option = Option() option.optionset = optionset option.uri_prefix = uri.split('/options/')[0] option.key = uri.split('/')[-1] option.comment = get_value_from_treenode( option_node, get_ns_tag('dc:comment', nsmap)) option.order = get_value_from_treenode(option_node, 'order') for element in option_node.findall('text'): setattr(option, 'text_' + element.attrib['lang'], element.text) option.additional_input = make_bool( get_value_from_treenode(option_node, 'additional_input')) try: OptionSetUniqueKeyValidator(optionset).validate() except ValidationError: log.info('Optionset not saving "' + str(uri) + '" due to validation error') pass else: log.info('Option saving to "' + str(uri) + '"') option.save()