Exemple #1
0
    def handle(self, text):

        tokens = self.check_message_valid_and_clean(text)

        if not tokens:
            return
        clinic_code = LocationCode(tokens[0])
        name = tokens[2]
        pin = tokens[4]
        worker_type = clinic_code.get_worker_type()
        location_type = clinic_code.get_location_type()

        if is_already_valid_connection_type(self.msg.connection, worker_type):
            # 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 False

        try:
            location = Location.objects.get(slug__iexact=clinic_code.slug,
                                            type__slug__in=location_type)
            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(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)
Exemple #2
0
    def handle(self, text):

        tokens = self.check_message_valid_and_clean(text)
        
        if not tokens:
            return
        clinic_code = LocationCode(tokens[0])
        name = tokens[2]
        pin = tokens[4]
        worker_type = clinic_code.get_worker_type()
        location_type = clinic_code.get_location_type()
        
        if is_already_valid_connection_type(self.msg.connection, worker_type):
            # 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 False
        
        try:
            location = Location.objects.get(slug__iexact=clinic_code.slug,
                                            type__slug__in=location_type)
            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(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)
Exemple #3
0
    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_cba_type())
        return self.broadcast(text, contacts)
Exemple #4
0
    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_cba_type())
        return self.broadcast(text, contacts)
Exemple #5
0
    def handle(self, text):
        """
        Sends to all other contacts in the same district
        """
        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_district_worker_type())
        )
        return self.broadcast(text, contacts)
Exemple #6
0
    def handle(self, text):
        '''
        Sends to all other contacts in the same district
        '''
        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_district_worker_type())
        return self.broadcast(text, contacts)
Exemple #7
0
    def send_results (self, message):
        """
        Sends the actual results in response to the message
        (comes after PIN workflow).
        """
        results = self.waiting_for_pin[message.connection]
        clinic  = get_clinic_or_default(message.contact)
        if not results:
            # how did this happen?
            self.error("Problem reporting results for %s to %s -- there was nothing to report!" % \
                       (clinic, message.connection.contact))
            message.respond("Sorry, there are no new EID results for %s." % clinic)
            self.waiting_for_pin.pop(message.connection)
        else: 
            responses = build_results_messages(results)
            
            for resp in responses: 
                message.respond(resp)

            message.respond(INSTRUCTIONS, name=message.connection.contact.name)
            
            for r in results:
                r.notification_status = 'sent'
                r.result_sent_date = datetime.now()
                r.save()
                
            self.waiting_for_pin.pop(message.connection)
            
            # remove pending contacts for this clinic and notify them it 
            # was taken care of 
            clinic_connections = [contact.default_connection for contact in \
                                  Contact.active.filter\
                                  (location=clinic)]
            
            for conn in clinic_connections:
                if conn in self.waiting_for_pin:
                    self.waiting_for_pin.pop(conn)
                    OutgoingMessage(conn, RESULTS_PROCESSED, 
                                    name=message.connection.contact.name).send()
            
            self.last_collectors[clinic] = \
                        message.connection.contact
Exemple #8
0
 def handle(self, text):
     '''
     Sends to all other contacts in the same district
     '''
     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)        
     
     
     tokens = text.strip().lower().split(' ')
     if tokens[0] == 'example':
         self.respond(self.EXAMPLE_MESSAGE_ONE)
         self.respond(self.EXAMPLE_MESSAGE_TWO)
         return True
     
     group = self.PATTERN.search(text.strip())
     if not group:
         self.respond(self.MALFORMED_MESSAGE)
         return True
     
     tokens = group.groups()
     
     msg_part=text[len(tokens[0]):].strip()
     if tokens[0].lower() == 'dho':
         contacts = Contact.active.location(location)\
                         .exclude(id=self.msg.contact.id)\
                         .filter(types=get_district_worker_type())
         return self.broadcast(msg_part, contacts)
     elif tokens[0].lower() == 'all':
         contacts = \
         Contact.active.filter(location__slug__startswith=location.slug[:4]).\
         exclude(id=self.msg.contact.id)
         return self.broadcast(msg_part, contacts)
     else:
         self.help()
         
         
         
         
Exemple #9
0
 def default(self, message):
     # collect our bad pin responses, if they were generated.  See comment
     # in handle()
     if hasattr(message, "possible_bad_pin"):
         message.respond(BAD_PIN)                
         return True
     
     # additionally if this is your correct pin then respond that someone
     # has already processed the results (this assumes the only reason
     # you'd ever send your PIN in would be after receiving a notification)
     # This could be more robust, but keeping it simple.
     clinic = get_clinic_or_default(message.contact)
     if is_eligible_for_results(message.connection) \
        and clinic in self.last_collectors \
        and message.text.strip().upper() == message.contact.pin.upper():
         if message.contact == self.last_collectors[clinic]:
             message.respond(SELF_COLLECTED, name=message.connection.contact.name)
         else:
             message.respond(ALREADY_COLLECTED, name=message.connection.contact.name, 
                      collector=self.last_collectors[clinic])
         return True
     return self.mocker.default(message)
Exemple #10
0
 def handle (self, message):
     key = message.text.strip().upper()
     key = key[:4]
     
     if re.match(self.CHECK_REGEX, message.text, re.IGNORECASE):
         if not is_eligible_for_results(message.connection):
             message.respond(NOT_REGISTERED)
             return True
         
         clinic = get_clinic_or_default(message.contact)
         # this allows people to check the results for their clinic rather
         # than wait for them to be initiated by us on a schedule
         results = self._pending_results(clinic)
         if results:
             message.respond(RESULTS_READY, name=message.contact.name,
                             count=results.count())
             self._mark_results_pending(results, [message.connection])
         else:
             message.respond(NO_RESULTS, name=message.contact.name,
                             clinic=clinic.name)
         return True
     elif message.connection in self.waiting_for_pin \
        and message.connection.contact:
         pin = message.text.strip()
         if pin.upper() == message.connection.contact.pin.upper():
             self.send_results(message)
             return True
         else:
             # lets hide a magic field in the message so we can respond 
             # in the default phase if no one else catches this.  We 
             # don't respond here or return true in case this was a 
             # valid keyword for another app.
             message.possible_bad_pin = True
     
     # Finally, check if our mocker wants to do anything with this message, 
     # and notify the router if so.
     elif self.mocker.handle(message):
         return True
Exemple #11
0
    def handle(self, text):
        '''
        Sends to all other contacts in the same district
        '''
        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)

        tokens = text.strip().lower().split(' ')
        if tokens[0] == 'example':
            self.respond(self.EXAMPLE_MESSAGE_ONE)
            self.respond(self.EXAMPLE_MESSAGE_TWO)
            return True

        group = self.PATTERN.search(text.strip())
        if not group:
            self.respond(self.MALFORMED_MESSAGE)
            return True

        tokens = group.groups()

        msg_part = text[len(tokens[0]):].strip()
        if tokens[0].lower() == 'dho':
            contacts = Contact.active.location(location)\
                            .exclude(id=self.msg.contact.id)\
                            .filter(types=get_district_worker_type())
            return self.broadcast(msg_part, contacts)
        elif tokens[0].lower() == 'all':
            contacts = \
            Contact.active.filter(location__slug__startswith=location.slug[:4]).\
            exclude(id=self.msg.contact.id)
            return self.broadcast(msg_part, contacts)
        else:
            self.help()
Exemple #12
0
    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)
                     
        
Exemple #13
0
    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)
                     
        
Exemple #14
0
    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)