Example #1
0
    def _get_section_data(self, patient_model, context_model, raw=False):
        # we need raw values for initial data
        # display values for the read only
        assert not self.section.allow_multiple

        pairs = []
        data = patient_model.get_dynamic_data(self.review.registry,
                                              collection="cdes",
                                              context_id=context_model.pk,
                                              flattened=True)

        def get_field_value(cde_model):
            # closure to make things easier ...
            # this assumes the section in form in registry selected ..
            # ( we should enforce this as a validation rule )
            # the data is passed in once to avoid reloading multiple
            # times
            raw_value = patient_model.get_form_value(self.review.registry.code,
                                                     self.form.name,
                                                     self.section.code,
                                                     cde_model.code, False,
                                                     context_model.pk, data)
            if raw:
                return raw_value
            else:
                return cde_model.get_display_value(raw_value)

        if raw:
            # return a dictionary
            d = {}
            for cde_model in get_normal_fields(self.section):
                delimited_key = mongo_key_from_models(self.form, self.section,
                                                      cde_model)
                try:
                    raw_value = get_field_value(cde_model)
                    d[delimited_key] = raw_value
                except KeyError:
                    pass

            return d

        # if not raw return a list of pairs of display values

        for cde_model in get_normal_fields(self.section):
            if raw:
                field = cde_model.code
            else:
                field = cde_model.name
            try:
                value = get_field_value(cde_model)
            except KeyError:
                if raw:
                    value = Missing.VALUE
                else:
                    value = Missing.DISPLAY_VALUE

            pairs.append((field, value))

        return pairs
Example #2
0
def _get_non_multiple_mongo_keys(registry_model):
    # return a list of delimited mongo keys for the supplied registry
    # skip the generated questionnaire ( the data from which is copied to the target clinical forms anyway)
    # skip multisections  as these are handled separately
    delimited_keys = []
    from rdrf.helpers.utils import mongo_key_from_models
    for form_model in registry_model.forms:
        if not form_model.is_questionnaire:
            for section_model in form_model.section_models:
                if not section_model.allow_multiple:
                    for cde_model in section_model.cde_models:
                        delimited_key = mongo_key_from_models(
                            form_model, section_model, cde_model)
                        delimited_keys.append(delimited_key)
    return delimited_keys
Example #3
0
    def create_cde_field(self, spec):
        if isinstance(spec, tuple):
            form_model, section_model, cde_model = spec
            field_label = cde_model.name
            if section_model is None:
                section_model = self._deduce_section(form_model, cde_model)
                if section_model is None:
                    raise Exception("cannot deduce section for %s %s" %
                                    (form_model.name, cde_model.code))
            field_name = mongo_key_from_models(form_model, section_model,
                                               cde_model)
        elif isinstance(spec, dict):
            field_dict = spec
            field_name = field_dict["name"]
            field_label = field_dict["label"]
            target_dict = field_dict["target"]

            form_name = target_dict["form"]
            form_model = RegistryForm.objects.get(registry=self.registry_model,
                                                  name=form_name)
            cde_code = target_dict["cde"]
            cde_model = CommonDataElement.objects.get(code=cde_code)

            section_code = target_dict["section"]
            section_model = Section.objects.get(code=section_code)
        else:
            raise Exception("unknown spec: %s" % spec)

        cde_model.is_required = False

        field_factory = FieldFactory(self.registry_model, form_model,
                                     section_model, cde_model)

        field = field_factory.create_field()
        field.rdrf_tag = FieldTags.DATA_ENTRY

        field.label = field_label

        return field_name, field
Example #4
0
 def delimited_key(self):
     from rdrf.helpers.utils import mongo_key_from_models
     return mongo_key_from_models(self.form_model, self.section_model,
                                  self.cde_model)
Example #5
0
 def display_value2(self, form_model, section_model, cde_model,
                    mongo_value):
     mongo_key = mongo_key_from_models(form_model, section_model, cde_model)
     return self.display_value(mongo_key, mongo_value)
Example #6
0
    def _get_section_data(self,
                          patient_model,
                          context_model,
                          raw=False,
                          use_fields=False):
        # we need raw values for initial data
        # display values for the read only
        if self.section:
            assert not self.section.allow_multiple

        pairs = []
        data = patient_model.get_dynamic_data(self.review.registry,
                                              collection="cdes",
                                              context_id=context_model.pk,
                                              flattened=True)
        if raw:
            if use_fields:
                allowed_cde_codes = [
                    x.strip() for x in self.fields.strip().split(",")
                ]

                def get_fields_iterator():
                    for cde_model in self.section.cde_models:
                        if cde_model.code in allowed_cde_codes:
                            yield cde_model

                def get_fields_from_anywhere():
                    for section_model in self.form.section_models:
                        if not section_model.allow_multiple:
                            for cde_model in section_model.cde_models:
                                if cde_model.code in allowed_cde_codes:
                                    yield self.form, section_model, cde_model

                if self.section:
                    cde_iterator = get_fields_iterator
                else:
                    cde_iterator = get_fields_from_anywhere
            else:

                def section_iterator():
                    for cde_model in get_normal_fields(self.section):
                        yield cde_model

                cde_iterator = section_iterator

            # return a dictionary
            d = {}
            for thing in cde_iterator():
                if type(thing) is tuple:
                    form_model, section_model, cde_model = thing
                else:
                    form_model = self.form
                    section_model = self.section
                    cde_model = thing

                delimited_key = mongo_key_from_models(form_model,
                                                      section_model, cde_model)
                try:
                    raw_value = get_field_value(patient_model,
                                                self.review.registry,
                                                context_model, form_model,
                                                section_model, cde_model, data,
                                                raw)
                    d[delimited_key] = raw_value
                except KeyError:
                    pass

            return d

        # if not raw return a list of pairs of display values

        for cde_model in get_normal_fields(self.section):
            if raw:
                field = cde_model.code
            else:
                field = cde_model.name
            try:
                value = get_field_value(patient_model, self.review.registry,
                                        context_model, self.form, self.section,
                                        cde_model, data, raw)
            except KeyError:
                if raw:
                    value = Missing.VALUE
                else:
                    value = Missing.DISPLAY_VALUE

            pairs.append((field, value))

        return pairs