Changeset 8215 for django/branches/gis/django/contrib/auth
- Timestamp:
- 08/05/08 12:15:33 (4 months ago)
- Files:
-
- django/branches/gis (modified) (1 prop)
- django/branches/gis/django/contrib/auth/admin.py (modified) (1 diff)
- django/branches/gis/django/contrib/auth/decorators.py (modified) (1 diff)
- django/branches/gis/django/contrib/auth/forms.py (modified) (12 diffs)
- django/branches/gis/django/contrib/auth/management/commands/createsuperuser.py (modified) (1 diff)
- django/branches/gis/django/contrib/auth/models.py (modified) (1 diff)
- django/branches/gis/django/contrib/auth/tests/basic.py (modified) (1 diff)
- django/branches/gis/django/contrib/auth/tests/forms.py (modified) (3 diffs)
- django/branches/gis/django/contrib/auth/tests/__init__.py (modified) (2 diffs)
- django/branches/gis/django/contrib/auth/tests/tokens.py (copied) (copied from django/trunk/django/contrib/auth/tests/tokens.py)
- django/branches/gis/django/contrib/auth/tests/views.py (copied) (copied from django/trunk/django/contrib/auth/tests/views.py)
- django/branches/gis/django/contrib/auth/tokens.py (copied) (copied from django/trunk/django/contrib/auth/tokens.py)
- django/branches/gis/django/contrib/auth/urls.py (modified) (1 diff)
- django/branches/gis/django/contrib/auth/views.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis
- Property svnmerge-integrated changed from /django/trunk:1-7978 to /django/trunk:1-8214
django/branches/gis/django/contrib/auth/admin.py
r7979 r8215 1 1 from django.contrib.auth.models import User, Group 2 2 from django.core.exceptions import PermissionDenied 3 from django import oldforms,template3 from django import template 4 4 from django.shortcuts import render_to_response 5 5 from django.http import HttpResponseRedirect django/branches/gis/django/contrib/auth/decorators.py
r7176 r8215 1 1 try: 2 from functools import wraps,update_wrapper2 from functools import update_wrapper 3 3 except ImportError: 4 from django.utils.functional import wraps,update_wrapper # Python 2.3, 2.4 fallback.4 from django.utils.functional import update_wrapper # Python 2.3, 2.4 fallback. 5 5 6 6 from django.contrib.auth import REDIRECT_FIELD_NAME django/branches/gis/django/contrib/auth/forms.py
r7979 r8215 1 1 from django.contrib.auth.models import User 2 2 from django.contrib.auth import authenticate 3 from django.contrib.auth.tokens import default_token_generator 3 4 from django.contrib.sites.models import Site 4 5 from django.template import Context, loader 5 from django.core import validators6 6 from django import forms 7 7 from django.utils.translation import ugettext_lazy as _ 8 from django.utils.http import int_to_base36 8 9 9 10 class UserCreationForm(forms.ModelForm): … … 14 15 help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."), 15 16 error_message = _("This value must contain only letters, numbers and underscores.")) 16 password1 = forms.CharField(label=_("Password"), max_length=60,widget=forms.PasswordInput)17 password2 = forms.CharField(label=_("Password confirmation"), max_length=60,widget=forms.PasswordInput)18 17 password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) 18 password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput) 19 19 20 class Meta: 20 21 model = User 21 22 fields = ("username",) 22 23 23 24 def clean_username(self): 24 25 username = self.cleaned_data["username"] … … 28 29 return username 29 30 raise forms.ValidationError(_("A user with that username already exists.")) 30 31 31 32 def clean_password2(self): 32 33 password1 = self.cleaned_data["password1"] … … 35 36 raise forms.ValidationError(_("The two password fields didn't match.")) 36 37 return password2 37 38 38 39 def save(self, commit=True): 39 40 user = super(UserCreationForm, self).save(commit=False) … … 49 50 """ 50 51 username = forms.CharField(label=_("Username"), max_length=30) 51 password = forms.CharField(label=_("Password"), max_length=30,widget=forms.PasswordInput)52 52 password = forms.CharField(label=_("Password"), widget=forms.PasswordInput) 53 53 54 def __init__(self, request=None, *args, **kwargs): 54 55 """ … … 61 62 self.user_cache = None 62 63 super(AuthenticationForm, self).__init__(*args, **kwargs) 63 64 64 65 def clean(self): 65 66 username = self.cleaned_data.get('username') 66 67 password = self.cleaned_data.get('password') 67 68 68 69 if username and password: 69 70 self.user_cache = authenticate(username=username, password=password) … … 72 73 elif not self.user_cache.is_active: 73 74 raise forms.ValidationError(_("This account is inactive.")) 74 75 75 76 # TODO: determine whether this should move to its own method. 76 77 if self.request: 77 78 if not self.request.session.test_cookie_worked(): 78 79 raise forms.ValidationError(_("Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in.")) 79 80 80 81 return self.cleaned_data 81 82 82 83 def get_user_id(self): 83 84 if self.user_cache: 84 85 return self.user_cache.id 85 86 return None 86 87 87 88 def get_user(self): 88 89 return self.user_cache 89 90 90 91 class PasswordResetForm(forms.Form): 91 email = forms.EmailField(label=_("E-mail"), max_length= 40)92 92 email = forms.EmailField(label=_("E-mail"), max_length=75) 93 93 94 def clean_email(self): 94 95 """ … … 99 100 if len(self.users_cache) == 0: 100 101 raise forms.ValidationError(_("That e-mail address doesn't have an associated user account. Are you sure you've registered?")) 101 102 def save(self, domain_override=None, email_template_name='registration/password_reset_email.html'): 102 103 def save(self, domain_override=None, email_template_name='registration/password_reset_email.html', 104 use_https=False, token_generator=default_token_generator): 103 105 """ 104 Calculates a new password randomly and sends it to the user.106 Generates a one-use only link for resetting password and sends to the user 105 107 """ 106 108 from django.core.mail import send_mail 107 109 for user in self.users_cache: 108 new_pass = User.objects.make_random_password()109 user.set_password(new_pass)110 user.save()111 110 if not domain_override: 112 111 current_site = Site.objects.get_current() … … 117 116 t = loader.get_template(email_template_name) 118 117 c = { 119 'new_password': new_pass,120 118 'email': user.email, 121 119 'domain': domain, 122 120 'site_name': site_name, 121 'uid': int_to_base36(user.id), 123 122 'user': user, 123 'token': token_generator.make_token(user), 124 'protocol': use_https and 'https' or 'http', 124 125 } 125 126 send_mail(_("Password reset on %s") % site_name, 126 127 t.render(Context(c)), None, [user.email]) 127 128 128 class PasswordChangeForm(forms.Form):129 class SetPasswordForm(forms.Form): 129 130 """ 130 A form that lets a user change his/her password. 131 A form that lets a user change set his/her password without 132 entering the old password 131 133 """ 132 old_password = forms.CharField(label=_("Old password"), max_length=30, widget=forms.PasswordInput) 133 new_password1 = forms.CharField(label=_("New password"), max_length=30, widget=forms.PasswordInput) 134 new_password2 = forms.CharField(label=_("New password confirmation"), max_length=30, widget=forms.PasswordInput) 135 134 new_password1 = forms.CharField(label=_("New password"), widget=forms.PasswordInput) 135 new_password2 = forms.CharField(label=_("New password confirmation"), widget=forms.PasswordInput) 136 136 137 def __init__(self, user, *args, **kwargs): 137 138 self.user = user 138 super(PasswordChangeForm, self).__init__(*args, **kwargs) 139 139 super(SetPasswordForm, self).__init__(*args, **kwargs) 140 141 def clean_new_password2(self): 142 password1 = self.cleaned_data.get('new_password1') 143 password2 = self.cleaned_data.get('new_password2') 144 if password1 and password2: 145 if password1 != password2: 146 raise forms.ValidationError(_("The two password fields didn't match.")) 147 return password2 148 149 def save(self, commit=True): 150 self.user.set_password(self.cleaned_data['new_password1']) 151 if commit: 152 self.user.save() 153 return self.user 154 155 class PasswordChangeForm(SetPasswordForm): 156 """ 157 A form that lets a user change his/her password by entering 158 their old password. 159 """ 160 old_password = forms.CharField(label=_("Old password"), widget=forms.PasswordInput) 161 140 162 def clean_old_password(self): 141 163 """ … … 146 168 raise forms.ValidationError(_("Your old password was entered incorrectly. Please enter it again.")) 147 169 return old_password 148 149 def clean_new_password2(self): 150 password1 = self.cleaned_data.get('new_password1') 151 password2 = self.cleaned_data.get('new_password2') 152 if password1 and password2: 153 if password1 != password2: 154 raise forms.ValidationError(_("The two password fields didn't match.")) 155 return password2 156 157 def save(self, commit=True): 158 self.user.set_password(self.cleaned_data['new_password1']) 159 if commit: 160 self.user.save() 161 return self.user 170 PasswordChangeForm.base_fields.keyOrder = ['old_password', 'new_password1', 'new_password2'] 162 171 163 172 class AdminPasswordChangeForm(forms.Form): … … 165 174 A form used to change the password of a user in the admin interface. 166 175 """ 167 password1 = forms.CharField(label=_("Password"), max_length=60,widget=forms.PasswordInput)168 password2 = forms.CharField(label=_("Password (again)"), max_length=60,widget=forms.PasswordInput)169 176 password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) 177 password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput) 178 170 179 def __init__(self, user, *args, **kwargs): 171 180 self.user = user 172 181 super(AdminPasswordChangeForm, self).__init__(*args, **kwargs) 173 182 174 183 def clean_password2(self): 175 184 password1 = self.cleaned_data.get('password1') … … 179 188 raise forms.ValidationError(_("The two password fields didn't match.")) 180 189 return password2 181 190 182 191 def save(self, commit=True): 183 192 """ django/branches/gis/django/contrib/auth/management/commands/createsuperuser.py
r7642 r8215 8 8 import sys 9 9 from optparse import make_option 10 from django.contrib.auth.models import User , UNUSABLE_PASSWORD10 from django.contrib.auth.models import User 11 11 from django.core import validators 12 12 from django.core.management.base import BaseCommand, CommandError django/branches/gis/django/contrib/auth/models.py
r7979 r8215 359 359 return False 360 360 361 def has_perms(self, perm_list): 362 return False 363 361 364 def has_module_perms(self, module): 362 365 return False django/branches/gis/django/contrib/auth/tests/basic.py
r7979 r8215 55 55 u'!' 56 56 """ 57 58 from django.test import TestCase59 from django.core import mail60 61 class PasswordResetTest(TestCase):62 fixtures = ['authtestdata.json']63 urls = 'django.contrib.auth.urls'64 65 def test_email_not_found(self):66 "Error is raised if the provided email address isn't currently registered"67 response = self.client.get('/password_reset/')68 self.assertEquals(response.status_code, 200)69 response = self.client.post('/password_reset/', {'email': 'not_a_real_email@email.com'})70 self.assertContains(response, "That e-mail address doesn't have an associated user account")71 self.assertEquals(len(mail.outbox), 0)72 73 def test_email_found(self):74 "Email is sent if a valid email address is provided for password reset"75 response = self.client.post('/password_reset/', {'email': 'staffmember@example.com'})76 self.assertEquals(response.status_code, 302)77 self.assertEquals(len(mail.outbox), 1)django/branches/gis/django/contrib/auth/tests/forms.py
r7979 r8215 3 3 >>> from django.contrib.auth.models import User 4 4 >>> from django.contrib.auth.forms import UserCreationForm, AuthenticationForm 5 >>> from django.contrib.auth.forms import PasswordChangeForm 5 >>> from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm 6 6 7 7 The user already exists. … … 96 96 [] 97 97 98 SetPasswordForm: 99 100 The two new passwords do not match. 101 102 >>> data = { 103 ... 'new_password1': 'abc123', 104 ... 'new_password2': 'abc', 105 ... } 106 >>> form = SetPasswordForm(user, data) 107 >>> form.is_valid() 108 False 109 >>> form["new_password2"].errors 110 [u"The two password fields didn't match."] 111 112 The success case. 113 114 >>> data = { 115 ... 'new_password1': 'abc123', 116 ... 'new_password2': 'abc123', 117 ... } 118 >>> form = SetPasswordForm(user, data) 119 >>> form.is_valid() 120 True 121 122 PasswordChangeForm: 123 98 124 The old password is incorrect. 99 125 … … 133 159 True 134 160 161 Regression test - check the order of fields: 162 163 >>> PasswordChangeForm(user, {}).fields.keys() 164 ['old_password', 'new_password1', 'new_password2'] 165 135 166 """ django/branches/gis/django/contrib/auth/tests/__init__.py
r7979 r8215 1 from django.contrib.auth.tests.basic import BASIC_TESTS, PasswordResetTest 1 from django.contrib.auth.tests.basic import BASIC_TESTS 2 from django.contrib.auth.tests.views import PasswordResetTest 2 3 from django.contrib.auth.tests.forms import FORM_TESTS 4 from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS 3 5 4 6 __test__ = { … … 6 8 'PASSWORDRESET_TESTS': PasswordResetTest, 7 9 'FORM_TESTS': FORM_TESTS, 10 'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS 8 11 } django/branches/gis/django/contrib/auth/urls.py
r7836 r8215 6 6 7 7 urlpatterns = patterns('', 8 ('^logout/$', 'django.contrib.auth.views.logout'), 9 ('^password_change/$', 'django.contrib.auth.views.password_change'), 10 ('^password_change/done/$', 'django.contrib.auth.views.password_change_done'), 11 ('^password_reset/$', 'django.contrib.auth.views.password_reset') 8 (r'^logout/$', 'django.contrib.auth.views.logout'), 9 (r'^password_change/$', 'django.contrib.auth.views.password_change'), 10 (r'^password_change/done/$', 'django.contrib.auth.views.password_change_done'), 11 (r'^password_reset/$', 'django.contrib.auth.views.password_reset'), 12 (r'^password_reset/done/$', 'django.contrib.auth.views.password_reset_done'), 13 (r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'), 14 (r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'), 12 15 ) 13 16 django/branches/gis/django/contrib/auth/views.py
r7979 r8215 1 from django.conf import settings 1 2 from django.contrib.auth import REDIRECT_FIELD_NAME 2 3 from django.contrib.auth.decorators import login_required 3 4 from django.contrib.auth.forms import AuthenticationForm 4 from django.contrib.auth.forms import PasswordResetForm, PasswordChangeForm, AdminPasswordChangeForm 5 from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm, PasswordChangeForm, AdminPasswordChangeForm 6 from django.contrib.auth.tokens import default_token_generator 5 7 from django.core.exceptions import PermissionDenied 6 8 from django.shortcuts import render_to_response, get_object_or_404 7 9 from django.contrib.sites.models import Site, RequestSite 8 from django.http import HttpResponseRedirect 10 from django.http import HttpResponseRedirect, Http404 9 11 from django.template import RequestContext 10 from django.utils.http import urlquote 12 from django.utils.http import urlquote, base36_to_int 11 13 from django.utils.html import escape 12 14 from django.utils.translation import ugettext as _ … … 66 68 return HttpResponseRedirect('%s?%s=%s' % (login_url, urlquote(redirect_field_name), urlquote(next))) 67 69 70 # 4 views for password reset: 71 # - password_reset sends the mail 72 # - password_reset_done shows a success message for the above 73 # - password_reset_confirm checks the link the user clicked and 74 # prompts for a new password 75 # - password_reset_complete shows a success message for the above 76 68 77 def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html', 69 78 email_template_name='registration/password_reset_email.html', 70 password_reset_form=PasswordResetForm ):79 password_reset_form=PasswordResetForm, token_generator=default_token_generator): 71 80 if request.method == "POST": 72 81 form = password_reset_form(request.POST) 73 82 if form.is_valid(): 83 opts = {} 84 opts['use_https'] = request.is_secure() 85 opts['token_generator'] = token_generator 74 86 if is_admin_site: 75 form.save(domain_override=request.META['HTTP_HOST'])87 opts['domain_override'] = request.META['HTTP_HOST'] 76 88 else: 77 if Site._meta.installed:78 form.save(email_template_name=email_template_name)79 else:80 form.save(domain_override=RequestSite(request).domain, email_template_name=email_template_name)89 opts['email_template_name'] = email_template_name 90 if not Site._meta.installed: 91 opts['domain_override'] = RequestSite(request).domain 92 form.save(**opts) 81 93 return HttpResponseRedirect('%sdone/' % request.path) 82 94 else: … … 88 100 def password_reset_done(request, template_name='registration/password_reset_done.html'): 89 101 return render_to_response(template_name, context_instance=RequestContext(request)) 102 103 def password_reset_confirm(request, uidb36=None, token=None, template_name='registration/password_reset_confirm.html', 104 token_generator=default_token_generator, set_password_form=SetPasswordForm): 105 """ 106 View that checks the hash in a password reset link and presents a 107 form for entering a new password. 108 """ 109 assert uidb36 is not None and token is not None # checked by URLconf 110 try: 111 uid_int = base36_to_int(uidb36) 112 except ValueError: 113 raise Http404 114 115 user = get_object_or_404(User, id=uid_int) 116 context_instance = RequestContext(request) 117 118 if token_generator.check_token(user, token): 119 context_instance['validlink'] = True 120 if request.method == 'POST': 121 form = set_password_form(user, request.POST) 122 if form.is_valid(): 123 form.save() 124 return HttpResponseRedirect("../done/") 125 else: 126 form = set_password_form(None) 127 else: 128 context_instance['validlink'] = False 129 form = None 130 context_instance['form'] = form 131 return render_to_response(template_name, context_instance=context_instance) 132 133 def password_reset_complete(request, template_name='registration/password_reset_complete.html'): 134 return render_to_response(template_name, context_instance=RequestContext(request, 135 {'login_url': settings.LOGIN_URL})) 90 136 91 137 def password_change(request, template_name='registration/password_change_form.html'):
