# 需要导入模块: from django.conf import settings [as 别名]
# 或者: from django.conf.settings import PROJECT_ROOT [as 别名]
def handle(self, *args, **options):
module = options['module']
if module not in self.importers:
raise CommandError("Importer %s not found. Valid importers: %s" % (module, self.imp_list))
imp_class = self.importers[module]
if hasattr(settings, 'PROJECT_ROOT'):
root_dir = settings.PROJECT_ROOT
else:
root_dir = settings.BASE_DIR
importer = imp_class({'data_path': os.path.join(root_dir, 'data'),
'verbosity': int(options['verbosity']),
'cached': options['cached'],
'single': options['single'],
'remap': options['remap'],
'force': options['force']})
# Activate the default language for the duration of the import
# to make sure translated fields are populated correctly.
old_lang = get_language()
activate(settings.LANGUAGES[0][0])
for imp_type in self.importer_types:
name = "import_%s" % imp_type
method = getattr(importer, name, None)
if options[imp_type]:
if not method:
raise CommandError("Importer {} does not support importing {}".format(importer.name, imp_type))
if imp_type == 'courses' and 'extension_course' not in settings.INSTALLED_APPS:
raise CommandError("Course extension must be installed when importing courses.")
else:
if not options['all']:
continue
if method:
method()
activate(old_lang)