Skip to content

Instantly share code, notes, and snippets.

@bmihelac
Created September 30, 2015 08:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmihelac/434fceb6ba8e752f08d3 to your computer and use it in GitHub Desktop.
Save bmihelac/434fceb6ba8e752f08d3 to your computer and use it in GitHub Desktop.
Import management command for django-import-export
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import mimetypes
from optparse import make_option
from django.utils.encoding import force_text
from django.utils.translation import ugettext as _
from django.core.management.base import BaseCommand
from import_export.formats import base_formats
FORMATS = {
'text/csv': base_formats.CSV,
'application/vnd.ms-excel': base_formats.XLS,
}
class Command(BaseCommand):
args = '<import_file_name>'
option_list = BaseCommand.option_list + (
make_option('--resource_class',
dest='resource_class',
default=None,
help='Resource class as dotted path, ie: mymodule.resources.MyResource'), # noqa
make_option('--model_name',
dest='model_name',
default=None,
help='Model name, ie: auth.User'), # noqa
make_option('--dry-run',
action='store_true',
dest='dry_run',
default=False,
help='Dry run'),
make_option('--raise-errors',
action='store_true',
dest='raise_errors',
help='Raise errors'),
make_option('--no-raise-errors',
action='store_false',
dest='raise_errors',
help='Do not raise errors'),
)
def get_resource_class(self, resource_class, model_name):
from django.utils.module_loading import import_by_path
from django.db import models
from import_export.resources import modelresource_factory
if not resource_class:
return modelresource_factory(models.get_model(model_name))
else:
return import_by_path(resource_class)
def handle(self, *args, **options):
dry_run = options.get('dry_run')
if dry_run:
self.stdout.write(self.style.NOTICE(_('Dry run')))
raise_errors = options.get('raise_errors', None)
if raise_errors is None:
raise_errors = not dry_run
import_file_name = args[0]
mimetype, encosing = mimetypes.guess_type(import_file_name)
input_format = FORMATS[mimetype]()
resource_class = self.get_resource_class(
options.get('resource_class'),
options.get('model_name')
)
resource = resource_class()
import_file = open(import_file_name, input_format.get_read_mode())
data = import_file.read()
dataset = input_format.create_dataset(data)
result = resource.import_data(
dataset,
dry_run=dry_run,
raise_errors=raise_errors
)
if result.has_errors():
self.stdout.write(self.style.ERROR(_('Errors')))
for error in result.base_errors:
self.stdout.write(error.error, self.style.ERROR)
for line, errors in result.row_errors():
for error in errors:
self.stdout.write(self.style.ERROR(
_('Line number') + ': ' + force_text(line) + ' - '
+ force_text(error.error)))
else:
self.stdout.write(self.style.NOTICE(_('OK')))
import_file.close()
@int-ua
Copy link

int-ua commented Jan 16, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment