def verify_phone(phone, **kwargs): phone = phonenumbers.parse(phone, "US") if not phonenumbers.is_valid_number(phone): raise utils.APIException("Invalid phone number") if phone.country_code != 1: raise utils.APIException("We only support US/CA numbers now :(") phone = utils.e164(phone) ip = _try_to_get_ip(kwargs) yesterday = utils.now() - timedelta(days=1) five = utils.now() - timedelta(seconds=5) if db.PhoneVerify.objects.filter(phone=phone, created__gt=five).count(): return [] invalid = False invalid |= db.UserInfo.objects.filter(phone=phone).count() > 0 invalid |= db.PhoneBlacklist.objects.filter(phone=phone).count() > 0 invalid |= ( db.PhoneVerify.objects.filter(phone=phone, created__gt=yesterday).count() > settings.SPARK_AUTH_MAX_PHONE_DAY ) if ip: invalid |= ( db.PhoneVerify.objects.filter(ip=ip, created__gt=yesterday).count() > settings.SPARK_AUTH_MAX_PHONE_DAY ) if invalid: raise utils.APIException("invalid phone number") code = db.base36(4) db.PhoneVerify.objects.create(phone=phone, code=code, ip=ip) message.sms_send(phone, "sparkles phone auth code: " + code) return []
def clean_phone(self): phone = phonenumbers.parse(self.data.get('phone'), 'US') if not phonenumbers.is_valid_number(phone): raise forms.ValidationError("Invalid phone number") if phone.country_code != 1: print phone.country_code raise forms.ValidationError('We only support US/CA numbers now') phone = utils.e164(phone) invalid = False invalid |= (db.UserInfo.objects.filter(phone=phone).count() > 0) invalid |= (db.PhoneBlacklist.objects.filter(phone=phone).count() > 0) if invalid: raise forms.ValidationError("Incorrect phone verify code") return phone