Skip to content
Snippets Groups Projects
Commit c62f6d3f authored by Piotr Gawron's avatar Piotr Gawron
Browse files

import of csv with subjects did not work with custom fields for subjects that existed in db

parent 7e66dc3a
No related branches found
No related tags found
1 merge request!366Resolve "import custom fields data bug"
Pipeline #48168 failed
......@@ -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
......
......@@ -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)
......
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment