def set_context(self): if self.patient is not None: patient_contexts = self.patient.context_models if len(patient_contexts) == 1: self.default_context_model = patient_contexts[0] elif len(patient_contexts) == 0: from rdrf.db.contexts_api import RDRFContextManager self.patient.save() manager = RDRFContextManager(self.registry) default_context = manager.get_or_create_default_context(self.patient) self.default_context_model = default_context else: self.default_context_model = None # ???
def set_parameters(self, request): self.user = request.user self.registry_code = request.GET.get("registry_code") self.registry_model = get_object_or_404(Registry, code=self.registry_code) self.clinicians_have_patients = self.registry_model.has_feature( "clinicians_have_patients") self.form_progress = FormProgress(self.registry_model) self.supports_contexts = self.registry_model.has_feature("contexts") self.rdrf_context_manager = RDRFContextManager(self.registry_model) def getint(param): try: return int(request.POST.get(param) or 0) except ValueError: return 0 self.search_term = request.POST.get("search[value]") or "" self.draw = getint("draw") # unknown self.start = getint("start") # offset self.length = getint("length") # page size self.page_number = ( (self.start / self.length) if self.length else 0) + 1 self.sort_field, self.sort_direction = self.get_ordering(request) self.columns = self.get_configure_columns()
def get(self, request, registry_code, context_id=None): context = {} if request.user.is_authenticated(): parent = ParentGuardian.objects.get(user=request.user) registry = Registry.objects.get(code=registry_code) self.registry = registry self.rdrf_context_manager = RDRFContextManager(self.registry) patients_objects = parent.patient.all() patients = [] forms_objects = RegistryForm.objects.filter( registry=registry).order_by('position') progress = form_progress.FormProgress(registry) for patient in patients_objects: forms = [] for form in forms_objects: forms.append({ "form": form, "progress": progress.get_form_progress(form, patient), "current": progress.get_form_currency(form, patient), "readonly": request.user.has_perm("rdrf.form_%s_is_readonly" % form.id) }) self.set_rdrf_context(patient, context_id) patients.append({ "patient": patient, "consent": consent_status_for_patient(registry_code, patient), "context_id": self.rdrf_context.pk, "forms": forms }) context['parent'] = parent context['patients'] = patients context['registry_code'] = registry_code self.set_rdrf_context(parent, context_id) context['context_id'] = self.rdrf_context.pk return render(request, 'rdrf_cdes/parent.html', context)
def _get_initial_context(self, registry_code, patient_model): from rdrf.models.definition.models import Registry registry_model = Registry.objects.get(code=registry_code) rdrf_context_manager = RDRFContextManager(registry_model) return rdrf_context_manager.get_or_create_default_context( patient_model, new_patient=True)
def set_value(self, patient_model, mongo_record, new_value, **kwargs): from datetime import datetime from rdrf.db.contexts_api import RDRFContextManager context_id = kwargs.get("context_id", None) if context_id is None: context_manager = RDRFContextManager(self.registry_model) default_context_model = context_manager.get_or_create_default_context( patient_model) context_id = default_context_model.pk if not self.section_model.allow_multiple: if mongo_record is None: # create a new blank record section_dict = { "code": self.section_model.code, "cdes": [{ "code": self.cde_model.code, "value": new_value }], "allow_multiple": False } form_timestamp_key = "%s_timestamp" % self.form_model.name form_timestamp_value = datetime.now() form_dict = { "name": self.form_model.name, form_timestamp_key: form_timestamp_value, "sections": [section_dict] } mongo_record = { "forms": [form_dict], "django_id": patient_model.pk, "django_model": "Patient", "context_id": context_id } return patient_model, mongo_record else: form_exists = False section_exists = False cde_exists = False forms = mongo_record["forms"] for form_dict in forms: if form_dict["name"] == self.form_model.name: form_exists = True for section_dict in form_dict["sections"]: if section_dict["code"] == self.section_model.code: section_exists = True for cde_dict in section_dict["cdes"]: if cde_dict["code"] == self.cde_model.code: cde_exists = True cde_dict["value"] = new_value if not cde_exists: cde_dict = { "code": self.cde_model.code, "value": new_value } section_dict["cdes"].append(cde_dict) if not section_exists: section_dict = { "code": self.section_model.code, "allow_multiple": False, "cdes": [{ "code": self.cde_model.code, "value": new_value }] } form_dict["sections"].append(section_dict) if not form_exists: form_timestamp_key = "%s_timestamp" form_timestamp_value = datetime.now() form_dict = { "name": self.form_model.name, "sections": [{ "code": self.section_model.code, "allow_multiple": False, "cdes": [{ "code": self.cde_model.code, "value": new_value }] }], form_timestamp_key: form_timestamp_value } mongo_record["forms"].append(form_dict) return patient_model, mongo_record else: # todo - NB multisection items are being replaced as a whole # in the questionnaire return patient_model, mongo_record
def num_contexts(patient_model, registry_model): return len([ c for c in patient_model.context_models if c.registry.code == registry_model.code ]) if __name__ == "__main__": registry_code = sys.argv[1] try: registry_model = Registry.objects.get(code=registry_code) except Registry.DoesNotExist: print("Registry %s does not exist on site - aborting" % registry_code) sys.exit(1) rdrf_context_manager = RDRFContextManager(registry_model) errors = processed = 0 for p in Patient.objects.filter(rdrf_registry__in=[registry_model]): if num_contexts(p, registry_model) == 0: print("%s has no default context - creating one" % display(p)) try: default_context = rdrf_context_manager.create_initial_context_for_new_patient( p) print("created context OK for %s" % display(p)) processed += 1 except Exception as ex: print("Error creating default context for %s: %s " % (display(p), ex)) errors += 1