Ticket #14881: django-auth-string-pk-support.patch
File django-auth-string-pk-support.patch, 5.4 KB (added by , 14 years ago) |
---|
-
django/contrib/auth/urls.py
# HG changeset patch -- Bitbucket.org # Project django-nonrel # URL http://bitbucket.org/wkornewald/django-nonrel/overview # User Waldemar Kornewald <wkornewald> # Date 1291970757 -3600 # Node ID a632a02c9ff26f80d0565551cb6ea8c23699dd71 # Parent af6be5bfdb85ce5f88467b391346b6183fd645c5 Fixed auth app's password reset feature on DBs that have a string-based AutoField. Thanks a lot to Jonas Haag for the patch!
a b urlpatterns = patterns('', 11 11 (r'^password_change/done/$', 'django.contrib.auth.views.password_change_done'), 12 12 (r'^password_reset/$', 'django.contrib.auth.views.password_reset'), 13 13 (r'^password_reset/done/$', 'django.contrib.auth.views.password_reset_done'), 14 (r'^reset/(?P<uidb 36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),14 (r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'), 15 15 (r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'), 16 16 ) 17 -
django/contrib/auth/views.py
a b from django.shortcuts import render_to_r 13 13 from django.contrib.sites.models import get_current_site 14 14 from django.http import HttpResponseRedirect, Http404 15 15 from django.template import RequestContext 16 from django.utils.http import urlquote, base36_to_int16 from django.utils.http import urlquote, urlsafe_base64_decode 17 17 from django.utils.translation import ugettext as _ 18 18 from django.contrib.auth.models import User 19 19 from django.views.decorators.cache import never_cache … … def password_reset_done(request, templat 131 131 return render_to_response(template_name, context_instance=RequestContext(request)) 132 132 133 133 # Doesn't need csrf_protect since no-one can guess the URL 134 def password_reset_confirm(request, uidb 36=None, token=None, template_name='registration/password_reset_confirm.html',134 def password_reset_confirm(request, uidb64=None, token=None, template_name='registration/password_reset_confirm.html', 135 135 token_generator=default_token_generator, set_password_form=SetPasswordForm, 136 136 post_reset_redirect=None): 137 137 """ 138 138 View that checks the hash in a password reset link and presents a 139 139 form for entering a new password. 140 140 """ 141 assert uidb 36is not None and token is not None # checked by URLconf141 assert uidb64 is not None and token is not None # checked by URLconf 142 142 if post_reset_redirect is None: 143 143 post_reset_redirect = reverse('django.contrib.auth.views.password_reset_complete') 144 144 try: 145 uid _int = base36_to_int(uidb36)146 user = User.objects.get(id=uid _int)145 uid = urlsafe_base64_decode(str(uidb64)) 146 user = User.objects.get(id=uid) 147 147 except (ValueError, User.DoesNotExist): 148 148 user = None 149 149 -
django/contrib/auth/tests/templates/registration/password_reset_email.html
a b 1 {{ protocol }}://{{ domain }}/reset/{{ uid }} -{{ token }}/1 {{ protocol }}://{{ domain }}/reset/{{ uid }}/{{ token }}/ -
django/contrib/auth/forms.py
a b from django.contrib.sites.models import 5 5 from django.template import Context, loader 6 6 from django import forms 7 7 from django.utils.translation import ugettext_lazy as _ 8 from django.utils.http import int_to_base368 from django.utils.http import urlsafe_base64_encode 9 9 10 10 class UserCreationForm(forms.ModelForm): 11 11 """ … … class PasswordResetForm(forms.Form): 134 134 'email': user.email, 135 135 'domain': domain, 136 136 'site_name': site_name, 137 'uid': int_to_base36(user.id),137 'uid': urlsafe_base64_encode(str(user.id)), 138 138 'user': user, 139 139 'token': token_generator.make_token(user), 140 140 'protocol': use_https and 'https' or 'http', -
django/utils/http.py
a b 1 1 import re 2 2 import urllib 3 import base64 4 from binascii import Error as BinasciiError 3 5 from email.Utils import formatdate 4 6 5 7 from django.utils.encoding import smart_str, force_unicode … … def http_date(epoch_seconds=None): 73 75 74 76 def base36_to_int(s): 75 77 """ 76 Convert da base 36 string to an integer78 Converts a base 36 string to an integer 77 79 """ 78 80 return int(s, 36) 79 81 … … def int_to_base36(i): 98 100 factor -= 1 99 101 return ''.join(base36) 100 102 103 def urlsafe_base64_encode(s): 104 return base64.urlsafe_b64encode(s).rstrip('\n=') 105 106 def urlsafe_base64_decode(s): 107 assert isinstance(s, str) 108 try: 109 return base64.urlsafe_b64decode(s.ljust(len(s) + len(s) % 4, '=')) 110 except (LookupError, BinasciiError), e: 111 raise ValueError(e) 112 101 113 def parse_etags(etag_str): 102 114 """ 103 115 Parses a string with one or several etags passed in If-None-Match and -
django/contrib/auth/tests/views.py
a b class PasswordResetTest(AuthViewsTestCas 102 102 103 103 def test_confirm_invalid_user(self): 104 104 # Ensure that we get a 200 response for a non-existant user, not a 404 105 response = self.client.get('/reset/123456 -1-1/')105 response = self.client.get('/reset/123456/1-1/') 106 106 self.assertEquals(response.status_code, 200) 107 107 self.assert_("The password reset link was invalid" in response.content)