示例#1
0
    def validate_phone(self, phone):
        instance = self.instance
        if (instance and phone != instance.phone and
                self.context['request'].user != instance):
            raise serializers.ValidationError(_("Cannot update phone"))

        if instance and instance.phone == phone:
            return phone

        if phone:
            # make sure that the new phone updated by a user is not a duplicate
            # of an unverified phone already linked to a different user
            if VerificationDevice.objects.filter(
                    unverified_phone=phone).exists():
                raise serializers.ValidationError(
                    _("User with this Phone number already exists."))
            try:
                parse_phone(phone)
            except NumberParseException:
                raise serializers.ValidationError(
                    _("Please enter a valid country code."))
        else:
            phone = None

            if (instance and instance.phone and
                    not instance.email and self.initial_data.get('email')):
                raise serializers.ValidationError(
                    _('It is not possible to change from phone to email for '
                      'your account identifier.'))
        return phone
示例#2
0
 def clean_phone(self):
     phone = self.data.get('phone')
     if phone:
         if VerificationDevice.objects.filter(
                 unverified_phone=phone).exists():
             raise forms.ValidationError(
                 _("User with this Phone number already exists."))
         try:
             parse_phone(phone)
         except NumberParseException:
             raise forms.ValidationError(
                 _("Please enter a valid country code."))
     else:
         phone = None
     return phone
示例#3
0
    def validate_phone(self, phone):
        if phone:
            if VerificationDevice.objects.filter(
                    unverified_phone=phone).exists():
                raise serializers.ValidationError(
                    _("User with this Phone number already exists."))
            try:
                parse_phone(phone)
            except NumberParseException:
                raise serializers.ValidationError(
                    _("Please enter a valid country code."))
        else:
            phone = None

        return phone
示例#4
0
    def clean_password(self):
        password = self.data.get('password')
        validate_password(password)
        errors = []

        email = self.data.get('email')
        if email:
            email = email.split('@')
            if email[0].casefold() in password.casefold():
                errors.append(_("Passwords cannot contain your email."))

        username = self.data.get('username')
        if len(username) and username.casefold() in password.casefold():
            errors.append(_("The password is too similar to the username."))

        phone = self.data.get('phone')
        if phone and phone_validator(phone):
            try:
                phone = str(parse_phone(phone).national_number)
                if phone in password:
                    errors.append(_("Passwords cannot contain your phone."))
            except NumberParseException:
                pass

        if errors:
            raise forms.ValidationError(errors)

        return password
示例#5
0
    def phone(self, phone_number):
        """ Find businesses by phone. """

        parsed_phone = parse_phone(phone_number, 'US')
        prepared_phone = format_number(parsed_phone, PhoneNumberFormat.E164)

        url = self.api % 'phone_search'
        parameters = {'phone': prepared_phone}
        response = self._request(url, parameters)
        print response['businesses'][0]['categories']
        return self._ids(response)
示例#6
0
    def validate_new_password(self, password):
        user = self.context['request'].user
        validate_password(password, user=user)

        username = user.username
        if len(username) and username.casefold() in password.casefold():
            raise serializers.ValidationError(
                _("The password is too similar to the username."))

        phone = user.phone
        if phone and phone_validator(phone):
            phone = str(parse_phone(phone).national_number)
            if phone in password:
                raise serializers.ValidationError(
                    _("Passwords cannot contain your phone."))

        return password
示例#7
0
    def clean_password(self):
        if not self.user or not self.user.update_profile:
            raise forms.ValidationError(
                _("The password for this user can not "
                  "be changed."))

        password = self.cleaned_data['password']
        validate_password(password, user=self.user)

        username = self.user.username
        if len(username) and username.casefold() in password.casefold():
            raise forms.ValidationError(
                _("The password is too similar to the username."))

        phone = self.user.phone
        if phone and phone_validator(phone):
            phone = str(parse_phone(phone).national_number)
            if phone in password:
                raise forms.ValidationError(
                    _("Passwords cannot contain your phone."))
        return password
示例#8
0
 def validate_phone(self, value):
     try:
         parse_phone(value)
     except phonenumberutil.NumberParseException:
         raise ValidationError("invalid phone number", "phone")
示例#9
0
 def clean_username(self):
     try:
         phone = parse_phone(self.cleaned_data['username'])
         return format_number(phone, PhoneNumberFormat.E164)
     except Exception as e:
         raise ValidationError("Phone number is not valid") from e