def from_directory(cls, dir, **options): if not os.path.exists(dir): raise AttributeError("Directory '{0}' does not exit.".format(dir)) migration_kwargs, import_kwargs = cls._clean_options(options) selected_models = migration_kwargs.pop('models') selected_serializer = import_kwargs.pop('serializer') found_models = [] files_and_serializers = {} for file_name in os.listdir(dir): matchobj = FILE_NAME_RE.search(file_name) if matchobj is None: continue app_config = apps.get_app_config(matchobj.group(1)) model = app_config.get_model(matchobj.group(2)) if selected_models and model not in selected_models: continue serializer = (selected_serializer or serializers.get_serializer(matchobj.group(3))()) found_models.append(model) files_and_serializers[model] = (file_name, serializer) model_list = sort_dependencies(found_models) for model in model_list: migration = ModelMigration(model, **migration_kwargs) file_name, serializer = files_and_serializers[model] file_path = os.path.join(dir, file_name) with serializer.open_to_load(file_path) as file: migration.import_data(file, serializer, **import_kwargs)
def test_no_file(self): migration = ModelMigration(Alphabet) source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv') with open(source_path, 'r') as source_file: source = source_file.read() migration.import_data(source, CsvSerializer, DirectPlan) output = StringIO() call_command( 'export_model', 'datamigrations_commands_tests.Alphabet', format='csv', stdout=output, ) result = output.getvalue() self.assertEqual(source.splitlines(), result.splitlines())
def test_labels(self): migration = ModelMigration(Category) source_path = os.path.join(MIGRATIONS_DIR, 'category.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) result_path = os.path.join(self.temp_dir, 'backup') MultipleExportFacade.to_file_path( result_path, models=['datamigrations_facades_tests.Category'], ) with open(result_path, 'r') as result_file: result = result_file.read() self.assertEqual(1, result.count('Type: MODEL;')) self.assertIn('Name: datamigrations_facades_tests.category;', result)
def test_file(self): migration = ModelMigration(Alphabet) source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv') result_path = os.path.join(self.temp_dir, 'alphabet.csv') with open(source_path, 'r') as source_file: source = source_file.read() migration.import_data(source, CsvSerializer, DirectPlan) SingleExportFacade.to_file_path( result_path, model='datamigrations_facades_tests.Alphabet', serializer='csv', ) with open(result_path, 'r') as result_file: result = result_file.read() self.assertEqual(source.splitlines(), result.splitlines())
def test_file(self): migration = ModelMigration(Author) source_path = os.path.join(MIGRATIONS_DIR, 'author.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) migration = ModelMigration(Category) source_path = os.path.join(MIGRATIONS_DIR, 'category.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) migration = ModelMigration(Tag) source_path = os.path.join(MIGRATIONS_DIR, 'tag.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) migration = ModelMigration(Post) source_path = os.path.join(MIGRATIONS_DIR, 'post.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) migration = ModelMigration(PostTags) source_path = os.path.join(MIGRATIONS_DIR, 'post_tags.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) source_path = os.path.join(MIGRATIONS_DIR, 'backup') result_path = os.path.join(self.temp_dir, 'backup') output = StringIO() call_command( 'export_models', file=result_path, format='json', natural=True, stdout=output, ) with open(source_path, 'r') as source_file: source = source_file.read() with open(result_path, 'r') as result_file: result = result_file.read() self.assertEqual(source.splitlines(), result.splitlines()) self.assertIn('Objects were successfully exported.', output.getvalue())
def test_labels(self): migration = ModelMigration(Category) source_path = os.path.join(MIGRATIONS_DIR, 'category.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) result_path = os.path.join(self.temp_dir, 'backup') output = StringIO() call_command( 'export_models', 'datamigrations_commands_tests.Category', file=result_path, format='json', natural=True, stdout=output, ) with open(result_path, 'r') as result_file: result = result_file.read() self.assertEqual(1, result.count('Type: MODEL;')) self.assertIn('Name: datamigrations_commands_tests.category;', result)
def test_file(self): migration = ModelMigration(Author) source_path = os.path.join(MIGRATIONS_DIR, 'author.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) migration = ModelMigration(Category) source_path = os.path.join(MIGRATIONS_DIR, 'category.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) migration = ModelMigration(Tag) source_path = os.path.join(MIGRATIONS_DIR, 'tag.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) migration = ModelMigration(Post) source_path = os.path.join(MIGRATIONS_DIR, 'post.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) migration = ModelMigration(PostTags) source_path = os.path.join(MIGRATIONS_DIR, 'post_tags.json') with open(source_path, 'r') as source_file: migration.import_data(source_file, JsonSerializer, DirectPlan) source_path = os.path.join(MIGRATIONS_DIR, 'backup') result_path = os.path.join(self.temp_dir, 'backup') MultipleExportFacade.to_file_path( result_path, serializer='json', use_natural_keys=True, ) with open(source_path, 'r') as source_file: source = source_file.read() with open(result_path, 'r') as result_file: result = result_file.read() self.assertEqual(source.splitlines(), result.splitlines())
def from_file(cls, file, **options): migration_kwargs, import_kwargs = cls._clean_options(options) migration = ModelMigration(**migration_kwargs) migration.import_data(file, **import_kwargs)