From 37234e1423fb4ce64ceafe7bec90960b8da86793 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 10 Sep 2019 12:18:11 +0200 Subject: Serialization - restore: manage history (or not) --- ishtar_common/serializers.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'ishtar_common/serializers.py') diff --git a/ishtar_common/serializers.py b/ishtar_common/serializers.py index 99ec79933..6ff0ae9bb 100644 --- a/ishtar_common/serializers.py +++ b/ishtar_common/serializers.py @@ -276,7 +276,7 @@ def full_serialization(operation_queryset=None, site_queryset=None, return archive_name -def restore_serialized(archive_name, delete_existing=False): +def restore_serialized(archive_name, user=None, delete_existing=False): with zipfile.ZipFile(archive_name, "r") as zip_file: # check version info = json.loads(zip_file.read("info.json").decode("utf-8")) @@ -321,15 +321,41 @@ def restore_serialized(archive_name, delete_existing=False): if delete_existing: model.objects.all().delete() data = zip_file.read(json_filename).decode("utf-8") + # regenerate labels, add a new version, etc. + historized = hasattr(model, "history_modifier") and ( + hasattr(model, "history_creator")) + need_resave = hasattr(model, "CACHED_LABELS") or \ + hasattr(model, "cached_label") or \ + (user and historized) idx = -1 for idx, obj in enumerate(deserialize("json", data)): + extra_attrs = {} + if historized and not user: + keys = obj.object.natural_key() + old_obj = None + try: + old_obj = model.objects.get_by_natural_key( + *keys) + except model.DoesNotExist: + pass + if old_obj and (old_obj.history_creator or + old_obj.history_modifier): + extra_attrs = { + "history_modifier_id": + old_obj.history_modifier_id, + "history_creator_id": + old_obj.history_creator_id + } obj.save() - # force reindex - if hasattr(model, "CACHED_LABELS") or \ - hasattr(model, "cached_label"): + if need_resave or extra_attrs: obj = model.objects.get(id=obj.object.id) - obj.skip_history_when_saving = True + if user: + obj.history_modifier = user + else: + obj.skip_history_when_saving = True obj._no_move = True + for k in extra_attrs: + setattr(obj, k, extra_attrs[k]) obj.save() if idx >= 0: result.append((model.__name__, idx + 1)) -- cgit v1.2.3