Beispiel #1
0
def validate_xml_data(request):
    # template_id = request.session['currentTemplateID']
    try:
        xsd_tree_str = str(request.session['xmlDocTree'])

        form_id = request.session['form_id']
        root_element = SchemaElement.objects.get(pk=form_id)

        xml_renderer = XmlRenderer(root_element)
        xml_data = xml_renderer.render()

        # validate XML document
        common.validateXMLDocument(xml_data, xsd_tree_str)
    except etree.XMLSyntaxError as xse:
        # xmlParseEntityRef exception: use of & < > forbidden
        message = "Validation Failed. </br> May be caused by : </br> - Syntax problem </br> - Use of forbidden symbols : '&' or '<' or '>'"
        response_dict = {'errors': message}
        return HttpResponse(json.dumps(response_dict),
                            content_type='application/javascript')
    except Exception as e:
        message = e.message.replace('"', '\'')
        response_dict = {'errors': message}
        return HttpResponse(json.dumps(response_dict),
                            content_type='application/javascript')

    return HttpResponse(json.dumps({}), content_type='application/javascript')
Beispiel #2
0
def save_xml_data_to_db(request):
    form_data_id = request.session['curateFormData']
    form_data = FormData.objects.get(pk=form_data_id)

    form_id = request.session['form_id']
    root_element = SchemaElement.objects.get(pk=form_id)

    xml_renderer = XmlRenderer(root_element)
    xml_string = xml_renderer.render()

    template_id = form_data.template

    # Parse data from form
    form = SaveDataForm(request.POST)
    if not form.data['title'].lower().endswith('.xml'):
        form.data['title'] += ".xml"

    if not form.is_valid():
        return HttpResponseBadRequest('Invalid form name')

    if xml_string == "" or xml_string is None:
        return HttpResponseBadRequest('No XML data found')

    try:
        # update data if id is present
        if form_data.xml_data_id is not None:
            XMLdata.update_content(form_data.xml_data_id,
                                   content=xml_string,
                                   title=form.data['title'])
            #Specific MDCS
            XMLdata.update_publish(form_data.xml_data_id)
        else:
            # create new data otherwise
            xml_data = XMLdata(schemaID=template_id,
                               xml=xml_string,
                               title=form.data['title'],
                               iduser=str(request.user.id))
            xml_data_id = xml_data.save()
            XMLdata.update_publish(xml_data_id)

        print 'SchemaID====', template_id
        print 'xml_data_id====', xml_data_id
        request.session['xml_data_id'] = str(xml_data_id)
        print 'request.session-xml_data_id', request.session['xml_data_id']

        if form_data.schema_element_root is not None:
            delete_branch_from_db(form_data.schema_element_root.pk)

        form_data.delete()

        return HttpResponse('ok')
    except Exception, e:
        message = e.message.replace('"', '\'')
        return HttpResponseBadRequest(message)
def download_current_xml(request):
    form_data_id = request.session['curateFormData']
    form_data = FormData.objects.get(pk=ObjectId(form_data_id))

    form_id = request.session['form_id']
    xml_root_element = SchemaElement.objects.get(pk=form_id)
    xml_renderer = XmlRenderer(xml_root_element)
    xml_data = StringIO(xml_renderer.render().encode("utf-8"))

    response = HttpResponse(FileWrapper(xml_data), content_type='application/xml')
    response['Content-Disposition'] = 'attachment; filename=' + form_data.name + '.xml'
    return response
Beispiel #4
0
def download_current_xml(request):
    form_data_id = request.session['curateFormData']
    form_data = FormData.objects.get(pk=ObjectId(form_data_id))

    form_id = request.session['form_id']
    xml_root_element = SchemaElement.objects.get(pk=form_id)
    xml_renderer = XmlRenderer(xml_root_element)
    xml_data = StringIO(xml_renderer.render().encode("utf-8"))

    response = HttpResponse(FileWrapper(xml_data), content_type='application/xml')
    response['Content-Disposition'] = 'attachment; filename=' + form_data.name + '.xml'
    return response
def save_xml_data_to_db(request):
    form_data_id = request.session['curateFormData']
    form_data = FormData.objects.get(pk=form_data_id)
    form_id = request.session['form_id']
    root_element = SchemaElement.objects.get(pk=form_id)
    xml_renderer = XmlRenderer(root_element)
    xml_string = xml_renderer.render()
    template_id = form_data.template

    # Parse data from form
    form = SaveDataForm(request.POST)
    if not form.data['title'].lower().endswith('.xml'):
        form.data['title'] += ".xml"

    if not form.is_valid():
        return HttpResponseBadRequest('Invalid form name')

    if xml_string == "" or xml_string is None:
        return HttpResponseBadRequest('No XML data found')

    try:
        # update form data if id is present
        if form_data.xml_data_id is not None:
            if not form_data.isNewVersionOfRecord:
                #Update the record
                XMLdata.update_content(form_data.xml_data_id,
                                       xml_string,
                                       title=form.data['title'])
                #Delete form_data
                if form_data.schema_element_root is not None:
                    delete_branch_from_db(form_data.schema_element_root.pk)
                form_data.delete()
            else:
                form_data.xml_data = xml_string
                form_data.save()
        else:
            # create new data otherwise
            xml_data = XMLdata(schemaID=template_id,
                               xml=xml_string,
                               title=form.data['title'],
                               iduser=str(request.user.id))
            xml_data.save()
            #Delete form_data because we just create an XmlData and we don't need anymore the formdata
            if form_data.schema_element_root is not None:
                delete_branch_from_db(form_data.schema_element_root.pk)
            form_data.delete()

        return HttpResponse('ok')
    except Exception, e:
        message = e.message.replace('"', '\'')
        return HttpResponseBadRequest(message)
Beispiel #6
0
def save_form(request):
    """Save the current form in MongoDB. Convert it to XML format first.

    :param request:
    :return:
    """
    form_id = request.session['form_id']
    root_element = SchemaElement.objects.get(pk=form_id)

    xml_renderer = XmlRenderer(root_element)
    xml_data = xml_renderer.render()

    form_data_id = request.session['curateFormData']
    form_data = FormData.objects.get(pk=form_data_id)
    form_data.xml_data = xml_data
    form_data.save()

    return HttpResponse(json.dumps({}), content_type='application/json')
def save_form(request):
    """Save the current form in MongoDB. Convert it to XML format first.

    :param request:
    :return:
    """
    form_id = request.session['form_id']
    root_element = SchemaElement.objects.get(pk=form_id)

    xml_renderer = XmlRenderer(root_element)
    xml_data = xml_renderer.render()

    form_data_id = request.session['curateFormData']
    form_data = FormData.objects.get(pk=form_data_id)
    form_data.xml_data = xml_data
    form_data.save()

    return HttpResponse(json.dumps({}), content_type='application/json')
Beispiel #8
0
    def test_xml_renderer(self):
        main_directory = self.schema_data_handler.dirname
        main_dir_len = len(main_directory) + 1

        filepath_list = []

        for root, dirs, files in walk(main_directory):
            for filename in files:
                file_ext = splitext(filename)

                if file_ext[1] == '.xsd':
                    full_path = join(root, file_ext[0])
                    filepath_list.append(full_path[main_dir_len:])

        report_content = []
        error_count = 0

        for filepath in filepath_list:
            report_line = filepath

            try:
                xsd_data = self.schema_data_handler.get_xsd2(filepath)
                self.request.session['xmlDocTree'] = etree.tostring(xsd_data)

                root_pk = generate_form(self.request)
                root_element = SchemaElement.objects.get(pk=root_pk)

                renderer = XmlRenderer(root_element)
                renderer.render()

                if len(renderer.warnings) != 0:
                    report_line += ',NOK,' + ','.join(renderer.warnings) + '\n'
                    error_count += 1
                else:  # No warnings yielded by the renderer
                    report_line += ',OK\n'
            except Exception as e:
                report_line += ',NOK,' + e.message + '\n'
                error_count += 1

            report_content.append(report_line)

        with open(join(SITE_ROOT, 'full_tests_report.csv'),
                  'w') as report_file:
            report_file.writelines(report_content)
    def test_xml_renderer(self):
        main_directory = self.schema_data_handler.dirname
        main_dir_len = len(main_directory) + 1

        filepath_list = []

        for root, dirs, files in walk(main_directory):
            for filename in files:
                file_ext = splitext(filename)

                if file_ext[1] == '.xsd':
                    full_path = join(root, file_ext[0])
                    filepath_list.append(full_path[main_dir_len:])

        report_content = []
        error_count = 0

        for filepath in filepath_list:
            report_line = filepath

            try:
                xsd_data = self.schema_data_handler.get_xsd2(filepath)
                self.request.session['xmlDocTree'] = etree.tostring(xsd_data)

                root_pk = generate_form(self.request)
                root_element = SchemaElement.objects.get(pk=root_pk)

                renderer = XmlRenderer(root_element)
                renderer.render()

                if len(renderer.warnings) != 0:
                    report_line += ',NOK,' + ','.join(renderer.warnings) + '\n'
                    error_count += 1
                else:  # No warnings yielded by the renderer
                    report_line += ',OK\n'
            except Exception as e:
                report_line += ',NOK,' + e.message + '\n'
                error_count += 1

            report_content.append(report_line)

        with open(join(SITE_ROOT, 'full_tests_report.csv'), 'w') as report_file:
            report_file.writelines(report_content)
def load_xml(request):
    if 'form_id' not in request.session:
        return HttpResponse(status=HTTP_404_NOT_FOUND)

    xml_form_id = SchemaElement.objects.get(pk=request.session['form_id'])

    xml_renderer = XmlRenderer(xml_form_id)
    xml_string = xml_renderer.render()

    xslt_path = os.path.join(settings.SITE_ROOT, 'static', 'resources', 'xsl', 'nmrr-detail.xsl')
    xslt = etree.parse(xslt_path)
    transform = etree.XSLT(xslt)

    xml_tree = ""
    if xml_string != "":
        dom = etree.fromstring(xml_string)
        newdom = transform(dom)
        xml_tree = str(newdom)

    response_dict = {"XMLHolder": xml_tree}
    return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
Beispiel #11
0
def load_xml(request):
    if 'form_id' not in request.session:
        return HttpResponse(status=HTTP_404_NOT_FOUND)

    xml_form_id = SchemaElement.objects.get(pk=request.session['form_id'])

    xml_renderer = XmlRenderer(xml_form_id)
    xml_string = xml_renderer.render()

    xslt_path = os.path.join(settings.SITE_ROOT, 'static', 'resources', 'xsl', 'nmrr-detail.xsl')
    xslt = etree.parse(xslt_path)
    transform = etree.XSLT(xslt)

    xml_tree = ""
    if xml_string != "":
        dom = etree.fromstring(xml_string)
        newdom = transform(dom)
        xml_tree = str(newdom)

    response_dict = {"XMLHolder": xml_tree}
    return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
def validate_xml_data(request):
    # template_id = request.session['currentTemplateID']
    try:
        xsd_tree_str = str(request.session['xmlDocTree'])

        form_id = request.session['form_id']
        root_element = SchemaElement.objects.get(pk=form_id)

        xml_renderer = XmlRenderer(root_element)
        xml_data = xml_renderer.render()

        # validate XML document
        common.validateXMLDocument(xml_data, xsd_tree_str)
    except etree.XMLSyntaxError as xse:
        # xmlParseEntityRef exception: use of & < > forbidden
        message = "Validation Failed. </br> May be caused by : </br> - Syntax problem </br> - Use of forbidden symbols : '&' or '<' or '>'"
        response_dict = {'errors': message}
        return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
    except Exception as e:
        message = e.message.replace('"', '\'')
        response_dict = {'errors': message}
        return HttpResponse(json.dumps(response_dict), content_type='application/javascript')

    return HttpResponse(json.dumps({}), content_type='application/javascript')