Exemplo n.º 1
0
    def save_dynamic_fields(self):
        default_context_model = self.patient.default_context(self.registry)
        wrapper = DynamicDataWrapper(self.patient,
                                     rdrf_context_id=default_context_model.pk)
        wrapper.current_form_model = None
        dynamic_data_dict = {}
        form_names = set([])
        for reg_code, form_name, section_code, cde_code, value in self._get_dynamic_data(
        ):
            form_names.add(form_name)
            delimited_key = settings.FORM_SECTION_DELIMITER.join(
                [form_name, section_code, cde_code])
            dynamic_data_dict[delimited_key] = value

        for original_multiple_section, element_list in self._get_multiple_sections(
        ):
            if original_multiple_section in dynamic_data_dict:
                dynamic_data_dict[original_multiple_section].extend(
                    element_list)
            else:
                dynamic_data_dict[original_multiple_section] = element_list

        self._update_timestamps(form_names, dynamic_data_dict)
        wrapper.save_dynamic_data(self.registry.code,
                                  "cdes",
                                  dynamic_data_dict,
                                  parse_all_forms=True)
Exemplo n.º 2
0
 def save_for_patient(self, patient_model, context_model=None):
     self.reset()
     from rdrf.db.dynamic_data import DynamicDataWrapper
     if context_model is None:
         wrapper = DynamicDataWrapper(patient_model)
     else:
         wrapper = DynamicDataWrapper(patient_model, rdrf_context_id=context_model.pk)
     dynamic_data = wrapper.load_dynamic_data(
         self.registry_model.code, "cdes", flattened=False)
     return self.save_progress(patient_model, dynamic_data, context_model)
Exemplo n.º 3
0
def rpc_create_patient_from_questionnaire(request, questionnaire_response_id):
    from rdrf.models.definition.models import QuestionnaireResponse
    from rdrf.workflows.questionnaires.questionnaires import PatientCreator, PatientCreatorError
    from rdrf.db.dynamic_data import DynamicDataWrapper
    from django.db import transaction
    from django.urls import reverse
    from django.utils.translation import ugettext as _

    if not (request.user.is_superuser or request.user.is_staff):
        return {
            "status": "fail",
            "message": _("Permission error. Patient cannot be created!")
        }

    qr = QuestionnaireResponse.objects.get(pk=questionnaire_response_id)
    patient_creator = PatientCreator(qr.registry, request.user)
    wrapper = DynamicDataWrapper(qr)
    questionnaire_data = wrapper.load_dynamic_data(qr.registry.code, "cdes")
    patient_id = None
    patient_blurb = None
    patient_link = None
    created_patient = "Not Created!"

    try:
        with transaction.atomic():
            created_patient = patient_creator.create_patient(
                None, qr, questionnaire_data)
            status = "success"
            message = "Patient created successfully"
            patient_blurb = "Patient %s created successfully" % created_patient
            patient_id = created_patient.pk
            patient_link = reverse('patient_edit',
                                   args=[qr.registry.code, patient_id])

    except PatientCreatorError as pce:
        message = "Error creating patient: %s.Patient not created" % pce
        status = "fail"

    except Exception as ex:
        message = "Unhandled error during patient creation: %s. Patient not created" % ex
        status = "fail"

    return {
        "status": status,
        "message": message,
        "patient_id": patient_id,
        "patient_name": "%s" % created_patient,
        "patient_link": patient_link,
        "patient_blurb": patient_blurb
    }
Exemplo n.º 4
0
 def _get_registry_specific_data(self, patient_model):
     if patient_model is None:
         return {}
     mongo_wrapper = DynamicDataWrapper(patient_model)
     return mongo_wrapper.load_registry_specific_data(self.registry_model)
Exemplo n.º 5
0
def get_nested_clinical_data(registry_code, patient_id, context_id):
    patient_model = Patient.objects.get(pk=patient_id)
    wrapper = DynamicDataWrapper(patient_model, rdrf_context_id=context_id)
    return wrapper.load_dynamic_data(registry_code, "cdes", flattened=False)
Exemplo n.º 6
0
 def _save_registry_specific_data_in_mongo(self, patient):
     data = patient.mongo_patient_data
     mongo_wrapper = DynamicDataWrapper(patient)
     mongo_wrapper.save_registry_specific_data(data)
Exemplo n.º 7
0
 def _get_patient_record(self, patient, collection="cdes"):
     wrapper = DynamicDataWrapper(patient)
     return wrapper.load_dynamic_data(self.registry_model.code,
                                      collection,
                                      flattened=False)
 def __init__(self, registry_model, patient_model):
     self.registry_model = registry_model
     self.patient_model = patient_model
     self.mongo_wrapper = DynamicDataWrapper(self.patient_model)
class RegistrySpecificFieldsHandler(object):
    def __init__(self, registry_model, patient_model):
        self.registry_model = registry_model
        self.patient_model = patient_model
        self.mongo_wrapper = DynamicDataWrapper(self.patient_model)

    def save_registry_specific_data_in_mongo(self, request):
        if self.registry_model.patient_fields and self.allowed_to_write_data():
            mongo_patient_data = {self.registry_model.code: {}}
            for cde_model, field_object in self.registry_model.patient_fields:
                if not cde_model.datatype == "file":
                    try:
                        field_value = request.POST[cde_model.code]
                        mongo_patient_data[self.registry_model.code][
                            cde_model.code] = field_value
                    except MultiValueDictKeyError:
                        continue
                else:
                    form_value = self._get_file_form_value(cde_model, request)
                    if form_value == FileCommand.PRESERVE:
                        # preserve existing value
                        existing_data = self.get_registry_specific_data()
                        if existing_data and self.registry_model.code in existing_data:
                            data = existing_data[self.registry_model.code][
                                cde_model.code]
                            if data:
                                form_value = data
                            else:
                                form_value = {}
                        else:
                            form_value = {}

                    elif form_value == FileCommand.DELETE:
                        form_value = {}

                    processed_value = self._process_file_cde_value(
                        cde_model, form_value)
                    mongo_patient_data[self.registry_model.code][
                        cde_model.code] = processed_value

            self.mongo_wrapper.save_registry_specific_data(mongo_patient_data)

    def allowed_to_write_data(self):
        if self.registry_model.has_feature("family_linkage"):
            return self.patient_model.is_index
        else:
            return True

    def _delete_existing_file_in_fs(self, file_cde_model):
        existing_data = self.get_registry_specific_data()
        file_upload_wrapper = existing_data[self.registry_model.code][
            file_cde_model.code]
        filestorage.delete_file_wrapper(file_upload_wrapper)

    def _process_file_cde_value(self, file_cde_model, form_value):
        if is_uploaded_file(form_value):
            return filestorage.store_file(self.registry_model.code,
                                          file_cde_model.code,
                                          form_value,
                                          form_name="reg_spec",
                                          section_code="reg_spec")
        else:
            return form_value

    def _get_file_form_value(self, file_cde_model, request):
        clear_key = file_cde_model.code + "-clear"
        if file_cde_model.code in request.FILES:
            in_memory_uploaded_file = request.FILES[file_cde_model.code]
            return in_memory_uploaded_file

        elif clear_key in request.POST:
            clear_value = request.POST[clear_key]
            if clear_value == "on":
                return FileCommand.DELETE

        elif file_cde_model.code in request.POST:
            posted_value = request.POST[file_cde_model.code]
            if posted_value == "":
                return FileCommand.PRESERVE
            return posted_value

        else:
            raise Exception("file cde not found")

    def get_registry_specific_fields(self, user):
        if user.is_superuser:
            fields = self.registry_model.patient_fields
            return fields

        if self.registry_model not in user.registry.all():
            return []
        else:
            return self.registry_model.patient_fields

    def create_registry_specific_patient_form_class(self, user, form_class):
        additional_fields = OrderedDict()
        field_pairs = self.get_registry_specific_fields(user)
        if not field_pairs:
            return form_class

        for cde, field_object in field_pairs:
            additional_fields[cde.code] = field_object

        new_form_class = type(form_class.__name__, (form_class, ),
                              additional_fields)
        return new_form_class

    def get_registry_specific_section_fields(self, user):
        field_pairs = self.get_registry_specific_fields(user)
        fieldset_title = self.registry_model.specific_fields_section_title
        field_list = [pair[0].code for pair in field_pairs]
        return fieldset_title, field_list

    # loading
    def get_registry_specific_data(self):
        if self.patient_model is None:
            return {}
        data = self.mongo_wrapper.load_registry_specific_data(
            self.registry_model)
        return data