示例#1
0
    def save_model(self, request, obj, form, change):
        """ Do save and process command - cant commit False
            since then file wont be found for reopening via right charset
        """
        form.save()
        from csvimport.management.commands.importcsv import Command

        cmd = Command()
        if obj.upload_file:
            obj.file_name = obj.upload_file.name
            obj.encoding = ""
            defaults = self.filename_defaults(obj.file_name)
            cmd.setup(
                mappings=obj.field_list,
                modelname=obj.model_name,
                charset=obj.encoding,
                uploaded=obj.upload_file,
                defaults=defaults,
            )
        errors = cmd.run(logid=obj.id)
        if errors:
            obj.error_log = "\n".join(errors)
        obj.import_user = str(request.user)
        obj.import_date = datetime.now()
        obj.save()
示例#2
0
    def command(
        self,
        csvfile=None,
        mappings="",
        modelname="csvimport.Item",
        charset="",
        expected_errs=[],
        defaults="country=KE(Country|code)",
        uploaded=None,
        nameindexes=False,
        deduplicate=True,
        delimiter=",",
        reader=True,
        clean=True,
        bulk=False,
        time=False,
    ):
        """ Run core csvimport command to parse file """
        cmd = ImportCommand()
        uploaded = DummyFileObj()
        uploaded.set_path(csvfile)
        cmd.setup(
            mappings=mappings,
            modelname=modelname,
            charset=charset,
            defaults=defaults,
            uploaded=uploaded,
            nameindexes=nameindexes,
            deduplicate=deduplicate,
            delimiter=delimiter,
            reader=reader,
            clean=clean,
            bulk=bulk,
        )

        # Report back any unnexpected parse errors
        # and confirm those that are expected.
        # Fail test if they are not matching
        if time:
            return timeit.Timer(cmd.run).timeit(number=1)
        errors = cmd.run("commandtest")
        expected = [err for err in DEFAULT_ERRS]
        if expected_errs:
            expected.extend(expected_errs)
        for err in expected:
            try:
                errors.remove(err)
            except:
                pass
        if errors:
            for err in errors:
                if err.startswith("Matched Columns"):
                    errors.remove(err)
                else:
                    print(err)
        self.assertEqual(errors, [])
示例#3
0
    def command(self,
                csvfile=None,
                mappings='',
                modelname='csvimport.Item',
                charset='',
                expected_errs=[],
                defaults='country=KE(Country|code)',
                uploaded=None,
                nameindexes=False,
                deduplicate=True,
                delimiter=',',
                reader=True,
                clean=True,
                bulk=False,
                time=False
                ):
        """ Run core csvimport command to parse file """
        cmd = ImportCommand()
        uploaded = DummyFileObj()
        uploaded.set_path(csvfile)
        cmd.setup(mappings=mappings,
                  modelname=modelname,
                  charset=charset,
                  defaults=defaults,
                  uploaded=uploaded,
                  nameindexes=nameindexes,
                  deduplicate=deduplicate,
                  delimiter=delimiter,
                  reader=reader,
                  clean=clean,
                  bulk=bulk
                  )

        # Report back any unnexpected parse errors
        # and confirm those that are expected.
        # Fail test if they are not matching
        if time:
            return timeit.Timer(cmd.run).timeit(number=1)
        errors = cmd.run('commandtest')
        expected = [err for err in DEFAULT_ERRS]
        if expected_errs:
            expected.extend(expected_errs)
        for err in expected:
            try:
                errors.remove(err)
            except:
                pass
        if errors:
            for err in errors:
                if err.startswith("Matched Columns"):
                    errors.remove(err)
                else:
                    print (err)
        self.assertEqual(errors, [])
示例#4
0
    def command(self,
                csvfile=None,
                mappings='',
                modelname='csvimport.Item',
                charset='',
                expected_errs=[],
                defaults='country=KE(Country|code)',
                uploaded=None,
                nameindexes=False,
                deduplicate=True,
                delimiter=',',
                reader=True):
        """ Run core csvimport command to parse file """
        cmd = ImportCommand()
        uploaded = DummyFileObj()
        uploaded.set_path(csvfile)
        cmd.setup(mappings=mappings,
                  modelname=modelname,
                  charset=charset,
                  defaults=defaults,
                  uploaded=uploaded,
                  nameindexes=nameindexes,
                  deduplicate=deduplicate,
                  delimiter=delimiter,
                  reader=reader)

        # Report back any unnexpected parse errors
        # and confirm those that are expected.
        # Fail test if they are not matching
        errors = cmd.run(logid='commandtest')
        expected = [err for err in DEFAULT_ERRS]
        if expected_errs:
            expected.extend(expected_errs)
        for err in expected:
            try:
                errors.remove(err)
            except:
                pass
        if errors:
            for err in errors:
                print(err)
        self.assertEqual(errors, [])
示例#5
0
    def command(self, 
                csvfile=None, 
                mappings='',
                modelname='tests.Item',
                charset='',
                expected_errs=[],
                defaults='country=KE(Country|code)',
                uploaded=None,
                nameindexes=False,
                deduplicate=True
                ):
        """ Run core csvimport command to parse file """
        cmd = ImportCommand()
        uploaded = DummyFileObj()
        uploaded.set_path(csvfile)
        cmd.setup(mappings=mappings,
                  modelname=modelname,
                  charset=charset,
                  defaults=defaults,
                  uploaded=uploaded,
                  nameindexes=nameindexes,
                  deduplicate=deduplicate
                  )

        # Report back any unnexpected parse errors
        # and confirm those that are expected.
        # Fail test if they are not matching
        errors = cmd.run(logid='commandtest')
        #raise Exception(errors)
        expected = [err for err in DEFAULT_ERRS]
        if expected_errs:
            expected.extend(expected_errs)
        for err in expected:
            try:
                errors.remove(err)
            except:
                pass
        if errors:
            for err in errors:
                print err
        self.assertEqual(errors, [])
 def save_model(self, request, obj, form, change):
     """ Do save and process command - cant commit False
         since then file wont be found for reopening via right charset
     """
     form.save()
     from csvimport.management.commands.importcsv import Command
     cmd = Command()
     if obj.upload_file:
         obj.file_name = obj.upload_file.name
         obj.encoding = ''
         defaults = self.filename_defaults(obj.file_name)
         cmd.setup(mappings=obj.field_list,
                   modelname=obj.model_name,
                   charset=obj.encoding,
                   uploaded=obj.upload_file,
                   defaults=defaults)
     errors = cmd.run(logid=obj.id)
     if errors:
         obj.error_log = '\n'.join(errors)
     obj.import_user = str(request.user)
     obj.import_date = datetime.now()
     obj.save()