Skip to content

Commit

Permalink
Merge branch 'xml_export' of github.com:rdmorganiser/rdmo into xml_ex…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
mheger committed Jan 19, 2017
2 parents b15686c + 9264a7c commit f2ae953
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 55 deletions.
8 changes: 4 additions & 4 deletions apps/conditions/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ def render(self, data):

xml = SimplerXMLGenerator(stream, "utf-8")
xml.startDocument()
xml.startElement('Conditions', {})
xml.startElement('conditions', {})

for condition in data:
self._condition(xml, condition)

xml.endElement('Conditions')
xml.endElement('conditions')
xml.endDocument()
return stream.getvalue()

def _condition(self, xml, condition):
xml.startElement('Condition', {})
xml.startElement('condition', {})
self._text_element(xml, 'identifier', {}, condition["identifier"])
self._text_element(xml, 'uri', {}, condition["uri"])
self._text_element(xml, 'comment', {}, condition["comment"])
self._text_element(xml, 'source', {}, condition["source"])
self._text_element(xml, 'relation', {}, condition["relation"])
self._text_element(xml, 'target_text', {}, condition["target_text"])
self._text_element(xml, 'target_option', {}, condition["target_option"])
xml.endElement('Condition')
xml.endElement('condition')

def _text_element(self, xml, tag, condition, text):
xml.startElement(tag, condition)
Expand Down
32 changes: 32 additions & 0 deletions apps/conditions/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from apps.domain.models import Attribute
from apps.options.models import Option

from .models import *


def import_xml(conditions_node):

for condition_node in conditions_node.iterchildren():

try:
condition = Condition.objects.get(identifier=condition_node.identifier)
except Condition.DoesNotExist:
condition = Condition(identifier=condition_node.identifier)

condition.identifier = condition_node.identifier
condition.uri = condition_node.uri
condition.comment = condition_node.comment
condition.relation = condition_node.relation
condition.target_text = condition_node.target_text

try:
condition.source = Attribute.objects.get(identifier=condition.source)
except Attribute.DoesNotExist:
condition.source = None

try:
condition.target_option = Option.objects.get(identifier=condition.source)
except Option.DoesNotExist:
condition.target_option = None

condition.save()
5 changes: 4 additions & 1 deletion apps/conditions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def conditions_export(request, format):
def conditions_export_xml(request):
queryset = Condition.objects.all()
serializer = ExportSerializer(queryset, many=True)
return HttpResponse(XMLRenderer().render(serializer.data), content_type="application/xml")

response = HttpResponse(XMLRenderer().render(serializer.data), content_type="application/xml")
response['Content-Disposition'] = 'attachment; filename="conditions.xml"'
return response


class ConditionViewSet(viewsets.ModelViewSet):
Expand Down
16 changes: 16 additions & 0 deletions apps/core/management/commands/import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.core.management.base import BaseCommand

from apps.core.utils import import_xml


class Command(BaseCommand):

def add_arguments(self, parser):
parser.add_argument('xmlfile', action='store', default=False, help='RDMO XML export file')

def handle(self, *args, **options):
print options['xmlfile']

with open(options['xmlfile']) as f:
xml_string = f.read()
import_xml(xml_string)
21 changes: 21 additions & 0 deletions apps/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import csv
from tempfile import mkstemp

from lxml import objectify
import pypandoc

from django.conf import settings
Expand All @@ -10,6 +11,10 @@
from django.utils.six.moves.urllib.parse import urlparse
from django.utils.translation import ugettext_lazy as _

from apps.conditions.utils import import_xml as import_conditions
from apps.options.utils import import_xml as import_options
from apps.domain.utils import import_xml as import_domain


def get_script_alias(request):
return request.path[:-len(request.path_info)]
Expand Down Expand Up @@ -101,3 +106,19 @@ def render_to_csv(request, title, rows):
writer.writerow(tuple(row))

return response


def import_xml(xml_string):
xml_root = objectify.fromstring(xml_string)

if xml_root.tag == 'conditions':
import_conditions(xml_root)

elif xml_root.tag == 'options':
import_options(xml_root)

elif xml_root.tag == 'domain':
import_domain(xml_root)

else:
raise Exception('This is not a proper RDMO XML Export.')
68 changes: 34 additions & 34 deletions apps/domain/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def render(self, data):

xml = SimplerXMLGenerator(stream, "utf-8")
xml.startDocument()
xml.startElement('Domain', {
xml.startElement('domain', {
'xmlns:dc': "http://purl.org/dc/elements/1.1/"
})

Expand All @@ -30,76 +30,76 @@ def render(self, data):
else:
self._attribute_entity(xml, attribute_entity)

xml.endElement('Domain')
xml.endElement('domain')
xml.endDocument()
return stream.getvalue()

def _attribute(self, xml, attribute):
xml.startElement('Attribute', {})
xml.startElement('attribute', {})
self._text_element(xml, 'dc:identifier', {}, attribute["identifier"])
self._text_element(xml, 'dc:uri', {}, attribute["uri"])
self._text_element(xml, 'dc:comment', {}, attribute["comment"])
self._text_element(xml, 'is_collection', {}, attribute["is_collection"])
self._text_element(xml, 'value_type', {}, attribute["value_type"])
self._text_element(xml, 'unit', {}, attribute["unit"])

if 'range' in attribute and attribute['range']:
self._range(xml, attribute['range'])

if 'verbosename' in attribute and attribute['verbosename']:
self._verbosename(xml, attribute['verbosename'])

if 'optionsets' in attribute and attribute['optionsets']:
xml.startElement('OptionSets', {})
xml.startElement('optionsets', {})

for optionset in attribute['optionsets']:
xml.startElement('OptionSet', optionset)
xml.endElement('OptionSet')
xml.startElement('optionset', optionset)
xml.endElement('optionset')

xml.endElement('OptionSets')
xml.endElement('optionsets')

if 'conditions' in attribute and attribute['conditions']:
xml.startElement('Conditions', {})
xml.startElement('conditions', {})

for condition in attribute['conditions']:
xml.startElement('Condition', condition)
xml.endElement('Condition')

xml.endElement('Conditions')
xml.startElement('condition', condition)
xml.endElement('condition')

if 'range' in attribute and attribute['range']:
self._range(xml, attribute['range'])
xml.endElement('conditions')

if 'verbosename' in attribute and attribute['verbosename']:
self._verbosename(xml, attribute['verbosename'])

xml.endElement('Attribute')
xml.endElement('attribute')

def _attribute_entity(self, xml, attribute_entity):
xml.startElement('AttributeEntity', {})
xml.startElement('entity', {})
self._text_element(xml, 'dc:identifier', {}, attribute_entity["identifier"])
self._text_element(xml, 'dc:uri', {}, attribute_entity["uri"])
self._text_element(xml, 'dc:comment', {}, attribute_entity["comment"])
self._text_element(xml, 'is_collection', {}, attribute_entity["is_collection"])

if 'verbosename' in attribute_entity and attribute_entity['verbosename']:
self._verbosename(xml, attribute_entity['verbosename'])

if 'conditions' in attribute_entity and attribute_entity['conditions']:
xml.startElement('conditions', {})

for condition in attribute_entity['conditions']:
xml.startElement('condition', condition)
xml.endElement('condition')

xml.endElement('conditions')

if 'children' in attribute_entity:
xml.startElement('Children', {})
xml.startElement('children', {})

for child in attribute_entity['children']:
if child['is_attribute']:
self._attribute(xml, child)
else:
self._attribute_entity(xml, child)

xml.endElement('Children')

if 'conditions' in attribute_entity and attribute_entity['conditions']:
xml.startElement('Conditions', {})

for condition in attribute_entity['conditions']:
xml.startElement('Condition', condition)
xml.endElement('Condition')

xml.endElement('Conditions')

if 'verbosename' in attribute_entity and attribute_entity['verbosename']:
self._verbosename(xml, attribute_entity['verbosename'])
xml.endElement('children')

xml.endElement('AttributeEntity')
xml.endElement('entity')

def _range(self, xml, range):
xml.startElement('range', {})
Expand Down
119 changes: 119 additions & 0 deletions apps/domain/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from .models import *


def import_xml(domain_node):

nsmap = domain_node.nsmap

for entity_node in domain_node.iterchildren():

if entity_node.tag == 'entity':
_import_attribute_entity(entity_node, nsmap)
else:
_import_attribute(entity_node, nsmap)


def _import_attribute(attribute_node, nsmap, parent=None):

try:
attribute = Attribute.objects.get(
parent=parent,
identifier=attribute_node['{%(dc)s}identifier' % nsmap]
)
except Attribute.DoesNotExist:
attribute = Attribute(
parent=parent,
identifier=attribute_node['{%(dc)s}identifier' % nsmap]
)

attribute.uri = attribute_node['{%(dc)s}uri' % nsmap]
attribute.comment = attribute_node['{%(dc)s}comment' % nsmap]
attribute.is_collection = attribute_node.is_collection
attribute.value_type = attribute_node.value_type
attribute.unit = attribute_node.unit
attribute.save()

if hasattr(attribute_node, 'range'):
try:
range = Range.objects.get(attribute=attribute)
except Range.DoesNotExist:
range = Range(attribute=attribute)

range.minimum = attribute_node.range.minimum
range.maximum = attribute_node.range.maximum
range.step = attribute_node.range.step
range.save()

if hasattr(attribute_node, 'verbosename'):
try:
verbosename = VerboseName.objects.get(attribute_entity=attribute)
except VerboseName.DoesNotExist:
verbosename = VerboseName(attribute_entity=attribute)

verbosename.name_en = attribute_node.verbosename.name_en
verbosename.name_de = attribute_node.verbosename.name_de
verbosename.name_plural_en = attribute_node.verbosename.name_plural_en
verbosename.name_plural_de = attribute_node.verbosename.name_plural_de
verbosename.save()

if hasattr(attribute_node, 'optionsets'):
for optionset_node in attribute_node.optionsets.iterchildren():
try:
optionset = OptionSet.objects.get(identifier=optionset_node.get('identifier'))
attribute.optionsets.add(optionset)
except OptionSet.DoesNotExist:
pass

if hasattr(attribute_node, 'conditions'):
for condition_node in attribute_node.conditions.iterchildren():
try:
condition = Condition.objects.get(identifier=condition_node.get('identifier'))
attribute.conditions.add(condition)
except Condition.DoesNotExist:
pass


def _import_attribute_entity(entity_node, nsmap, parent=None):

try:
entity = AttributeEntity.objects.get(
parent=parent,
identifier=entity_node['{%(dc)s}identifier' % nsmap]
)
except AttributeEntity.DoesNotExist:
entity = AttributeEntity(
parent=parent,
identifier=entity_node['{%(dc)s}identifier' % nsmap]
)

entity.uri = entity_node['{%(dc)s}uri' % nsmap]
entity.comment = entity_node['{%(dc)s}comment' % nsmap]
entity.is_collection = entity_node.is_collection
entity.save()

if hasattr(entity_node, 'verbosename'):
try:
verbosename = VerboseName.objects.get(attribute_entity=entity)
except VerboseName.DoesNotExist:
verbosename = VerboseName(attribute_entity=entity)

verbosename.name_en = entity_node.verbosename.name_en
verbosename.name_de = entity_node.verbosename.name_de
verbosename.name_plural_en = entity_node.verbosename.name_plural_en
verbosename.name_plural_de = entity_node.verbosename.name_plural_de
verbosename.save()

if hasattr(entity_node, 'conditions'):
for condition_node in entity_node.conditions.iterchildren():
try:
condition = Condition.objects.get(identifier=condition_node.get('identifier'))
attribute.conditions.add(condition)
except Condition.DoesNotExist:
pass

if hasattr(entity_node, 'children'):
for child_node in entity_node.children.iterchildren():
if child_node.tag == 'attribute':
_import_attribute(child_node, nsmap, parent=entity)
else:
_import_attribute_entity(child_node, nsmap, parent=entity)
7 changes: 5 additions & 2 deletions apps/domain/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ def domain_export_csv(request):

@staff_member_required
def domain_export_xml(request):
queryset = AttributeEntity.objects.filter(is_attribute=False)
queryset = AttributeEntity.objects.get_cached_trees()
serializer = ExportSerializer(queryset, many=True)
return HttpResponse(XMLRenderer().render(serializer.data), content_type="application/xml")

response = HttpResponse(XMLRenderer().render(serializer.data), content_type="application/xml")
response['Content-Disposition'] = 'attachment; filename="domain.xml"'
return response


class AttributeEntityViewSet(viewsets.ModelViewSet):
Expand Down
Loading

0 comments on commit f2ae953

Please sign in to comment.