From 36e19acab7b98fe4a384071767a44a5333e64396 Mon Sep 17 00:00:00 2001 From: Carlos Vega <carlos.vega@uni.lu> Date: Sun, 19 Apr 2020 20:18:10 +0200 Subject: [PATCH] create list of changes and then make them with a provenance for each change --- smash/web/redcap_connector.py | 49 ++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/smash/web/redcap_connector.py b/smash/web/redcap_connector.py index 6f12fea5..01ada9e9 100644 --- a/smash/web/redcap_connector.py +++ b/smash/web/redcap_connector.py @@ -218,40 +218,41 @@ class RedcapConnector(object): smasch_appointment.visit.is_finished = True smasch_appointment.visit.save() if visit.virus is not None or visit.virus_inconclusive: + changes = None if visit.visit_number == 1 and subject.virus_test_1 != visit.virus: - subject.virus_test_1 = visit.virus - subject.virus_test_1_updated = datetime.datetime.now() - subject.save() + changes = [('virus_test_1', visit.virus), ('virus_test_1_updated', datetime.datetime.now())] if visit.visit_number == 2 and subject.virus_test_2 != visit.virus: - subject.virus_test_2 = visit.virus - subject.virus_test_2_updated = datetime.datetime.now() - subject.save() + changes = [('virus_test_2', visit.virus), ('virus_test_2_updated', datetime.datetime.now())] if visit.visit_number == 3 and subject.virus_test_3 != visit.virus: - subject.virus_test_3 = visit.virus - subject.virus_test_3_updated = datetime.datetime.now() - subject.save() + changes = [('virus_test_3', visit.virus), ('virus_test_3_updated', datetime.datetime.now())] if visit.visit_number == 4 and subject.virus_test_4 != visit.virus: - subject.virus_test_4 = visit.virus - subject.virus_test_4_updated = datetime.datetime.now() - subject.save() + changes = [('virus_test_4', visit.virus), ('virus_test_4_updated', datetime.datetime.now())] if visit.visit_number == 5 and subject.virus_test_5 != visit.virus: - subject.virus_test_5 = visit.virus - subject.virus_test_5_updated = datetime.datetime.now() - subject.save() + changes = [('virus_test_5', visit.virus), ('virus_test_5_updated', datetime.datetime.now())] if visit.visit_number == 1 and subject.virus_test_1_updated is None and visit.virus_inconclusive: - subject.virus_test_1_updated = datetime.datetime.now() - subject.save() + changes = [('virus_test_1_updated', datetime.datetime.now())] if visit.visit_number == 2 and subject.virus_test_2_updated is None and visit.virus_inconclusive: - subject.virus_test_2_updated = datetime.datetime.now() - subject.save() + changes = [('virus_test_2_updated', datetime.datetime.now())] if visit.visit_number == 3 and subject.virus_test_3_updated is None and visit.virus_inconclusive: - subject.virus_test_3_updated = datetime.datetime.now() - subject.save() + changes = [('virus_test_3_updated', datetime.datetime.now())] if visit.visit_number == 4 and subject.virus_test_4_updated is None and visit.virus_inconclusive: - subject.virus_test_4_updated = datetime.datetime.now() - subject.save() + changes = [('virus_test_4_updated', datetime.datetime.now())] if visit.visit_number == 5 and subject.virus_test_5_updated is None and visit.virus_inconclusive: - subject.virus_test_5_updated = datetime.datetime.now() + changes = [('virus_test_5_updated', datetime.datetime.now())] + # + if changes is not None: + for field, new_value in changes: + old_value = getattr(subject, field) + description = u'{} changed from "{}" to "{}"'.format(field, old_value, new_value) + p = Provenance(modified_table=StudySubject._meta.db_table, + modified_table_id=subject.id, + modification_author=self.importer_user, + previous_value=old_value, + new_value=new_value, + modification_description=description, + modified_field=field) + setattr(subject, field, new_value) + p.save() subject.save() return result -- GitLab