Merge branch 'master' into gilbert
[philo.git] / contrib / waldo / models.py
index f47dbd6..f63cdb1 100644 (file)
@@ -23,9 +23,7 @@ import urlparse
 
 class LoginMultiView(MultiView):
        """
 
 class LoginMultiView(MultiView):
        """
-       Handles login, registration, and forgotten passwords. In other words, this
-       multiview provides exclusively view and methods related to usernames and
-       passwords.
+       Handles exclusively methods and views related to logging users in and out.
        """
        login_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_login_related')
        login_form = WaldoAuthenticationForm
        """
        login_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_login_related')
        login_form = WaldoAuthenticationForm
@@ -129,11 +127,16 @@ class LoginMultiView(MultiView):
 
 
 class PasswordMultiView(LoginMultiView):
 
 
 class PasswordMultiView(LoginMultiView):
+       "Adds on views for password-related functions."
        password_reset_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_reset_related', blank=True, null=True)
        password_reset_confirmation_email = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_reset_confirmation_email_related', blank=True, null=True)
        password_set_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_set_related', blank=True, null=True)
        password_change_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_change_related', blank=True, null=True)
        
        password_reset_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_reset_related', blank=True, null=True)
        password_reset_confirmation_email = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_reset_confirmation_email_related', blank=True, null=True)
        password_set_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_set_related', blank=True, null=True)
        password_change_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_change_related', blank=True, null=True)
        
+       password_change_form = PasswordChangeForm
+       password_set_form = SetPasswordForm
+       password_reset_form = PasswordResetForm
+       
        @property
        def urlpatterns(self):
                urlpatterns = super(PasswordMultiView, self).urlpatterns
        @property
        def urlpatterns(self):
                urlpatterns = super(PasswordMultiView, self).urlpatterns
@@ -150,14 +153,14 @@ class PasswordMultiView(LoginMultiView):
                        )
                return urlpatterns
        
                        )
                return urlpatterns
        
-       def make_confirmation_link(self, confirmation_view, token_generator, user, node, token_args=None, reverse_kwargs=None):
+       def make_confirmation_link(self, confirmation_view, token_generator, user, node, token_args=None, reverse_kwargs=None, secure=False):
                token = token_generator.make_token(user, *(token_args or []))
                kwargs = {
                        'uidb36': int_to_base36(user.id),
                        'token': token
                }
                kwargs.update(reverse_kwargs or {})
                token = token_generator.make_token(user, *(token_args or []))
                kwargs = {
                        'uidb36': int_to_base36(user.id),
                        'token': token
                }
                kwargs.update(reverse_kwargs or {})
-               return node.construct_url(subpath=self.reverse(confirmation_view, kwargs=kwargs), with_domain=True)
+               return node.construct_url(subpath=self.reverse(confirmation_view, kwargs=kwargs), with_domain=True, secure=secure)
        
        def send_confirmation_email(self, subject, email, page, extra_context):
                text_content = page.render_to_string(extra_context=extra_context)
        
        def send_confirmation_email(self, subject, email, page, extra_context):
                text_content = page.render_to_string(extra_context=extra_context)
@@ -175,19 +178,24 @@ class PasswordMultiView(LoginMultiView):
                        return HttpResponseRedirect(request.node.get_absolute_url())
                
                if request.method == 'POST':
                        return HttpResponseRedirect(request.node.get_absolute_url())
                
                if request.method == 'POST':
-                       form = PasswordResetForm(request.POST)
+                       form = self.password_reset_form(request.POST)
                        if form.is_valid():
                                current_site = Site.objects.get_current()
                                for user in form.users_cache:
                                        context = {
                        if form.is_valid():
                                current_site = Site.objects.get_current()
                                for user in form.users_cache:
                                        context = {
-                                               'link': self.make_confirmation_link('password_reset_confirm', token_generator, user, request.node),
+                                               'link': self.make_confirmation_link('password_reset_confirm', token_generator, user, request.node, secure=request.is_secure()),
+                                               'user': user,
+                                               'site': current_site,
+                                               'request': request,
+                                               
+                                               # Deprecated... leave in for backwards-compatibility
                                                'username': user.username
                                        }
                                        self.send_confirmation_email('Confirm password reset for account at %s' % current_site.domain, user.email, self.password_reset_confirmation_email, context)
                                        messages.add_message(request, messages.SUCCESS, "An email has been sent to the address you provided with details on resetting your password.", fail_silently=True)
                                return HttpResponseRedirect('')
                else:
                                                'username': user.username
                                        }
                                        self.send_confirmation_email('Confirm password reset for account at %s' % current_site.domain, user.email, self.password_reset_confirmation_email, context)
                                        messages.add_message(request, messages.SUCCESS, "An email has been sent to the address you provided with details on resetting your password.", fail_silently=True)
                                return HttpResponseRedirect('')
                else:
-                       form = PasswordResetForm()
+                       form = self.password_reset_form()
                
                context = self.get_context()
                context.update(extra_context or {})
                
                context = self.get_context()
                context.update(extra_context or {})
@@ -211,14 +219,14 @@ class PasswordMultiView(LoginMultiView):
                
                if token_generator.check_token(user, token):
                        if request.method == 'POST':
                
                if token_generator.check_token(user, token):
                        if request.method == 'POST':
-                               form = SetPasswordForm(user, request.POST)
+                               form = self.password_set_form(user, request.POST)
                                
                                if form.is_valid():
                                        form.save()
                                        messages.add_message(request, messages.SUCCESS, "Password reset successful.")
                                        return HttpResponseRedirect(self.reverse('login', node=request.node))
                        else:
                                
                                if form.is_valid():
                                        form.save()
                                        messages.add_message(request, messages.SUCCESS, "Password reset successful.")
                                        return HttpResponseRedirect(self.reverse('login', node=request.node))
                        else:
-                               form = SetPasswordForm(user)
+                               form = self.password_set_form(user)
                        
                        context = self.get_context()
                        context.update(extra_context or {})
                        
                        context = self.get_context()
                        context.update(extra_context or {})
@@ -231,13 +239,13 @@ class PasswordMultiView(LoginMultiView):
        
        def password_change(self, request, extra_context=None):
                if request.method == 'POST':
        
        def password_change(self, request, extra_context=None):
                if request.method == 'POST':
-                       form = PasswordChangeForm(request.user, request.POST)
+                       form = self.password_change_form(request.user, request.POST)
                        if form.is_valid():
                                form.save()
                                messages.add_message(request, messages.SUCCESS, 'Password changed successfully.', fail_silently=True)
                                return HttpResponseRedirect('')
                else:
                        if form.is_valid():
                                form.save()
                                messages.add_message(request, messages.SUCCESS, 'Password changed successfully.', fail_silently=True)
                                return HttpResponseRedirect('')
                else:
-                       form = PasswordChangeForm(request.user)
+                       form = self.password_change_form(request.user)
                
                context = self.get_context()
                context.update(extra_context or {})
                
                context = self.get_context()
                context.update(extra_context or {})
@@ -251,8 +259,10 @@ class PasswordMultiView(LoginMultiView):
 
 
 class RegistrationMultiView(PasswordMultiView):
 
 
 class RegistrationMultiView(PasswordMultiView):
+       """Adds on the pages necessary for letting new users register."""
        register_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_register_related', blank=True, null=True)
        register_confirmation_email = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_register_confirmation_email_related', blank=True, null=True)
        register_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_register_related', blank=True, null=True)
        register_confirmation_email = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_register_confirmation_email_related', blank=True, null=True)
+       registration_form = RegistrationForm
        
        @property
        def urlpatterns(self):
        
        @property
        def urlpatterns(self):
@@ -269,18 +279,21 @@ class RegistrationMultiView(PasswordMultiView):
                        return HttpResponseRedirect(request.node.get_absolute_url())
                
                if request.method == 'POST':
                        return HttpResponseRedirect(request.node.get_absolute_url())
                
                if request.method == 'POST':
-                       form = RegistrationForm(request.POST)
+                       form = self.registration_form(request.POST)
                        if form.is_valid():
                                user = form.save()
                        if form.is_valid():
                                user = form.save()
+                               current_site = Site.objects.get_current()
                                context = {
                                context = {
-                                       'link': self.make_confirmation_link('register_confirm', token_generator, user, request.node)
+                                       'link': self.make_confirmation_link('register_confirm', token_generator, user, request.node, secure=request.is_secure()),
+                                       'user': user,
+                                       'site': current_site,
+                                       'request': request
                                }
                                }
-                               current_site = Site.objects.get_current()
                                self.send_confirmation_email('Confirm account creation at %s' % current_site.name, user.email, self.register_confirmation_email, context)
                                messages.add_message(request, messages.SUCCESS, 'An email has been sent to %s with details on activating your account.' % user.email, fail_silently=True)
                                return HttpResponseRedirect(request.node.get_absolute_url())
                else:
                                self.send_confirmation_email('Confirm account creation at %s' % current_site.name, user.email, self.register_confirmation_email, context)
                                messages.add_message(request, messages.SUCCESS, 'An email has been sent to %s with details on activating your account.' % user.email, fail_silently=True)
                                return HttpResponseRedirect(request.node.get_absolute_url())
                else:
-                       form = RegistrationForm()
+                       form = self.registration_form()
                
                context = self.get_context()
                context.update(extra_context or {})
                
                context = self.get_context()
                context.update(extra_context or {})
@@ -367,10 +380,14 @@ class AccountMultiView(RegistrationMultiView):
                                        
                                        email = form.cleaned_data.pop('email')
                                        
                                        
                                        email = form.cleaned_data.pop('email')
                                        
+                                       current_site = Site.objects.get_current()
+                                       
                                        context = {
                                        context = {
-                                               'link': self.make_confirmation_link('email_change_confirm', token_generator, request.user, request.node, token_args=[email], reverse_kwargs={'email': email.replace('@', '+')})
+                                               'link': self.make_confirmation_link('email_change_confirm', token_generator, request.user, request.node, token_args=[email], reverse_kwargs={'email': email.replace('@', '+')}, secure=request.is_secure()),
+                                               'user': request.user,
+                                               'site': current_site,
+                                               'request': request
                                        }
                                        }
-                                       current_site = Site.objects.get_current()
                                        self.send_confirmation_email('Confirm account email change at %s' % current_site.domain, email, self.email_change_confirmation_email, context)
                                        
                                        message = "An email has be sent to %s to confirm the email%s." % (email, bool(request.user.email) and " change" or "")
                                        self.send_confirmation_email('Confirm account email change at %s' % current_site.domain, email, self.email_change_confirmation_email, context)
                                        
                                        message = "An email has be sent to %s to confirm the email%s." % (email, bool(request.user.email) and " change" or "")