From c62f6d3f7fd0dd514c52ecb527a58d643c912f62 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Mon, 11 Oct 2021 14:42:07 +0200 Subject: [PATCH] import of csv with subjects did not work with custom fields for subjects that existed in db --- CHANGELOG | 5 ++- smash/web/importer/importer.py | 3 +- smash/web/tests/importer/test_importer.py | 39 +++++++++++++++++++++-- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 0ae5369d..6d2bb604 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -26,7 +26,10 @@ smasch (1.1.0~alpha.0-1) unstable; urgency=low multiple days (#430, #429) * bug fix: Search form should always redirect to web.views.kit_requests (#433) - * bug fix: printing mail template failed for general appointments (#435). + * bug fix: printing mail template failed for general appointments (#435) + * bug fix: import of csv with subjects did not work with custom fields for + subjects that existed in db (#415) + -- Piotr Gawron <piotr.gawron@uni.lu> Thu, 25 Feb 2021 17:00:00 +0200 diff --git a/smash/web/importer/importer.py b/smash/web/importer/importer.py index 0d7f66ea..9818085c 100644 --- a/smash/web/importer/importer.py +++ b/smash/web/importer/importer.py @@ -66,7 +66,7 @@ class Importer(EtlCommon): new_value = self.get_new_value(old_value, getattr(study_subject, field.name)) self.create_provenance_and_change_data(db_study_subject, field.name, new_value, StudySubject) db_study_subject.save() - + study_subject= db_study_subject self.merged_count += 1 else: if study_subject.type_id is None: @@ -77,6 +77,7 @@ class Importer(EtlCommon): self.create_provenance_for_new_object(Subject, study_subject.subject) self.create_provenance_for_new_object(StudySubject, study_subject) self.added_count += 1 + for custom_field_value in self.reader.get_custom_fields(study_subject): study_subject.set_custom_data_value(custom_field_value.study_subject_field, custom_field_value.value) diff --git a/smash/web/tests/importer/test_importer.py b/smash/web/tests/importer/test_importer.py index b81a3f9f..47895659 100644 --- a/smash/web/tests/importer/test_importer.py +++ b/smash/web/tests/importer/test_importer.py @@ -5,10 +5,12 @@ import logging from django.test import TestCase from django.utils import timezone -from web.importer import Importer -from web.models import Subject, StudySubject, Provenance, SubjectImportData -from web.tests.functions import create_study_subject, get_test_study, get_control_subject_type +from web.importer import Importer, CsvSubjectImportReader +from web.models import Subject, StudySubject, Provenance, SubjectImportData, EtlColumnMapping +from web.tests.functions import create_study_subject, get_test_study, get_control_subject_type, get_resource_path from .mock_reader import MockReader +from ...models.constants import CUSTOM_FIELD_TYPE_TEXT +from ...models.custom_data import CustomStudySubjectField logger = logging.getLogger(__name__) @@ -110,3 +112,34 @@ class TestImporter(TestCase): self.assertEqual(existing_study_subject.subject.last_name, subject.last_name) self.assertEqual(existing_study_subject.subject.date_born.strftime("%Y-%m-%d"), subject.date_born.strftime("%Y-%m-%d")) + + def test_load_with_merge_custom_field(self): + study_subject = create_study_subject(nd_number='Cov-000001') + study_subject.screening_number = study_subject.nd_number + study_subject.save() + + self.test_load_custom_field() + + def test_load_custom_field(self): + field = CustomStudySubjectField.objects.create(study=get_test_study(), name='my custom field', + type=CUSTOM_FIELD_TYPE_TEXT) + + subject_import_data = SubjectImportData.objects.create(study=get_test_study(), date_format="%d-%m-%Y") + EtlColumnMapping.objects.create(etl_data=subject_import_data, + column_name="screening_number", + csv_column_name="participant_id", + table_name=StudySubject._meta.db_table) + EtlColumnMapping.objects.create(etl_data=subject_import_data, + column_name="nd_number", + csv_column_name="participant_id", + table_name=StudySubject._meta.db_table) + + subject_import_data.filename = get_resource_path('import_custom_field.csv') + reader = CsvSubjectImportReader(subject_import_data) + + importer = Importer(reader=reader) + importer.execute() + + self.assertEqual(3, StudySubject.objects.count()) + study_subject = StudySubject.objects.filter(nd_number='Cov-000001').first() + self.assertEqual("Bla", study_subject.get_custom_data_value(field).value) -- GitLab