From ed5601b555f44b0fa0cfdeb7547c9d2a4576ec46 Mon Sep 17 00:00:00 2001
From: Carlos Vega <carlos.vega@uni.lu>
Date: Sun, 19 Apr 2020 19:51:48 +0200
Subject: [PATCH] added provenance to TnsCsvVisitImportReader

---
 .../importer/csv_tns_visit_import_reader.py   | 77 +++++++++++++++++--
 1 file changed, 71 insertions(+), 6 deletions(-)

diff --git a/smash/web/importer/csv_tns_visit_import_reader.py b/smash/web/importer/csv_tns_visit_import_reader.py
index 558cf076..55ca5f93 100644
--- a/smash/web/importer/csv_tns_visit_import_reader.py
+++ b/smash/web/importer/csv_tns_visit_import_reader.py
@@ -90,22 +90,70 @@ class TnsCsvVisitImportReader:
                     if len(visits) > 0:
                         logger.debug("Visit for subject " + nd_number + " already exists. Updating")
                         visit = visits[0]
-                        visit.datetime_begin = date
-                        visit.datetime_end = date + datetime.timedelta(days=14)
+
+                        changes = [('datetime_begin', date),
+                                   ('datetime_end', date + datetime.timedelta(days=14))]
+
+                        for field, new_value in changes:
+                            old_value = getattr(visit, field)
+                            description = u'{} changed from "{}" to "{}"'.format(field, old_value, new_value)
+                            p = Provenance(modified_table=Visit._meta.db_table,
+                                        modified_table_id=visit.id,
+                                        modification_author=self.importer_user,
+                                        previous_value=old_value,
+                                        new_value=new_value,
+                                        modification_description=description,
+                                        modified_field=field,
+                                        )
+                            setattr(visit, field, new_value)
+                            p.save()
+                        visit.save()
                     else:
                         visit = Visit.objects.create(subject=study_subject, visit_number=visit_number,
                                                      datetime_begin=date,
                                                      datetime_end=date + datetime.timedelta(days=14))
-                    visit.save()
+                        visit.save()
+                        #visit does not have id until .save() is done
+                        for field in Visit._meta.get_fields():
+                            if field.get_internal_type() == "CharField" or field.get_internal_type() == "DateField" or field.get_internal_type() is "BooleanField":
+                                new_value = getattr(visit, field.name)
+                                if new_value is not None and new_value != "":
+                                    description = u'{} changed from "{}" to "{}"'.format(field, '', new_value)
+                                    p = Provenance(modified_table=Visit._meta.db_table,
+                                                modified_table_id=visit.id,
+                                                modification_author=self.importer_user,
+                                                previous_value='',
+                                                new_value=new_value,
+                                                modification_description=description,
+                                                modified_field=field,
+                                                )
+                                    p.save()
+                        
                     result.append(visit)
 
                     appointments = Appointment.objects.filter(visit=visit, appointment_types=self.appointment_type)
                     if len(appointments) > 0:
                         logger.debug("Appointment for subject " + nd_number + " already set. Updating")
                         appointment = appointments[0]
-                        appointment.length = 60
-                        appointment.datetime_when = date
-                        appointment.location = location
+                        
+                        #(field, new_value)
+                        changes = [('length', 60),
+                                   ('datetime_when', date),
+                                   ('location', location)]
+
+                        for field, new_value in changes:
+                            old_value = getattr(appointment, field)
+                            description = u'{} changed from "{}" to "{}"'.format(field, old_value, new_value)
+                            p = Provenance(modified_table=Appointment._meta.db_table,
+                                        modified_table_id=appointment.id,
+                                        modification_author=self.importer_user,
+                                        previous_value=old_value,
+                                        new_value=new_value,
+                                        modification_description=description,
+                                        modified_field=field,
+                                        )
+                            setattr(appointment, field, new_value)
+                            p.save()
                         appointment.save()
                     else:
                         appointment = Appointment.objects.create(visit=visit, length=60, datetime_when=date,
@@ -113,6 +161,23 @@ class TnsCsvVisitImportReader:
                         if self.appointment_type is not None:
                             AppointmentTypeLink.objects.create(appointment_id=appointment.id,
                                                                appointment_type=self.appointment_type)
+                        
+                        appointment.save()
+                        #appointment does not have id until .save() is done
+                        for field in Appointment._meta.get_fields():
+                            if field.get_internal_type() == "CharField" or field.get_internal_type() == "DateField" or field.get_internal_type() is "BooleanField":
+                                new_value = getattr(appointment, field.name)
+                                if new_value is not None and new_value != "":
+                                    description = u'{} changed from "{}" to "{}"'.format(field, '', new_value)
+                                    p = Provenance(modified_table=Appointment._meta.db_table,
+                                                modified_table_id=appointment.id,
+                                                modification_author=self.importer_user,
+                                                previous_value='',
+                                                new_value=new_value,
+                                                modification_description=description,
+                                                modified_field=field,
+                                                )
+                                    p.save()
                     self.processed_count += 1
                 except:
                     self.problematic_count += 1
-- 
GitLab