def muac_summary(request, object_id=None, per_page="0", rformat="pdf", d="30"): '''A pdf report of chw perfomance within the last 30 days''' pdfrpt = PDFReport() d = int(d) pdfrpt.enableFooter(True) thirty_days = timedelta(days=d) today = date.today() duration_start = today - thirty_days duration_start = duration_start.replace(day=1) duration_end = today duration_str = _("CMAM Summary from %(start_date)s to %(end_date)s") % \ {'start_date': duration_start.strftime("%d %B, %Y"), \ 'end_date': today.strftime("%d %B, %Y")} pdfrpt.setTitle(Cfg.get("app_name") + _(": CMAM Summary from %(start_date)s to %(end_date)s") % \ {'start_date': duration_start, 'end_date': duration_end}) if object_id is None: clinics = Location.objects.filter(type__name="Clinic") for clinic in clinics: queryset, fields = ReportCHWStatus.muac_summary(duration_start, \ duration_end, clinic) title = "%s : %s" % (clinic.name, duration_str) pdfrpt.setTableData(queryset, fields, title, \ [0.3 * inch, 1 * inch, 0.8 * inch, 0.8 * inch, \ .8 * inch, .8 * inch, 0.8 * inch, 1 * inch]) if (int(per_page) == 1) is True: pdfrpt.setPageBreak() pdfrpt.setFilename("/tmp/report_per_page") else: if request.POST['clinic']: object_id = request.POST['clinic'] object_id = Location.objects.get(id=object_id) queryset, fields = ReportCHWStatus.muac_summary(duration_start, \ duration_end, object_id) c = object_id if rformat == "csv" or (request.POST and \ request.POST["format"].lower() == "csv"): file_name = c.name + ".csv" file_name = file_name.replace(" ", "_").replace("'", "") return handle_csv(request, queryset, fields, file_name) title = "%s : %s" % (c.name, duration_str) pdfrpt.setTableData(queryset, fields, title) return pdfrpt.render()
def measles_mini_summary_csv(request, file_name): '''A summary of measles report per clinic in csv format''' output = StringIO.StringIO() csvio = csv.writer(output) header = False summary = ReportCHWStatus.measles_mini_summary() rows = [] row = [] row.append(_("Facility")) row.append(_("No. Vaccinated")) row.append(_("No. Eligible")) row.append("%") rows.append(row) for info in summary: info["percentage"] = round(float(float(info["vaccinated_cases"]) / \ float(info["eligible_cases"])) * 100, 2) row = [] row.append(u"%(clinic)s" % info) row.append(u"%(vaccinated_cases)s" % info) row.append(u"%(eligible_cases)s" % info) row.append(u"%(percentage)s%%" % info) rows.append(row) # Write rows on CSV for row in rows: csvio.writerow([cell for cell in row]) response = HttpResponse(mimetype='text/csv') response['Content-Disposition'] = "attachment; filename=%s" % file_name response.write(output.getvalue()) return response
def activity_summary(self, message, period=None): '''Get a reporters activity summary''' reporter = message.persistant_connection.reporter duration_end = datetime.datetime.now() if period is None or period.lower() == "all": ml = MessageLog.objects.filter(sent_by=reporter)\ .order_by('created_at') if ml: duration_start = ml[0].created_at elif period.lower() == "month": last_30_days = datetime.timedelta(30) duration_start = duration_end - last_30_days elif period.lower() == "week": last_seven_days = datetime.timedelta(7) duration_start = duration_end - last_seven_days elif period.lower() == "day": duration_start = day_start(datetime.datetime.today()) else: duration_start = None msg = "No Summary" if duration_start is not None: summary = ReportCHWStatus.reporter_summary(duration_start, \ duration_end, reporter) msg = _("%(reporter)s: %(num_cases)s children, "\ "#new %(num_new_cases)s, #dead %(num_dead)s, "\ "#inactive %(num_cases_inactive)s" \ ", #mrdt %(num_malaria_reports)s, #muac %(num_muac)s. ")\ % summary message.respond(_("%s") % msg) return True
def vitamines_summary(self, message): """Send vitamines summary to health facilitators and those who are to receive alerts """ header = _("Vitamines Summary by clinic:") result = [] summary = ReportCHWStatus.vitamines_mini_summary() tmp = header for info in summary: if info["eligible_cases"] != 0: info["percentage"] = round( float(float(info["vaccinated_cases"]) / float(info["eligible_cases"])) * 100, 2 ) else: info["percentage"] = 0 item = " %(clinic)s: %(vaccinated_cases)s/%(eligible_cases)s " "%(percentage)s, " % info if len(tmp) + len(item) + 2 >= self.MAX_MSG_LEN: result.append(tmp) tmp = header tmp += item if tmp != header: result.append(tmp) subscribers = Reporter.objects.all() for text in result: for subscriber in subscribers: try: if ( subscriber.registered_self and ReporterGroup.objects.get(title="vitamines_summary") in subscriber.groups.only() ): # mobile = subscriber.connection().identity # message.forward(mobile, _("%(msg)s") % {'msg': text}) message.respond(_("%(msg)s") % {"msg": text}) except: """Alert Developer The group might have not been created, need to alert the developer/in charge here """ message.forward( Cfg.get("developer_mobile"), _("The group %(group)s has not been created.") % {"group": "measles_summary"}, ) return True
def measles_summary(self, message): '''Send measles summary to health facilitators and those who are to receive alerts ''' header = _(u"Measles Summary by clinic:") result = [] summary = ReportCHWStatus.measles_mini_summary() tmp = header for info in summary: if info['eligible_cases'] != 0: info['percentage'] = \ round(float(float(info['vaccinated_cases']) / \ float(info['eligible_cases'])) * 100, 0) else: info['percentage'] = 0 item = u" %(clinic)s: %(vaccinated_cases)s/%(eligible_cases)s "\ "%(percentage)s%%," % info if len(tmp) + len(item) + 2 >= self.MAX_MSG_LEN: result.append(tmp) tmp = header tmp += item if tmp != header: result.append(tmp) subscribers = Reporter.objects.all() for text in result: for subscriber in subscribers: try: if subscriber.registered_self \ and ReporterGroup.objects.get(title='measles_summary')\ in subscriber.groups.only(): mobile = subscriber.connection().identity message.forward(mobile, _("%(msg)s") % {'msg': text}) except: '''Alert Developer The group might have not been created, need to alert the developer/in charge here ''' message.forward(Cfg.get('developer_mobile'), \ _("The group %(group)s has not been created."\ % {'group': "measles_summary"})) return True