def delete_defaulter_cohort(request): if not Authorize.authorize(request.user,['superuser']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') cohort_uuid = request.GET['cohort_uuid'] DefaulterCohort.delete_defaulter_cohort(cohort_uuid) return HttpResponseRedirect('/ltfu/manage_defaulter_cohorts')
def delete_report_table(request): if not Authorize.authorize(request.user, ["superuser"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") if "report_table_id" in request.GET: rt = ReportTable.objects.get(id=request.GET["report_table_id"]) rt.delete() return HttpResponseRedirect("/")
def create_report_table(request): if not Authorize.authorize(request.user, ["superuser"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") if request.method == "POST": name = get_var(request.POST, "name") report_table_sql = get_var(request.POST, "report_table_sql") description = get_var(request.POST, "description") report_table_id = get_var(request.POST, "report_table_id") if name and report_table_sql: if report_table_id: rt = ReportTable.objects.get(id=report_table_id) rt.name = name rt.description = description rt.report_table_sql = report_table_sql ReportTableParameter.objects.filter(report_table_id=rt.id).delete() else: rt = ReportTable(name=name, report_table_sql=report_table_sql, description=description) rt.save() count = report_table_sql.count("%s") rt.create_parameters(request.POST, count) return HttpResponseRedirect("/amrs_reports/manage_report_tables") else: report_table_id = get_var(request.GET, "report_table_id") rt = None parameters = [] if report_table_id: rt = ReportTable.objects.get(id=report_table_id) parameters = rt.get_parameters() return render(request, "amrs_reports/create_report_table.html", {"report_table": rt, "parameters": parameters})
def delete_defaulter_cohort(request): if not Authorize.authorize(request.user, ["superuser"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") cohort_uuid = request.GET["cohort_uuid"] DefaulterCohort.delete_defaulter_cohort(cohort_uuid) return HttpResponseRedirect("/ltfu/manage_defaulter_cohorts")
def ltfu_clinic(request): if not Authorize.authorize(request.user,['outreach_supervisor','outreach_all']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') location_id = get_var_from_request(request,'location_id') locations = Location().get_locations() location = Location().get_location(location_id) ltfu_date = get_var_from_request(request,'ltfu_date') if location_id : rt = ReportTable.objects.filter(name='ltfu_clinic')[0] parameter_values = (ltfu_date,location_id) table = rt.run_report_table(parameter_values=parameter_values,as_dict=True)['rows'] rt = ReportTable.objects.filter(name='ltfu_stats')[0] result = rt.run_report_table(as_dict=True) ltfu_stats = result['rows'] return render(request,'ltfu/ltfu_clinic.html', {'ltfu_clinic_table':table, 'locations':locations, 'location':location, 'ltfu_stats':ltfu_stats, } )
def ajax_get_defaulter_cohort(request): if not Authorize.authorize(request.user, ["outreach_supervisor", "outreach_all", "outreach_worker"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") defaulter_cohort_id = request.POST["defaulter_cohort_id"] dc = DefaulterCohort.objects.get(id=defaulter_cohort_id) data = request.session.get(dc.location_uuid, "") if data is not None and data != "": patients = json.loads(request.session.get(dc.location_uuid, "")) else: patients = dc.get_patients() request.session[dc.location_uuid] = json.dumps(patients, cls=DjangoJSONEncoder) d = { "name": dc.name, "date_created": dc.date_created, "patients": patients, "id": dc.id, "location_uuid": dc.location_uuid, "uuid": dc.uuid, } d = json.dumps(d, cls=DjangoJSONEncoder) return HttpResponse(d, content_type="application/json")
def view_reason_missed_appt(request): if not Authorize.authorize(request.user,['outreach_all']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') start_date = get_var_from_request(request,'start_date') if start_date is None : start_date = '2014-01-01' end_date = get_var_from_request(request,'end_date') if end_date is None : end_date = '2020-01-01' all_locations = 0 location_ids_str = get_var_from_request(request,'location_ids') if location_ids_str is None or location_ids_str == '()': location_ids = ('-1',) all_locations = 1 location_ids_str = '' else : location_ids = tuple(location_ids_str.split(',')) rt = ReportTable.objects.get(name='retention_reason_missed_appt_total') parameter_values = (start_date,end_date,all_locations,location_ids) rows = rt.run_report_table(parameter_values=parameter_values,as_dict=True)['rows'] times_asked = rows[0]['total'] rt = ReportTable.objects.get(name='retention_reason_missed_appt') parameter_values = (start_date,end_date,all_locations,location_ids) rows = rt.run_report_table(parameter_values=parameter_values,as_dict=True)['rows'] coded_reasons = {} concept_ids = [] for row in rows : codes = row['reason_missed_appt'].split(' // ') for code in codes: if code in coded_reasons : coded_reasons[code]['count'] += row['count'] else : coded_reasons[code] = {'count':row['count']} concept_ids.append(code) concept_ids = tuple(concept_ids) rt = ReportTable.objects.get(name='amrs_concept_name') parameter_values = (concept_ids,) rows = rt.run_report_table(parameter_values=parameter_values,as_dict=True)['rows'] reasons = [] for r in rows: reasons.append({'concept_id':r['concept_id'], 'name':r['name'], 'count':coded_reasons[str(r['concept_id'])]['count'] }) return render(request,'ltfu/reason_missed_appt.html', {'reason_missed_appt':reasons, 'start_date':start_date, 'end_date':end_date, 'location_ids': location_ids_str, 'times_asked': times_asked, } )
def view_datasets(request): if not Authorize.authorize(request.user,['admin']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') if request.method == 'POST' : pass else : pass
def outreach_dashboard(request): if not Authorize.authorize(request.user,['outreach_supervisor','outreach_all','outreach_worker']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') clinics = DefaulterCohort.get_outreach_locations() device = get_device(request) return render(request,'ltfu/outreach_dashboard_mobile.html',{'clinics':clinics, 'device':device,})
def outreach_clinic_dashboard(request): if not Authorize.authorize(request.user,['outreach_supervisor','outreach_all','outreach_worker']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') location = Location.get_location(location_uuid) defaulter_cohort = DefaulterCohort.objects.filter(location_uuid=location_uuid,retired=0)[0] device = get_device(request) return render(request,'ltfu/outreach_clinic_dashboard_mobile.html',{'location':location, 'defaulter_cohort':defaulter_cohort, 'device':device})
def index(request): if not Authorize.authorize(request.user): return HttpResponseRedirect("amrs_user_validation/access_denied") device = u.get_device(request) if device["is_mobile"]: return render(request, "amrs_reports/index_mobile.html", {}) else: return render(request, "amrs_reports/index.html", {})
def delete_report(request): if not Authorize.authorize(request.user, ["superuser"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") if "report_id" in request.GET: r = Report.objects.get(id=request.GET["report_id"]) ReportMember.objects.filter(report_id=r.id).delete() r.delete() return HttpResponseRedirect("/")
def view_data_entry_forms_done(request): if not Authorize.authorize(request.user,['superuser']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') rt = ReportTable.objects.filter(name='data_entry_forms_done')[0] table = rt.run_report_table(as_dict=True)['rows'] return render(request,'ltfu/forms_done.html', {'forms_done' : table, })
def manage_defaulter_cohorts(request): if not Authorize.authorize(request.user,['superuser']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') dcs = DefaulterCohort.objects.all() return render(request, 'ltfu/manage_defaulter_cohorts.html', {"defaulter_cohorts":dcs, } )
def ltfu_clinics(request): if not Authorize.authorize(request.user,['outreach_supervisor','outreach_all']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') rt = ReportTable.objects.filter(name='ltfu_clinics')[0] ltfu_clinics_table = rt.run_report_table(as_dict=True)['rows'] parameters = ReportTableParameter.objects.filter(report_table_id=rt.id) return render(request,"ltfu/ltfu_clinics.html", {'report_table':rt, 'ltfu_clinics_table':ltfu_clinics_table, 'parameters':parameters, })
def create_dataset(request): if not Authorize.authorize(request.user,['admin']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') if request.method == 'POST' : pass else : dataset_id = get_var_from_request(request,'dataset_id') d = None if dataset_id is not None : d = AmrsDataset.objects.filter(id=dataset_id) return render(request,'amrs_data_manager/create_dataset.html',{'dataset':d})
def outreach_worker_defaulter_list(request): if not Authorize.authorize(request.user,['outreach_all','outreach_manager','outreach_supervisor','outreach_worker']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') cohort_uuid = get_var_from_request(request,'defaulter_cohort_uuid') prev_cohort_uuid = request.session.get('cohort_uuid') # this means the user is coming from a form submission page, otherwise there should always be a cohort_uuid if cohort_uuid is None : cohort_uuid = prev_cohort_uuid request.session['cohort_uuid'] = cohort_uuid dc = DefaulterCohort.objects.get(cohort_uuid=cohort_uuid) if prev_cohort_uuid is not None and prev_cohort_uuid == cohort_uuid: my_defaulter_list = json.loads(request.session.get('my_defaulter_list','')) else : my_defaulter_list = [] if my_defaulter_list is None or len(my_defaulter_list) == 0 : patient_uuids_in_use = [] sessions = Session.objects.filter(expire_date__gt=datetime.now()) print 'num sessions: ' + str(len(sessions)) for s in sessions: print 'user_id: ' + str(request.user.id) print 'session owner: ' + str(s.get_decoded().get('_auth_user_id')) session_owner_id = s.get_decoded().get('_auth_user_id') if session_owner_id is not None and request.user.id != int(session_owner_id): decoded = s.get_decoded().get('my_defaulter_list') if decoded is not None: defaulters = json.loads(decoded) for d in defaulters: patient_uuids_in_use.append(d['uuid']) #3/8/2014: the following is a temporary solution to deleting old sessions. it is NOT in the right place if session_owner_id is None: s.delete() my_defaulter_list = dc.get_defaulter_list(patient_uuids_in_use) request.session['my_defaulter_list'] = json.dumps(my_defaulter_list, cls=DjangoJSONEncoder) location = Location.get_location(dc.location_id) defaulter_cohorts = DefaulterCohort.objects.all().order_by('name') return render(request,'ltfu/outreach_worker_defaulter_list.html', {'defaulter_list':my_defaulter_list, 'location':location, 'defaulter_cohort':dc, 'defaulter_cohorts':defaulter_cohorts, } )
def ltfu_ampath(request): if not Authorize.authorize(request.user,['outreach_all']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') rt = ReportTable.objects.filter(name='ltfu_ampath')[0] result = rt.run_report_table(as_dict=True) rows = result['rows'] totals = {} for row in rows : for key, value in row.items(): try : totals[key] = int(totals[key]) + int(value) except Exception, e : print str(e) + ' in exception'
def create_defaulter_cohort(request): if not Authorize.authorize(request.user, ["outreach_supervisor", "outreach_all", "outreach_worker"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") location_uuid = request.GET["location_uuid"] dc = DefaulterCohort.create_defaulter_cohort(location_uuid) patients = dc.get_patients() request.session[dc.location_uuid] = json.dumps(patients, cls=DjangoJSONEncoder) device = get_device(request) return render( request, "ltfu/view_defaulter_cohort_mobile.html", {"defaulter_cohort": dc, "patients": patients, "device": device}, )
def ajax_get_new_defaulter_cohort(request): if not ajax_auth(request): return HttpResponseForbidden() if not Authorize.authorize(request.user,['outreach_supervisor','outreach_all','outreach_worker']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') defaulter_cohort_id = get_var_from_request(request,'defaulter_cohort_id') defaulter_cohort_uuid = get_var_from_request(request,'defaulter_cohort_uuid') if defaulter_cohort_id : dc = DefaulterCohort.objects.get(id=defaulter_cohort_id) else : dc = DefaulterCohort.objects.get(uuid=defaulter_cohort_uuid) new_dc = DefaulterCohort.create_defaulter_cohort(old_defaulter_cohort_id=dc.id) d = CohortCache.get_cohort(dc.id) return HttpResponse(d,content_type='application/json')
def get_report_as_pdf(request): if not Authorize.authorize(request.user, ["superuser"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") report_id = get_var_from_request(request, "report_id") if report_id: r = Report.objects.get(id=report_id) args = r.run_report(request.POST) template = r.template if template is None or template == "": template = "amrs_reports/view_report_pdf.html" template = get_template(template) context = {"report": r, "report_members": args} html = template.render(RequestContext(request, context)) response = HttpResponse(mimetype="application/pdf") HTML(string=html, url_fetcher=my_fetcher, base_url=request.build_absolute_uri()).write_pdf(response) return response
def view_indicators_by_clinic(request): if not Authorize.authorize(request.user) : return HttpResponseRedirect('/amrs_user_validation/access_denied') locations = Location.get_locations() location_groups = DerivedGroup.objects.filter(base_class='Location').order_by('name') start_date = get_var_from_request(request,'start_date') if start_date is None : start_date = '2000-01-01' end_date = get_var_from_request(request,'end_date') if end_date is None : end_date = '2020-01-01' try: values = (start_date,end_date,) rt = ReportTable.objects.get(name='hiv_clinic_key_indicators') clinic_indicators = rt.run_report_table(parameter_values=values,as_dict=True)['rows'] except Exception, e: clinic_indicators = {} print e
def view_defaulter_cohort(request): if not Authorize.authorize(request.user, ["outreach_supervisor", "outreach_all", "outreach_worker"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") location_uuid = request.GET["location_uuid"] dc = DefaulterCohort.objects.filter(location_uuid=location_uuid, retired=0)[0] data = request.session.get(dc.location_uuid, "") if data is not None and data != "": patients = json.loads(request.session.get(dc.location_uuid, "")) else: patients = dc.get_patients() request.session[dc.location_uuid] = json.dumps(patients, cls=DjangoJSONEncoder) device = get_device(request) return render( request, "ltfu/view_defaulter_cohort_mobile.html", {"defaulter_cohort": dc, "patients": patients, "device": device}, )
def run_report_table(request): if not Authorize.authorize(request.user, ["superuser"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") report_table_id = get_var_from_request(request, "report_table_id") if report_table_id: rt = ReportTable.objects.get(id=report_table_id) result = rt.run_report_table(request.POST) cols = result["cols"] rows = result["rows"] parameters = ReportTableParameter.objects.filter(report_table_id=rt.id) return render( request, "amrs_reports/view_report_table.html", {"report_table": rt, "cols": cols, "rows": rows, "parameters": parameters}, ) else: report_tables = ReportTable.objects.all() return render(request, "amrs_reports/run_report_table.html", {"report_tables": report_tables})
def view_indicators_by_provider(request): if not Authorize.authorize(request.user,['superuser']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') start_date = get_var_from_request(request,'start_date') if start_date is None : start_date = '2014-01-01' end_date = get_var_from_request(request,'end_date') if end_date is None : end_date = '2020-01-01' rt = ReportTable.objects.get(name='outreach_indicators_by_provider') parameter_values = (start_date,end_date) table = rt.run_report_table(parameter_values=parameter_values,as_dict=True)['rows'] return render(request,'ltfu/outreach_indicators.html', {'indicators':table, 'start_date':start_date, 'end_date':end_date, 'query_subject':'provider', } )
def index(request): if not Authorize.authorize(request.user) : return HttpResponseRedirect('/amrs_user_validation/access_denied') locations = Location.get_locations() location_groups = DerivedGroup.objects.filter(base_class='Location').order_by('name') system_indicators = request.session.get('hiv_system_indicators') if system_indicators is not None : system_indicators = simplejson.loads(system_indicators) if system_indicators is None : try: rt = ReportTable.objects.get(name='hiv_system_key_indicators') system_indicators = rt.run_report_table(as_dict=True)['rows'] if system_indicators : print 'saving system indicators to session' request.session['system_indicators'] = simplejson.dumps(system_indicators) except Exception, e: pass
def create_report(request): if not Authorize.authorize(request.user, ["superuser"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") if request.method == "POST": print request.POST name = get_var(request.POST, "report_name") description = get_var(request.POST, "description") template = get_var(request.POST, "template") report_id = get_var(request.POST, "report_id") if name: if report_id: r = Report.objects.get(id=report_id) r.name = name r.description = description else: r = Report(name=name, description=description) r.save() cur_index = 2 while True: row = get_var(request.POST, "report_table_values_" + str(cur_index)) if row is None: break rm = ReportMember(report_id=r.id) args = row.split(";;;") for arg in args: split = arg.split("===") setattr(rm, split[0], split[1]) rm.save() cur_index += 1 return HttpResponseRedirect("/") else: report_id = get_var(request.GET, "report_id") report_tables = ReportTable.objects.all().order_by("name") r = None if report_id: r = Report.objects.get(id=report_id) return render(request, "amrs_reports/create_report.html", {"report": r, "report_tables": report_tables})
def view_patient(request): if not Authorize.authorize(request.user,['outreach_supervisor','outreach_all','outreach_worker']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') dcm_id = request.GET.get('defaulter_cohort_member_id',None) patient_uuid = request.GET.get('patient_uuid',None) if dcm_id is not None : member = DefaulterCohortMember.objects.get(id=dcm_id) p = member.get_patient_info() patient_uuid = p['uuid'] else : qs = DefaulterCohortMember.objects.filter(patient_uuid=patient_uuid,retired=0) if qs.count() > 0 : member = qs[0] p = member.get_patient_info() else : p = Patient.get_patient_by_uuid(patient_uuid) device = get_device(request) encounters = RetentionDataset.get_patient_encounters(patient_uuid) encounter_types = EncounterType.get_encounter_types() locations = Location.get_locations() loc = {} enc_types = {} for l in locations : loc[l['location_id']] = l for e in encounter_types: enc_types[e['encounter_type_id']] = e for e in encounters : e['location_name'] = loc[e['location_id']]['name'] e['encounter_type_name'] = enc_types[e['encounter_type']]['name'] return render(request,'ltfu/view_patient_mobile.html', {'patient':p, 'encounters':encounters, 'device':device} )
def run_report(request): if not Authorize.authorize(request.user, ["superuser"]): return HttpResponseRedirect("/amrs_user_validation/access_denied") report_id = get_var_from_request(request, "report_id") if report_id: r = Report.objects.get(id=report_id) args = r.run_report(request.POST) template = r.template if template is None or template == "": template = "amrs_reports/view_report.html" return render(request, template, {"report": r, "report_members": args}) else: report_tables = ReportTable.objects.all() return render(request, "amrs_reports/run_report.html", {"report_tables": report_tables}) if request.method == "POST": report_id = get_var(request.POST, "report_id") r = Report.objects.get(id=report_id) result = r.run_report(request.POST) return render(request, result[0], result[1])
def run_report_table(request): if not Authorize.authorize(request.user,['superuser']) : return HttpResponseRedirect('/amrs_user_validation/access_denied') report_table_id = get_var_from_request(request,'report_table_id') if report_table_id : rt = ReportTable.objects.get(id=report_table_id) result = rt.run_report_table(request.POST) cols = result['cols'] rows = result['rows'] parameters = ReportTableParameter.objects.filter(report_table_id=rt.id) return render(request,"amrs_reports/view_report_table.html", {'report_table':rt, 'cols':cols, 'rows':rows, 'parameters':parameters, }) else : report_tables = ReportTable.objects.all() return render(request, "amrs_reports/run_report_table.html", {'report_tables':report_tables, })