diff --git a/smash/web/redcap_connector.py b/smash/web/redcap_connector.py index 7d448fc63de628c8e6ff6b870f9ac2e84afd59ea..4e1da4fa683708eb0a6c28dc46c7ae214622f143 100644 --- a/smash/web/redcap_connector.py +++ b/smash/web/redcap_connector.py @@ -8,7 +8,9 @@ import certifi import timeout_decorator from django_cron import CronJobBase, Schedule -from web.models import ConfigurationItem, StudySubject, Language +from django.forms.models import model_to_dict +from web.models.constants import GLOBAL_STUDY_ID +from web.models import ConfigurationItem, StudySubject, Language, Study, StudyRedCapColumns from web.models.constants import REDCAP_TOKEN_CONFIGURATION_TYPE, \ REDCAP_BASE_URL_CONFIGURATION_TYPE, CRON_JOB_TIMEOUT from web.models.inconsistent_subject import InconsistentField, InconsistentSubject @@ -178,11 +180,12 @@ class RedcapConnector(object): return result @staticmethod - def create_inconsistency_subject(red_cap_subject, study_subject, url): - fields = [] + def check_sex_consistency(red_cap_subject, study_subject): if study_subject.subject.sex != red_cap_subject.sex: - field = InconsistentField.create("sex", study_subject.subject.sex, red_cap_subject.sex) - fields.append(field) + return InconsistentField.create("sex", study_subject.subject.sex, red_cap_subject.sex) + + @staticmethod + def check_birth_date_consistency(red_cap_subject, study_subject): subject_date_born = "" if study_subject.subject.date_born is not None: subject_date_born = study_subject.subject.date_born.strftime('%Y-%m-%d') @@ -192,14 +195,20 @@ class RedcapConnector(object): if len(redcap_subject_date_born) > 10: redcap_subject_date_born = redcap_subject_date_born[:10] if subject_date_born != redcap_subject_date_born: - field = InconsistentField.create("date of birth", subject_date_born, redcap_subject_date_born) - fields.append(field) + return InconsistentField.create("date of birth", subject_date_born, redcap_subject_date_born) + + @staticmethod + def check_dead_consistency(red_cap_subject, study_subject): if study_subject.subject.dead != red_cap_subject.dead: - field = InconsistentField.create("dead", str(study_subject.subject.dead), str(red_cap_subject.dead)) - fields.append(field) + return InconsistentField.create("dead", str(study_subject.subject.dead), str(red_cap_subject.dead)) + + @staticmethod + def check_mpower_id_consistency(red_cap_subject, study_subject): if different_string(study_subject.mpower_id, red_cap_subject.mpower_id): - field = InconsistentField.create("mpower id", study_subject.mpower_id, red_cap_subject.mpower_id) - fields.append(field) + return InconsistentField.create("mpower id", study_subject.mpower_id, red_cap_subject.mpower_id) + + @staticmethod + def check_languages_consistency(red_cap_subject, study_subject): missing_language = False if len(red_cap_subject.languages) < 4: for language in study_subject.subject.languages.all(): @@ -215,8 +224,29 @@ class RedcapConnector(object): red_cap_subject_languages = "" for language in red_cap_subject.languages: red_cap_subject_languages += language.name + ", " - field = InconsistentField.create("languages", subject_languages, red_cap_subject_languages) - fields.append(field) + return InconsistentField.create("languages", subject_languages, red_cap_subject_languages) + + @staticmethod + def create_inconsistency_subject(red_cap_subject, study_subject, url): + #func dict + field_checks = { + 'sex': RedcapConnector.check_sex_consistency, + 'date_born': RedcapConnector.check_birth_date_consistency, + 'dead': RedcapConnector.check_dead_consistency, + 'mpower_id': RedcapConnector.check_mpower_id_consistency, + 'languages': RedcapConnector.check_languages_consistency + } + + fields = [] + + #get fields which are true from redcap columns + fields_to_check = [k for k,v in model_to_dict(study_subject.study.redcap_columns).iteritems() if v is True] + + for field_to_check in fields_to_check: + field = field_checks[field_to_check](red_cap_subject, study_subject) + if field is not None: + fields.append(field) + result = None if len(fields) > 0: result = InconsistentSubject.create(smash_subject=study_subject, url=url, fields=fields)