From eb9d989822c4314bb36d2b8179a598c8b9f2b9c9 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 19 Nov 2018 10:17:06 +0100 Subject: Treatment n<->1: copy and merge data --- archaeological_finds/models_treatments.py | 7 ++++++- ishtar_common/utils.py | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py index c7b888c0d..d92c98235 100644 --- a/archaeological_finds/models_treatments.py +++ b/archaeological_finds/models_treatments.py @@ -34,7 +34,8 @@ from ishtar_common.models import Document, GeneralType, \ ImageModel, BaseHistorizedItem, OwnPerms, HistoricalRecords, Person, \ Organization, ValueGetter, post_save_cache, ShortMenuItem, \ DashboardFormItem, ExternalIdManager -from ishtar_common.utils import cached_label_changed, get_current_year +from ishtar_common.utils import cached_label_changed, get_current_year, \ + update_data class TreatmentState(GeneralType): @@ -351,6 +352,10 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem, current_documents.append(document.pk) new_find.documents.add(document) + # data + new_find.data = update_data(new_find.data, upstream_item.data, + merge=True) + upstream_item.downstream_treatment = self upstream_item.history_modifier = self.history_modifier upstream_item.save() diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 4f968af31..ff67fc470 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -145,18 +145,24 @@ def check_model_access_control(request, model, available_perms=None): return allowed, own -def update_data(data_1, data_2): +def update_data(data_1, data_2, merge=False): """ Update a data directory taking account of key detail """ res = {} if not isinstance(data_1, dict) or not isinstance(data_2, dict): + if data_2 and not data_1: + return data_2 + if not merge: + return data_1 + if data_2 and data_2 != data_1: + return data_1 + u" ; " + data_2 return data_1 for k in data_1: if k not in data_2: res[k] = data_1[k] else: - res[k] = update_data(data_1[k], data_2[k]) + res[k] = update_data(data_1[k], data_2[k], merge=merge) for k in data_2: if k not in data_1: res[k] = data_2[k] -- cgit v1.2.3