Ejemplo n.º 1
0
    def command(self, filename, 
                defaults='country=KE(Country|code)',
                mappings='',
                expected_errs=[]):
        """ Run core csvimport command to parse file """
        cmd = Command()
        uploaded = DummyFileObj()
        uploaded.set_path(filename)
        cmd.setup(mappings=mappings,
                  modelname='tests.Item',
                  charset='',
                  uploaded=uploaded,
                  defaults=defaults)

        # 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)
            expected.reverse()
        for err in expected:
            try:
                error = errors.pop()
                self.assertEqual(error, err)
            except:
                pass
        if errors:
            for err in errors:
                print err
        self.assertEqual(errors, [])
Ejemplo n.º 2
0
    def command(self,
                filename,
                defaults='country=KE(Country|code)',
                mappings='',
                expected_errs=[]):
        """ Run core csvimport command to parse file """
        cmd = Command()
        uploaded = DummyFileObj()
        uploaded.set_path(filename)
        cmd.setup(mappings=mappings,
                  modelname='tests.Item',
                  charset='',
                  uploaded=uploaded,
                  defaults=defaults)

        # 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)
            expected.reverse()
        for err in expected:
            try:
                error = errors.pop()
                self.assertEqual(error, err)
            except:
                pass
        if errors:
            for err in errors:
                print err
        self.assertEqual(errors, [])
Ejemplo n.º 3
0
    def command(self, filename, defaults='country=KE(Country|code)'):
        """ Run core csvimport command to parse file """
        cmd = Command()
        uploaded = DummyFileObj()
        uploaded.set_path(filename)
        cmd.setup(mappings='', 
                  modelname='tests.Item',
                  charset='',
                  uploaded=uploaded,
                  defaults=defaults)

        # Report back any parse errors and fail test if they exist
        errors = cmd.run(logid='commandtest')
        error = errors.pop()
        self.assertEqual(error, 'Using mapping from first row of CSV file')
        if errors:
            for err in errors:
                print err
        self.assertEqual(errors, [])
Ejemplo n.º 4
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.csvimport 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, 
                   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()
Ejemplo n.º 5
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.csvimport 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,
                   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()