Django

Code

Show
Ignore:
Timestamp:
08/05/08 12:15:33 (4 months ago)
Author:
jbronn
Message:

gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.

Files:

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  
    11from django.contrib.auth.models import User, Group 
    22from django.core.exceptions import PermissionDenied 
    3 from django import oldforms, template 
     3from django import template 
    44from django.shortcuts import render_to_response 
    55from django.http import HttpResponseRedirect 
  • django/branches/gis/django/contrib/auth/decorators.py

    r7176 r8215  
    11try: 
    2     from functools import wraps, update_wrapper 
     2    from functools import update_wrapper 
    33except 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. 
    55 
    66from django.contrib.auth import REDIRECT_FIELD_NAME 
  • django/branches/gis/django/contrib/auth/forms.py

    r7979 r8215  
    11from django.contrib.auth.models import User 
    22from django.contrib.auth import authenticate 
     3from django.contrib.auth.tokens import default_token_generator 
    34from django.contrib.sites.models import Site 
    45from django.template import Context, loader 
    5 from django.core import validators 
    66from django import forms 
    77from django.utils.translation import ugettext_lazy as _ 
     8from django.utils.http import int_to_base36 
    89 
    910class UserCreationForm(forms.ModelForm): 
     
    1415        help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."), 
    1516        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 
    1920    class Meta: 
    2021        model = User 
    2122        fields = ("username",) 
    22      
     23 
    2324    def clean_username(self): 
    2425        username = self.cleaned_data["username"] 
     
    2829            return username 
    2930        raise forms.ValidationError(_("A user with that username already exists.")) 
    30      
     31 
    3132    def clean_password2(self): 
    3233        password1 = self.cleaned_data["password1"] 
     
    3536            raise forms.ValidationError(_("The two password fields didn't match.")) 
    3637        return password2 
    37      
     38 
    3839    def save(self, commit=True): 
    3940        user = super(UserCreationForm, self).save(commit=False) 
     
    4950    """ 
    5051    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 
    5354    def __init__(self, request=None, *args, **kwargs): 
    5455        """ 
     
    6162        self.user_cache = None 
    6263        super(AuthenticationForm, self).__init__(*args, **kwargs) 
    63      
     64 
    6465    def clean(self): 
    6566        username = self.cleaned_data.get('username') 
    6667        password = self.cleaned_data.get('password') 
    67          
     68 
    6869        if username and password: 
    6970            self.user_cache = authenticate(username=username, password=password) 
     
    7273            elif not self.user_cache.is_active: 
    7374                raise forms.ValidationError(_("This account is inactive.")) 
    74          
     75 
    7576        # TODO: determine whether this should move to its own method. 
    7677        if self.request: 
    7778            if not self.request.session.test_cookie_worked(): 
    7879                raise forms.ValidationError(_("Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in.")) 
    79          
     80 
    8081        return self.cleaned_data 
    81      
     82 
    8283    def get_user_id(self): 
    8384        if self.user_cache: 
    8485            return self.user_cache.id 
    8586        return None 
    86      
     87 
    8788    def get_user(self): 
    8889        return self.user_cache 
    8990 
    9091class 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 
    9394    def clean_email(self): 
    9495        """ 
     
    99100        if len(self.users_cache) == 0: 
    100101            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): 
    103105        """ 
    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 
    105107        """ 
    106108        from django.core.mail import send_mail 
    107109        for user in self.users_cache: 
    108             new_pass = User.objects.make_random_password() 
    109             user.set_password(new_pass) 
    110             user.save() 
    111110            if not domain_override: 
    112111                current_site = Site.objects.get_current() 
     
    117116            t = loader.get_template(email_template_name) 
    118117            c = { 
    119                 'new_password': new_pass, 
    120118                'email': user.email, 
    121119                'domain': domain, 
    122120                'site_name': site_name, 
     121                'uid': int_to_base36(user.id), 
    123122                'user': user, 
     123                'token': token_generator.make_token(user), 
     124                'protocol': use_https and 'https' or 'http', 
    124125            } 
    125126            send_mail(_("Password reset on %s") % site_name, 
    126127                t.render(Context(c)), None, [user.email]) 
    127128 
    128 class PasswordChangeForm(forms.Form): 
     129class SetPasswordForm(forms.Form): 
    129130    """ 
    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 
    131133    """ 
    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 
    136137    def __init__(self, user, *args, **kwargs): 
    137138        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 
     155class 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 
    140162    def clean_old_password(self): 
    141163        """ 
     
    146168            raise forms.ValidationError(_("Your old password was entered incorrectly. Please enter it again.")) 
    147169        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 
     170PasswordChangeForm.base_fields.keyOrder = ['old_password', 'new_password1', 'new_password2'] 
    162171 
    163172class AdminPasswordChangeForm(forms.Form): 
     
    165174    A form used to change the password of a user in the admin interface. 
    166175    """ 
    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 
    170179    def __init__(self, user, *args, **kwargs): 
    171180        self.user = user 
    172181        super(AdminPasswordChangeForm, self).__init__(*args, **kwargs) 
    173      
     182 
    174183    def clean_password2(self): 
    175184        password1 = self.cleaned_data.get('password1') 
     
    179188                raise forms.ValidationError(_("The two password fields didn't match.")) 
    180189        return password2 
    181      
     190 
    182191    def save(self, commit=True): 
    183192        """ 
  • django/branches/gis/django/contrib/auth/management/commands/createsuperuser.py

    r7642 r8215  
    88import sys 
    99from optparse import make_option 
    10 from django.contrib.auth.models import User, UNUSABLE_PASSWORD 
     10from django.contrib.auth.models import User 
    1111from django.core import validators 
    1212from django.core.management.base import BaseCommand, CommandError 
  • django/branches/gis/django/contrib/auth/models.py

    r7979 r8215  
    359359        return False 
    360360 
     361    def has_perms(self, perm_list): 
     362        return False 
     363 
    361364    def has_module_perms(self, module): 
    362365        return False 
  • django/branches/gis/django/contrib/auth/tests/basic.py

    r7979 r8215  
    5555u'!' 
    5656""" 
    57  
    58 from django.test import TestCase 
    59 from django.core import mail 
    60  
    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  
    33>>> from django.contrib.auth.models import User 
    44>>> 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 
    66 
    77The user already exists. 
     
    9696[] 
    9797 
     98SetPasswordForm: 
     99 
     100The 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() 
     108False 
     109>>> form["new_password2"].errors 
     110[u"The two password fields didn't match."] 
     111 
     112The success case. 
     113 
     114>>> data = { 
     115...     'new_password1': 'abc123', 
     116...     'new_password2': 'abc123', 
     117... } 
     118>>> form = SetPasswordForm(user, data) 
     119>>> form.is_valid() 
     120True 
     121 
     122PasswordChangeForm: 
     123 
    98124The old password is incorrect. 
    99125 
     
    133159True 
    134160 
     161Regression test - check the order of fields: 
     162 
     163>>> PasswordChangeForm(user, {}).fields.keys() 
     164['old_password', 'new_password1', 'new_password2'] 
     165 
    135166""" 
  • django/branches/gis/django/contrib/auth/tests/__init__.py

    r7979 r8215  
    1 from django.contrib.auth.tests.basic import BASIC_TESTS, PasswordResetTest 
     1from django.contrib.auth.tests.basic import BASIC_TESTS 
     2from django.contrib.auth.tests.views import PasswordResetTest 
    23from django.contrib.auth.tests.forms import FORM_TESTS 
     4from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS 
    35 
    46__test__ = { 
     
    68    'PASSWORDRESET_TESTS': PasswordResetTest, 
    79    'FORM_TESTS': FORM_TESTS, 
     10    'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS 
    811} 
  • django/branches/gis/django/contrib/auth/urls.py

    r7836 r8215  
    66 
    77urlpatterns = 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'), 
    1215) 
    1316 
  • django/branches/gis/django/contrib/auth/views.py

    r7979 r8215  
     1from django.conf import settings 
    12from django.contrib.auth import REDIRECT_FIELD_NAME 
    23from django.contrib.auth.decorators import login_required 
    34from django.contrib.auth.forms import AuthenticationForm 
    4 from django.contrib.auth.forms import PasswordResetForm, PasswordChangeForm, AdminPasswordChangeForm 
     5from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm, PasswordChangeForm, AdminPasswordChangeForm 
     6from django.contrib.auth.tokens import default_token_generator 
    57from django.core.exceptions import PermissionDenied 
    68from django.shortcuts import render_to_response, get_object_or_404 
    79from django.contrib.sites.models import Site, RequestSite 
    8 from django.http import HttpResponseRedirect 
     10from django.http import HttpResponseRedirect, Http404 
    911from django.template import RequestContext 
    10 from django.utils.http import urlquote 
     12from django.utils.http import urlquote, base36_to_int 
    1113from django.utils.html import escape 
    1214from django.utils.translation import ugettext as _ 
     
    6668    return HttpResponseRedirect('%s?%s=%s' % (login_url, urlquote(redirect_field_name), urlquote(next))) 
    6769 
     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 
    6877def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html', 
    6978        email_template_name='registration/password_reset_email.html', 
    70         password_reset_form=PasswordResetForm): 
     79        password_reset_form=PasswordResetForm, token_generator=default_token_generator): 
    7180    if request.method == "POST": 
    7281        form = password_reset_form(request.POST) 
    7382        if form.is_valid(): 
     83            opts = {} 
     84            opts['use_https'] = request.is_secure() 
     85            opts['token_generator'] = token_generator 
    7486            if is_admin_site: 
    75                 form.save(domain_override=request.META['HTTP_HOST']) 
     87                opts['domain_override'] = request.META['HTTP_HOST'] 
    7688            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
    8193            return HttpResponseRedirect('%sdone/' % request.path) 
    8294    else: 
     
    88100def password_reset_done(request, template_name='registration/password_reset_done.html'): 
    89101    return render_to_response(template_name, context_instance=RequestContext(request)) 
     102 
     103def 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 
     133def 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})) 
    90136 
    91137def password_change(request, template_name='registration/password_change_form.html'):