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

merging subject data on import implemented

parent a5038856
No related branches found
No related tags found
1 merge request!231Resolve "auto-import of subject data"
...@@ -3,6 +3,8 @@ import logging ...@@ -3,6 +3,8 @@ import logging
import sys import sys
import traceback import traceback
from django.db import models
from subject_import_reader import SubjectImportReader from subject_import_reader import SubjectImportReader
from warning_counter import MsgCounterHandler from warning_counter import MsgCounterHandler
from web.models import StudySubject, Subject from web.models import StudySubject, Subject
...@@ -57,7 +59,21 @@ class Importer(object): ...@@ -57,7 +59,21 @@ class Importer(object):
db_study_subjects = StudySubject.objects.filter(screening_number=study_subject.screening_number) db_study_subjects = StudySubject.objects.filter(screening_number=study_subject.screening_number)
if db_study_subjects.count() > 0: if db_study_subjects.count() > 0:
db_study_subject = db_study_subjects.first() 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: else:
study_subject.subject.save() study_subject.subject.save()
study_subject.subject = Subject.objects.filter(id=study_subject.subject.id)[0] study_subject.subject = Subject.objects.filter(id=study_subject.subject.id)[0]
......
# coding=utf-8 # coding=utf-8
import datetime
import logging import logging
from django.test import TestCase from django.test import TestCase
from mock_reader import MockReader from mock_reader import MockReader
from tests.functions import create_study_subject
from web.importer import Importer from web.importer import Importer
from web.models import Subject, StudySubject, Study from web.models import Subject, StudySubject, Study
from web.models.constants import GLOBAL_STUDY_ID from web.models.constants import GLOBAL_STUDY_ID
...@@ -70,3 +72,35 @@ class TestImporter(TestCase): ...@@ -70,3 +72,35 @@ class TestImporter(TestCase):
self.assertEqual(0, importer.added_count) self.assertEqual(0, importer.added_count)
self.assertEqual(1, importer.problematic_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"))
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