Beispiel #1
0
    def handle_import(self):
        if not self.parameters:
            return

        try:
            portal_type = self.parameters['portal_type']
            import_file = self.parameters['import_file']

            format = self.get_format_from_filename(import_file.filename)

            if not format in supported_formats:
                raise ContentImportError(_(
                    u'${format} is not supported, supported are ${formats}',
                    mapping={
                        'format': format,
                        'formats': ', '.join(supported_formats)
                    }
                ))

            count = import_people(
                self.request,
                self.context,
                portal_type,
                format,
                import_file.data
            )

            self.status = _(
                u'Successfully imported ${count} records', mapping=dict(
                    count=count
                )
            )
        except ContentImportError, e:
            transaction.abort()
            self.raise_action_error(e.translate(self.request))
    def test_import_people_csv(self):
        folder = self.new_temporary_folder()
        model = """<?xml version='1.0' encoding='utf8'?>
        <model  xmlns="http://namespaces.plone.org/supermodel/schema"
                xmlns:people="http://namespaces.plone.org/supermodel/people">
            <schema>
                <people:title>
                    <people:item>firstname</people:item>
                </people:title>
                <field name="firstname" type="zope.schema.TextLine">
                    <title>First Name</title>
                </field>
                <field name="lastname" type="zope.schema.TextLine">
                    <title>Last Name</title>
                </field>
            </schema>
        </model>"""

        portal_type = self.new_temporary_type(
            model_source=model,
            behaviors=[interfaces.INameFromPerson.__identifier__],
        ).id

        with self.user('admin'):
            import_people(self.request, folder, portal_type, 'csv', dedent("""
                First Name,Last Name,Town
                Peter,Griffin ,Quahog
                Glenn,  Quagmire   ,Quahog
            """.lstrip('\n')))

        self.assertEqual(folder['peter'].firstname, 'Peter')
        self.assertEqual(folder['peter'].lastname, 'Griffin')
        self.assertEqual(folder['glenn'].firstname, 'Glenn')
        self.assertEqual(folder['glenn'].lastname, 'Quagmire')

        # no town in the schema
        self.assertFalse(hasattr(folder['peter'], 'town'))
        self.assertFalse(hasattr(folder['glenn'], 'town'))
    def test_import_people_error_in_row(self):
        folder = self.new_temporary_folder()
        model = """<?xml version='1.0' encoding='utf8'?>
        <model  xmlns="http://namespaces.plone.org/supermodel/schema"
                xmlns:people="http://namespaces.plone.org/supermodel/people">
            <schema>
                <people:title>
                    <people:item>name</people:item>
                </people:title>
                <field name="name" type="zope.schema.TextLine">
                    <title>Name</title>
                </field>
                <field name="age" type="zope.schema.Int">
                    <title>Age</title>
                </field>
            </schema>
        </model>"""

        portal_type = self.new_temporary_type(
            model_source=model,
            behaviors=[interfaces.INameFromPerson.__identifier__],
        ).id

        self.login('admin')

        # error with a column available
        try:
            import_people(self.request, folder, portal_type, 'csv', dedent("""
                Name,Age
                Macallan,15
                Glenmorangie,Sixteen
            """.lstrip('\n')))
        except ContentImportError, e:
            self.assertIn('invalid literal for int', e.message)
            self.assertEqual(e.rownumber, 2)
            self.assertEqual(e.colname, 'Age')
                Name,Age
                Macallan,15
                Glenmorangie,Sixteen
            """.lstrip('\n')))
        except ContentImportError, e:
            self.assertIn('invalid literal for int', e.message)
            self.assertEqual(e.rownumber, 2)
            self.assertEqual(e.colname, 'Age')
        else:
            assert False, "The exception should have occurred."

        # error without column available
        try:
            # no folder is given
            import_people(self.request, None, portal_type, 'csv', dedent("""
                Name,Age
                Macallan,15
            """.lstrip('\n')))
        except ContentImportError, e:
            self.assertIn('Missing required parameter', e.message)
            self.assertEqual(e.rownumber, 1)
            self.assertEqual(e.colname, None)
        else:
            assert False, "The exception should have occurred."

    def test_import_people_schema_validation(self):
        model = loadString("""<?xml version='1.0' encoding='utf8'?>
        <model  xmlns="http://namespaces.plone.org/supermodel/schema"
                xmlns:people="http://namespaces.plone.org/supermodel/people">
            <schema>
                <people:title>
                    <people:item>name</people:item>