def malnut(request, object_id=None, per_page="0", rformat="pdf"): '''List @Risk Malnutrition Cases per clinic ''' pdfrpt = PDFReport() fourteen_days = timedelta(days=30) today = datetime.now() duration_start = day_start(today - fourteen_days) duration_end = today pdfrpt.setTitle(_("%(app_name)s: @Risk Malnutrition Cases from"\ " %(start_date)s to %(end_date)s"\ % {'start_date': duration_start.date(), 'end_date': duration_end.date(), 'app_name': Cfg.get("app_name")})) #pdfrpt.setRowsPerPage(66) pdfrpt.setNumOfColumns(2) pdfrpt.setLandscape(True) if object_id is None and not request.POST: clinics = Location.objects.filter(type__name="Clinic") for clinic in clinics: queryset, fields = \ ReportAllPatients.malnutrition_at_risk(duration_start, \ duration_end, clinic) c = clinic subtitle = _("%(clinic)s: @Risk Malnutrition Cases from "\ "%(start_date)s to %(end_date)s" % \ {'clinic': c.name, 'start_date': duration_start.date(), \ 'end_date': duration_end.date()}) pdfrpt.setTableData(queryset, fields, subtitle, \ [0.2 * inch, 0.4 * inch, 1 * inch, 0.3 * inch, \ .3 * inch, .8 * inch, .5 * inch, .2 * inch, \ 0.5 * inch, 0.8 * inch, 1 * inch]) if (int(per_page) == 1) is True: pdfrpt.setPageBreak() pdfrpt.setFilename("/tmp/malnutrition_at_risk") else: if request.POST['clinic']: object_id = request.POST['clinic'] object_id = Location.objects.get(id=object_id) queryset, fields = \ ReportAllPatients.malnutrition_at_risk(duration_start, \ duration_end, object_id) subtitle = _("%(clinic)s: @Risk Malnutrition Cases from "\ "%(start_date)s to %(end_date)s" % \ {'clinic': object_id.name, \ 'start_date': duration_start.date(), \ 'end_date': duration_end.date()}) pdfrpt.setTableData(queryset, fields, subtitle, \ [0.2 * inch, 0.4 * inch, 1 * inch, 0.3 * inch, \ .3 * inch, .8 * inch, .5 * inch, .2 * inch, \ 0.5 * inch, 0.8 * inch, 1 * inch]) pdfrpt.setFilename("/tmp/malnutrition_at_risk") return pdfrpt.render()
def malnutrition_screening(request, object_id=None, per_page="0", \ rformat="pdf"): ''' Malnutrition Screening Form Originally for SN CC ''' pdfrpt = [] pdfrpt = PDFReport() #fourteen_days = timedelta(days=30) today = datetime.now() #duration_start = day_start(today - fourteen_days) #duration_end = today pdfrpt.setTitle("%(app_name)s: Formulaire de Depistage" % \ {'app_name': Cfg.get("app_name")}) #pdfrpt.setRowsPerPage(66) pdfrpt.setNumOfColumns(1) pdfrpt.setLandscape(True) pdfrpt.setFontSize(10) if object_id is None and not request.POST: sites = Location.objects.filter(type__name="Site") for site in sites: queryset, fields = \ ReportAllPatients.malnutrition_screening_info(site) subtitle = _("%(site_name)s: Registre des Enfants pour: " % \ {'site_name': site.name}) pdfrpt.setTableData(queryset, fields, subtitle, \ [0.4 * inch, 1.5 * inch, 0.4 * inch, 0.5 * inch, \ 0.5 * inch, 1.0 * inch, 0.9 * inch, 1.0 * inch, \ 0.7 * inch, 0.7 * inch, 0.7 * inch, 0.7 * inch, \ 0.8 * inch, 1.0 * inch]) if (int(per_page) == 1) is True: pdfrpt.setPageBreak() pdfrpt.setFilename("/tmp/malnutrition_at_risk") else: if request.POST['zone']: site_id = request.POST['zone'] site = Location.objects.get(id=site_id) queryset, fields = \ ReportAllPatients.malnutrition_screening_info(site) subtitle = _("Registre des Enfants pour: %(site_name)s" % \ {'site_name': site.name}) # no nom sex dob age mere sms numero pb poids oedems # ddepistage consulter pdfrpt.setTableData(queryset, fields, subtitle, \ [0.4 * inch, 1.5 * inch, 0.4 * inch, 0.5 * inch, \ 0.5 * inch, 1.0 * inch, 0.9 * inch, 1.0 * inch, \ 0.7 * inch, 0.7 * inch, 0.7 * inch, 0.7 * inch, \ 0.8 * inch, 1.0 * inch]) filename = "/tmp/formulaire_de_depistage" + \ datetime.today().strftime("%Y_%m_%d_%H_%M_%S") pdfrpt.setFilename(filename) return pdfrpt.render()
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 last_30_days(request, object_id=None, per_page="0", rformat="pdf", d="30"): '''A pdf report of chw perfomance within the last 30 days''' pdfrpt = PDFReport() pdfrpt.enableFooter(True) d = int(d) thirty_days = timedelta(days=d) ninty_days = timedelta(days=90) today = date.today() duration_start = today - thirty_days muac_duration_start = today - ninty_days duration_end = today pdfrpt.setTitle(_(Cfg.get("app_name") + ": CHW 30 Day Performance Report,"\ " from %(start_date)s " \ "to %(end_date)s" % ({'start_date': duration_start, \ 'end_date': duration_end}))) title = _("CHW 30 Day Performance Report, 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.\ get_providers_by_clinic(duration_start, duration_end, \ muac_duration_start, clinic) stitle = "%s: %s" % (clinic.name, title) pdfrpt.setTableData(queryset, fields, stitle, \ [0.3 * inch, 1 * inch, 0.8 * inch, 0.8 * inch, .8 * inch, \ .8 * inch, 0.8 * inch, 1 * inch, 1 * 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.\ get_providers_by_clinic(duration_start, duration_end, \ muac_duration_start, object_id) stitle = "%s: %s" % (object_id.name, title) if rformat == "csv" or (request.POST \ and request.POST["format"].lower() == "csv"): file_name = object_id.name + ".csv" file_name = file_name.replace(" ", "_").replace("'", "") return handle_csv(request, queryset, fields, file_name) pdfrpt.setTableData(queryset, fields, stitle) return pdfrpt.render()
def patients_by_age(request, object_id=None, per_page="0", rformat="pdf"): ''' Children Screening per age for SN CC ''' pdfrpt = PDFReport() #pdfrpt.setTitle(_("ChildCount Senegal: Listing Enfant par Age")) pdfrpt.setTitle(_(Cfg.get("app_name") + \ ": Listing Enfant par Age" )) #pdfrpt.setRowsPerPage(66) pdfrpt.setNumOfColumns(1) pdfrpt.setLandscape(True) pdfrpt.setFontSize(10) if object_id is None and not request.POST: age_mois = 0 cases = Case.objects.order_by("location").distinct() queryset, fields = ReportAllPatients.by_age(age_mois, cases) subtitle = _("Registre des Enfants pour: ") #%(site.name) pdfrpt.setTableData(queryset, fields, subtitle, \ [0.2 * inch, 1.5 * inch, 0.5 * inch, 0.7 * inch, \ 0.5 * inch, 1.5 * inch,1.0 * inch, 0.5 * inch, 0.7 * inch, \ 0.5 * inch, 0.5 * inch, 0.7 * inch, 1.5 * inch, \ 1 * inch]) if (int(per_page) == 1) is True: pdfrpt.setPageBreak() pdfrpt.setFilename("/tmp/Listing_Enfant") else: cases = Case.objects.order_by("location").distinct() if request.POST['age']: age_mois = int(request.POST['age']) str_age = request.POST['age'] queryset, fields = ReportAllPatients.by_age(age_mois, cases) subtitle = _("Registre des Enfants de : %(age)s mois" % \ {'age': str_age}) # no nom sex dob age mere sms numero pb poids oedems #depistage consulter pdfrpt.setTableData(queryset, fields, subtitle, \ [0.2 * inch, 1.5 * inch, 0.5 * inch, 0.7 * inch, \ 0.5 * inch, 1.5 * inch,1.0 * inch, 0.5 * inch, 0.7 * inch, \ 0.5 * inch, 0.5 * inch, 0.7 * inch, 1.5 * inch, \ 1 * inch]) filename = "/tmp/Listing_Enfant_" + \ datetime.today().strftime("%Y_%m_%d_%H_%M_%S") pdfrpt.setFilename(filename) return pdfrpt.render()
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_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
def dead_cases_report(request, rformat="pdf"): '''List of Cases/Patient per CHW''' today = datetime.now().strftime("%d %B,%Y") pdfrpt = PDFReport() pdfrpt.setLandscape(True) title = _("%(app_name)s: Dead Cases report as of %(date)s" % \ {'app_name': Cfg.get("app_name"), 'date': today}) pdfrpt.setTitle(title) pdfrpt.setNumOfColumns(1) queryset, fields = \ ReportAllPatients.death_report_by_provider() if queryset: pdfrpt.setTableData(queryset, fields, title, \ [0.3 * inch, 0.4 * inch, 1 * inch, 0.4 * inch, \ 0.5 * inch, 0.8 * inch, 0.5 * inch, 1 * inch, \ 0.6 * inch, 1.4 * inch, 1.5 * inch, 1 * inch, \ 1.3 * inch, 1.2 * inch]) pdfrpt.setFilename("/tmp/deathreport_") return pdfrpt.render()
def patients_by_chw(request, object_id=None, per_page="0", rformat="pdf"): '''List of Cases/Patient per CHW''' today = datetime.now().strftime("%d %B,%Y") pdfrpt = PDFReport() pdfrpt.setLandscape(True) pdfrpt.setPrintOnBothSides(True) pdfrpt.setTitle(_("%(app_name)s: Cases Reports by CHW as of %(date)s") % {'date': today,'app_name':Cfg.get("app_name")}) pdfrpt.setNumOfColumns(2) #pdfrpt.setRowsPerPage(88) if object_id is None: if request.POST and request.POST['zone']: # providers = Case.objects.filter(location=request.POST['zone']).\ # values('reporter', 'location').distinct() providers = Reporter.objects.filter(location=request.POST['zone']) #per_page = "1" else: providers = Reporter.objects.all() # providers = Case.objects.order_by("location").\ # values('reporter', 'location').distinct() for reporter in providers: queryset, fields = ReportAllPatients.by_provider(reporter) if queryset: cinfo = {'loc': reporter.location, 'lname': reporter.last_name, 'fname': reporter.first_name, 'date': today} c = _("%(loc)s: %(lname)s %(fname)s: %(date)s" % cinfo) pdfrpt.setTableData(queryset, fields, c, \ [0.3 * inch, 0.4 * inch, 1 * inch, 0.4 * inch, \ 0.3 * inch, 0.8 * inch, 0.5 * inch, 1 * inch, \ 0.8 * inch]) if (int(per_page) == 1) is True: pdfrpt.setPageBreak() pdfrpt.setFilename("/tmp/report_per_page") else: if request.POST and request.POST['provider']: object_id = request.POST['provider'] reporter = Reporter.objects.get(id=object_id) queryset, fields = ReportAllPatients.by_provider(reporter) if queryset: cinfo = {'loc': reporter.location, 'lname': reporter.last_name, 'fname': reporter.first_name, 'date': today} c = _("%(loc)s: %(lname)s %(fname)s: %(date)s" % cinfo) if rformat == "csv" or (request.POST \ and request.POST["format"].lower() == "csv"): file_name = reporter.last_name + ".csv" file_name = file_name.replace(" ", "_").replace("'", "") return handle_csv(request, queryset, fields, file_name) pdfrpt.setTableData(queryset, fields, c, \ [0.3 * inch, 0.4 * inch, 1 * inch, 0.4 * inch, \ 0.3 * inch, 0.4 * inch, 0.5 * inch, 1 * inch, \ 1 * inch]) if (int(per_page) == 1) is True: pdfrpt.setPageBreak() pdfrpt.setFilename("/tmp/report_per_page") return pdfrpt.render()
def join(self, message, location_code, last_name, first_name, role=None): ''' register as a user and join the system Format: join [location code] [last name] [first name] [role - leave blank for CHEW] ''' #default alias for everyone until further notice username = None # do not skip roles for now role_code = role try: name = "%(fname)s %(lname)s" % {'fname': first_name, \ 'lname': last_name} # parse the name, and create a reporter alias, fn, ln = Reporter.parse_name(name) if not message.persistant_connection.reporter: rep = Reporter(alias=alias, first_name=fn, last_name=ln) else: rep = message.persistant_connection.reporter rep.alias = alias rep.first_name = fn rep.last_name = ln rep.save() # attach the reporter to the current connection message.persistant_connection.reporter = rep message.persistant_connection.save() # something went wrong - at the # moment, we don't care what except: message.respond(_("Join Error. Unable to register your account.")) if role_code == None or role_code.__len__() < 1: role_code = Cfg.get('default_chw_role') reporter = message.persistant_connection.reporter # check location code try: location = Location.objects.get(code=location_code) except models.ObjectDoesNotExist: message.forward(reporter.connection().identity, \ _(u"Join Error. Provided location code (%(loc)s) is wrong.") \ % {'loc': location_code}) return True # check that location is a clinic (not sure about that) #if not clinic.type in LocationType.objects.filter(name='Clinic'): # message.forward(reporter.connection().identity, \ # _(u"Join Error. You must provide a Clinic code.")) # return True # set location reporter.location = location # check role code try: role = Role.objects.get(code=role_code) except models.ObjectDoesNotExist: message.forward(reporter.connection().identity, \ _(u"Join Error. Provided Role code (%(role)s) is wrong.") \ % {'role': role_code}) return True reporter.role = role # set account active # /!\ we use registered_self as active reporter.registered_self = True # save modifications reporter.save() # inform target message.forward(reporter.connection().identity, \ _("Success. You are now registered as %(role)s at %(loc)s with " \ "alias @%(alias)s.") \ % {'loc': location, 'role': reporter.role, 'alias': reporter.alias}) #inform admin if message.persistant_connection.reporter != reporter: message.respond(_("Success. %(reporter)s is now registered as " \ "%(role)s at %(loc)s with alias @%(alias)s.") \ % {'reporter': reporter, 'loc': location, \ 'role': reporter.role, 'alias': reporter.alias}) return True
message.respond(format) return True except Exception, e: pass return False try: self.handled = func(self, message, *captures) except HandlerFailed, e: message.respond(e.message) self.handled = True except Exception, e: # TODO: log this exception # FIXME: also, put the contact number in the config message.respond(_("An error occurred. Please call %(mobile)s") \ % {'mobile': Cfg.get('developer_mobile')}) return True raise message.was_handled = bool(self.handled) return self.handled def cleanup(self, message): ''' log message ''' if bool(self.handled): log = MessageLog(mobile=message.peer, sent_by=message.persistant_connection.reporter, text=message.text, was_handled=message.was_handled) log.save() def outgoing(self, message):