diff --git a/smash/web/importer/importer.py b/smash/web/importer/importer.py index 2b089534df235d9e75cff8696dd8364719ead303..2c7702f0de38fb9a29156e87b207b382f7a7b1ff 100644 --- a/smash/web/importer/importer.py +++ b/smash/web/importer/importer.py @@ -3,11 +3,12 @@ import logging import sys import traceback -from django.db import models +from django.conf import settings +from django.contrib.auth.models import User from subject_import_reader import SubjectImportReader from warning_counter import MsgCounterHandler -from web.models import StudySubject, Subject +from web.models import StudySubject, Subject, Provenance, Worker from web.models.constants import GLOBAL_STUDY_ID logger = logging.getLogger(__name__) @@ -24,6 +25,16 @@ class Importer(object): self.warning_count = 0 self.study_subjects = [] + self.importer_user = None + + importer_user_name = getattr(settings, "IMPORTER_USER", None) + if importer_user_name is not None: + user = User.objects.filter(username=importer_user_name) + if user is None: + logger.warn("User does not exist: " + importer_user_name) + else: + self.importer_user = Worker.objects.filter(user=user) + def execute(self): self.added_count = 0 self.problematic_count = 0 @@ -63,21 +74,74 @@ class Importer(object): if field.get_internal_type() == "CharField" or field.get_internal_type() == "DateField" or field.get_internal_type() is "BooleanField": new_value = getattr(study_subject.subject, field.name) if new_value is not None and new_value != "": + old_value = getattr(db_study_subject.subject, field.name) + description = '{} changed from "{}" to "{}"'.format(field, old_value, new_value) + p = Provenance(modified_table=Subject._meta.db_table, + modified_table_id=db_study_subject.subject.id, + modification_author=self.importer_user, + previous_value=old_value, + new_value=new_value, + modification_description=description, + modified_field=field, + ) setattr(db_study_subject.subject, field.name, new_value) - print db_study_subject.subject.first_name + p.save() + db_study_subject.subject.save() for field in StudySubject._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(study_subject, field.name) if new_value is not None and new_value != "": + old_value = getattr(db_study_subject, field.name) + description = '{} changed from "{}" to "{}"'.format(field, old_value, new_value) + p = Provenance(modified_table=Subject._meta.db_table, + modified_table_id=db_study_subject.id, + modification_author=self.importer_user, + previous_value=old_value, + new_value=new_value, + modification_description=description, + modified_field=field, + ) setattr(db_study_subject, field.name, new_value) + p.save() db_study_subject.save() self.merged_count += 1 else: study_subject.subject.save() study_subject.subject = Subject.objects.filter(id=study_subject.subject.id)[0] study_subject.save() + + for field in Subject._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(study_subject.subject, field.name) + if new_value is not None and new_value != "": + description = '{} changed from "{}" to "{}"'.format(field, '', new_value) + p = Provenance(modified_table=Subject._meta.db_table, + modified_table_id=study_subject.subject.id, + modification_author=self.importer_user, + previous_value='', + new_value=new_value, + modification_description=description, + modified_field=field, + ) + p.save() + + for field in StudySubject._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(study_subject, field.name) + if new_value is not None and new_value != "": + description = '{} changed from "{}" to "{}"'.format(field, '', new_value) + p = Provenance(modified_table=Subject._meta.db_table, + modified_table_id=study_subject.id, + modification_author=self.importer_user, + previous_value='', + new_value=new_value, + modification_description=description, + modified_field=field, + ) + p.save() + self.added_count += 1 def get_summary(self): diff --git a/smash/web/tests/importer/test_importer.py b/smash/web/tests/importer/test_importer.py index d36223006ff0e808d69079cbdccdc52af18059d0..a5e95a1ea900a4a1742abece15e00cfea13160d2 100644 --- a/smash/web/tests/importer/test_importer.py +++ b/smash/web/tests/importer/test_importer.py @@ -8,7 +8,7 @@ from django.test import TestCase from mock_reader import MockReader from web.tests.functions import create_study_subject from web.importer import Importer -from web.models import Subject, StudySubject, Study +from web.models import Subject, StudySubject, Study, Provenance from web.models.constants import GLOBAL_STUDY_ID logger = logging.getLogger(__name__) @@ -33,10 +33,12 @@ class TestImporter(TestCase): importer = Importer(filename="empty.csv", reader=MockReader(study_subjects)) subject_counter = Subject.objects.count() study_subject_counter = StudySubject.objects.count() + provenance_counter = Provenance.objects.count() importer.execute() self.assertEqual(subject_counter + 1, Subject.objects.count()) self.assertEqual(study_subject_counter + 1, StudySubject.objects.count()) + self.assertNotEqual(provenance_counter, Provenance.objects.count()) self.assertEqual(1, importer.added_count) self.assertEqual(0, importer.problematic_count) @@ -87,12 +89,14 @@ class TestImporter(TestCase): study_subjects.append(study_subject) importer = Importer(filename="empty.csv", reader=MockReader(study_subjects)) + provenance_counter = Provenance.objects.count() subject_counter = Subject.objects.count() study_subject_counter = StudySubject.objects.count() importer.execute() self.assertEqual(subject_counter, Subject.objects.count()) self.assertEqual(study_subject_counter, StudySubject.objects.count()) + self.assertNotEqual(provenance_counter, Provenance.objects.count()) self.assertEqual(0, importer.added_count) self.assertEqual(1, importer.merged_count)