-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add domain import function and a few minor fixes
- Loading branch information
Showing
5 changed files
with
98 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,119 @@ | ||
from rdmo.core.utils import get_ns_tag | ||
import logging | ||
|
||
from rdmo.core.utils import get_ns_map, get_ns_tag | ||
from rdmo.conditions.models import Condition | ||
from rdmo.options.models import OptionSet | ||
|
||
from .models import AttributeEntity, Attribute, Range, VerboseName | ||
|
||
|
||
def import_domain(domain_node): | ||
from .models import AttributeEntity, Attribute, VerboseName | ||
|
||
nsmap = domain_node.nsmap | ||
log = logging.getLogger(__name__) | ||
|
||
for entity_node in domain_node.iterchildren(): | ||
|
||
def import_domain(domain_node): | ||
nsmap = get_ns_map(domain_node.getroot()) | ||
log.info('Starting to parse domain node') | ||
for entity_node in domain_node.iter(): | ||
if entity_node.tag == 'entity': | ||
import_attribute_entity(entity_node, nsmap) | ||
else: | ||
elif entity_node.tag == 'attribute': | ||
import_attribute(entity_node, nsmap) | ||
|
||
|
||
def import_attribute_entity(entity_node, nsmap, parent=None): | ||
|
||
uri = entity_node[get_ns_tag('dc:uri', nsmap)].text | ||
uri = entity_node.find(get_ns_tag('dc:uri', nsmap)).text | ||
log.info('Importing attribute_entity with uri "' + uri + '"...') | ||
|
||
try: | ||
entity = AttributeEntity.objects.get(uri=uri, parent=parent) | ||
except AttributeEntity.DoesNotExist: | ||
log.info('Attribute entity import: ' + str(AttributeEntity.DoesNotExist)) | ||
entity = AttributeEntity() | ||
|
||
entity.parent = parent | ||
entity.uri_prefix = uri.split('/domain/')[0] | ||
entity.key = uri.split('/')[-1] | ||
entity.comment = entity_node[get_ns_tag('dc:comment', nsmap)] | ||
entity.is_collection = entity_node['is_collection'] == 'True' | ||
entity.comment = entity_node.find(get_ns_tag('dc:comment', nsmap)) | ||
entity.is_collection = entity_node.find('is_collection') == 'True' | ||
entity.save() | ||
|
||
if hasattr(entity_node, 'verbosename'): | ||
import_verbose_name(entity_node.verbosename, entity) | ||
if entity_node.find('verbosename').text is not None: | ||
import_verbose_name(entity_node.find('verbosename').text, entity) | ||
|
||
if hasattr(entity_node, 'conditions'): | ||
for condition_node in entity_node.conditions.iterchildren(): | ||
if entity_node.find('conditions') is not None: | ||
for condition_node in entity_node.find('conditions').findall('condition'): | ||
try: | ||
condition_uri = condition_node.get(get_ns_tag('dc:uri', nsmap)) | ||
condition_uri = condition_node.find(get_ns_tag('dc:uri', nsmap)) | ||
condition = Condition.objects.get(uri=condition_uri) | ||
entity.conditions.add(condition) | ||
except Condition.DoesNotExist: | ||
log.info('Condition import failed: ' + str(Condition.DoesNotExist)) | ||
pass | ||
|
||
if hasattr(entity_node, 'children'): | ||
for child_node in entity_node.children.iterchildren(): | ||
if child_node.tag == 'entity': | ||
import_attribute_entity(child_node, nsmap, parent=entity) | ||
else: | ||
import_attribute(child_node, nsmap, parent=entity) | ||
for child_node in entity_node.findall('children'): | ||
if child_node.tag == 'entity': | ||
import_attribute_entity(child_node, nsmap, parent=entity) | ||
else: | ||
import_attribute(child_node, nsmap, parent=entity) | ||
|
||
|
||
def import_attribute(attribute_node, nsmap, parent=None): | ||
|
||
uri = attribute_node[get_ns_tag('dc:uri', nsmap)].text | ||
|
||
try: | ||
attribute = Attribute.objects.get(uri=uri) | ||
except Attribute.DoesNotExist: | ||
attribute = Attribute() | ||
|
||
attribute.parent = parent | ||
attribute.uri_prefix = uri.split('/domain/')[0] | ||
attribute.key = uri.split('/')[-1] | ||
attribute.comment = attribute_node[get_ns_tag('dc:comment', nsmap)] | ||
attribute.is_collection = attribute_node['is_collection'] == 'True' | ||
attribute.value_type = attribute_node['value_type'] | ||
attribute.unit = attribute_node['unit'] | ||
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 | ||
uri = attribute_node.find(get_ns_tag('dc:uri', nsmap)).text | ||
except Exception as e: | ||
log.error(e) | ||
else: | ||
log.info('Importing attribute with uri "' + uri + '"...') | ||
|
||
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_verbose_name(verbosename_node, entity): | ||
if verbosename_node is not None: | ||
try: | ||
try: | ||
verbosename = VerboseName.objects.get(attribute_entity=entity) | ||
except VerboseName.DoesNotExist: | ||
verbosename = VerboseName(attribute_entity=entity) | ||
|
||
for element in verbosename_node['name']: | ||
setattr(verbosename, 'name_' + element.get('lang'), element.text) | ||
for element in verbosename_node['name_plural']: | ||
setattr(verbosename, 'name_plural_' + element.get('lang'), element.text) | ||
verbosename.save() | ||
except AttributeError: | ||
pass | ||
attribute = Attribute.objects.get(uri=uri) | ||
except Attribute.DoesNotExist: | ||
attribute = Attribute() | ||
|
||
attribute.parent = parent | ||
attribute.uri_prefix = uri.split('/domain/')[0] | ||
attribute.key = uri.split('/')[-1] | ||
attribute.comment = attribute_node.find(get_ns_tag('dc:comment', nsmap)) | ||
attribute.is_collection = attribute_node.find('is_collection') == 'True' | ||
attribute.value_type = attribute_node.find('value_type') | ||
attribute.unit = attribute_node.find('unit') | ||
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_range(range_node, attribute): | ||
if range_node is not None: | ||
def import_verbose_name(verbosename_node, entity): | ||
try: | ||
try: | ||
try: | ||
range = Range.objects.get(attribute=attribute) | ||
except Range.DoesNotExist: | ||
range = Range(attribute=attribute) | ||
|
||
range.minimum = range_node['minimum'] | ||
range.maximum = range_node['maximum'] | ||
range.step = range_node['step'] | ||
range.save() | ||
except AttributeError: | ||
pass | ||
verbosename = VerboseName.objects.get(attribute_entity=entity) | ||
except VerboseName.DoesNotExist: | ||
verbosename = VerboseName(attribute_entity=entity) | ||
for element in verbosename_node.findall('name'): | ||
setattr(verbosename, 'name_' + element.get('lang'), element.text) | ||
for element in verbosename_node.findall('name_plural'): | ||
setattr(verbosename, 'name_plural_' + element.get('lang'), element.text) | ||
verbosename.save() | ||
except Exception as e: | ||
log.info('An exception occured: ' + str(e)) | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{% extends 'core/page.html' %} | ||
{% load i18n %} | ||
{% load core_tags %} | ||
|
||
{% block page %} | ||
|
||
<form action="/domain/import/xml/" method="post" enctype="multipart/form-data"> | ||
{% csrf_token %} {% include 'core/bootstrap_form_fields.html' %} | ||
<input type="submit" id="submit" value="Submit File" /> | ||
</form> | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters