Commits

Peter Sanchez committed deb0bf3

Working on completing baseacct

Comments (0)

Files changed (5)

webutils/baseacct/__init__.py

 from django.conf import settings
 from django.core.exceptions import ImproperlyConfigured
-from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm
-from forms import ResetForm
+from django.contrib.auth.forms import AuthenticationForm
+from forms import ResetForm, PasswordChangeForm
 from webutils.djtools.easyconfig import EasyConfig
 
 
         return self.config.get_object('get_password_change_redirect', None)
     
     def get_password_change_form(self):
-        return self.config.get_object('get_password_change_form', PasswordChangeForm)
+        return self.config.get_object(
+            'get_password_change_form',
+            PasswordChangeForm,
+        )

webutils/baseacct/forms.py

                 )
             return email.lower()
 
-        raise forms.ValidationError(u'This field is required.')
+        raise forms.ValidationError(u'This field is required.')
+
+
+class PasswordChangeForm(forms.Form):
+    old_password = forms.CharField(
+        label=_('Old Password'),
+        widget=forms.PasswordInput,
+    )
+    new_password = forms.CharField(
+        label=_('New Password'),
+        widget=forms.PasswordInput,
+    )
+    new_password2 = forms.CharField(
+        label=_('Confirm New Password'),
+        widget=forms.PasswordInput,
+    )
+    
+    def __init__(self, user, *args, **kwargs):
+        self.user = user
+        super(PasswordChangeForm, self).__init__(*args, **kwargs)
+    
+    def clean_old_password(self):
+        if 'old_password' in self.cleaned_data:
+            oldpw = self.cleaned_data['old_password']
+            if not self.user.check_password(oldpw):
+                raise forms.ValidationError(_(
+                    'Your old password was entered incorrectly. ' + \
+                    'Please enter it again.'
+                ))
+            return oldpw
+        raise forms.ValidationError(_('This field is required.'))
+    
+    def clean_new_password2(self):
+        if 'new_password2' in self.cleaned_data:
+            pw1 = self.cleaned_data.get('new_password')
+            pw2 = self.cleaned_data.get('new_password2')
+            if pw1 != pw2:
+                raise forms.ValidationError(_(
+                    'The two password fields didn\'t match.'
+                ))
+            return pw2
+        raise forms.ValidationError(_('This field is required.'))
+    
+    def save(self):
+        self.user.set_password(self.cleaned_data['new_password'])
+        self.user.save()

webutils/baseacct/urls.py

 from webutils.baseacct import Config
 from django.contrib.auth import views as auth_views
+from django.views.generic.simple import direct_to_template
 from django.conf.urls.defaults import *
 
 
         {'login_url': config.get_login_url()},
         name='baseacct-logout-login'),
     url(r'^password_change/$',
-        auth_views.password_change, {
-            'template_name': 'baseacct/password_change.html',
-            'post_change_redirect': config.get_password_change_redirect(),
+        'password_change', {
+            'template': 'baseacct/password_change.html',
+            'password_change_redirect': config.get_password_change_redirect(),
             'password_change_form': config.get_password_change_form(),
         }, name='baseacct-password-change'),
     url(r'^password_change/done/$',
-        auth_views.password_change_done, {
-            'template_name': 'baseacct/password_change_done.html',
+        direct_to_template, {
+            'template': 'baseacct/password_change_done.html',
         }, name='baseacct-password-change-done'),
     url(r'^reset/$',
         'reset', {

webutils/baseacct/views.py

 from django.conf import settings
 from django.contrib.auth.models import User
+from django.contrib.auth.decorators import login_required
 from django.template import loader, RequestContext
 from django.shortcuts import render_to_response, redirect
 from webutils.baseacct import Config
     return render_to_response(template, {
         'form': form,
     }, context_instance=RequestContext(request))
+
+
+@login_required
+def password_change(request, template,
+                    password_change_redirect, password_change_form):
+    if password_change_redirect is None:
+        password_change_redirect = 'baseacct-password-change-done'
+
+    if request.method == 'POST':
+        form = password_change_form(request.user, request.POST)
+        if form.is_valid():
+            form.save()
+            return redirect(password_change_redirect)
+    else:
+        form = password_change_form(request.user)
+    
+    return render_to_response(template, {
+        'form': form,
+    }, context_instance=RequestContext(request))

webutils/djtools/easyconfig.py

         try:
             package = import_module(config)
         except ImportError:
-            raise ImproperlyConfigured(
-                'The %s setting refers to a non-existing package.' % \
+            path = '.'.join(config.split('.')[:-1])
+            pkg = config.split('.')[-1]
+            try:
+                package = import_module(path, package=pkg)
+            except ImportError:
+                raise ImproperlyConfigured(
+                    'The %s setting refers to a non-existing package.' % \
                                                             self.setting_name
-            )
+                )
 
         return package