class PasswordMixin(object): password = wtforms.fields.PasswordField(i18n.lazy_gettext('Password'), [ wtforms.validators.InputRequired(), wtforms.validators.Length( max=FIELD_MAXLENGTH, message=i18n.lazy_gettext( "Field cannot be longer than %(max)d characters.")) ])
class UsernameMixin(object): username = wtforms.fields.StringField(i18n.lazy_gettext('Username'), [ wtforms.validators.InputRequired(), UniqueValueValidation, wtforms.validators.Length( max=FIELD_MAXLENGTH, message=i18n.lazy_gettext( "Field cannot be longer than %(max)d characters.")), ])
class AuthIDMixin(object): auth_id = wtforms.fields.StringField( i18n.lazy_gettext('Username or Email Address'), [ wtforms.validators.InputRequired(), wtforms.validators.Length( max=FIELD_MAXLENGTH, message=i18n.lazy_gettext( "Field cannot be longer than %(max)d characters.")), ])
class TermsAgreedMixin(object): terms_agreed = wtforms.fields.BooleanField( i18n.lazy_gettext( 'I confirm that I have read the terms and conditions.'), [ wtforms.validators.InputRequired( i18n.lazy_gettext( 'Please tick to confirm that you have read the terms and conditions of entry.' )) ])
class ConfirmPasswordMixin(PasswordMixin): c_password = wtforms.fields.PasswordField( i18n.lazy_gettext('Confirm Password'), [ wtforms.validators.InputRequired(), wtforms.validators.EqualTo( 'password', i18n.lazy_gettext('Passwords must match.')), wtforms.validators.Length( max=FIELD_MAXLENGTH, message=i18n.lazy_gettext( "Field cannot be longer than %(max)d characters.")) ])
class GAESearchLimitMixin(object): limit = wtforms.fields.SelectField(i18n.lazy_gettext('Results per page'), [ wtforms.validators.Length( max=1000, message=i18n.lazy_gettext( "The maximum results per page is %(max)d.")), ], choices=((u'5', u'5'), (u'20', u'20'), (u'50', u'50'), (u'100', u'100'), (u'250', u'250')), default=u'5')
class PhoneMixin(object): phone = wtforms.fields.StringField(i18n.lazy_gettext('Phone'), [ wtforms.validators.InputRequired(), wtforms.validators.Length( max=PHONE_MAXLENGTH, message=i18n.lazy_gettext( "Phone number must be less than %(max)d characters long.")), wtforms.validators.regexp( MOBILE_REGEXP, message=i18n.lazy_gettext( 'Invalid characters found in phone number.')) ])
class MobilePhoneMixin(object): mobile = wtforms.fields.StringField(i18n.lazy_gettext('Mobile'), [ wtforms.validators.Length( min=8, max=15, message=i18n.lazy_gettext( "Mobile number must be between %(min)d and %(max)d characters long." )), wtforms.validators.regexp( MOBILE_REGEXP, message=i18n.lazy_gettext( 'Invalid characters found in mobile number.')) ])
class EmailAddressMixin(object): email_address = wtforms.fields.StringField(i18n.lazy_gettext('Email'), [ wtforms.validators.InputRequired(), UniqueValueValidation, wtforms.validators.Length( max=FIELD_MAXLENGTH, message=i18n.lazy_gettext( "Email address must be less than between %(max)d characters long." )), wtforms.validators.regexp( EMAIL_REGEXP, flags=re.UNICODE, message=i18n.lazy_gettext('Invalid email address.')) ])
class LanguagePreferenceMixin(object): language_preference = wtforms.fields.SelectField( i18n.lazy_gettext('Preferred Language'), [ # custom validator to check that value is in list specified. LanguageCodeValidation, wtforms.validators.InputRequired(), wtforms.validators.Length( max=2, message=i18n.lazy_gettext( "Language code must be %(max)d characters.")), ], choices=STATIC_LANGUAGE_CODES_TUPLE, default='en')
class UserSortOptions(GAESortOptions): sort_by = wtforms.fields.SelectField( i18n.lazy_gettext(u'Sort By'), [], choices=( (u'NONE', i18n.lazy_gettext(u'None')), (u'username', i18n.lazy_gettext(u'Username')), (u'first_name', i18n.lazy_gettext(u'First Name')), (u'last_name', i18n.lazy_gettext(u'Last Name')), (u'email_address', i18n.lazy_gettext(u'Primary Email')), (u'recovery_email_address', i18n.lazy_gettext(u'Recovery Email')), (u'language_preference', i18n.lazy_gettext(u'Language')), (u'created', i18n.lazy_gettext(u'Registered')), (u'updated', i18n.lazy_gettext(u'Updated')), ), default=DEFAULT_NONE_VALUE)
class NameMixin(object): first_name = wtforms.fields.StringField(i18n.lazy_gettext('First Name'), [ wtforms.validators.InputRequired(), wtforms.validators.Length( max=FIELD_MAXLENGTH, message=i18n.lazy_gettext( "Field cannot be longer than %(max)d characters.")), ]) last_name = wtforms.fields.StringField(i18n.lazy_gettext('Last Name'), [ wtforms.validators.InputRequired(), wtforms.validators.Length( max=FIELD_MAXLENGTH, message=i18n.lazy_gettext( "Field cannot be longer than %(max)d characters.")), ])
class UserFilterOptions(GAEFilterOptions): language_preference = wtforms.fields.SelectField( i18n.lazy_gettext(u'Preferred Language'), [ LanguageCodeValidationWithDefaultSupport, ], choices=LANG_TUPLE, default=DEFAULT_NONE_VALUE)
class PlaceholderForm(BaseForm): """ We remove the CSRF protection from the placeholder form; chances are if you are using it then you don't have a session mechanism setup, meaning there would be nowhere to save the csrf token for verification. """ class Meta: csrf = False required_input = wtforms.fields.StringField( label=i18n.lazy_gettext('Required Input'), default='Here is some text', validators=[wtforms.validators.InputRequired()])
class RecaptchaMixin(object): recaptcha2 = Recaptcha2InputField( i18n.lazy_gettext('reCaptcha'), [], )
class SortDirectionMixin(object): sort_direction = wtforms.fields.SelectField( i18n.lazy_gettext('Sort Direction'), [], choices=((u'ASCENDING', u'Ascending'), (u'DESCENDING', u'Descending')), default=u'ASCENDING')
class SortByPlaceholderMixin(object): sort_by = wtforms.fields.SelectField(i18n.lazy_gettext(u'Sort By'), [], choices=((DEFAULT_NONE_VALUE, u'No Sort')), default=DEFAULT_NONE_VALUE)
class SearchWithFiltersMixin(object): query = wtforms.fields.StringField(i18n.lazy_gettext('Search'), [ SearchQueryRequired, ])
class SearchField(object): query = wtforms.fields.StringField(i18n.lazy_gettext('Search'), [ wtforms.validators.InputRequired(), ])
def test_lazy_gettext(self): self.assertEqual(i18n.lazy_gettext("foo"), "foo")
def test_lazy_gettext(self): self.assertEqual(i18n.lazy_gettext('foo'), u'foo')
class DeleteModelForm(BaseForm, BaseModelMixin): confirm = wtforms.fields.BooleanField( i18n.lazy_gettext('I confirm that I want to delete this item'), validators=[wtforms.validators.InputRequired()])
class AddressMixin(object): address_1 = wtforms.fields.StringField(i18n.lazy_gettext('Address 1'), [ wtforms.validators.InputRequired(), wtforms.validators.Length( max=ADDRESS_MAXLENGTH, message=i18n.lazy_gettext( "Address 1 cannot be longer than %(max)d characters.")), ]) address_2 = wtforms.fields.StringField(i18n.lazy_gettext('Address 2'), [ wtforms.validators.Length( max=ADDRESS_MAXLENGTH, message=i18n.lazy_gettext( "Address 2 cannot be longer than %(max)d characters.")), ]) address_3 = wtforms.fields.StringField(i18n.lazy_gettext('Address 3'), [ wtforms.validators.Length( max=ADDRESS_MAXLENGTH, message=i18n.lazy_gettext( "Address 3 cannot be longer than %(max)d characters.")), ]) city = wtforms.fields.StringField(i18n.lazy_gettext('City'), [ wtforms.validators.InputRequired(), wtforms.validators.Length( max=CITY_MAXLENGTH, message=i18n.lazy_gettext( "City cannot be longer than %(max)d characters.")), ]) state = wtforms.fields.StringField( i18n.lazy_gettext('County/State'), [ # In theory county will now be an alphanumeric code so length is not required # Need to check that US states work when passing to SagePay wtforms.validators.Length( max=CITY_MAXLENGTH, message=i18n.lazy_gettext( "County/State cannot be longer than %(max)d characters.")), ]) county = ExtendedSelectField( i18n.lazy_gettext('County (Required for UK companies)'), [ UKCountyValidation, ], choices=UK_COUNTIES_TUPLE) post_code = wtforms.fields.StringField( i18n.lazy_gettext('Post Code/Zip Code (Required for UK companies)'), [ UKPostCodeValidation, ]) country = wtforms.fields.SelectField( i18n.lazy_gettext('Country'), [ # custom validator to check that value is in list specified. CountryCodeValidation, wtforms.validators.InputRequired(), wtforms.validators.Length( max=2, message=i18n.lazy_gettext( "Country code must be %(max)d characters.")), ], choices=STATIC_COUNTRY_LABLES_TUPLE, default='GB')
class TwitterMixin(object): twitter = wtforms.fields.StringField(i18n.lazy_gettext('Twitter'), [])
class FacebookMixin(object): facebook = wtforms.fields.StringField(i18n.lazy_gettext('Facebook'), [])
import re import json import urllib import urllib2 import wtforms from datetime import datetime import wtforms.csrf.session as csrf_lib import webapp2_extras.i18n as i18n from .utils import STATIC_LANGUAGE_CODES_TUPLE, UK_COUNTY_SET, STATIC_COUNTRY_CODES_SET, STATIC_LANGUAGE_CODES_SET, UK_COUNTIES_TUPLE, STATIC_COUNTRY_LABLES_TUPLE, US_STATES_SET, eu_country DEFAULT_NONE_VALUE = u'NONE' BOOLEAN_VALUE_TUPLE = ( (DEFAULT_NONE_VALUE, u'--'), (u'yes', i18n.lazy_gettext(u'Yes')), (u'no', i18n.lazy_gettext(u'No')), ) REQUEST_CONFIG_KEYS = [u'csrf_config'] # These keys are provided by google for testing. They will always validate. NEVER use them in production. TEST_RECAPTCHA_SITE_KEY = u'6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI' TEST_RECAPTCHA_SITE_SECRET = u'6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe' # Constant values to be used in other parts of the package. FIELD_MAXLENGTH = 50 # intended to stop maliciously long input ADDRESS_MAXLENGTH = 100 CITY_MAXLENGTH = 40 PHONE_MAXLENGTH = 20
class WebsiteMixin(object): website = wtforms.fields.StringField(i18n.lazy_gettext('Website'), [])
import re import json import urllib import urllib2 import wtforms from datetime import datetime import wtforms.csrf.session as csrf_lib import webapp2_extras.i18n as i18n from .utils import STATIC_LANGUAGE_CODES_TUPLE, UK_COUNTY_SET, STATIC_COUNTRY_CODES_SET, STATIC_LANGUAGE_CODES_SET, UK_COUNTIES_TUPLE, STATIC_COUNTRY_LABLES_TUPLE, US_STATES_SET, eu_country DEFAULT_NONE_VALUE = u'NONE' BOOLEAN_VALUE_TUPLE = ( (DEFAULT_NONE_VALUE, u'--'), (u'yes', i18n.lazy_gettext(u'Yes')), (u'no', i18n.lazy_gettext(u'No')), ) REQUEST_CONFIG_KEYS = [u'csrf_config'] # These keys are provided by google for testing. They will always validate. NEVER use them in production. TEST_RECAPTCHA_SITE_KEY = u'6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI' TEST_RECAPTCHA_SITE_SECRET = u'6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe' # Constant values to be used in other parts of the package. FIELD_MAXLENGTH = 50 # intended to stop maliciously long input ADDRESS_MAXLENGTH = 100 CITY_MAXLENGTH = 40 PHONE_MAXLENGTH = 20 EMAIL_REGEXP = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"