Package madrona :: Package openid :: Module forms
[hide private]

Source Code for Module madrona.openid.forms

  1  # -*- coding: utf-8 -*- 
  2  # Copyright 2007, 2008,2009 by Benoît Chesneau <benoitc@e-engura.org> 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  # 
 16  import re 
 17   
 18  from django import forms 
 19  from django.contrib.auth.models import User 
 20  from django.contrib.auth import authenticate 
 21  from django.utils.translation import ugettext as _ 
 22  from django.conf import settings 
 23  # needed for some linux distributions like debian 
 24  try: 
 25      from openid.yadis import xri 
 26  except ImportError: 
 27      from yadis import xri 
 28   
 29  from madrona.openid.models import UserAssociation 
 30   
 31   
32 -class OpenidSigninForm(forms.Form):
33 """ signin form """ 34 openid_url = forms.CharField(max_length=255, 35 widget=forms.widgets.TextInput(attrs={'class': 'required openid'})) 36
37 - def clean_openid_url(self):
38 """ test if openid is accepted """ 39 if 'openid_url' in self.cleaned_data: 40 openid_url = self.cleaned_data['openid_url'] 41 if xri.identifierScheme(openid_url) == 'XRI' and getattr( 42 settings, 'OPENID_DISALLOW_INAMES', False 43 ): 44 raise forms.ValidationError(_('i-names are not supported')) 45 return self.cleaned_data['openid_url']
46 47 attrs_dict = {'class': 'required login'} 48 username_re = re.compile(r'^\w+$') 49
50 -class OpenidRegisterForm(forms.Form):
51 """ openid signin form """ 52 username = forms.CharField(max_length=30, 53 widget=forms.widgets.TextInput(attrs=attrs_dict)) 54 email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, 55 maxlength=200)), label=u'Email address') 56
57 - def __init__(self, *args, **kwargs):
58 super(OpenidRegisterForm, self).__init__(*args, **kwargs) 59 self.user = None
60
61 - def clean_username(self):
62 """ test if username is valid and exist in database """ 63 if 'username' in self.cleaned_data: 64 if not username_re.search(self.cleaned_data['username']): 65 raise forms.ValidationError(_("Usernames can only contain \ 66 letters, numbers and underscores")) 67 try: 68 user = User.objects.get( 69 username__exact=self.cleaned_data['username'] 70 ) 71 except User.DoesNotExist: 72 return self.cleaned_data['username'] 73 except User.MultipleObjectsReturned: 74 raise forms.ValidationError(u'There is already more than one \ 75 account registered with that username. Please try \ 76 another.') 77 self.user = user 78 raise forms.ValidationError(_("This username is already \ 79 taken. Please choose another."))
80
81 - def clean_email(self):
82 """For security reason one unique email in database""" 83 if 'email' in self.cleaned_data: 84 try: 85 user = User.objects.get(email=self.cleaned_data['email']) 86 except User.DoesNotExist: 87 return self.cleaned_data['email'] 88 except User.MultipleObjectsReturned: 89 raise forms.ValidationError(u'There is already more than one \ 90 account registered with that e-mail address. Please try \ 91 another.') 92 raise forms.ValidationError(_("This email is already \ 93 registered in our database. Please choose another."))
94 95
96 -class AssociateOpenID(forms.Form):
97 """ new openid association form """ 98 openid_url = forms.CharField(max_length=255, 99 widget=forms.widgets.TextInput(attrs={'class': 'required openid'})) 100
101 - def __init__(self, user, *args, **kwargs):
102 super(AssociateOpenID, self).__init__(*args, **kwargs) 103 self.user = user
104
105 - def clean_openid_url(self):
106 """ test if openid is accepted """ 107 if 'openid_url' in self.cleaned_data: 108 openid_url = self.cleaned_data['openid_url'] 109 if xri.identifierScheme(openid_url) == 'XRI' and getattr( 110 settings, 'OPENID_DISALLOW_INAMES', False 111 ): 112 raise forms.ValidationError(_('i-names are not supported')) 113 114 try: 115 rel = UserAssociation.objects.get(openid_url__exact=openid_url) 116 except UserAssociation.DoesNotExist: 117 return self.cleaned_data['openid_url'] 118 119 if rel.user != self.user: 120 raise forms.ValidationError(_("This openid is already \ 121 registered in our database by another account. Please choose another.")) 122 123 raise forms.ValidationError(_("You already associated this openid to your account."))
124
125 -class OpenidDissociateForm(OpenidSigninForm):
126 """ form used to dissociate an openid. """ 127 openid_url = forms.CharField(max_length=255, widget=forms.widgets.HiddenInput())
128