Skip to content

Commit

Permalink
Refactor code structure changed
Browse files Browse the repository at this point in the history
some generic function were moved to core
  • Loading branch information
triole committed Feb 27, 2018
1 parent 72effc7 commit b3bd7c1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 147 deletions.
38 changes: 38 additions & 0 deletions rdmo/core/imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import logging
import time
import defusedxml.ElementTree as ET
from random import randint

log = logging.getLogger(__name__)


def generate_tempfile_name():
t = int(round(time.time() * 1000))
r = randint(10000, 99999)
fn = '/tmp/upload_' + str(t) + '_' + str(r) + '.xml'
return fn


def handle_uploaded_file(filedata):
tempfilename = generate_tempfile_name()
with open(tempfilename, 'wb+') as destination:
for chunk in filedata.chunks():
destination.write(chunk)
return tempfilename


def validate_xml(tempfilename, root_tag):
tree = None
exit_code = 0
try:
tree = ET.parse(tempfilename)
except Exception as e:
exit_code = 1
log.info('Xml parsing error: ' + str(e))
pass
else:
root = tree.getroot()
if root.tag != root_tag:
log.info('Validation failed. Xml\'s root node is "' + root_tag + '" and not "' + root.tag + '".')
exit_code = 1
return exit_code, tree
22 changes: 19 additions & 3 deletions rdmo/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import os
import csv
from tempfile import mkstemp

import os
import logging
import pypandoc
import re

import defusedxml.ElementTree as ET

from tempfile import mkstemp

from django.conf import settings
from django.template.loader import get_template
from django.http import HttpResponse, HttpResponseBadRequest
from django.utils.six.moves.urllib.parse import urlparse
from django.utils.translation import ugettext_lazy as _

log = logging.getLogger(__name__)


def get_script_alias(request):
return request.path[:-len(request.path_info)]
Expand Down Expand Up @@ -38,6 +44,16 @@ def get_next(request):
return get_script_alias(request) + next


def get_ns_map(treenode):
nsmap = {}
treestring = ET.tostring(treenode, encoding='utf8', method='xml')
match = re.search(r'(xmlns:)(.*?)(=")(.*?)(")', str(treestring))
if bool(match) is True:
nsmap = {match.group(2): match.group(4)}
log.info("Nsmap contruction result: " + str(nsmap))
return nsmap


def get_ns_tag(tag, nsmap):
tag_split = tag.split(':')
return '{%s}%s' % (nsmap[tag_split[0]], tag_split[1])
Expand Down
12 changes: 11 additions & 1 deletion rdmo/domain/templates/domain/domain.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ <h2>{% trans 'Export' %}</h2>

<ul class="list-unstyled">
<li >
<a href="{% url 'domain_export' 'xml' %}" target="_blank">
<a href="{% url 'domain_import' 'xml' %}" target="_blank">
{% trans 'XML' %}
</a>
</li>
</ul>

<h2>{% trans 'Import' %}</h2>

<ul class="list-unstyled">
<li >
<a href="{% url 'domain_import' 'xml' %}" target="_blank">
{% trans 'XML' %}
</a>
</li>
Expand Down
129 changes: 0 additions & 129 deletions rdmo/domain/utils.py

This file was deleted.

16 changes: 2 additions & 14 deletions rdmo/projects/imports.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging, re
import logging

import defusedxml.ElementTree as ET

from rdmo.core.utils import get_ns_tag
from rdmo.core.utils import get_ns_tag, get_ns_map
from rdmo.domain.models import Attribute
from rdmo.options.models import Option
from rdmo.questions.utils import Catalog
Expand Down Expand Up @@ -34,16 +32,6 @@ def get_value_from_xml_node(xml_node, element, what_to_get=None):
return r


def get_ns_map(treenode):
nsmap = {}
treestring = ET.tostring(treenode, encoding='utf8', method='xml')
match = re.search(r'(xmlns:)(.*?)(=")(.*?)(")', str(treestring))
if bool(match) is True:
nsmap = {match.group(2): match.group(4)}
log.info("Nsmap contruction result: " + str(nsmap))
return nsmap


def import_project(project_node, user):
nsmap = get_ns_map(project_node.getroot())
project_title = get_value_from_xml_node(project_node, 'title')
Expand Down

0 comments on commit b3bd7c1

Please sign in to comment.