summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/models_imports.py1
-rw-r--r--ishtar_common/serializers.py35
-rw-r--r--ishtar_common/tests.py27
3 files changed, 57 insertions, 6 deletions
diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py
index 46cf742eb..b4b41787b 100644
--- a/ishtar_common/models_imports.py
+++ b/ishtar_common/models_imports.py
@@ -113,6 +113,7 @@ class ImporterType(models.Model):
blank=True, null=True, max_length=500)
available = models.BooleanField(_(u"Available"), default=True)
objects = ImporterTypeManager()
+ SERIALIZATION_EXCLUDE = ["users"]
class Meta:
verbose_name = _(u"Importer - Type")
diff --git a/ishtar_common/serializers.py b/ishtar_common/serializers.py
index e9b904d6f..4867b58f6 100644
--- a/ishtar_common/serializers.py
+++ b/ishtar_common/serializers.py
@@ -56,6 +56,8 @@ def archive_serialization(result, archive_dir=None, archive=False,
types (default False)
:param archive: if True return a zip file containing all the file serialized
(default False)
+ :param archive_name: path to the archive if not provided a new archive is
+ created
:return: string containing the json serialization of types unless
return_empty_types or archive is set to True
"""
@@ -172,7 +174,7 @@ def generic_get_results(model_list, dirname):
for idx in range(len(new_result)):
for excluded_field in model.SERIALIZATION_EXCLUDE:
new_result[idx]["fields"].pop(excluded_field)
- result[key] = json.dumps(new_result)
+ result[key] = json.dumps(new_result, indent=2)
return result
@@ -209,16 +211,38 @@ CONF_MODEL_LIST = [
def conf_serialization(archive=False, return_empty_types=False,
archive_name=None):
- media_archive = generic_archive_files(CONF_MODEL_LIST)
- result = generic_get_results(CONF_MODEL_LIST, "common_conf")
+ media_archive = None
+ if archive:
+ media_archive = generic_archive_files(CONF_MODEL_LIST)
+ result = generic_get_results(CONF_MODEL_LIST, "common_configuration")
full_archive = archive_serialization(
- result, archive_dir="common_conf", archive=archive,
+ result, archive_dir="common_configuration", archive=archive,
return_empty_types=return_empty_types, archive_name=archive_name)
+ if not media_archive:
+ return full_archive
with ZipFile(full_archive, 'a') as current_zip:
current_zip.write(media_archive, arcname="media.zip")
return full_archive
+IMPORT_MODEL_LIST = [
+ models.Regexp, models.ImporterModel, models.ImporterType,
+ models.ValueFormater, models.ImporterColumn,
+ models.FormaterType, models.ImporterDefault, models.ImporterDefaultValues,
+ models.ImportTarget, models.ImporterDefaultValues,
+ models.ImporterDuplicateField
+]
+
+
+def importer_serialization(archive=False, return_empty_types=False,
+ archive_name=None):
+ result = generic_get_results(IMPORT_MODEL_LIST, "common_imports")
+ full_archive = archive_serialization(
+ result, archive_dir="common_imports", archive=archive,
+ return_empty_types=return_empty_types, archive_name=archive_name)
+ return full_archive
+
+
def restore_serialized(archive_name, delete_existing=False):
with zipfile.ZipFile(archive_name, "r") as zip_file:
# check version
@@ -230,7 +254,8 @@ def restore_serialized(archive_name, delete_existing=False):
)
DIRS = (
- ("types", [None]), ("common_conf", CONF_MODEL_LIST)
+ ("types", [None]), ("common_configuration", CONF_MODEL_LIST),
+ ("common_imports", IMPORT_MODEL_LIST),
)
namelist = zip_file.namelist()
for current_dir, model_list in DIRS:
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index 0e3832e24..7be176c05 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -52,7 +52,8 @@ from ishtar_common import views
from ishtar_common.apps import admin_site
from ishtar_common.serializers import type_serialization, \
SERIALIZATION_VERSION, serialization_info, \
- restore_serialized, conf_serialization, CONF_MODEL_LIST
+ restore_serialized, conf_serialization, CONF_MODEL_LIST, \
+ importer_serialization, IMPORT_MODEL_LIST
from ishtar_common.utils import post_save_geo, update_data, move_dict_data, \
rename_and_simplify_media_name, try_fix_file
@@ -651,6 +652,15 @@ class SerializationTest(TestCase):
self.create_default_conf()
self.generic_serialization_test(conf_serialization)
+ def create_default_importer(self):
+ models.ValueFormater.objects.create(
+ name="Test", slug="test", format_string="HOPS-{}"
+ )
+
+ def test_importer_serialization(self):
+ self.create_default_importer()
+ self.generic_serialization_test(importer_serialization)
+
def test_serialization_zip(self):
zip_filename = type_serialization(archive=True)
# only check the validity of the zip, the type content is tested above
@@ -733,6 +743,21 @@ class SerializationTest(TestCase):
os.path.isfile(values["document_template"].template.path)
)
+ def test_importer_restore(self):
+ self.create_default_importer()
+ current_number = {}
+ for model in IMPORT_MODEL_LIST:
+ current_number[model.__name__] = model.objects.count()
+ zip_filename = importer_serialization(archive=True)
+ restore_serialized(zip_filename, delete_existing=True)
+ for model in IMPORT_MODEL_LIST:
+ previous_nb = current_number[model.__name__]
+ current_nb = model.objects.count()
+ self.assertEqual(
+ previous_nb, current_nb,
+ msg="Restore for model {} failed. Initial: {}, restored: "
+ "{}.".format(model.__name__, previous_nb, current_nb))
+
class AccessControlTest(TestCase):
def test_administrator(self):