diff --git a/datadownloader/models.py b/datadownloader/models.py index 7b3b07b..24d4640 100644 --- a/datadownloader/models.py +++ b/datadownloader/models.py @@ -69,13 +69,15 @@ def _get_datadump_bin(self): return 'datadump' def _dump_media(self): - return [settings.MEDIA_ROOT.replace("%s/" % os.getcwd(), ''), ] + return [ + settings.MEDIA_ROOT, + ] def _dump_db(self): self._clean_dumps_path() subprocess.check_output(self._get_datadump_bin()) dump_path = os.path.join(settings.BASE_DIR, 'dumps') - return [dump_path.replace("%s/" % os.getcwd(), ''), ] + return [dump_path] def _clean_dumps_path(self): dumps_path = os.path.join(settings.BASE_DIR, 'dumps') @@ -84,8 +86,6 @@ def _clean_dumps_path(self): os.mkdir(dumps_path) def create(self): - # be sure to be in project root folder - os.chdir(settings.BASE_DIR) folders = [] if self.data_type == 'db': folders.extend(self._dump_db()) @@ -97,7 +97,8 @@ def create(self): with tarfile.open(self.path, "w:gz") as tar: for folder in folders: - tar.add(folder) + archive_name = folder.replace(settings.BASE_DIR, '').lstrip('/') + tar.add(folder, archive_name) def destroy(self): os.remove(self.path) diff --git a/datadownloader/tests/test_models.py b/datadownloader/tests/test_models.py index 7c5376f..042f501 100644 --- a/datadownloader/tests/test_models.py +++ b/datadownloader/tests/test_models.py @@ -59,13 +59,16 @@ def test_create(self): dump.create() binary_call = dump._get_datadump_bin() sp.check_output.assert_called_once_with(binary_call) - media_path = settings.MEDIA_ROOT.replace("%s/" % os.getcwd(), '') - db_path = os.path.join(settings.BASE_DIR, - 'dumps').replace("%s/" % os.getcwd(), '') + media_path = settings.MEDIA_ROOT + media_name = 'medias' + + db_path = os.path.join(settings.BASE_DIR, 'dumps') + db_name = 'dumps' + tf.assert_has_calls([ mock.call.open(expected_dump_path, 'w:gz'), mock.call.open().__enter__(), - mock.call.open().__enter__().add(media_path), - mock.call.open().__enter__().add(db_path), + mock.call.open().__enter__().add(media_path, media_name), + mock.call.open().__enter__().add(db_path, db_name), mock.call.open().__exit__(None, None, None), ])