Exemplo n.º 1
0
 def validate_phone(self, field):
     # Step 1: Validate number
     try:
         # Assume Indian number if no country code is specified
         # TODO: Guess country from IP address
         parsed_number = phonenumbers.parse(field.data, 'IN')
         if not phonenumbers.is_valid_number(parsed_number):
             raise ValueError("Invalid number")
     except (phonenumbers.NumberParseException, ValueError):
         raise forms.StopValidation(
             _("This does not appear to be a valid phone number"))
     number = phonenumbers.format_number(
         parsed_number, phonenumbers.PhoneNumberFormat.E164)
     # Step 2: Check if number has already been claimed
     existing = UserPhone.get(phone=number)
     if existing is not None:
         if existing.user == current_auth.user:
             raise forms.ValidationError(
                 _("You have already registered this phone number"))
         else:
             raise forms.ValidationError(
                 _("This phone number has already been claimed"))
     existing = UserPhoneClaim.get_for(user=current_auth.user, phone=number)
     if existing is not None:
         raise forms.ValidationError(
             _("This phone number is pending verification"))
     # Step 3: If validations pass, use the reformatted number
     field.data = number  # Save stripped number
Exemplo n.º 2
0
def validate_unique_discount_coupon_code(form, field):
    if (DiscountCoupon.query.join(DiscountPolicy).filter(
            DiscountPolicy.organization == form.edit_parent.organization,
            DiscountCoupon.code == field.data,
    ).notempty()):
        raise forms.StopValidation(
            __("This discount coupon code already exists. Please enter a different coupon code"
               ))
Exemplo n.º 3
0
def optional_url(form, field):
    """
    Validate URL only if present.
    """
    if not field.data:
        raise forms.validators.StopValidation()
    else:
        if '://' not in field.data:
            field.data = 'http://' + field.data
        validator = forms.validators.URL(message="This does not appear to be a valid URL.")
        try:
            return validator(form, field)
        except forms.ValidationError as e:
            raise forms.StopValidation(unicode(e))
Exemplo n.º 4
0
def validate_emailclaim(form, field):
    existing = UserEmailClaim.get_for(user=current_auth.user, email=field.data)
    if existing is not None:
        raise forms.StopValidation(
            _("This email address is pending verification"))