Skip to content

Commit

Permalink
Add domain import function
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Mar 1, 2018
1 parent 2bf8c58 commit 498ef0a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
10 changes: 6 additions & 4 deletions rdmo/core/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@


def get_value_from_xml_node(xml_node, element, what_to_get=None):
r = None
r = ''
try:
if what_to_get == 'attrib':
r = str(xml_node.find(element).attrib)
r = xml_node.find(element).attrib
elif what_to_get == 'tag':
r = str(xml_node.find(element).tag)
r = xml_node.find(element).tag
else:
r = str(xml_node.find(element).text)
r = xml_node.find(element).text
except Exception as e:
log.debug('Unable to extract "' + element + '" from "' + str(xml_node) + '". ' + str(e))
pass
else:
if r is None:
r = ''
try:
r = r.encode('utf-8', 'ignore')
except Exception as e:
Expand Down
16 changes: 10 additions & 6 deletions rdmo/domain/imports.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from rdmo.core.imports import get_value_from_xml_node
from rdmo.core.utils import get_ns_map, get_ns_tag
from rdmo.conditions.models import Condition
from rdmo.options.models import OptionSet
Expand Down Expand Up @@ -32,17 +33,18 @@ def import_attribute_entity(entity_node, nsmap, parent=None):
entity.parent = parent
entity.uri_prefix = uri.split('/domain/')[0]
entity.key = uri.split('/')[-1]
entity.comment = entity_node.find(get_ns_tag('dc:comment', nsmap))
entity.comment = get_value_from_xml_node(entity_node, get_ns_tag('dc:comment', nsmap))
entity.is_collection = entity_node.find('is_collection') == 'True'
log.info('Saving entity ' + str(entity))
entity.save()

if entity_node.find('verbosename').text is not None:
import_verbose_name(entity_node.find('verbosename').text, entity)
import_verbose_name(get_value_from_xml_node(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 = condition_node.find(get_ns_tag('dc:uri', nsmap))
condition_uri = condition_node.get(get_ns_tag('dc:uri', nsmap))
condition = Condition.objects.get(uri=condition_uri)
entity.conditions.add(condition)
except Condition.DoesNotExist:
Expand Down Expand Up @@ -72,10 +74,11 @@ def import_attribute(attribute_node, nsmap, parent=None):
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.comment = get_value_from_xml_node(attribute_node, 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.value_type = get_value_from_xml_node(attribute_node, 'value_type')
attribute.unit = get_value_from_xml_node(attribute_node, 'unit')
log.info('Saving attribute ' + str(attribute))
attribute.save()

if hasattr(attribute_node, 'range'):
Expand Down Expand Up @@ -113,6 +116,7 @@ def import_verbose_name(verbosename_node, entity):
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)
log.info('Saving verbosename ' + str(verbosename))
verbosename.save()
except Exception as e:
log.info('An exception occured: ' + str(e))
Expand Down
1 change: 1 addition & 0 deletions rdmo/domain/utils.py
6 changes: 3 additions & 3 deletions rdmo/domain/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -74,8 +74,8 @@ class DomainImportXMLView(ObjectPermissionMixin, TemplateView):
# model = Project
permission_required = 'projects.export_project_object'
# form_class = ProjectForm
success_url = '/'
template_name = 'domain/domain_upload.html'
success_url = '/domain'
template_name = 'domain/file_upload.html'

def get(self, request, *args, **kwargs):
form = UploadFileForm()
Expand Down
2 changes: 1 addition & 1 deletion rdmo/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ProjectImportXMLView(ObjectPermissionMixin, TemplateView):
permission_required = 'projects.export_project_object'
form_class = ProjectForm
success_url = '/'
template_name = 'projects/project_upload.html'
template_name = 'projects/file_upload.html'

def get(self, request, *args, **kwargs):
form = UploadFileForm()
Expand Down

0 comments on commit 498ef0a

Please sign in to comment.