def __init__(self, report, case_dict): next_visit = VISIT_SCHEDULE[0] last_inter = None for action in case_dict['actions']: if action['xform_xmlns'] in LAST_INTERACTION_LIST: last_inter = action for visit_key, visit in enumerate(VISIT_SCHEDULE): for key, action in enumerate(case_dict['actions']): if visit['xmlns'] == action['xform_xmlns']: try: next_visit = VISIT_SCHEDULE[visit_key + 1] del case_dict['actions'][key] break except IndexError: next_visit = 'last' self.next_visit = next_visit if last_inter: self.last_interaction = last_inter['date'] self.app_dict = get_cloudcare_app(report.domain, SUCCEED_CM_APPNAME) self.latest_build = ApplicationBase.get_latest_build( report.domain, self.app_dict['_id'])['_id'] super(PatientListReportDisplay, self).__init__(report, case_dict) self.update_target_date_case_properties()
def get_app_build(app_dict): domain = Domain.get_by_name(app_dict["domain"]) if domain.use_cloudcare_releases: return ApplicationBase.get(app_dict["_id"]).get_latest_app()["_id"] else: return ApplicationBase.get_latest_build(app_dict["domain"], app_dict["_id"])["_id"] return None
def get_app_build(app_dict): domain = Domain._get_by_name(app_dict['domain']) if domain.use_cloudcare_releases: return ApplicationBase.get(app_dict['_id']).get_latest_app()['_id'] else: return ApplicationBase.get_latest_build(app_dict['domain'], app_dict['_id'])['_id'] return None
def get_cloudcare_app(): """ Total hack function to get direct links to the cloud care application pages """ from corehq.apps.cloudcare import api app = api.get_cloudcare_app(PACT_DOMAIN, PACT_CLOUD_APPNAME) app_id = app['_id'] pact_cloudcare = filter(lambda x: x['name']['en'] == PACT_CLOUDCARE_MODULE, app['modules']) forms = pact_cloudcare[0]['forms'] ret = dict((f['name']['en'], ix) for (ix, f) in enumerate(forms)) url_root = '/a/%(domain)s/cloudcare/apps/view/%(build_id)s/%(module_id)s/%(form_id)s/case/%(case_id)s/enter/' ret['url_root'] = url_root ret['domain'] = PACT_DOMAIN ret['app_id'] = app_id latest_build = ApplicationBase.get_latest_build(PACT_DOMAIN, app_id) if latest_build is not None: latest_build_id = latest_build['_id'] ret['build_id'] = latest_build_id ret['module_id'] = 0 return ret
def get_app_build(app_dict): domain = Domain.get_by_name(app_dict['domain']) if domain.use_cloudcare_releases: return ApplicationBase.get(app_dict['_id']).get_latest_app()['_id'] else: return ApplicationBase.get_latest_build(app_dict['domain'], app_dict['_id'])['_id'] return None
def __init__(self, report, case_dict): next_visit = VISIT_SCHEDULE[0] last_inter = None for action in case_dict['actions']: if action['xform_xmlns'] in LAST_INTERACTION_LIST: last_inter = action for visit_key, visit in enumerate(VISIT_SCHEDULE): for key, action in enumerate(case_dict['actions']): if visit['xmlns'] == action['xform_xmlns']: try: next_visit = VISIT_SCHEDULE[visit_key + 1] del case_dict['actions'][key] break except IndexError: next_visit = 'last' self.next_visit = next_visit if last_inter: self.last_interaction = last_inter['date'] self.app_dict = get_cloudcare_app(report.domain, SUCCEED_CM_APPNAME) self.latest_build = ApplicationBase.get_latest_build(report.domain, self.app_dict['_id'])['_id'] super(PatientListReportDisplay, self).__init__(report, case_dict) self.update_target_date_case_properties()
def get_cloudcare_app(): """ Total hack function to get direct links to the cloud care application pages """ from corehq.apps.cloudcare import api app = api.get_cloudcare_app(PACT_DOMAIN, PACT_CLOUD_APPNAME) app_id = app["_id"] pact_cloudcare = filter(lambda x: x["name"]["en"] == PACT_CLOUDCARE_MODULE, app["modules"]) forms = pact_cloudcare[0]["forms"] ret = dict((f["name"]["en"], ix) for (ix, f) in enumerate(forms)) url_root = "/a/%(domain)s/cloudcare/apps/view/%(build_id)s/%(module_id)s/%(form_id)s/case/%(case_id)s/enter/" ret["url_root"] = url_root ret["domain"] = PACT_DOMAIN ret["app_id"] = app_id latest_build = ApplicationBase.get_latest_build(PACT_DOMAIN, app_id) if latest_build is not None: latest_build_id = latest_build["_id"] ret["build_id"] = latest_build_id ret["module_id"] = 0 return ret
def cloudcare_main(request, domain, urlPath): try: preview = string_to_boolean(request.REQUEST.get("preview", "false")) except ValueError: # this is typically only set at all if it's intended to be true so this # is a reasonable default for "something went wrong" preview = True app_access = ApplicationAccess.get_by_domain(domain) if not preview: apps = get_cloudcare_apps(domain) if request.project.use_cloudcare_releases: # replace the apps with the last starred build of each app, removing the ones that aren't starred apps = filter(lambda app: app.is_released, [get_app(domain, app['_id'], latest=True) for app in apps]) # convert to json apps = [get_app_json(app) for app in apps] else: # legacy functionality - use the latest build regardless of stars apps = [get_app_json(ApplicationBase.get_latest_build(domain, app['_id'])) for app in apps] else: apps = ApplicationBase.view('app_manager/applications_brief', startkey=[domain], endkey=[domain, {}]) apps = [get_app_json(app) for app in apps if app and app.application_version == V2] # trim out empty apps apps = filter(lambda app: app, apps) apps = filter(lambda app: app_access.user_can_access_app(request.couch_user, app), apps) def _default_lang(): if apps: # unfortunately we have to go back to the DB to find this return Application.get(apps[0]["_id"]).build_langs[0] else: return "en" # default language to user's preference, followed by # first app's default, followed by english language = request.couch_user.language or _default_lang() def _url_context(): # given a url path, returns potentially the app and case, if they're # selected. the front end optimizes with these to avoid excess server # calls # there's an annoying dependency between this logic and backbone's # url routing that seems hard to solve well. this needs to be synced # with apps.js if anything changes # for apps anything with "view/app/" works # for cases it will be: # "view/:app/:module/:form/case/:case/" # could use regex here but this is actually simpler with the potential # absence of a trailing slash split = urlPath.split('/') app_id = split[1] if len(split) >= 2 else None case_id = split[5] if len(split) >= 6 else None app = None if app_id: if app_id in [a['_id'] for a in apps]: app = look_up_app_json(domain, app_id) else: messages.info(request, _("That app is no longer valid. Try using the " "navigation links to select an app.")) if app is None and len(apps) == 1: app = look_up_app_json(domain, apps[0]['_id']) def _get_case(domain, case_id): case = CommCareCase.get(case_id) assert case.domain == domain, "case %s not in %s" % (case_id, domain) return case.get_json() case = _get_case(domain, case_id) if case_id else None return { "app": app, "case": case } context = { "domain": domain, "language": language, "apps": apps, "apps_raw": apps, "preview": preview, "maps_api_key": settings.GMAPS_API_KEY, 'offline_enabled': toggles.OFFLINE_CLOUDCARE.enabled(request.user.username), 'sessions_enabled': request.couch_user.is_commcare_user() } context.update(_url_context()) return render(request, "cloudcare/cloudcare_home.html", context)
def cloudcare_main(request, domain, urlPath): try: preview = string_to_boolean(request.REQUEST.get("preview", "false")) except ValueError: # this is typically only set at all if it's intended to be true so this # is a reasonable default for "something went wrong" preview = True app_access = ApplicationAccess.get_by_domain(domain) if not preview: apps = get_cloudcare_apps(domain) if request.project.use_cloudcare_releases: # replace the apps with the last starred build of each app, removing the ones that aren't starred apps = filter( lambda app: app.is_released, [get_app(domain, app['_id'], latest=True) for app in apps]) # convert to json apps = [get_app_json(app) for app in apps] else: # legacy functionality - use the latest build regardless of stars apps = [ get_app_json( ApplicationBase.get_latest_build(domain, app['_id'])) for app in apps ] else: apps = ApplicationBase.view('app_manager/applications_brief', startkey=[domain], endkey=[domain, {}]) apps = [ get_app_json(app) for app in apps if app and app.application_version == V2 ] # trim out empty apps apps = filter(lambda app: app, apps) apps = filter( lambda app: app_access.user_can_access_app(request.couch_user, app), apps) def _default_lang(): if apps: # unfortunately we have to go back to the DB to find this return Application.get(apps[0]["_id"]).build_langs[0] else: return "en" # default language to user's preference, followed by # first app's default, followed by english language = request.couch_user.language or _default_lang() def _url_context(): # given a url path, returns potentially the app, parent, and case, if # they're selected. the front end optimizes with these to avoid excess # server calls # there's an annoying dependency between this logic and backbone's # url routing that seems hard to solve well. this needs to be synced # with apps.js if anything changes # for apps anything with "view/app/" works # for cases it will be: # "view/:app/:module/:form/case/:case/" # if there are parent cases, it will be: # "view/:app/:module/:form/parent/:parent/case/:case/ # could use regex here but this is actually simpler with the potential # absence of a trailing slash split = urlPath.split('/') app_id = split[1] if len(split) >= 2 else None if len(split) >= 5 and split[4] == "parent": parent_id = split[5] case_id = split[7] if len(split) >= 7 else None else: parent_id = None case_id = split[5] if len(split) >= 6 else None app = None if app_id: if app_id in [a['_id'] for a in apps]: app = look_up_app_json(domain, app_id) else: messages.info( request, _("That app is no longer valid. Try using the " "navigation links to select an app.")) if app is None and len(apps) == 1: app = look_up_app_json(domain, apps[0]['_id']) def _get_case(domain, case_id): case = CommCareCase.get(case_id) assert case.domain == domain, "case %s not in %s" % (case_id, domain) return case.get_json() case = _get_case(domain, case_id) if case_id else None if parent_id is None and case is not None: parent_id = case.get('indices', {}).get('parent', {}).get('case_id', None)
def report_context(self): ret = {} try: case = self.get_case() has_error = False except ResourceNotFound: has_error = True case = None if case is None: self.report_template_path = "error.html" if has_error: ret['error_message'] = "Patient not found" else: ret['error_message'] = "No patient selected" return ret def get_form_url(app_dict, app_build_id, module_idx, form, case_id=None): try: module = app_dict['modules'][module_idx] form_idx = [ix for (ix, f) in enumerate(module['forms']) if f['xmlns'] == form][0] except IndexError: form_idx = None return html.escape(get_cloudcare_form_url(domain=self.domain, app_build_id=app_build_id, module_id=module_idx, form_id=form_idx, case_id=case_id)) try: cm_app_dict = get_cloudcare_app(case['domain'], SUCCEED_CM_APPNAME) latest_cm_build = ApplicationBase.get_latest_build(case['domain'], cm_app_dict['_id'])['_id'] pm_app_dict = get_cloudcare_app(case['domain'], SUCCEED_PM_APPNAME) latest_pm_build = ApplicationBase.get_latest_build(case['domain'], pm_app_dict['_id'])['_id'] chw_app_dict = get_cloudcare_app(case['domain'], SUCCEED_CHW_APPNAME) latest_chw_build = ApplicationBase.get_latest_build(case['domain'], chw_app_dict['_id'])['_id'] except ResourceNotFound as ex: self.report_template_path = "error.html" ret['error_message'] = ex.message return ret ret['patient'] = case ret['root_url'] = '?patient_id=%s' % case['_id'] ret['view_mode'] = self.view_mode ret['patient_status_access'] = self.patient_status_access if self.view_mode == 'info': self.report_template_path = "patient_info.html" patient_info = PatientInfoDisplay(case) # check user role: user = self.request.couch_user if is_pm_or_pi(user): ret['edit_patient_info_url'] = get_form_url(pm_app_dict, latest_pm_build, PM_APP_PM_MODULE, PM2) elif is_cm(user): ret['edit_patient_info_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_PD_MODULE, PM2) elif is_chw(user): ret['edit_patient_info_url'] = get_form_url(chw_app_dict, latest_chw_build, CHW_APP_PD_MODULE, PM2) if is_pm_or_pi(user): ret['upcoming_appointments_url'] = get_form_url(pm_app_dict, latest_pm_build, PM_APP_PM_MODULE, PM2) elif is_cm(user): ret['upcoming_appointments_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_PD_MODULE, PM2) elif is_chw(user): ret['upcoming_appointments_url'] = get_form_url(chw_app_dict, latest_chw_build, CHW_APP_MA_MODULE, PM2) ret['general_information'] = patient_info.general_information ret['contact_information'] = patient_info.contact_information ret['most_recent_lab_exams'] = patient_info.most_recent_lab_exams ret['allergies'] = patient_info.allergies elif self.view_mode == 'submissions': if self.submission_user_access: tabular_context = super(PatientInfoReport, self).report_context tabular_context.update(ret) self.report_template_path = "patient_submissions.html" tabular_context['patient_id'] = self.request_params['patient_id'] return tabular_context else: self.report_template_path = "error.html" ret['error_message'] = "Cannot access report(incorrect user role)" return ret elif self.view_mode == 'interactions': self.report_template_path = "patient_interactions.html" ret['problem_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_PD_MODULE, PD1) ret['huddle_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_HUD_MODULE, HUD2) ret['cm_phone_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_CM_MODULE, CM6) ret['chw_phone_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_CHW_MODULE, CHW3) ret['cm_visits_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_APPOINTMENTS_MODULE, AP2) ret['anti_thrombotic_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_MEDICATIONS_MODULE, PD2AM) ret['blood_pressure_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_MEDICATIONS_MODULE, PD2BPM) ret['cholesterol_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_MEDICATIONS_MODULE, PD2CHM) ret['depression_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_MEDICATIONS_MODULE, PD2DIABM) ret['diabetes_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_MEDICATIONS_MODULE, PD2DEPM) ret['smoking_cessation_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_MEDICATIONS_MODULE, PD2SCM) ret['other_meds_url'] = get_form_url(cm_app_dict, latest_cm_build, CM_APP_MEDICATIONS_MODULE, PD2OM) ret['interaction_table'] = [] for visit_key, visit in enumerate(VISIT_SCHEDULE): if case["randomization_date"]: target_date = (case["randomization_date"] + timedelta(days=visit['days'])).strftime(OUTPUT_DATE_FORMAT) else: target_date = EMPTY_FIELD interaction = { 'url': '', 'name': visit['visit_name'], 'target_date': target_date, 'received_date': EMPTY_FIELD, 'completed_by': EMPTY_FIELD, 'scheduled_date': EMPTY_FIELD } for key, action in enumerate(case['actions']): if visit['xmlns'] == action['xform_xmlns']: interaction['received_date'] = action['date'].strftime(INTERACTION_OUTPUT_DATE_FORMAT) try: user = CouchUser.get(action['user_id']) interaction['completed_by'] = user.raw_username except ResourceNotFound: interaction['completed_by'] = EMPTY_FIELD del case['actions'][key] break if visit['show_button']: interaction['url'] = get_form_url(cm_app_dict, latest_cm_build, visit['module_idx'], visit['xmlns']) if 'scheduled_source' in visit and case.get_case_property(visit['scheduled_source']): interaction['scheduled_date'] = (case.get_case_property(visit['scheduled_source'])).strftime(INTERACTION_OUTPUT_DATE_FORMAT) ret['interaction_table'].append(interaction) medication = [] for med_prop in MEDICATION_DETAILS: medication.append(getattr(case, med_prop, EMPTY_FIELD)) ret['medication_table'] = medication elif self.view_mode == 'plan': self.report_template_path = "patient_plan.html" elif self.view_mode == 'status': if self.patient_status_access: self.report_template_path = "patient_status.html" ret['disenroll_patient_url'] = get_form_url(pm_app_dict, latest_pm_build, PM_APP_PM_MODULE, PM3) ret['change_patient_data_url'] = get_form_url(pm_app_dict, latest_pm_build, PM_APP_PM_MODULE, PM4) else: self.report_template_path = "error.html" ret['error_message'] = "Only PMs can disenrollment participants" return ret else: raise Http404 return ret