Example #1
0
    def verification_triples(self):
        logger.debug("ver triples ..")
        if self.form:
            logger.debug("form set ...")
            if self.section:
                logger.debug("section set ...")
                if self.fields:
                    codes = [x.strip() for x in self.fields.split(",")]
                    cde_models = [
                        cde_model
                        for cde_model in get_normal_fields(self.section)
                        if cde_model.code in codes
                    ]
                else:
                    cde_models = get_normal_fields(self.section.cde_models)

                for cde_model in cde_models:
                    logger.debug(
                        "yielding %s %s %s" %
                        (self.form.name, self.section.code, cde_model.code))
                    yield self.form, self.section, cde_model
            else:
                for section_model in self.form.section_models:
                    for cde_model in get_normal_fields(section_model):
                        logger.debug("yielding %s %s %s" %
                                     (self.form.name, section_model.code,
                                      cde_model.code))
                        yield self.form, section_model, cde_model
        else:
            logger.debug("returning []")
            return []  # ??
Example #2
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 #3
0
    def verification_triples(self):
        if self.form:
            if self.section:
                if self.fields:
                    codes = [x.strip() for x in self.fields.split(",")]
                    cde_models = [
                        cde_model
                        for cde_model in get_normal_fields(self.section)
                        if cde_model.code in codes
                    ]
                else:
                    cde_models = get_normal_fields(self.section.cde_models)

                for cde_model in cde_models:
                    yield self.form, self.section, cde_model
            else:
                for section_model in self.form.section_models:
                    for cde_model in get_normal_fields(section_model):
                        yield self.form, section_model, cde_model
        else:
            return []  # ??
Example #4
0
    def generate_fields_from_section(self,
                                     form_model,
                                     section_model,
                                     include_verification=False):
        if section_model is None:
            return {}
        d = {}
        for cde_model in get_normal_fields(section_model):
            field_name, field = self.create_cde_field(
                (form_model, section_model, cde_model))
            field.rdrf_tag = FieldTags.DATA_ENTRY
            if include_verification:
                ver_field_name, ver_field = self.generate_verification_field(
                    field_name)
                d[ver_field_name] = ver_field

            d.update({field_name: field})
        return d
Example #5
0
    def _update_section_data(self, patient_model, context_model, form_data,
                             user):
        logger.debug("updating section data for %s ..." % self.code)
        registry_model = self.review.registry
        if not registry_model.has_feature("contexts"):
            # set_form_value requires this
            # default context is determined in method..
            context_to_use = None
        else:
            context_to_use = context_model

        registry_code = registry_model.code
        form_model = self.form
        form_name = form_model.name
        section_model = self.section
        section_code = section_model.code
        codes = [cde.code for cde in get_normal_fields(section_model)]
        error_msg = "Bad field in %s" % self.code
        for field_id in form_data:
            if field_id.startswith("metadata_"):
                # bookkeeping field not part of section
                continue
            logger.debug("updating %s" % field_id)
            field_form_model, field_section_model, field_cde_model = models_from_mongo_key(
                registry_model, field_id)
            if field_form_model.name != form_model.name:
                raise ValidationError(error_msg)
            if field_section_model.code != section_model.code:
                raise ValidationError(error_msg)
            if field_cde_model.code not in codes:
                raise ValidationError(error_msg)

            cde_code = field_cde_model.code
            answer = form_data[field_id]
            patient_model.set_form_value(registry_code,
                                         form_name,
                                         section_code,
                                         cde_code,
                                         answer,
                                         context_to_use,
                                         save_snapshot=True,
                                         user=user)
            logger.debug("%s.%s.%s set to %s" %
                         (form_name, section_code, cde_code, answer))
Example #6
0
 def section_iterator():
     for cde_model in get_normal_fields(self.section):
         yield cde_model
Example #7
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