def handle(self, text): # make sure they are registered with the system if not (self.msg.contact and self.msg.contact.is_help_admin): self.respond(self.UNGREGISTERED) return text = text.strip() if not text: self.help() return location_slug = text.split()[0][0:6] #get only PPDDFF from 1st token try: txt_count = text.split()[1] ic = InputCleaner() count = ic.words_to_digits(txt_count) except (IndexError, ValueError, AttributeError): count = 5 if count == 0: count = 5 try: location = Location.objects.get(slug__iexact=location_slug) except Location.DoesNotExist: self.respond("Sorry, I don't know about a location with code " "%(code)s. Please check your code and try again.", code=location_slug) return active_contacts = Contact.active.filter(Q(location=location) | Q(location__parent=location), Q(types= const.get_clinic_worker_type()))\ .order_by('pk') if active_contacts: contact_list = " ****".join(contact.name + ";" + contact.default_connection.identity + "." for contact in active_contacts[0:count]) self.respond("Contacts at %s: %s" % (location.name, contact_list)) else: self.respond("There are no active contacts at %s" % location.name)
def handle(self, text): original_text = text if not self.msg.contact: self.respond(UNGREGISTERED) return b = InputCleaner() try: count = int(b.try_replace_oil_with_011(text.strip())) except ValueError: text = b.words_to_digits(text) if not text: text= self.get_only_number(original_text) if text: count = int(text) else: self.respond("%s %s" % (SORRY, HELP)) return else: self.info("Converted %s to %s" % (original_text, text)) count = int(text) count = abs(count) #just in case we change our general cleaning routine if count < 1: self.respond("Sorry, the number of DBS samples sent must be greater than 0 (zero).") return # record this in our records SampleNotification.objects.create(contact=self.msg.contact, location=self.msg.contact.location, count=count, count_in_text=original_text[0:160]) clinic = get_clinic_or_default(self.msg.contact) self.respond(SENT, name=self.msg.contact.name, count=count, clinic=clinic)
def handle(self, text): # make sure they are registered with the system if not (self.msg.contact and self.msg.contact.is_help_admin): self.respond(self.UNGREGISTERED) return text = text.strip() if not text: self.help() return start_days_ago = end_days_ago = 0 ic = InputCleaner() try: txt_start_days_ago = text.split()[0] start_days_ago = int(ic.words_to_digits(txt_start_days_ago)) except (IndexError, ValueError, AttributeError, TypeError): start_days_ago = 0 try: txt_end_days_ago = text.split()[1] end_days_ago = int(ic.words_to_digits(txt_end_days_ago)) except (IndexError, ValueError, AttributeError, TypeError): end_days_ago = 0 if start_days_ago < end_days_ago: start_days_ago, end_days_ago = end_days_ago, start_days_ago now = datetime.date.today() today = datetime.datetime(now.year, now.month, now.day) startdate = today - datetime.timedelta(days=start_days_ago) enddate = today - datetime.timedelta(days=end_days_ago - 1) - \ datetime.timedelta(seconds=0.1) # Not sure if uncommenting the code below will improve performance. # payloads = Payload.objects.filter(Q(incoming_date__gt=startdate) | # Q(incoming_date=startdate), # Q(incoming_date__lt=enddate) | # Q(incoming_date=enddate)) # if not payloads: # self.respond("Period %(startdate)s to %(enddate)s. No payloads", # startdate=startdate.strftime("%d/%m/%Y"), # enddate=enddate.strftime("%d/%m/%Y")) # return from django.db import connection cursor = connection.cursor() cursor.execute('select source, count(*) as count from \ labresults_payload where incoming_date BETWEEN %s AND %s group by \ source', [startdate, enddate]) rows = cursor.fetchall() if not rows: self.respond("Period %(startdate)s to %(enddate)s. No payloads", startdate=startdate.strftime("%d/%m/%Y"), enddate=enddate.strftime("%d/%m/%Y")) return #build formartted message msg_header = 'PAYLOADS. Period: %s to %s. ' % (startdate.strftime("%d/%m/%Y") , enddate.strftime("%d/%m/%Y")) msg_data = ' ****'.join(row[0] + ";" + str(row[1]) for row in rows) full_msg = msg_header + msg_data self.respond(full_msg)
def handle(self, text): b = InputCleaner() if not is_eligible_for_results(self.msg.connection): self.respond(self.NOT_ELIGIBLE) return if not text or not text.strip(): return clinic_code = text.split()[0] #staff with zeros in case someone just send PP or PPDD if b.try_replace_oil_with_011(clinic_code[0:6]).isdigit(): clinic_code = clinic_code + "00000" clinic_code = clinic_code[0:6] district_facilities = None province_facilities = None try: location = Location.objects.get(slug__iexact=clinic_code) if location.type.slug == 'districts': district_facilities = Location.objects.filter(parent=location, type__slug__in= const.CLINIC_SLUGS) elif location.type.slug == 'provinces': province_facilities = Location.objects.filter(parent__parent= location, type__slug__in= const.CLINIC_SLUGS) except Location.DoesNotExist: # maybe it's a district like 403000 try: clinic_code = clinic_code.replace('000', '0') district_facilities = Location.objects.filter(slug__startswith= clinic_code, type__slug__in= const.CLINIC_SLUGS) location = district_facilities[0].parent except IndexError: #maybe it's a province like 400000 try: clinic_code = clinic_code.replace('000', '0') province_facilities = Location.objects.filter(slug__startswith= clinic_code, type__slug__in= const.CLINIC_SLUGS) location = province_facilities[0].parent.parent except IndexError: self.respond("Sorry, I don't know about a location with code %(code)s. Please check your code and try again.", code=clinic_code) return text = text.strip() text = b.remove_double_spaces(text) today = datetime.datetime.today() try: month = int(b.words_to_digits(text.split()[1][0:3])) except (IndexError, TypeError): month = today.month if month not in range(1, 13): month = today.month startdate = datetime.datetime(today.year, month, 1) if month == 12: enddate = datetime.datetime(today.year, 12, 31) else: enddate = datetime.datetime(today.year, month + 1, 1) - datetime.timedelta(seconds=1) report_values = self.get_facility_report(location, startdate, enddate, district_facilities, province_facilities) rpt_header = "SENT RESULTS\n%s\n%s to %s" % (location.name, startdate.strftime("%d/%m/%Y"), enddate.strftime("%d/%m/%Y")) rpt_data = '\n'.join(key + ";" + str(value) for key, value in report_values.items()) msg = rpt_header + '\n' + rpt_data self.respond(msg)
def handle(self, text): b = InputCleaner() if not is_eligible_for_results(self.msg.connection): self.respond(self.NOT_ELIGIBLE) return if not text or not text.strip(): return clinic_code = text.split()[0] #staff with zeros in case someone just send PP or PPDD if b.try_replace_oil_with_011(clinic_code[0:6]).isdigit(): clinic_code = clinic_code + "00000" clinic_code = clinic_code[0:6] district_facilities = None province_facilities = None try: location = Location.objects.get(slug__iexact=clinic_code) if location.type.slug == 'districts': district_facilities = Location.objects.filter( parent=location, type__slug__in=const.CLINIC_SLUGS) elif location.type.slug == 'provinces': province_facilities = Location.objects.filter( parent__parent=location, type__slug__in=const.CLINIC_SLUGS) except Location.DoesNotExist: # maybe it's a district like 403000 try: clinic_code = clinic_code.replace('000', '0') district_facilities = Location.objects.filter( slug__startswith=clinic_code, type__slug__in=const.CLINIC_SLUGS) location = district_facilities[0].parent except IndexError: #maybe it's a province like 400000 try: clinic_code = clinic_code.replace('000', '0') province_facilities = Location.objects.filter( slug__startswith=clinic_code, type__slug__in=const.CLINIC_SLUGS) location = province_facilities[0].parent.parent except IndexError: self.respond( "Sorry, I don't know about a location with code %(code)s. Please check your code and try again.", code=clinic_code) return text = text.strip() text = b.remove_double_spaces(text) today = datetime.datetime.today() try: month = int(b.words_to_digits(text.split()[1][0:3])) except (IndexError, TypeError): month = today.month if month not in range(1, 13): month = today.month startdate = datetime.datetime(today.year, month, 1) if month == 12: enddate = datetime.datetime(today.year, 12, 31) else: enddate = datetime.datetime(today.year, month + 1, 1) - datetime.timedelta(seconds=1) report_values = self.get_facility_report(location, startdate, enddate, district_facilities, province_facilities) rpt_header = "SENT RESULTS\n%s\n%s to %s" % ( location.name, startdate.strftime("%d/%m/%Y"), enddate.strftime("%d/%m/%Y")) rpt_data = '\n'.join(key + ";" + str(value) for key, value in report_values.items()) msg = rpt_header + '\n' + rpt_data self.respond(msg)