def _parse(self): if not self.is_multisection: for key in self.form_data: if key == "timestamp": self.global_timestamp = self.form_data[key] elif key.endswith("_timestamp"): self.form_timestamps[key] = self.form_data[key] elif key == "custom_consent_data": self.custom_consents = self.form_data[key] elif key == "PatientDataAddressSection": self.address_data = self.form_data[key] elif is_delimited_key(key): if self.skip_bad_key is True: try: form_model, section_model, cde_model = models_from_mongo_key(self.registry_model, key) except BadKeyError: logger.debug(f"Skipping key: {key}") else: form_model, section_model, cde_model = models_from_mongo_key(self.registry_model, key) value = self.form_data[key] self.parsed_data[(form_model, section_model, cde_model)] = self._parse_value(value) else: # multisections extracted from the form like this (ugh): # the delimited keys will(should) always be cdes from the same form and section # {u'testmultisection': [ # {u'DELETE': False, u'testform____testmultisection____DM1FatigueTV': u'DM1FatigueDozingNever', # u'testform____testmultisection____DM1FatigueDrug': u'd1'}, # # {u'DELETE': False, u'testform____testmultisection____DM1FatigueTV': u'DM1FatigueDozingSlightChance', u'testform____testmultisection____DM1FatigueDrug': u'd2'}]} multisection_code = self._get_multisection_code() self._parse_multisection(multisection_code)
def get_mongo_value(registry_code, nested_data, delimited_key, multisection_index=None): """ Grabs a CDE value out of the mongo document. nested_data: mongo document dict delimited_key: form_name____section_code____cde_code """ registry_model = Registry.objects.get(code=registry_code) form_model, section_model, cde_model = models_from_mongo_key( registry_model, delimited_key) if multisection_index is None: sectionp = None else: def sectionp(s, ij): return ij[1] == multisection_index cdes = find_cdes(nested_data, form_model.name, section_model.code, cde_model.code, sectionp=sectionp, multisection=multisection_index is not None) for cde in cdes: return cde["value"] return None
def mk_ver(delimited_key): form_model, section_model, cde_model = models_from_mongo_key( registry_model, delimited_key) v = VerifiableCDE(registry_model, form_model=form_model, section_model=section_model, cde_model=cde_model) return v
def display_name(self, key): if is_delimited_key(key): try: form_model, section_model, cde_model = models_from_mongo_key( self.registry_model, key) except BadKeyError: logger.error("key %s refers to non-existant models" % key) return key human_name = "%s/%s/%s" % ( form_model.name, section_model.display_name, cde_model.name) return human_name else: return key
def display_value(self, key, mongo_value): # return the display value for ranges if is_delimited_key(key): try: form_model, section_model, cde_model = models_from_mongo_key( self.registry_model, key) except BadKeyError: logger.error("Key %s refers to non-existant models" % key) return mongo_value if cde_model.pv_group: # look up the stored code and return the display value range_dict = cde_model.pv_group.as_dict() for value_dict in range_dict["values"]: if mongo_value == value_dict["code"]: return value_dict["value"] return mongo_value
def _parse_all_forms(self): # used in questionnaire approval handling where all form data was being saved in one go # generated questionnaire gets fanned out to all forms for key in self.form_data: if key == "timestamp": self.global_timestamp = self.form_data[key] elif key.endswith("_timestamp"): self.form_timestamps[key] = self.form_data[key] elif key == "custom_consent_data": pass elif key == "PatientDataAddressSection": pass elif is_multisection(key): self._parse_multisection(key) elif is_delimited_key(key): form_model, section_model, cde_model = models_from_mongo_key(self.registry_model, key) value = self.form_data[key] self.parsed_data[(form_model, section_model, cde_model)] = self._parse_value(value)
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))
def _parse_multisection(self, multisection_code): self._parse_timestamps() the_form_model = None the_section_model = None multisection_item_list = self.form_data[multisection_code] if len(multisection_item_list) == 0: from rdrf.models.definition.models import Section section_model = Section.objects.get(code=multisection_code) self.parsed_multisections[(self.form_model, section_model)] = [] return items = [] for item_dict in multisection_item_list: if "DELETE" in item_dict and item_dict["DELETE"]: continue # rdrf #606 if not item_dict: continue item = [] for key in item_dict: if is_delimited_key(key): value = item_dict[key] form_model, section_model, cde_model = models_from_mongo_key( self.registry_model, key) if the_form_model is None: the_form_model = form_model if the_section_model is None: the_section_model = section_model value = self._parse_value(value) cde_dict = {"code": cde_model.code, "value": value} item.append(cde_dict) items.append(item) if the_form_model is None: # rdrf #606 # this can arise if a multisection is completely blanked out # no form model / no data return self.parsed_multisections[(the_form_model, the_section_model)] = items
def store_file_by_key(registry_code, patient_record, key, file_obj): registry = Registry.objects.get(code=registry_code) form, section, cde = models_from_mongo_key(registry, key) return store_file(registry_code, cde.code, file_obj, form.name, section.code)