Beispiel #1
0
 def check(self):
     file_type, encoding = mimetypes.guess_type(self.file_name)
     if file_type == 'application/xml':
         self.root = read_xml_file(self.file_name)
         if self.root and self.root.tag == 'project':
             self.ns_map = get_ns_map(self.root)
             return True
Beispiel #2
0
 def check(self):
     file_type, encoding = mimetypes.guess_type(self.file_name)
     if file_type == 'application/xml':
         self.root = read_xml_file(self.file_name)
         if self.root:
             self.ns_map = get_ns_map(self.root)
             if self.root.tag == '{{{ns0}}}resource'.format(**self.ns_map):
                 return True
Beispiel #3
0
    def post(self, request):
        try:
            uploaded_file = request.FILES['uploaded_file']
        except KeyError:
            return HttpResponseRedirect(self.get_success_url())
        else:
            import_tmpfile_name = handle_uploaded_file(uploaded_file)

        root = read_xml_file(import_tmpfile_name)
        if root is None:
            logger.info('Xml parsing error. Import failed.')
            return render(
                request,
                'core/error.html', {
                    'title':
                    _('Import error'),
                    'errors': [
                        _('The content of the xml file does not consist of well formed data or markup.'
                          )
                    ]
                },
                status=400)

        else:
            try:
                elements = flat_xml_to_elements(root)
            except (KeyError, TypeError):
                return render(
                    request,
                    'core/error.html', {
                        'title': _('Import error'),
                        'errors': [_('This is not a RDMO XML file.')]
                    },
                    status=400)

            if check_permissions(elements, request.user):
                # store information in session for ProjectCreateImportView
                request.session['import_file_name'] = uploaded_file.name
                request.session['import_tmpfile_name'] = import_tmpfile_name
                request.session['import_success_url'] = self.get_success_url()

                return render(
                    request, 'management/upload.html', {
                        'file_name': uploaded_file.name,
                        'elements': import_elements(elements)
                    })
            else:
                return render(request,
                              'core/error.html', {
                                  'title': _('Import error'),
                                  'errors': [_('Forbidden.')]
                              },
                              status=403)
Beispiel #4
0
 def handle(self, *args, **options):
     root = read_xml_file(options['xmlfile'])
     if root is None:
         raise CommandError(
             _('The content of the xml file does not consist of well formed data or markup.'
               ))
     elif root.tag != 'rdmo':
         raise CommandError(_('This XML does not contain RDMO content.'))
     else:
         elements = flat_xml_to_elements(root)
         save = {element.get('uri'): True for element in elements}
         import_elements(elements, save=save)
Beispiel #5
0
def test_missing_parent(db, settings):
    count = Option.objects.count()

    xml_file = os.path.join(settings.BASE_DIR, 'xml',
                            'options-missing-parent.xml')
    root = read_xml_file(xml_file)
    elements = flat_xml_to_elements(root)
    checked = {element.get('uri'): True for element in elements}
    instances = import_elements(elements, parents={}, save=checked)

    # one instance has an error
    assert len([instance.errors for instance in instances
                if instance.errors]) == 1

    # no option has been imported
    assert Option.objects.count() == count
Beispiel #6
0
def test_non_unique_path(db, settings):
    count = Attribute.objects.count()

    xml_file = os.path.join(settings.BASE_DIR, 'xml',
                            'domain-non-unique-path.xml')
    root = read_xml_file(xml_file)
    elements = flat_xml_to_elements(root)
    checked = {element.get('uri'): True for element in elements}
    instances = import_elements(elements, parents={}, save=checked)

    # one instance has an error
    assert len([instance.errors for instance in instances
                if instance.errors]) == 1

    # two instances have no error
    assert len([
        instance.errors for instance in instances if not instance.errors
    ]) == 2

    # only 2 attributes have been imported
    assert Attribute.objects.count() == count + 2
Beispiel #7
0
def test_import_post(db, settings, client, username, password, file_name):
    client.login(username=username, password=password)

    xml_file = os.path.join(settings.BASE_DIR, 'xml', file_name)

    session = client.session
    session['import_file_name'] = file_name
    session['import_tmpfile_name'] = xml_file
    session.save()

    root = read_xml_file(xml_file)
    elements = flat_xml_to_elements(root)
    checked = [element.get('uri') for element in elements]
    data = {uri: ['on'] for uri in checked}

    url = reverse('import')
    response = client.post(url, data)

    assert response.status_code == status_map['import_post'][username]

    if not password:
        assert response.url.startswith('/account/login/'), response.content
Beispiel #8
0
    def post(self, request):
        impor_file_name = request.session['import_file_name']
        import_tmpfile_name = request.session.get('import_tmpfile_name')

        # parse the form data, which is <uri: [parent, checked]> or <uri: [checked]>
        parents = {}
        checked = {}
        for key, values in request.POST.lists():
            if key.startswith('http'):
                try:
                    parents[key] = None if values[0] == 'null' else values[0]
                    checked[key] = True if values[1] == 'on' else False
                except IndexError:
                    parents[key] = False
                    checked[key] = True if values[0] == 'on' else False

        root = read_xml_file(import_tmpfile_name)
        if root is None:
            logger.info('Xml parsing error. Import failed.')
            return render(
                request,
                'core/error.html', {
                    'title':
                    _('Import error'),
                    'errors': [
                        _('The content of the xml file does not consist of well formed data or markup.'
                          )
                    ]
                },
                status=400)

        else:
            try:
                elements = flat_xml_to_elements(root)
            except (KeyError, TypeError):
                return render(
                    request,
                    'core/error.html', {
                        'title': _('Import error'),
                        'errors': [_('This is not a RDMO XML file.')]
                    },
                    status=400)

            if check_permissions(elements, request.user):
                if checked:
                    return render(
                        request, 'management/import.html', {
                            'file_name':
                            impor_file_name,
                            'elements':
                            import_elements(
                                elements, parents=parents, save=checked),
                            'success_url':
                            self.get_success_url()
                        })
                else:
                    # if nothing was checked, just return to the success_url
                    return HttpResponseRedirect(self.get_success_url())

            else:
                return render(request,
                              'core/error.html', {
                                  'title': _('Import error'),
                                  'errors': [_('Forbidden.')]
                              },
                              status=403)