def save(self, *args, **kwargs):
        if self.contact_phone_number:

            if self.pk is not None:
                oldItem = Patient.objects.get(pk=self.pk)

                if oldItem.health_facility_id != self.health_facility_id:
                    # If the health facility has changed send out a message to
                    # the caregiver.
                    text = wordings.get_patient_location_message(self)
                    tasks.send_sms.delay(self.contact_phone_number, text)

                if oldItem.status != self.status:
                    # If the status has changed send out a message to the
                    # caregiver.
                    text = wordings.get_patient_status_message(self)
                    tasks.send_sms.delay(self.contact_phone_number, text)

            else:
                # Send the text messages
                text = wordings.get_patient_info_message(self)
                tasks.send_sms.delay(self.contact_phone_number, text)

                text = wordings.get_reply_info_message(self)
                tasks.send_sms.delay(self.contact_phone_number, text)

                tasks.send_sms.delay(
                    self.contact_phone_number,
                    wordings.INITIAL_MESSAGE,
                )

                if self.health_facility:
                    text = wordings.get_patient_location_message(self)
                    tasks.send_sms.delay(self.contact_phone_number, text)

                if self.status:
                    text = wordings.get_patient_status_message(self)
                    tasks.send_sms.delay(self.contact_phone_number, text)

        super(Patient, self).save(*args,
                                  **kwargs)  # Call the "real" save() method.
    def save(self, *args, **kwargs):
        if self.contact_phone_number:

            if self.pk is not None:
                oldItem = Patient.objects.get(pk=self.pk)

                if oldItem.health_facility_id != self.health_facility_id:
                    # If the health facility has changed send out a message to
                    # the caregiver.
                    text = wordings.get_patient_location_message(self)
                    tasks.send_sms.delay(self.contact_phone_number, text)

                if oldItem.status != self.status:
                    # If the status has changed send out a message to the
                    # caregiver.
                    text = wordings.get_patient_status_message(self)
                    tasks.send_sms.delay(self.contact_phone_number, text)

            else:
                # Send the text messages
                text = wordings.get_patient_info_message(self)
                tasks.send_sms.delay(self.contact_phone_number, text)

                text = wordings.get_reply_info_message(self)
                tasks.send_sms.delay(self.contact_phone_number, text)

                tasks.send_sms.delay(self.contact_phone_number, wordings.INITIAL_MESSAGE)

                if self.health_facility:
                    text = wordings.get_patient_location_message(self)
                    tasks.send_sms.delay(self.contact_phone_number, text)

                if self.status:
                    text = wordings.get_patient_status_message(self)
                    tasks.send_sms.delay(self.contact_phone_number, text)

        super(Patient, self).save(*args, **kwargs)  # Call the "real" save() method.
def smswebhook(request):
    """
    This is called by the sms gateway. For now this is rapidpro
    """

    # Check if the number is in the cache
    if not cache.get(request.POST["phone"]):
        # Only allow a call every 10 seconds
        cache.set(request.POST["phone"], '', 5)

        long_cache_name = "long_%s" % request.POST["phone"]

        if cache.get(long_cache_name):
            cache.set(long_cache_name, cache.get(long_cache_name) + 1)
        else:
            cache.set(long_cache_name, 1)

        if cache.get(long_cache_name) >= 25:
            params = {
                'phone': request.POST["phone"],
                'text': wordings.TOO_MANY_REQUESTS_EVER
            }
            return HttpResponse(json.dumps(params))
    else:
        params = {
            'phone': request.POST["phone"],
            'text': wordings.TOO_MANY_REQUESTS
        }
        return HttpResponse(json.dumps(params))

    sms_content = request.POST["text"]

    patient_code_regex = re.compile(PATIENT_REGEX)
    patient_codes = patient_code_regex.findall(sms_content)

    if len(patient_codes) == 1:
        # Seams to be a patient reference

        if Patient.objects.filter(info_code__iexact=sms_content).count() == 1:
            pat = Patient.objects.get(info_code__iexact=sms_content)

            if pat.health_facility:
                text = wordings.get_patient_location_message(pat)

                if pat.status:
                    text = wordings.get_patient_status_message(pat)
                    tasks.send_sms.delay(pat.contact_phone_number, text)

            else:
                text = wordings.PATIENT_NO_INFO

            params = {
                'phone': request.POST["phone"],
                'text': text
            }

        else:
            params = {
                'phone': request.POST["phone"],
                'text': wordings.PATIENT_NOT_FOUND
            }
    else:
        params = {
            'phone': request.POST["phone"],
            'text': wordings.INVALID_ID
        }

    return HttpResponse(json.dumps(params))