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 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