X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/ca53fa5dad4374b66e7a9b810557cbf237138fee..f69e27528218f5db3e4fafd536be49f473e3b8fd:/contrib/waldo/models.py diff --git a/contrib/waldo/models.py b/contrib/waldo/models.py index e33ba95..f63cdb1 100644 --- a/contrib/waldo/models.py +++ b/contrib/waldo/models.py @@ -4,187 +4,305 @@ from django.contrib import messages from django.contrib.auth import authenticate, login, views as auth_views from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm, PasswordChangeForm 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.urlresolvers import reverse +from django.core.mail import EmailMultiAlternatives, send_mail 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.utils.translation import ugettext as _ from django.views.decorators.cache import never_cache from django.views.decorators.csrf import csrf_protect from philo.models import MultiView, Page -from philo.contrib.waldo.forms import LOGIN_FORM_KEY, LoginForm, RegistrationForm -from philo.contrib.waldo.tokens import default_token_generator - - -ERROR_MESSAGE = ugettext_lazy("Please enter a correct username and password. Note that both fields are case-sensitive.") - - -def get_field_data(obj, fields): - if fields == None: - fields = [field.name for field in obj._meta.fields if field.editable] - - return dict([(field.name, field.value_from_object(obj)) for field in obj._meta.fields if field.name in fields]) +from philo.contrib.waldo.forms import WaldoAuthenticationForm, RegistrationForm, UserAccountForm +from philo.contrib.waldo.tokens import registration_token_generator, email_token_generator +import urlparse 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') - password_reset_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_reset_related') - register_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_register_related') - register_confirmation_email = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_register_confirmation_email_related') + login_form = WaldoAuthenticationForm @property def urlpatterns(self): - urlpatterns = patterns('', - url(r'^login/$', self.login, name='login'), - url(r'^logout/$', self.logout, name='logout') + return patterns('', + url(r'^login$', self.login, name='login'), + url(r'^logout$', self.logout, name='logout'), ) - urlpatterns += patterns('', - url(r'^password/reset/$', csrf_protect(self.password_reset), name='password_reset'), - url(r'^password/reset/(?P\w+)/(?P[^/]+)/$', - self.password_reset_confirm, name='password_reset_confirm') - ) - urlpatterns += patterns('', - url(r'^register/$', csrf_protect(self.register), name='register'), - url(r'^register/(?P\w+)/(?P[^/]+)/$', - self.register_confirm, name='register_confirm') - ) - return urlpatterns - - def get_context(self, extra_dict=None): - context = {} - context.update(extra_dict or {}) - return context - def display_login_page(self, request, message, node=None, extra_context=None): - request.session.set_test_cookie() + def set_requirement_redirect(self, request, redirect=None): + "Figure out where someone should end up after landing on a `requirement` page like the login page." + if redirect is not None: + pass + elif 'requirement_redirect' in request.session: + return + else: + referrer = request.META.get('HTTP_REFERER', None) - redirect = request.META.get('HTTP_REFERER', None) - path = request.get_full_path() - if redirect != path: - if redirect is None: - redirect = '/'.join(path.split('/')[:-2]) - request.session['redirect'] = redirect + if referrer is not None: + referrer = urlparse.urlparse(referrer) + host = referrer[1] + if host != request.get_host(): + referrer = None + else: + redirect = '%s?%s' % (referrer[2], referrer[4]) - if request.POST: - form = LoginForm(request.POST) + path = request.get_full_path() + if referrer is None or redirect == path: + # Default to the index page if we can't find a referrer or + # if we'd otherwise redirect to where we already are. + redirect = request.node.get_absolute_url() + + request.session['requirement_redirect'] = redirect + + def get_requirement_redirect(self, request, default=None): + redirect = request.session.pop('requirement_redirect', None) + # Security checks a la django.contrib.auth.views.login + if not redirect or ' ' in redirect: + redirect = default else: - form = LoginForm() - context = self.get_context({ - 'message': message, - 'form': form - }) - context.update(extra_context or {}) - return self.login_page.render_to_response(node, request, extra_context=context) + netloc = urlparse.urlparse(redirect)[1] + if netloc and netloc != request.get_host(): + redirect = default + if redirect is None: + redirect = request.node.get_absolute_url() + return redirect - def login(self, request, node=None, extra_context=None): + @never_cache + def login(self, request, extra_context=None): """ Displays the login form for the given HttpRequest. """ - context = self.get_context(extra_context) + self.set_requirement_redirect(request) - from django.contrib.auth.models import User + # Redirect already-authenticated users to the index page. + if request.user.is_authenticated(): + messages.add_message(request, messages.INFO, "You are already authenticated. Please log out if you wish to log in as a different user.") + return HttpResponseRedirect(self.get_requirement_redirect(request)) - # If this isn't already the login page, display it. - if not request.POST.has_key(LOGIN_FORM_KEY): - if request.POST: - message = _("Please log in again, because your session has expired.") - else: - message = "" - return self.display_login_page(request, message, node, 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) + if request.method == 'POST': + form = self.login_form(request=request, data=request.POST) + if form.is_valid(): + redirect = self.get_requirement_redirect(request) + login(request, form.get_user()) + + if request.session.test_cookie_worked(): + request.session.delete_test_cookie() + + return HttpResponseRedirect(redirect) else: - request.session.delete_test_cookie() + form = self.login_form() - # Check the password. - username = request.POST.get('username', None) - password = request.POST.get('password', None) - user = authenticate(username=username, password=password) - if user is None: - message = ERROR_MESSAGE - if username is not None and u'@' in username: - # Mistakenly entered e-mail address instead of username? Look it up. - try: - user = User.objects.get(email=username) - except (User.DoesNotExist, User.MultipleObjectsReturned): - message = _("Usernames cannot contain the '@' character.") - else: - if user.check_password(password): - message = _("Your e-mail address is not your username." - " Try '%s' instead.") % user.username - else: - message = _("Usernames cannot contain the '@' character.") - return self.display_login_page(request, message, node, context) - - # The user data is correct; log in the user in and continue. - else: - if user.is_active: - login(request, user) - redirect = request.session.pop('redirect') - return HttpResponseRedirect(redirect) - else: - return self.display_login_page(request, ERROR_MESSAGE, node, context) - login = never_cache(login) + request.session.set_test_cookie() + + context = self.get_context() + context.update(extra_context or {}) + context.update({ + 'form': form + }) + return self.login_page.render_to_response(request, extra_context=context) - def logout(self, request): - return auth_views.logout(request, request.META['HTTP_REFERER']) + @never_cache + def logout(self, request, extra_context=None): + 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) + self.set_requirement_redirect(request, redirect=request.path) + if request.POST: + messages.add_message(request, messages.ERROR, "Please log in again, because your session has expired.") + return HttpResponseRedirect(self.reverse('login', node=request.node)) + return view(request, *args, **kwargs) return inner + class Meta: + abstract = True + + +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_change_form = PasswordChangeForm + password_set_form = SetPasswordForm + password_reset_form = PasswordResetForm + + @property + def urlpatterns(self): + urlpatterns = super(PasswordMultiView, self).urlpatterns + + if self.password_reset_page and self.password_reset_confirmation_email and self.password_set_page: + urlpatterns += patterns('', + url(r'^password/reset$', csrf_protect(self.password_reset), name='password_reset'), + url(r'^password/reset/(?P\w+)/(?P[^/]+)$', self.password_reset_confirm, name='password_reset_confirm'), + ) + + if self.password_change_page: + urlpatterns += patterns('', + url(r'^password/change$', csrf_protect(self.login_required(self.password_change)), name='password_change'), + ) + return urlpatterns + + 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 {}) + 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): - 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, 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 = self.password_reset_form(request.POST) + 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, 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: + form = self.password_reset_form() + + context = self.get_context() + context.update(extra_context or {}) + context.update({ + 'form': form + }) + return self.password_reset_page.render_to_response(request, extra_context=context) + + 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. + """ + assert uidb36 is not None and token is not None + try: + uid_int = base36_to_int(uidb36) + except: + raise Http404 + + user = get_object_or_404(User, id=uid_int) + + if token_generator.check_token(user, token): + if request.method == '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: + form = self.password_set_form(user) + + context = self.get_context() + context.update(extra_context or {}) + context.update({ + 'form': form + }) + return self.password_set_page.render_to_response(request, extra_context=context) + + raise Http404 - def password_reset(self, request, node=None, extra_context=None): - pass + def password_change(self, request, extra_context=None): + if request.method == '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: + form = self.password_change_form(request.user) + + context = self.get_context() + context.update(extra_context or {}) + context.update({ + 'form': form + }) + return self.password_change_page.render_to_response(request, extra_context=context) - def password_reset_confirm(self, request, node=None, extra_context=None): - pass + class Meta: + abstract = True + + +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) + registration_form = RegistrationForm - def register(self, request, node=None, extra_context=None, token_generator=default_token_generator): + @property + def urlpatterns(self): + urlpatterns = super(RegistrationMultiView, self).urlpatterns + if self.register_page and self.register_confirmation_email: + urlpatterns += patterns('', + url(r'^register$', csrf_protect(self.register), name='register'), + url(r'^register/(?P\w+)/(?P[^/]+)$', self.register_confirm, name='register_confirm') + ) + return urlpatterns + + 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) + form = self.registration_form(request.POST) if form.is_valid(): user = form.save() current_site = Site.objects.get_current() - token = default_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('/')) context = { - 'link': link + 'link': self.make_confirmation_link('register_confirm', token_generator, user, request.node, secure=request.is_secure()), + 'user': user, + 'site': current_site, + 'request': request } 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) - return HttpResponseRedirect('') + 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({'form': form}) + context = self.get_context() context.update(extra_context or {}) - return self.register_page.render_to_response(node, request, extra_context=context) + context.update({ + 'form': form + }) + 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): + 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 @@ -197,112 +315,159 @@ class LoginMultiView(MultiView): raise Http404 user = get_object_or_404(User, id=uid_int) - if default_token_generator.check_token(user, token): + if token_generator.check_token(user, token): user.is_active = True - user.save() - messages.add_message(request, messages.SUCCESS, "Your account's been created! Go ahead and log in.") - return self.post_register_confirm_redirect(request, node) + true_password = user.password + temp_password = token_generator.make_token(user) + try: + user.set_password(temp_password) + user.save() + authenticated_user = authenticate(username=user.username, password=temp_password) + login(request, authenticated_user) + finally: + # if anything goes wrong, do our best make sure that the true password is restored. + user.password = true_password + user.save() + return self.post_register_confirm_redirect(request) raise Http404 - def post_register_confirm_redirect(self, request, node): - return HttpResponseRedirect('/%s/%s/' % (node.get_absolute_url().strip('/'), reverse('login', urlconf=self).strip('/'))) + def post_register_confirm_redirect(self, request): + return HttpResponseRedirect(request.node.get_absolute_url()) class Meta: abstract = True -class AccountMultiView(LoginMultiView): +class AccountMultiView(RegistrationMultiView): """ - Subclasses may define an account_profile model, fields from the User model - to include in the account, and fields from the account profile to use in - the account. + By default, the `account` consists of the first_name, last_name, and email fields + of the User model. Using a different account model is as simple as writing a form that + accepts a User instance as the first argument. """ - manage_account_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_manage_account_page') - user_fields = ['first_name', 'last_name', 'email'] - required_user_fields = user_fields - account_profile = None - account_profile_fields = None + manage_account_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_manage_account_related', blank=True, null=True) + email_change_confirmation_email = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_email_change_confirmation_email_related', blank=True, null=True, help_text="If this is left blank, email changes will be performed without confirmation.") + + account_form = UserAccountForm @property def urlpatterns(self): urlpatterns = super(AccountMultiView, self).urlpatterns - urlpatterns += patterns('', - url(r'^account/$', self.login_required(self.account_view), name='account') - ) + if self.manage_account_page: + urlpatterns += patterns('', + url(r'^account$', self.login_required(self.account_view), name='account'), + ) + if self.email_change_confirmation_email: + urlpatterns += patterns('', + url(r'^account/email/(?P\w+)/(?P[\w.]+[+][\w.]+)/(?P[^/]+)$', self.email_change_confirm, name='email_change_confirm') + ) return urlpatterns - def get_account_forms(self): - user_form = forms.models.modelform_factory(User, fields=self.user_fields) - - if self.account_profile is None: - profile_form = None - else: - profile_form = forms.models.modelform_factory(self.account_profile, fields=self.account_profile_fields or [field.name for field in self.account_profile._meta.fields if field.editable and field.name != 'user']) - - for field_name, field in user_form.base_fields.items(): - if field_name in self.required_user_fields: - field.required = True - return user_form, profile_form - - def get_account_form_instances(self, user, data=None): - form_instances = [] - user_form, profile_form = self.get_account_forms() - if data is None: - form_instances.append(user_form(instance=user)) - if profile_form: - form_instances.append(profile_form(instance=self.account_profile._default_manager.get_or_create(user=user)[0])) - else: - form_instances.append(user_form(data, instance=user)) - if profile_form: - form_instances.append(profile_form(data, instance=self.account_profile._default_manager.get_or_create(user=user)[0])) - - return form_instances - - def account_view(self, request, node=None, extra_context=None): + 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) + form = self.account_form(request.user, request.POST, request.FILES) - for form in form_instances: - if not form.is_valid(): - break - else: - for form in form_instances: - form.save() - messages.add_message(request, messages.SUCCESS, "Account information saved.", fail_silently=True) - return HttpResponseRedirect('') + if form.is_valid(): + message = "Account information saved." + redirect = self.get_requirement_redirect(request, default='') + if 'email' in form.changed_data and self.email_change_confirmation_email: + # ModelForms modify their instances in-place during + # validation, so reset the instance's email to its + # previous value here, then remove the new value + # from cleaned_data. We only do this if an email + # change confirmation email is available. + request.user.email = form.initial['email'] + + email = form.cleaned_data.pop('email') + + current_site = Site.objects.get_current() + + context = { + '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 + } + 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 "") + if not request.user.email: + message += " You will need to confirm the email before accessing pages that require a valid account." + redirect = '' + + form.save() + + if redirect != '': + message += " Here you go!" + + messages.add_message(request, messages.SUCCESS, message, fail_silently=True) + return HttpResponseRedirect(redirect) else: - form_instances = self.get_account_form_instances(request.user) + form = self.account_form(request.user) - context = self.get_context({ - 'forms': form_instances - }) + context = self.get_context() context.update(extra_context or {}) - return self.manage_account_page.render_to_response(node, request, extra_context=context) + context.update({ + 'form': form + }) + 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() - forms = [] - forms.append(user_form(data=get_field_data(user, self.user_fields))) - - if profile_form is not None: - profile = self.account_profile._default_manager.get_or_create(user=user)[0] - forms.append(profile_form(data=get_field_data(profile, self.account_profile_fields))) - - for form in forms: - if not form.is_valid(): - return False - return True + form = self.account_form(user, {}) + form.data = form.initial + return form.is_valid() 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.") - return self.account_view(request, *args, **kwargs) + messages.add_message(request, messages.ERROR, "You need to add some account information before you can access that page.", fail_silently=True) + if self.manage_account_page: + self.set_requirement_redirect(request, redirect=request.path) + redirect = self.reverse('account', node=request.node) + else: + redirect = node.get_absolute_url() + return HttpResponseRedirect(redirect) return view(request, *args, **kwargs) inner = self.login_required(inner) return inner + def post_register_confirm_redirect(self, request): + if self.manage_account_page: + messages.add_message(request, messages.INFO, 'Welcome! Please fill in some more information.', fail_silently=True) + return HttpResponseRedirect(self.reverse('account', node=request.node)) + return super(AccountMultiView, self).post_register_confirm_redirect(request) + + 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. + """ + assert uidb36 is not None and token is not None and email is not None + + try: + uid_int = base36_to_int(uidb36) + except: + raise Http404 + + user = get_object_or_404(User, id=uid_int) + + email = '@'.join(email.rsplit('+', 1)) + + if email == user.email: + # Then short-circuit. + raise Http404 + + if token_generator.check_token(user, email, token): + user.email = email + user.save() + messages.add_message(request, messages.SUCCESS, 'Email changed successfully.') + if self.manage_account_page: + redirect = self.reverse('account', node=request.node) + else: + redirect = request.node.get_absolute_url() + return HttpResponseRedirect(redirect) + + raise Http404 + class Meta: abstract = True \ No newline at end of file