From 91766b4cdcdba8684c238adf834831f53144f735 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Sun, 5 Apr 2020 22:02:13 +0200 Subject: [PATCH] merging subject data on import implemented --- smash/web/importer/importer.py | 18 +++++++++++- smash/web/tests/importer/test_importer.py | 34 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/smash/web/importer/importer.py b/smash/web/importer/importer.py index 1e9b377e..2b089534 100644 --- a/smash/web/importer/importer.py +++ b/smash/web/importer/importer.py @@ -3,6 +3,8 @@ import logging import sys import traceback +from django.db import models + from subject_import_reader import SubjectImportReader from warning_counter import MsgCounterHandler from web.models import StudySubject, Subject @@ -57,7 +59,21 @@ class Importer(object): db_study_subjects = StudySubject.objects.filter(screening_number=study_subject.screening_number) if db_study_subjects.count() > 0: db_study_subject = db_study_subjects.first() - raise NotImplementedError() + 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 != "": + setattr(db_study_subject.subject, field.name, new_value) + print db_study_subject.subject.first_name + 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 != "": + setattr(db_study_subject, field.name, new_value) + 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] diff --git a/smash/web/tests/importer/test_importer.py b/smash/web/tests/importer/test_importer.py index da4bb9c3..fe12f9be 100644 --- a/smash/web/tests/importer/test_importer.py +++ b/smash/web/tests/importer/test_importer.py @@ -1,10 +1,12 @@ # coding=utf-8 +import datetime import logging from django.test import TestCase from mock_reader import MockReader +from tests.functions import create_study_subject from web.importer import Importer from web.models import Subject, StudySubject, Study from web.models.constants import GLOBAL_STUDY_ID @@ -70,3 +72,35 @@ class TestImporter(TestCase): self.assertEqual(0, importer.added_count) self.assertEqual(1, importer.problematic_count) + + def test_import_merge_subject(self): + existing_study_subject = create_study_subject() + study_subjects = [] + subject = Subject() + subject.first_name = "XYZ" + subject.last_name = "AAA" + subject.date_born = datetime.datetime.now() + study_subject = StudySubject() + study_subject.screening_number = existing_study_subject.screening_number + study_subject.subject = subject + study_subject.study = self.study + study_subjects.append(study_subject) + + importer = Importer(filename="empty.csv", reader=MockReader(study_subjects)) + 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.assertEqual(0, importer.added_count) + self.assertEqual(1, importer.merged_count) + self.assertEqual(0, importer.problematic_count) + self.assertEqual(0, importer.warning_count) + + existing_study_subject = StudySubject.objects.filter(id=existing_study_subject.id)[0] + self.assertEquals(existing_study_subject.subject.first_name, subject.first_name) + self.assertEquals(existing_study_subject.subject.last_name, subject.last_name) + self.assertEquals(existing_study_subject.subject.date_born.strftime("%Y-%m-%d"), + subject.date_born.strftime("%Y-%m-%d")) -- GitLab