示例#1
0
文件: views.py 项目: dimagi/bhoma
def pi_details(request):
    year = int(request.GET["year"])
    month = int(request.GET["month"])
    clinic = request.GET["clinic"]
    report_slug = request.GET["report"]
    col_slug = request.GET["col"]
    results = get_db().view(const.get_view_name(report_slug), reduce=False,
                            key=[year, month -1, clinic, col_slug], include_docs=True)
    forms = []
    for row in results:
        num, denom = row["value"]
        # only count forms for now, and make sure they have a patient id 
        # and contributed to the report denominator
        if row["doc"]["doc_type"] == "CXFormInstance" and denom > 0:
            form = CXFormInstance.wrap(row["doc"])
            try:
                form.patient_id = form.xpath("case/patient_id")
                form.bhoma_patient_id = CPatient.get(form.patient_id).formatted_id
            except ResourceNotFound:
                form.patient = form.patient_id = form.bhoma_patient_id = None
            form.num = num
            form.denom = denom
            form.good = num == denom
            forms.append(form)
        elif row["doc"]["doc_type"] == "PregnancyReportRecord" and denom > 0:
            # for the pregnancy PI force the aggregated pregnancy docs
            # to look like forms 
            preg = PregnancyReportRecord.wrap(row["doc"])
            try:
                preg.bhoma_patient_id = CPatient.get(preg.patient_id).formatted_id
            except ResourceNotFound:
                form.patient = form.patient_id = form.bhoma_patient_id = None
            preg.num = num
            preg.denom = denom
            preg.good = num == denom
            preg.encounter_date = preg.first_visit_date
            forms.append(preg)  
        
    title = "PI Details - %s: %s (%s, %s)" % (const.get_name(report_slug), 
                                              const.get_display_name(report_slug, col_slug),
                                              datetime(year, month, 1).strftime("%B %Y"),
                                              clinic_display_name(clinic))   
                                             
    return render_to_response(request, "reports/pi_details.html", 
                              {"report": {"name": title},
                               "forms": forms})