Changeset 2507
- Timestamp:
- 01/27/10 16:56:14 (7 weeks ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/0.9/pylucid_project/pylucid_plugins/auth/forms.py
r2001 r2507 5 5 from django.contrib import auth 6 6 from django.contrib.auth.models import User 7 from django.utils.translation import ugettext as _ 7 8 8 9 from pylucid_project.utils import crypt … … 36 37 ) 37 38 sha_b = forms.CharField( 38 min_length=crypt.HASH_LEN /2, max_length=crypt.HASH_LEN/239 min_length=crypt.HASH_LEN / 2, max_length=crypt.HASH_LEN / 2 39 40 ) 40 41 … … 53 54 54 55 # Fill with null, to match the full SHA1 hexdigest length. 55 fill_len = crypt.HASH_LEN - (crypt.HASH_LEN /2)56 fill_len = crypt.HASH_LEN - (crypt.HASH_LEN / 2) 56 57 temp_value = ("0" * fill_len) + sha_value 57 58 … … 69 70 # Should normaly never be send back! 70 71 raw_password = forms.CharField( 71 help_text="(required)", required=False, widget =forms.PasswordInput()72 help_text="(required)", required=False, widget=forms.PasswordInput() 72 73 ) 73 74 … … 75 76 label="SHA1 for django", 76 77 help_text="(automatic generated with JavaScript.)", 77 widget =forms.TextInput(attrs={"readonly":"readonly", "size":"40"}),78 widget=forms.TextInput(attrs={"readonly":"readonly", "size":"40"}), 78 79 min_length=crypt.HASH_LEN, max_length=crypt.HASH_LEN 79 80 ) … … 81 82 label="SHA1 for PyLucid", 82 83 help_text="(automatic generated with JavaScript.)", 83 widget =forms.TextInput(attrs={"readonly":"readonly", "size":"40"}),84 widget=forms.TextInput(attrs={"readonly":"readonly", "size":"40"}), 84 85 min_length=crypt.HASH_LEN, max_length=crypt.HASH_LEN 85 86 ) … … 98 99 # FORMS 99 100 100 class BaseModelForm(forms.ModelForm): 101 """ 102 A model form witch don't validate unique fields. 103 104 This ModelForm is only for generating the forms and not for create/update 105 any database data. So a field unique Test would like generate Errors like: 106 User with this Username already exists. 107 108 see also: 109 http://www.jensdiemer.de/_command/118/blog/detail/30/ (de) 110 http://www.python-forum.de/topic-16000.html (de) 111 """ 112 def validate_unique(self): 113 pass 114 115 116 class UsernameForm(BaseModelForm): 101 #class BaseModelForm(forms.ModelForm): 102 # """ 103 # A model form witch don't validate unique fields. 104 # 105 # This ModelForm is only for generating the forms and not for create/update 106 # any database data. So a field unique Test would like generate Errors like: 107 # User with this Username already exists. 108 # 109 # see also: 110 # http://www.jensdiemer.de/_command/118/blog/detail/30/ (de) 111 # http://www.python-forum.de/topic-16000.html (de) 112 # """ 113 # def __init__(self, *args, **kwargs): 114 # """ Change field meta in a DRY way """ 115 # super(BaseModelForm, self).__init__(*args, **kwargs) 116 # 117 # self.model.full_validate = self._skip 118 # 119 # def _skip(self, *args, **kwargs): 120 # pass 121 # 122 # def validate_unique(self): 123 # pass 124 125 class UsernameForm(forms.Form): 117 126 """ 118 127 form for input the username, used in auth.login() 119 """ 128 129 FIXME: This is not DRY. 130 """ 131 username = forms.CharField(max_length=_('30'), label=_('Username'), 132 help_text=_('Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores).') 133 ) 134 120 135 def is_valid(self): 121 136 """ do a secont validation: try to get the user from database and 122 check if he is active """ 137 check if he is active """ 123 138 is_valid = super(UsernameForm, self).is_valid() 124 139 if not is_valid: 125 140 return False 126 141 127 142 username = self.cleaned_data["username"] 128 143 try: 129 self.user = User.objects.get(username =username)144 self.user = User.objects.get(username=username) 130 145 except User.DoesNotExist, e: 131 146 self._errors["username"] = ("User doesn't exist!",) 132 147 return False 148 149 return True 150 151 # class Meta: 152 # model = User 153 # fields = ("username",) 154 155 156 class PasswordForm(forms.Form): 157 """ 158 form for input the username, used in auth._sha_login() 133 159 134 return True 135 136 class Meta: 137 model = User 138 fields=("username",) 139 140 141 class PasswordForm(BaseModelForm): 142 """ 143 form for input the username, used in auth._sha_login() 144 """ 145 def __init__(self, *args, **kwargs): 146 """ Change field meta in a DRY way """ 147 super(PasswordForm, self).__init__(*args, **kwargs) 148 149 self.fields['password'].widget = forms.PasswordInput() 150 self.fields['password'].help_text = "" 160 FIXME: This is not DRY. 161 """ 162 password = forms.CharField(max_length=_('128'), label=_('Password'), widget=forms.PasswordInput() 163 ) 164 165 # def __init__(self, *args, **kwargs): 166 # """ Change field meta in a DRY way """ 167 # super(PasswordForm, self).__init__(*args, **kwargs) 168 # 169 # self.fields['password'].widget = forms.PasswordInput() 170 # self.fields['password'].help_text = "" 151 171 152 172 def is_valid(self, username): … … 154 174 if not is_valid: 155 175 return False 156 176 157 177 password = self.cleaned_data["password"] 158 178 self.user = auth.authenticate(username=username, password=password) … … 160 180 self._errors["password"] = ("Wrong password!",) 161 181 return False 162 182 163 183 return True 164 184 165 class Meta:166 model = User167 fields=("password",)168 169 170 class ResetForm( BaseModelForm):185 # class Meta: 186 # model = User 187 # fields = ("password",) 188 189 190 class ResetForm(forms.Form): 171 191 """ 172 192 from for input username and email, used in auth.pass_reset() 173 193 """ 174 def __init__(self, *args, **kwargs): 175 super(ResetForm, self).__init__(*args, **kwargs) 176 # User.email is normaly a not required field, here it's required! 177 self.fields['email'].required = True 178 179 class Meta: 180 model = User 181 fields=("username","email") 194 username = forms.CharField(max_length=_('30'), label=_('Username'), 195 help_text=_('Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores).') 196 ) 197 email = forms.EmailField(max_length=_('75'), label=_('E-mail address')) 198 199 # def __init__(self, *args, **kwargs): 200 # super(ResetForm, self).__init__(*args, **kwargs) 201 # # User.email is normaly a not required field, here it's required! 202 # self.fields['email'].required = True 203 204 # class Meta: 205 # model = User 206 # fields = ("username", "email")