def clean_link_id(self): """Accepts link_id of users which may be invited. """ assert isSet(self.request_data.organization) invited_user = None link_id_cleaner = cleaning.clean_link_id('link_id') try: link_id = link_id_cleaner(self) except djangoforms.ValidationError, e: if e.code != 'invalid': raise email_cleaner = cleaning.clean_email('link_id') try: email_address = email_cleaner(self) except djangoforms.ValidationError, e: if e.code != 'invalid': raise msg = ugettext(u'Enter a valid link_id or email address.') raise djangoforms.ValidationError(msg, code='invalid')
def clean_email(self): email_cleaner = cleaning.clean_email('email') try: email_address = email_cleaner(self) except djangoforms.ValidationError as e: if e.code != 'invalid': raise msg = ugettext(u'Enter a valid email address.') raise djangoforms.ValidationError(msg, code='invalid') account = users.User(email_address) user_account = accounts.normalizeAccount(account) user = User.all().filter('account', user_account).get() if not user: raise djangoforms.ValidationError( "There is no user with that email address") self.cleaned_data['user'] = user q = GCIProfile.all() q.filter('program', self.request_data.program) q.ancestor(user) self.cleaned_data['profile'] = q.get()
def clean_email(self): email_cleaner = cleaning.clean_email('email') try: email_address = email_cleaner(self) except djangoforms.ValidationError, e: if e.code != 'invalid': raise msg = ugettext(u'Enter a valid email address.') raise djangoforms.ValidationError(msg, code='invalid')
def _clean_one_link_id(self): invited_user = None link_id_cleaner = cleaning.clean_link_id('link_id') try: link_id = link_id_cleaner(self) except djangoforms.ValidationError, e: if e.code != 'invalid': raise email_cleaner = cleaning.clean_email('link_id') try: email_address = email_cleaner(self) except djangoforms.ValidationError, e: if e.code != 'invalid': raise msg = ugettext(u'Enter a valid link_id or email address.') raise djangoforms.ValidationError(msg, code='invalid')
def _clean_identifier(self, identifier): user_to_invite = None # first check if the field represents a valid link_id try: existing_user_cleaner = cleaning.clean_existing_user('identifier') user_to_invite = existing_user_cleaner(self) except gci_forms.ValidationError, e: if e.code != 'invalid': raise # otherwise check if the field represents a valid email address email_cleaner = cleaning.clean_email('identifier') try: email = email_cleaner(self) except gci_forms.ValidationError, e: if e.code != 'invalid': raise msg = ugettext(u'Enter a valid link_id or email address.') raise gci_forms.ValidationError(msg, code='invalid')
def testCleanEmail(self): """Tests that an email is cleaned. """ field_name = 'test_email' clean_field = cleaning.clean_email(field_name) #Test that the same value is returned, the cleaned_data of the from does #not change and there is no error message if the value of the field has a #valid email field_value = '*****@*****.**' cleaned_data_before = {field_name: field_value} self.form.cleaned_data = cleaned_data_before.copy() self.assertEqual(clean_field(self.form), field_value) self.assertEqual(self.form.cleaned_data, cleaned_data_before) self.assertEqual(self.form._errors, {}) #Test that forms.ValidationError is raised if email is not valid. field_value = '#$test&*(' cleaned_data_before = {field_name: field_value} self.form.cleaned_data = cleaned_data_before.copy() self.assertRaises(forms.ValidationError, clean_field, self.form) self.assertEqual(self.form.cleaned_data, cleaned_data_before) self.assertEqual(self.form._errors, {})
def clean_email(self): email_cleaner = cleaning.clean_email("email") try: email_address = email_cleaner(self) except djangoforms.ValidationError as e: if e.code != "invalid": raise msg = ugettext(u"Enter a valid email address.") raise djangoforms.ValidationError(msg, code="invalid") account = users.User(email_address) user_account = accounts.normalizeAccount(account) user = User.all().filter("account", user_account).get() if not user: raise djangoforms.ValidationError("There is no user with that email address") self.cleaned_data["user"] = user q = GCIProfile.all() q.filter("program", self.request_data.program) q.ancestor(user) self.cleaned_data["profile"] = q.get()
class ProfileForm(forms.ModelForm): """Django form for profile page. """ TWO_LETTER_STATE_REQ = ['United States', 'Canada'] def __init__(self, bound_field_class, request_data=None, **kwargs): super(ProfileForm, self).__init__(bound_field_class, **kwargs) self.fields['given_name'].group = "2. Contact Info (Private)" self.fields['surname'].group = "2. Contact Info (Private)" self.request_data = request_data self.program = request_data.program if request_data else None public_name = fields.CharField(required=True) clean_public_name = cleaning.clean_html_content('public_name') clean_name_on_documents = cleaning.clean_html_content('name_on_documents') clean_im_network = cleaning.clean_html_content('im_network') clean_im_handle = cleaning.clean_html_content('im_handle') clean_program_knowledge = cleaning.clean_html_content('program_knowledge') clean_email = cleaning.clean_email('email') clean_phone = cleaning.clean_phone_number('phone') def clean_given_name(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['given_name']) def clean_surname(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['surname']) def clean_res_street(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['res_street']) def clean_res_city(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['res_city']) def clean_res_street_extra(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['res_street_extra']) def clean_res_state(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['res_state']) def clean_res_postalcode(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['res_postalcode']) def clean_ship_name(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['ship_name']) def clean_ship_street(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['ship_street']) def clean_ship_street_extra(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['ship_street_extra']) def clean_ship_city(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['ship_city']) def clean_ship_state(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['ship_state']) def clean_ship_postalcode(self): return cleaning.cleanValidAddressCharacters( self.cleaned_data['ship_postalcode']) clean_home_page = cleaning.clean_url('home_page') clean_blog = cleaning.clean_url('blog') clean_photo_url = cleaning.clean_url('photo_url') def clean(self): country = self.cleaned_data.get('res_country') state = self.cleaned_data.get('res_state') if country in self.TWO_LETTER_STATE_REQ and (not state or len(state) != 2): self._errors['res_state'] = [ "Please use a 2-letter state/province name" ] country = self.cleaned_data.get('ship_country') state = self.cleaned_data.get('ship_state') if country in self.TWO_LETTER_STATE_REQ and (not state or len(state) != 2): self._errors['ship_state'] = [ "Please use a 2-letter state/province name" ] return self.cleaned_data