def setUp(self): super(TestApp, self).setUp() type, _ = LocationType.objects.get_or_create(singular="clinic", plural="clinics", slug="clinics") clinic = Location.objects.create(type=type, name="demo", slug="demo") self.clinic_zone= Location.objects.create(type=get_zone_type(), name="child", slug="child", parent=clinic) clinic_worker = self.create_contact(name="clinic_worker", location=clinic, types=[get_clinic_worker_type()]) clinic_worker2 = self.create_contact(name="clinic_worker2", location=self.clinic_zone, types=[get_clinic_worker_type()]) # script = "help_admin > hello world" # self.runScript(script) # connection = Connection.objects.get(identity="help_admin") # help_admin = Contact.objects.create(alias='help_admin', is_active = True, name="help_admin", # location=clinic_zone,is_help_admin = True) # help_admin.types.add(const.get_clinic_worker_type()) # # connection.contact = help_admin # connection.save() cba = self.create_contact(name="cba", location=clinic, types=[get_cba_type()]) cba2 = self.create_contact(name="cba2", location=self.clinic_zone, types=[get_cba_type()]) active_contacts = Contact.active.all() self.all = [clinic_worker, clinic_worker2, cba, cba2] self.expected_keyword_to_groups = \ {"ALL": [clinic_worker, clinic_worker2, cba, cba2], "CLINIC": [clinic_worker, clinic_worker2], "CBA": [cba, cba2], }
def setUp(self): super(TestApp, self).setUp() type, _ = LocationType.objects.get_or_create(singular="clinic", plural="clinics", slug="clinics") district_type, _ = LocationType.objects.get_or_create( singular="district", plural="districts", slug="districts") self.district = Location.objects.create(type=district_type, name="Mansa", slug="403000") self.district2 = Location.objects.create(type=district_type, name="Lusaka", slug="402000") clinic = Location.objects.create(type=type, name="Central Clinic", slug="403020") clinic.parent = self.district clinic.save() self.clinic2 = Location.objects.create(type=type, name="Other Clinic", slug="402020") self.clinic2.parent = self.district2 self.clinic2.save() self.clinic_zone = Location.objects.create(type=get_zone_type(), name="child", slug="child", parent=clinic) clinic_worker = self.create_contact(name="clinic_worker", location=clinic, types=[get_clinic_worker_type()]) clinic_worker2 = self.create_contact(name="clinic_worker2", location=clinic, types=[get_clinic_worker_type()]) cba = self.create_contact(name="cba", location=self.clinic_zone, types=[get_cba_type()]) cba2 = self.create_contact(name="cba2", location=self.clinic_zone, types=[get_cba_type()]) active_contacts = Contact.active.all() self.all = [clinic_worker, clinic_worker2, cba, cba2] self.expected_keyword_to_groups = \ {"ALL": [clinic_worker, clinic_worker2, cba, cba2], "CLINIC": [clinic_worker, clinic_worker2], "CBA": [cba, cba2], }
def get_clinics_not_retriving_results_alerts(self, days=None): self.set_retrieving_start(days) my_alerts = [] clinics = Location.\ objects.filter( supportedlocation__supported=True, lab_results__notification_status='notified', lab_results__arrival_date__lt=self.retrieving_ref_date ).distinct() for clinic in clinics: contacts = \ Contact.active.filter(Q(location=clinic) | Q(location__parent=clinic), Q(types=const.get_clinic_worker_type())).\ order_by('pk') days_late = self.days_ago(self.earliest_pending_result_arrival_date(clinic)) level = Alert.HIGH_LEVEL if days_late >= (2 * self.retrieving_days) else Alert.LOW_LEVEL my_alerts.append(Alert(Alert.LONG_PENDING_RESULTS, "%s clinic have not"\ " retrieved their results. Please call and enquire " "(%s)" % (clinic.name, ", ".join(contact.name + ":" + contact.default_connection.identity for contact in contacts)), clinic.name, days_late, -days_late, level, "" )) return self.retrieving_days, sorted(my_alerts, key=itemgetter(5))
def testMsg(self): """ msg dho blah blah : goes to fellow dho's at district msg all blah blah : goes to both dho's and clinic workers in district """ dho = self.create_contact(name="dho", location=self.district, types=[get_district_worker_type()]) dho2 = self.create_contact(name="dho2", location=self.district, types=[get_district_worker_type()]) # control contacts dho3 = self.create_contact(name="dho3", location=self.district2, types=[get_district_worker_type()]) clinic_worker3 = self.create_contact(name="clinic_worker3", location=self.clinic2, types=[get_clinic_worker_type()]) script = """ dho > msg my own way dho < To send a message to DHOs in your district, SEND: MSG DHO (your message). To send to both DHOs and clinic worker SEND: MSG ALL (your message) dho > msg dho < To send a message to DHOs in your district, SEND: MSG DHO (your message). To send to both DHOs and clinic worker SEND: MSG ALL (your message) """ self.runScript(script) script = """ dho > msg all testing dho blasting """ self.runScript(script) msgs = self.receiveAllMessages() self.assertEqual(3, len(msgs)) expected_recipients = ["dho2", "clinic_worker", "clinic_worker2"] actual_recipients = [] for msg in msgs: self.assertEqual(msg.text, "testing dho blasting [from dho to MSG]") actual_recipients.append(msg.contact.name) difference = list( set(actual_recipients).difference(set(expected_recipients))) self.assertEqual([], difference) script = """ dho > msg dho testing dho blasting """ self.runScript(script) msgs = self.receiveAllMessages() # no extra msgs sent self.assertEqual(1, len(msgs)) self.assertEqual('dho2', msgs[0].contact.name) self.assertEqual(msgs[0].text, 'testing dho blasting [from dho to MSG]')
def get_clinics_not_retriving_results_alerts(self, days=None): self.set_retrieving_start(days) my_alerts = [] clinics = Location.\ objects.filter( supportedlocation__supported=True, lab_results__notification_status='notified', lab_results__arrival_date__lt=self.retrieving_ref_date ).distinct() for clinic in clinics: contacts = \ Contact.active.filter(Q(location=clinic) | Q(location__parent=clinic), Q(types=const.get_clinic_worker_type())).\ order_by('pk') days_late = self.days_ago( self.earliest_pending_result_arrival_date(clinic)) level = Alert.HIGH_LEVEL if days_late >= ( 2 * self.retrieving_days) else Alert.LOW_LEVEL my_alerts.append(Alert(Alert.LONG_PENDING_RESULTS, "%s clinic have not"\ " retrieved their results. Please call and enquire " "(%s)" % (clinic.name, ", ".join(contact.name + ":" + contact.default_connection.identity for contact in contacts)), clinic.name, days_late, -days_late, level, "" )) return self.retrieving_days, sorted(my_alerts, key=itemgetter(5))
def notify_clinic_of_changed_records(self, clinic): """Notifies clinic of the new status for changed results.""" changed_results = [] updated_results = self._updated_results(clinic) if not updated_results: return for updated_result in updated_results: if updated_result.record_change: changed_results.append(updated_result) if not changed_results: return contacts = \ Contact.active.filter(Q(location=clinic)|Q(location__parent=clinic), Q(types=const.get_clinic_worker_type())).\ order_by('pk') if not contacts: self.warning("No contacts registered to receive results at %s! " "These will go unreported until clinic staff " "register at this clinic." % clinic) return RESULTS_CHANGED = "URGENT: A result sent to your clinic has changed. Please send your pin, get the new result and update your logbooks." if len(changed_results) > 1: RESULTS_CHANGED = "URGENT: Some results sent to your clinic have changed. Please send your pin, get the new results and update your logbooks." all_msgs = [] help_msgs = [] for contact in contacts: msg = OutgoingMessage(connection=contact.default_connection, template=RESULTS_CHANGED) all_msgs.append(msg) contact_details = [] for contact in contacts: contact_details.append("%s:%s" % (contact.name, contact.default_connection.identity)) if all_msgs: self.send_messages(all_msgs) self._mark_results_pending(changed_results, (msg.connection for msg in all_msgs)) for help_admin in Contact.active.filter(is_help_admin=True): h_msg = OutgoingMessage( help_admin.default_connection, "Make a followup for changed results %s: %s. Contacts = %s" % (clinic.name, ";****".join("ID=" + res.requisition_id + ", Result=" + res.result + ", old value=" + res.old_value for res in changed_results), ", ".join(contact_detail for contact_detail in contact_details)) ) help_msgs.append(h_msg) if help_msgs: self.send_messages(help_msgs) logger.info("sent following message to help admins: %s" % help_msgs[0].text) else: logger.info("There are no help admins")
def get_clinics_not_sending_dbs_alerts(self, day=None): my_alerts = [] self.set_clinic_sent_dbs_start_dates(day) clinics = Location.objects.filter(supportedlocation__supported=True ).\ exclude(lab_results__entered_on__gte= self.clinic_sent_dbs_referal_date.date()).\ exclude(samplenotification__date__gte= self.clinic_sent_dbs_referal_date.date()).distinct() active_clinics = Location.\ objects.filter( Q(lab_results__notification_status='sent', lab_results__result_sent_date__gte =self.clinic_sent_dbs_referal_date.date()), Q( contact__message__text__iregex='\s*check\s*') | Q(parent__contact__message__text__iregex='\s*check\s*' ) | Q( contact__message__text__istartswith='The results for sample') | Q(parent__contact__message__text__istartswith='The results for sample' ) | Q( contact__message__text__istartswith='There are currently no results') | Q(parent__contact__message__text__istartswith='There are currently no results' ) ).distinct() for clinic in clinics: additional_text = "" if clinic not in active_clinics: additional_text = "The last time this clinic used Results160 was "\ + "%s days ago." % self.days_ago(self.last_retrieved_or_checked(clinic)) contacts = \ Contact.active.filter(Q(location=clinic) | Q(location__parent=clinic), Q(types=const.get_clinic_worker_type())).\ order_by('pk') days_late = self.days_ago(self.last_sent_samples(clinic)) level = Alert.HIGH_LEVEL if days_late >= (2 * self.clinic_notification_days) else Alert.LOW_LEVEL my_alerts.append(Alert(Alert.CLINIC_NOT_USING_SYSTEM, "Clinic " "has no record of sending DBS samples in the last" " %s days. Please check " "that they have supplies by calling (%s)." "" % (days_late, ", ".join(contact.name + ":" + contact.default_connection.identity for contact in contacts)), clinic.name, days_late, -days_late, level, additional_text )) return self.clinic_notification_days, sorted(my_alerts, key=itemgetter(5))
def is_eligible_for_results(connection): """ Whether a person (by connection) meets all the prerequisites for receiving lab results """ return connection.contact is not None \ and connection.contact.is_active \ and const.get_clinic_worker_type() in connection.contact.types.all() \ and connection.contact.pin is not None \ and connection.contact.location is not None
def get_clinics_not_sending_dbs_alerts(self, day=None): my_alerts = [] self.set_clinic_sent_dbs_start_dates(day) clinics = Location.objects.filter(supportedlocation__supported=True ).\ exclude(lab_results__entered_on__gte= self.clinic_sent_dbs_referal_date.date()).\ exclude(samplenotification__date__gte= self.clinic_sent_dbs_referal_date.date()).distinct() active_clinics = Location.\ objects.filter( Q(lab_results__notification_status='sent', lab_results__result_sent_date__gte =self.clinic_sent_dbs_referal_date.date()), Q( contact__message__text__iregex='\s*check\s*') | Q(parent__contact__message__text__iregex='\s*check\s*' ) | Q( contact__message__text__istartswith='The results for sample') | Q(parent__contact__message__text__istartswith='The results for sample' ) | Q( contact__message__text__istartswith='There are currently no results') | Q(parent__contact__message__text__istartswith='There are currently no results' ) ).distinct() for clinic in clinics: additional_text = "" if clinic not in active_clinics: additional_text = "The last time this clinic used Results160 was "\ + "%s days ago." % self.days_ago(self.last_retrieved_or_checked(clinic)) contacts = \ Contact.active.filter(Q(location=clinic) | Q(location__parent=clinic), Q(types=const.get_clinic_worker_type())).\ order_by('pk') days_late = self.days_ago(self.last_sent_samples(clinic)) level = Alert.HIGH_LEVEL if days_late >= ( 2 * self.clinic_notification_days) else Alert.LOW_LEVEL my_alerts.append( Alert( Alert.CLINIC_NOT_USING_SYSTEM, "Clinic " "has no record of sending DBS samples in the last" " %s days. Please check " "that they have supplies by calling (%s)." "" % (days_late, ", ".join(contact.name + ":" + contact.default_connection.identity for contact in contacts)), clinic.name, days_late, -days_late, level, additional_text)) return self.clinic_notification_days, sorted(my_alerts, key=itemgetter(5))
def fake_pending_results(self, clinic): """Notifies clinic staff that results are ready via sms, except this is fake!""" contacts = Contact.active.filter(types=const.get_clinic_worker_type()).location(clinic) results = get_fake_results(3, clinic) for contact in contacts: msg = OutgoingMessage(connection=contact.default_connection, template=RESULTS_READY, name=contact.name, count=len(results)) msg.send() self._mark_results_pending(results, msg.connection)
def testMsg(self): """ msg dho blah blah : goes to fellow dho's at district msg all blah blah : goes to both dho's and clinic workers in district """ dho = self.create_contact(name="dho", location=self.district, types=[get_district_worker_type()]) dho2 = self.create_contact(name="dho2", location=self.district, types=[get_district_worker_type()]) # control contacts dho3 = self.create_contact(name="dho3", location=self.district2, types=[get_district_worker_type()]) clinic_worker3 = self.create_contact(name="clinic_worker3", location=self.clinic2, types=[get_clinic_worker_type()]) script=""" dho > msg my own way dho < To send a message to DHOs in your district, SEND: MSG DHO (your message). To send to both DHOs and clinic worker SEND: MSG ALL (your message) dho > msg dho < To send a message to DHOs in your district, SEND: MSG DHO (your message). To send to both DHOs and clinic worker SEND: MSG ALL (your message) """ self.runScript(script) script=""" dho > msg all testing dho blasting """ self.runScript(script) msgs=self.receiveAllMessages() self.assertEqual(3,len(msgs)) expected_recipients = ["dho2","clinic_worker","clinic_worker2"] actual_recipients = [] for msg in msgs: self.assertEqual(msg.text,"testing dho blasting [from dho to MSG]") actual_recipients.append(msg.contact.name) difference = list(set(actual_recipients).difference(set(expected_recipients))) self.assertEqual([], difference) script=""" dho > msg dho testing dho blasting """ self.runScript(script) msgs=self.receiveAllMessages() # no extra msgs sent self.assertEqual(1, len(msgs)) self.assertEqual('dho2', msgs[0].contact.name) self.assertEqual(msgs[0].text, 'testing dho blasting [from dho to MSG]')
def handle(self, text): if self.msg.contact is None or \ self.msg.contact.location is None: self.respond(UNREGISTERED) return location = get_clinic_or_default(self.msg.contact) contacts = Contact.active.location(location)\ .exclude(id=self.msg.contact.id)\ .filter(types=get_clinic_worker_type()) return self.broadcast(text, contacts)
def fake_pending_results(self, clinic): """Notifies clinic staff that results are ready via sms, except this is fake!""" contacts = Contact.active.filter( types=const.get_clinic_worker_type()).location(clinic) results = get_fake_results(3, clinic) for contact in contacts: msg = OutgoingMessage(connection=contact.default_connection, template=RESULTS_READY, name=contact.name, count=len(results)) msg.send() self._mark_results_pending(results, msg.connection)
def setUp(self): super(TestApp, self).setUp() type, _ = LocationType.objects.get_or_create(singular="clinic", plural="clinics", slug="clinics") district_type, _ = LocationType.objects.get_or_create(singular="district", plural="districts", slug="districts") self.district = Location.objects.create(type=district_type, name="Mansa", slug="403000") self.district2 = Location.objects.create(type=district_type, name="Lusaka", slug="402000") clinic = Location.objects.create(type=type, name="Central Clinic", slug="403020") clinic.parent = self.district clinic.save() self.clinic2 = Location.objects.create(type=type, name="Other Clinic", slug="402020") self.clinic2.parent = self.district2 self.clinic2.save() self.clinic_zone= Location.objects.create(type=get_zone_type(), name="child", slug="child", parent=clinic) clinic_worker = self.create_contact(name="clinic_worker", location=clinic, types=[get_clinic_worker_type()]) clinic_worker2 = self.create_contact(name="clinic_worker2", location=clinic, types=[get_clinic_worker_type()]) cba = self.create_contact(name="cba", location=self.clinic_zone, types=[get_cba_type()]) cba2 = self.create_contact(name="cba2", location=self.clinic_zone, types=[get_cba_type()]) active_contacts = Contact.active.all() self.all = [clinic_worker, clinic_worker2, cba, cba2] self.expected_keyword_to_groups = \ {"ALL": [clinic_worker, clinic_worker2, cba, cba2], "CLINIC": [clinic_worker, clinic_worker2], "CBA": [cba, cba2], }
def get_worker_type(self): ''' Returns the worker_type based on the location_code Expects code of format PPDDFF exactly. ''' PP = self.slug[0:2] DD = self.slug[2:4] FF = self.slug[4:6] if FF == '00': if DD == '00': return const.get_province_worker_type() else: return const.get_district_worker_type() else: return const.get_clinic_worker_type()
def results_avail_messages(self, clinic): results = self._pending_results(clinic) contacts = \ Contact.active.filter(Q(location=clinic)|Q(location__parent=clinic), Q(types=const.get_clinic_worker_type())) if not contacts: self.warning("No contacts registered to receiver results at %s! " "These will go unreported until clinic staff " "register at this clinic." % clinic) all_msgs = [] for contact in contacts: msg = OutgoingMessage(connection=contact.default_connection, template=RESULTS_READY, name=contact.name, count=results.count()) all_msgs.append(msg) return all_msgs, results
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 testBlaster(self): script = "help_admin > hello world" self.runScript(script) connection = Connection.objects.get(identity="help_admin") help_admin = Contact.objects.create(alias='help_admin', is_active = True, name="help_admin", location=self.clinic_zone,is_help_admin = True) help_admin.types.add(const.get_clinic_worker_type()) connection.contact = help_admin connection.save() script = """ help_admin > blast hello clinic_worker < hello [from help_admin to Mwana Users] clinic_worker2 < hello [from help_admin to Mwana Users] cba < hello [from help_admin to Mwana Users] cba2 < hello [from help_admin to Mwana Users] """ self.runScript(script)
def testBlaster(self): script = "help_admin > hello world" self.runScript(script) connection = Connection.objects.get(identity="help_admin") help_admin = Contact.objects.create(alias='help_admin', is_active=True, name="help_admin", location=self.clinic_zone, is_help_admin=True) help_admin.types.add(const.get_clinic_worker_type()) connection.contact = help_admin connection.save() script = """ help_admin > blast hello clinic_worker < hello [from help_admin to Mwana Users] clinic_worker2 < hello [from help_admin to Mwana Users] cba < hello [from help_admin to Mwana Users] cba2 < hello [from help_admin to Mwana Users] """ self.runScript(script)
def testBootstrap(self): contact = Contact.objects.get(id=self.contact.id) self.assertEqual("clinic_worker", contact.default_connection.identity) self.assertEqual(self.clinic, contact.location) self.assertEqual("4567", contact.pin) self.assertTrue(const.get_clinic_worker_type() in contact.types.all())
def setUp(self): # this call is required if you want to override setUp super(LabresultsSetUp, self).setUp() self.type = LocationType.objects.get_or_create(singular="clinic", plural="clinics", slug=const.CLINIC_SLUGS[2])[0] self.type1 = LocationType.objects.get_or_create(singular="district", plural="districts", slug="districts")[0] self.type2 = LocationType.objects.get_or_create(singular="province", plural="provinces", slug="provinces")[0] self.luapula = Location.objects.create(type=self.type2, name="Luapula Province", slug="luapula") self.mansa = Location.objects.create(type=self.type1, name="Mansa District", slug="mansa", parent = self.luapula) self.samfya = Location.objects.create(type=self.type1, name="Samfya District", slug="samfya", parent = self.luapula) self.clinic = Location.objects.create(type=self.type, name="Mibenge Clinic", slug="402029", parent = self.samfya) self.mansa_central = Location.objects.create(type=self.type, name="Central Clinic", slug="403012", parent = self.mansa) self.support_clinic = Location.objects.create(type=self.type, name="Support Clinic", slug="spt") # this gets the backend and connection in the db script = "clinic_worker > hello world" self.runScript(script) connection = Connection.objects.get(identity="clinic_worker") self.contact = Contact.objects.create(alias="banda", name="John Banda", location=self.clinic, pin="4567") self.contact.types.add(const.get_clinic_worker_type()) connection.contact = self.contact connection.save() # create another one self.other_contact = Contact.objects.create(alias="mary", name="Mary Phiri", location=self.clinic, pin="6789") self.other_contact.types.add(const.get_clinic_worker_type()) Connection.objects.create(identity="other_worker", backend=connection.backend, contact=self.other_contact) connection.save() # create another worker for a different clinic self.central_clinic_worker = Contact.objects.create(alias="jp", name="James Phiri", location=self.mansa_central, pin="1111") self.central_clinic_worker.types.add(const.get_clinic_worker_type()) Connection.objects.create(identity="central_clinic_worker", backend=connection.backend, contact=self.central_clinic_worker) connection.save() # create support staff self.support_contact = Contact.objects.create(alias="ha1", name="Help Admin", location=self.support_clinic, pin="1111", is_help_admin=True) self.support_contact.types.add(const.get_clinic_worker_type()) Connection.objects.create(identity="support_contact", backend=connection.backend, contact=self.support_contact) connection.save() # create second support staff self.support_contact2 = Contact.objects.create(alias="ha2", name="Help Admin2", location=self.support_clinic, pin="2222", is_help_admin=True) self.support_contact2.types.add(const.get_clinic_worker_type()) Connection.objects.create(identity="support_contact2", backend=connection.backend, contact=self.support_contact2) connection.save()
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)