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): 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): b = InputCleaner() if is_eligible_for_results(self.msg.connection): # refuse re-registration if they're still active and eligible self.respond(self.ALREADY_REGISTERED, name=self.msg.connection.contact.name, location=self.msg.connection.contact.location) return text = text.strip() text = b.remove_double_spaces(text) if len(text) < (self.PIN_LENGTH + self.MIN_CLINIC_CODE_LENGTH + self.MIN_NAME_LENGTH + 1): self.mulformed_msg_help() return #signed pin if text[-5:-4] == '-' or text[-5:-4] == '+': self.invalid_pin(text[-5:]) return #too long pin if ' ' in text and text[1 + text.rindex(' '):].isdigit() and len(text[1 + text.rindex(' '):]) > self.PIN_LENGTH: self.invalid_pin(text[1 + text.rindex(' '):]) return #non-white space before pin if text[-5:-4] != ' ' and text[-4:-3] != ' ': self.respond("Sorry, you should put a space before your pin. " "Please make sure your code is a %s-digit number like %s. " "Send JOIN <CLINIC CODE> <YOUR NAME> <SECURITY CODE>." % ( self.PIN_LENGTH, ''.join(str(i) for i in range(1, int(self.PIN_LENGTH) + 1)))) return #reject invalid pin user_pin = text[-4:] if not user_pin: self.help() return elif len(user_pin) < 4: self.invalid_pin(user_pin) return elif not user_pin.isdigit(): self.invalid_pin(user_pin) return group = self.PATTERN.search(text) if group is None: self.mulformed_msg_help() return tokens = group.groups() if not tokens: self.mulformed_msg_help() return clinic_code = tokens[0].strip() clinic_code = b.try_replace_oil_with_011(clinic_code) #we expect all codes have format PPDDFF or PPDDFFS clinic_code = clinic_code[0:6] name = tokens[2] name = name.title().strip() pin = tokens[4].strip() if len(pin) != self.PIN_LENGTH: self.respond(self.INVALID_PIN) return if not name: self.respond("Sorry, you must provide a name to register. %s" % self.HELP_TEXT) return elif len(name) < self.MIN_NAME_LENGTH: self.respond("Sorry, you must provide a valid name to register. %s" % self.HELP_TEXT) return try: location = Location.objects.get(slug__iexact=clinic_code, type__slug__in=const.CLINIC_SLUGS) if self.msg.connection.contact is not None \ and self.msg.connection.contact.is_active: # this means they were already registered and active, but not yet # receiving results. clinic = get_clinic_or_default(self.msg.connection.contact) if clinic != location: self.respond(self.ALREADY_REGISTERED, name=self.msg.connection.contact.name, location=clinic) return True else: contact = self.msg.contact else: contact = Contact(location=location) clinic = get_clinic_or_default(contact) contact.name = name contact.pin = pin contact.save() contact.types.add(const.get_clinic_worker_type()) self.msg.connection.contact = contact self.msg.connection.save() self.respond("Hi %(name)s, thanks for registering for " "Results160 from %(location)s. " "Your PIN is %(pin)s. " "Reply with keyword 'HELP' if this is " "incorrect", name=contact.name, location=clinic.name, pin=pin) 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=clinic_code)
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)