-
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 first version of conditions import
still needs to be tested and reviewed
- Loading branch information
Showing
2 changed files
with
87 additions
and
85 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,51 +1,53 @@ | ||
import logging | ||
|
||
from rdmo.core.imports import get_value_from_xml_node | ||
from rdmo.domain.models import Attribute | ||
from rdmo.options.models import Option | ||
from rdmo.core.utils import get_ns_map, get_ns_tag | ||
|
||
from .models import Condition | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def import_conditions(optionsets_node): | ||
log.info("Importing options") | ||
|
||
nsmap = get_ns_map(optionsets_node.getroot()) | ||
|
||
for optionset_node in optionsets_node.findall('conditions'): | ||
uri = optionset_node.find(get_ns_tag('dc:uri', nsmap)).text | ||
|
||
# try: | ||
# optionset = OptionSet.objects.get(uri=uri) | ||
# except OptionSet.DoesNotExist: | ||
# log.info(OptionSet.DoesNotExist) | ||
# optionset = OptionSet() | ||
# | ||
# optionset.uri_prefix = uri.split('/options/')[0] | ||
# optionset.key = uri.split('/')[-1] | ||
# optionset.comment = get_value_from_xml_node(optionset_node, get_ns_tag('dc:comment', nsmap)) | ||
# optionset.order = get_value_from_xml_node(optionset_node, 'order') | ||
# log.info('Saving optionset ' + str(optionset)) | ||
# optionset.save() | ||
# | ||
# # for option_node in optionset_node.options.iterchildren(): | ||
# for options_node in optionset_node.findall('options'): | ||
# for option_node in options_node.findall('option'): | ||
# uri = option_node.find(get_ns_tag('dc:uri', nsmap)).text | ||
# | ||
# 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_xml_node(option_node, get_ns_tag('dc:comment', nsmap)) | ||
# log.info(get_value_from_xml_node(option_node, get_ns_tag('dc:comment', nsmap))) | ||
# option.order = get_value_from_xml_node(option_node, 'order') | ||
# for element in option_node.findall('text'): | ||
# setattr(option, 'text_' + element.get('lang'), element.text) | ||
# option.additional_input = utf8_to_bool(get_value_from_xml_node(option_node, 'additional_input')) | ||
# log.info('Saving option ' + str(option)) | ||
# option.save() | ||
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 = condition_node.find(get_ns_tag('dc:uri', nsmap)).text | ||
|
||
try: | ||
condition = Condition.objects.get(uri=condition_uri) | ||
except Condition.DoesNotExist: | ||
condition = Condition() | ||
|
||
condition.uri_prefix = condition_uri.split('/conditions/')[0] | ||
condition.key = condition_uri.split('/')[-1] | ||
condition.comment = get_value_from_xml_node(condition_node, get_ns_tag('dc:comment', nsmap)) | ||
condition.relation = get_value_from_xml_node(condition_node, 'relation') | ||
|
||
try: | ||
condition_source = get_value_from_xml_node(condition_node, 'source') | ||
source_uri = get_value_from_xml_node(condition_source, get_ns_tag('dc:uri', nsmap)) | ||
condition.source = Attribute.objects.get(uri=source_uri) | ||
# NOTE: remove exception handling later | ||
# except (AttributeError, Attribute.DoesNotExist): | ||
except Exception as e: | ||
log.error(str(e)) | ||
condition.source = None | ||
|
||
try: | ||
condition.target_text = get_value_from_xml_node(condition_node, 'target_text') | ||
except AttributeError: | ||
condition.target_text = None | ||
|
||
try: | ||
condition_target = get_value_from_xml_node(condition_node, 'target_option') | ||
option_uid = get_value_from_xml_node(condition_target, get_ns_tag('dc:uri', nsmap)) | ||
condition.target_option = Option.objects.get(uri=option_uid) | ||
except (AttributeError, Option.DoesNotExist): | ||
condition.target_option = None | ||
|
||
log.info('Saving condition ' + str(condition)) | ||
condition.save() |
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,42 +1,42 @@ | ||
from rdmo.core.utils import get_ns_tag | ||
from rdmo.domain.models import Attribute | ||
from rdmo.options.models import Option | ||
|
||
from .models import Condition | ||
|
||
|
||
def import_conditions(conditions_node): | ||
|
||
nsmap = conditions_node.nsmap | ||
|
||
for condition_node in conditions_node.iterchildren(): | ||
condition_uri = condition_node[get_ns_tag('dc:uri', nsmap)].text | ||
|
||
try: | ||
condition = Condition.objects.get(uri=condition_uri) | ||
except Condition.DoesNotExist: | ||
condition = Condition() | ||
|
||
condition.uri_prefix = condition_uri.split('/conditions/')[0] | ||
condition.key = condition_uri.split('/')[-1] | ||
condition.comment = condition_node[get_ns_tag('dc:comment', nsmap)] | ||
condition.relation = condition_node['relation'] | ||
|
||
try: | ||
source_uri = condition_node['source'].get(get_ns_tag('dc:uri', nsmap)) | ||
condition.source = Attribute.objects.get(uri=source_uri) | ||
except (AttributeError, Attribute.DoesNotExist): | ||
condition.source = None | ||
|
||
try: | ||
condition.target_text = condition_node['target_text'] | ||
except AttributeError: | ||
condition.target_text = None | ||
|
||
try: | ||
option_uid = condition_node['target_option'].get(get_ns_tag('dc:uri', nsmap)) | ||
condition.target_option = Option.objects.get(uri=option_uid) | ||
except (AttributeError, Option.DoesNotExist): | ||
condition.target_option = None | ||
|
||
condition.save() | ||
# from rdmo.core.utils import get_ns_tag | ||
# from rdmo.domain.models import Attribute | ||
# from rdmo.options.models import Option | ||
# | ||
# from .models import Condition | ||
# | ||
# | ||
# def import_conditions(conditions_node): | ||
# | ||
# nsmap = conditions_node.nsmap | ||
# | ||
# for condition_node in conditions_node.iterchildren(): | ||
# condition_uri = condition_node[get_ns_tag('dc:uri', nsmap)].text | ||
# | ||
# try: | ||
# condition = Condition.objects.get(uri=condition_uri) | ||
# except Condition.DoesNotExist: | ||
# condition = Condition() | ||
# | ||
# condition.uri_prefix = condition_uri.split('/conditions/')[0] | ||
# condition.key = condition_uri.split('/')[-1] | ||
# condition.comment = condition_node[get_ns_tag('dc:comment', nsmap)] | ||
# condition.relation = condition_node['relation'] | ||
# | ||
# try: | ||
# source_uri = condition_node['source'].get(get_ns_tag('dc:uri', nsmap)) | ||
# condition.source = Attribute.objects.get(uri=source_uri) | ||
# except (AttributeError, Attribute.DoesNotExist): | ||
# condition.source = None | ||
# | ||
# try: | ||
# condition.target_text = condition_node['target_text'] | ||
# except AttributeError: | ||
# condition.target_text = None | ||
# | ||
# try: | ||
# option_uid = condition_node['target_option'].get(get_ns_tag('dc:uri', nsmap)) | ||
# condition.target_option = Option.objects.get(uri=option_uid) | ||
# except (AttributeError, Option.DoesNotExist): | ||
# condition.target_option = None | ||
# | ||
# condition.save() |