X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/4f879ac556e18e95115681613d7e8d9b1e6b5333..e340bbbaf5346b307ef2dd1ed0f8f343f162a22b:/contrib/waldo/models.py?ds=inline diff --git a/contrib/waldo/models.py b/contrib/waldo/models.py index 93edab1..f1ee05a 100644 --- a/contrib/waldo/models.py +++ b/contrib/waldo/models.py @@ -6,11 +6,12 @@ from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm, Passwo from django.contrib.auth.models import User from django.contrib.auth.tokens import default_token_generator as password_token_generator from django.contrib.sites.models import Site -from django.core.mail import send_mail +from django.core.mail import EmailMultiAlternatives, send_mail from django.core.urlresolvers import reverse from django.db import models from django.http import Http404, HttpResponseRedirect from django.shortcuts import render_to_response, get_object_or_404 +from django.template.defaultfilters import striptags from django.utils.http import int_to_base36, base36_to_int from django.utils.translation import ugettext_lazy, ugettext as _ from django.views.decorators.cache import never_cache @@ -70,7 +71,7 @@ class LoginMultiView(MultiView): context.update(extra_dict or {}) return context - def display_login_page(self, request, message, node=None, extra_context=None): + def display_login_page(self, request, message, extra_context=None): request.session.set_test_cookie() referrer = request.META.get('HTTP_REFERER', None) @@ -81,10 +82,10 @@ class LoginMultiView(MultiView): if host != request.get_host(): referrer = None else: - redirect = ''.join(referrer[2:]) + redirect = '%s?%s' % (referrer[2], referrer[4]) if referrer is None: - redirect = node.get_absolute_url() + redirect = request.node.get_absolute_url() path = request.get_full_path() if redirect != path: @@ -101,12 +102,15 @@ class LoginMultiView(MultiView): 'form': form }) context.update(extra_context or {}) - return self.login_page.render_to_response(node, request, extra_context=context) + return self.login_page.render_to_response(request, extra_context=context) - def login(self, request, node=None, extra_context=None): + def login(self, request, extra_context=None): """ Displays the login form for the given HttpRequest. """ + if request.user.is_authenticated(): + return HttpResponseRedirect(request.node.get_absolute_url()) + context = self.get_context(extra_context) from django.contrib.auth.models import User @@ -117,12 +121,12 @@ class LoginMultiView(MultiView): message = _("Please log in again, because your session has expired.") else: message = "" - return self.display_login_page(request, message, node, context) + return self.display_login_page(request, message, context) # Check that the user accepts cookies. if not request.session.test_cookie_worked(): message = _("Looks like your browser isn't configured to accept cookies. Please enable cookies, reload this page, and try again.") - return self.display_login_page(request, message, node, context) + return self.display_login_page(request, message, context) else: request.session.delete_test_cookie() @@ -144,7 +148,7 @@ class LoginMultiView(MultiView): " Try '%s' instead.") % user.username else: message = _("Usernames cannot contain the '@' character.") - return self.display_login_page(request, message, node, context) + return self.display_login_page(request, message, context) # The user data is correct; log in the user in and continue. else: @@ -153,37 +157,46 @@ class LoginMultiView(MultiView): try: redirect = request.session.pop('redirect') except KeyError: - redirect = node.get_absolute_url() + redirect = request.node.get_absolute_url() return HttpResponseRedirect(redirect) else: - return self.display_login_page(request, ERROR_MESSAGE, node, context) + return self.display_login_page(request, ERROR_MESSAGE, context) login = never_cache(login) def logout(self, request): - return auth_views.logout(request, request.META['HTTP_REFERER']) + return auth_views.logout(request, request.META.get('HTTP_REFERER', request.node.get_absolute_url())) def login_required(self, view): - def inner(request, node=None, *args, **kwargs): + def inner(request, *args, **kwargs): if not request.user.is_authenticated(): login_url = reverse('login', urlconf=self).strip('/') - return HttpResponseRedirect('%s%s/' % (node.get_absolute_url(), login_url)) - return view(request, node=node, *args, **kwargs) + return HttpResponseRedirect('%s%s/' % (request.node.get_absolute_url(), login_url)) + return view(request, *args, **kwargs) return inner def send_confirmation_email(self, subject, email, page, extra_context): - message = page.render_to_string(extra_context=extra_context) + text_content = page.render_to_string(extra_context=extra_context) from_email = 'noreply@%s' % Site.objects.get_current().domain - send_mail(subject, message, from_email, [email]) + + if page.template.mimetype == 'text/html': + msg = EmailMultiAlternatives(subject, striptags(text_content), from_email, [email]) + msg.attach_alternative(text_content, 'text/html') + msg.send() + else: + send_mail(subject, text_content, from_email, [email]) - def password_reset(self, request, node=None, extra_context=None, token_generator=password_token_generator): + def password_reset(self, request, extra_context=None, token_generator=password_token_generator): + if request.user.is_authenticated(): + return HttpResponseRedirect(request.node.get_absolute_url()) + if request.method == 'POST': form = PasswordResetForm(request.POST) if form.is_valid(): current_site = Site.objects.get_current() for user in form.users_cache: token = token_generator.make_token(user) - link = 'http://%s/%s/%s/' % (current_site.domain, node.get_absolute_url().strip('/'), reverse('password_reset_confirm', urlconf=self, kwargs={'uidb36': int_to_base36(user.id), 'token': token}).strip('/')) + link = 'http://%s/%s/%s/' % (current_site.domain, request.node.get_absolute_url().strip('/'), reverse('password_reset_confirm', urlconf=self, kwargs={'uidb36': int_to_base36(user.id), 'token': token}).strip('/')) context = { 'link': link, 'username': user.username @@ -196,9 +209,9 @@ class LoginMultiView(MultiView): context = self.get_context({'form': form}) context.update(extra_context or {}) - return self.password_reset_page.render_to_response(node, request, extra_context=context) + return self.password_reset_page.render_to_response(request, extra_context=context) - def password_reset_confirm(self, request, node=None, extra_context=None, uidb36=None, token=None, token_generator=password_token_generator): + def password_reset_confirm(self, request, extra_context=None, uidb36=None, token=None, token_generator=password_token_generator): """ Checks that a given hash in a password reset link is valid. If so, displays the password set form. @@ -218,16 +231,16 @@ class LoginMultiView(MultiView): if form.is_valid(): form.save() messages.add_message(request, messages.SUCCESS, "Password reset successful.") - return HttpResponseRedirect('/%s/%s/' % (node.get_absolute_url().strip('/'), reverse('login', urlconf=self).strip('/'))) + return HttpResponseRedirect('/%s/%s/' % (request.node.get_absolute_url().strip('/'), reverse('login', urlconf=self).strip('/'))) else: form = SetPasswordForm(user) context = self.get_context({'form': form}) - return self.password_set_page.render_to_response(node, request, extra_context=context) + return self.password_set_page.render_to_response(request, extra_context=context) raise Http404 - def password_change(self, request, node=None, extra_context=None): + def password_change(self, request, extra_context=None): if request.method == 'POST': form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): @@ -239,11 +252,11 @@ class LoginMultiView(MultiView): context = self.get_context({'form': form}) context.update(extra_context or {}) - return self.password_change_page.render_to_response(node, request, extra_context=context) + return self.password_change_page.render_to_response(request, extra_context=context) - def register(self, request, node=None, extra_context=None, token_generator=registration_token_generator): + def register(self, request, extra_context=None, token_generator=registration_token_generator): if request.user.is_authenticated(): - return HttpResponseRedirect(node.get_absolute_url()) + return HttpResponseRedirect(request.node.get_absolute_url()) if request.method == 'POST': form = RegistrationForm(request.POST) @@ -251,21 +264,21 @@ class LoginMultiView(MultiView): user = form.save() current_site = Site.objects.get_current() token = token_generator.make_token(user) - link = 'http://%s/%s/%s/' % (current_site.domain, node.get_absolute_url().strip('/'), reverse('register_confirm', urlconf=self, kwargs={'uidb36': int_to_base36(user.id), 'token': token}).strip('/')) + link = 'http://%s/%s/%s/' % (current_site.domain, request.node.get_absolute_url().strip('/'), reverse('register_confirm', urlconf=self, kwargs={'uidb36': int_to_base36(user.id), 'token': token}).strip('/')) context = { 'link': link } 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('') + return HttpResponseRedirect(request.node.get_absolute_url()) else: form = RegistrationForm() context = self.get_context({'form': form}) context.update(extra_context or {}) - return self.register_page.render_to_response(node, request, extra_context=context) + return self.register_page.render_to_response(request, extra_context=context) - def register_confirm(self, request, node=None, extra_context=None, uidb36=None, token=None, token_generator=registration_token_generator): + def register_confirm(self, request, extra_context=None, uidb36=None, token=None, token_generator=registration_token_generator): """ Checks that a given hash in a registration link is valid and activates the given account. If so, log them in and redirect to @@ -290,12 +303,12 @@ class LoginMultiView(MultiView): # if anything goes wrong, ABSOLUTELY make sure that the true password is restored. user.password = true_password user.save() - return self.post_register_confirm_redirect(request, node) + return self.post_register_confirm_redirect(request) raise Http404 - def post_register_confirm_redirect(self, request, node): - return HttpResponseRedirect(node.get_absolute_url()) + def post_register_confirm_redirect(self, request): + return HttpResponseRedirect(request.node.get_absolute_url()) class Meta: abstract = True @@ -350,7 +363,7 @@ class AccountMultiView(LoginMultiView): return form_instances - def account_view(self, request, node=None, extra_context=None, token_generator=email_token_generator): + def account_view(self, request, extra_context=None, token_generator=email_token_generator, *args, **kwargs): if request.method == 'POST': form_instances = self.get_account_form_instances(request.user, request.POST) current_email = request.user.email @@ -370,7 +383,7 @@ class AccountMultiView(LoginMultiView): current_site = Site.objects.get_current() token = token_generator.make_token(request.user, email) - link = 'http://%s/%s/%s/' % (current_site.domain, node.get_absolute_url().strip('/'), reverse('email_change_confirm', urlconf=self, kwargs={'uidb36': int_to_base36(request.user.id), 'email': email.replace('@', '+'), 'token': token}).strip('/')) + link = 'http://%s/%s/%s/' % (current_site.domain, request.node.get_absolute_url().strip('/'), reverse('email_change_confirm', urlconf=self, kwargs={'uidb36': int_to_base36(request.user.id), 'email': email.replace('@', '+'), 'token': token}).strip('/')) context = { 'link': link } @@ -388,7 +401,7 @@ class AccountMultiView(LoginMultiView): 'forms': form_instances }) context.update(extra_context or {}) - return self.manage_account_page.render_to_response(node, request, extra_context=context) + return self.manage_account_page.render_to_response(request, extra_context=context) def has_valid_account(self, user): user_form, profile_form = self.get_account_forms() @@ -407,18 +420,19 @@ class AccountMultiView(LoginMultiView): def account_required(self, view): def inner(request, *args, **kwargs): if not self.has_valid_account(request.user): - messages.add_message(request, messages.ERROR, "You need to add some account information before you can post listings.", fail_silently=True) + if not request.method == "POST": + messages.add_message(request, messages.ERROR, "You need to add some account information before you can access this page.", fail_silently=True) return self.account_view(request, *args, **kwargs) return view(request, *args, **kwargs) inner = self.login_required(inner) return inner - def post_register_confirm_redirect(self, request, node): + def post_register_confirm_redirect(self, request): messages.add_message(request, messages.INFO, 'Welcome! Please fill in some more information.', fail_silently=True) - return HttpResponseRedirect('/%s/%s/' % (node.get_absolute_url().strip('/'), reverse('account', urlconf=self).strip('/'))) + return HttpResponseRedirect('/%s/%s/' % (request.node.get_absolute_url().strip('/'), reverse('account', urlconf=self).strip('/'))) - def email_change_confirm(self, request, node=None, extra_context=None, uidb36=None, token=None, email=None, token_generator=email_token_generator): + def email_change_confirm(self, request, extra_context=None, uidb36=None, token=None, email=None, token_generator=email_token_generator): """ Checks that a given hash in an email change link is valid. If so, changes the email and redirects to the account page. """ @@ -431,7 +445,7 @@ class AccountMultiView(LoginMultiView): user = get_object_or_404(User, id=uid_int) - email = email.replace('+', '@') + email = '@'.join(email.rsplit('+', 1)) if email == user.email: # Then short-circuit. @@ -441,7 +455,7 @@ class AccountMultiView(LoginMultiView): user.email = email user.save() messages.add_message(request, messages.SUCCESS, 'Email changed successfully.') - return HttpResponseRedirect('/%s/%s/' % (node.get_absolute_url().strip('/'), reverse('account', urlconf=self).strip('/'))) + return HttpResponseRedirect('/%s/%s/' % (request.node.get_absolute_url().strip('/'), reverse('account', urlconf=self).strip('/'))) raise Http404