def save()

in phones/models.py [0:0]


    def save(self, *args, **kwargs):
        try:
            realphone = RealPhone.verified_objects.get(user=self.user)
        except RealPhone.DoesNotExist:
            raise ValidationError("User does not have a verified real phone.")

        # if this number exists for this user, this is an update call
        existing_numbers = RelayNumber.objects.filter(user=self.user)
        this_number = existing_numbers.filter(number=self.number).first()
        update_user_profile_last_engagement = False
        if this_number and this_number.id == self.id:
            update_user_profile_last_engagement = any(
                [
                    self.enabled != this_number.enabled,
                    self.calls_forwarded != this_number.calls_forwarded,
                    self.calls_blocked != this_number.calls_blocked,
                    self.texts_forwarded != this_number.texts_forwarded,
                    self.texts_blocked != this_number.texts_blocked,
                ]
            )
            if update_user_profile_last_engagement:
                self.user.profile.last_engagement = datetime.now(UTC)
                self.user.profile.save()
            return super().save(*args, **kwargs)
        elif existing_numbers.exists():
            raise ValidationError("User can have only one relay number.")

        if RelayNumber.objects.filter(number=self.number).exists():
            raise ValidationError("This number is already claimed.")

        use_twilio = (
            self.vendor == "twilio" and not settings.PHONES_NO_CLIENT_CALLS_IN_TEST
        )

        if use_twilio:
            # Before saving into DB provision the number in Twilio
            client = twilio_client()

            # Since this will charge the Twilio account, first see if this
            # is running with TEST creds to avoid charges.
            if settings.TWILIO_TEST_ACCOUNT_SID:
                client = phones_config().twilio_test_client

            twilio_incoming_number = client.incoming_phone_numbers.create(
                phone_number=self.number,
                sms_application_sid=settings.TWILIO_SMS_APPLICATION_SID,
                voice_application_sid=settings.TWILIO_SMS_APPLICATION_SID,
            )

        # Assume number was selected through suggested_numbers, so same country
        # as realphone
        self.country_code = realphone.country_code.upper()

        # Add numbers to the Relay messaging service, so it goes into our
        # A2P 10DLC campaigns
        if use_twilio and self.country_code in settings.TWILIO_NEEDS_10DLC_CAMPAIGN:
            if settings.TWILIO_MESSAGING_SERVICE_SID:
                register_with_messaging_service(client, twilio_incoming_number.sid)
            else:
                events_logger.warning(
                    "Skipping Twilio Messaging Service registration, since"
                    " TWILIO_MESSAGING_SERVICE_SID is empty.",
                    extra={"number_sid": twilio_incoming_number.sid},
                )

        return super().save(*args, **kwargs)