from django.conf.urls.defaults import url, patterns, include
from django.contrib import messages
from django.contrib.auth import authenticate, login, views as auth_views
-from django.contrib.auth.forms import AuthenticationForm
+from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm, PasswordChangeForm
from django.contrib.auth.models import User
+from django.contrib.sites.models import Site
+from django.core.mail import 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
+from django.shortcuts import render_to_response, get_object_or_404
+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
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.")
-LOGIN_FORM_KEY = 'this_is_the_login_form'
-LoginForm = type('LoginForm', (AuthenticationForm,), {
- LOGIN_FORM_KEY: forms.BooleanField(widget=forms.HiddenInput, initial=True)
-})
def get_field_data(obj, fields):
class LoginMultiView(MultiView):
- login_page = models.ForeignKey(Page, related_name='login_related')
+ """
+ Handles login, registration, and forgotten passwords. In other words, this
+ multiview provides exclusively view and methods related to usernames and
+ passwords.
+ """
+ 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')
@property
def urlpatterns(self):
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<uidb36>\w+)/(?P<token>[^/]+)/$',
+ self.password_reset_confirm, name='password_reset_confirm')
+ )
+ urlpatterns += patterns('',
+ url(r'^register/$', csrf_protect(self.register), name='register'),
+ url(r'^register/(?P<uidb36>\w+)/(?P<token>[^/]+)/$',
+ self.register_confirm, name='register_confirm')
+ )
return urlpatterns
def get_context(self, extra_dict=None):
def login_required(self, view):
def inner(request, node=None, *args, **kwargs):
if not request.user.is_authenticated():
- root_url = node.get_path(Site.objects.get_current().root_node).strip('/')
login_url = reverse('login', urlconf=self).strip('/')
- return HttpResponseRedirect('/%s/%s/' % (root_url, login_url))
+ return HttpResponseRedirect('%s%s/' % (node.get_absolute_url(), login_url))
return view(request, node=node, *args, **kwargs)
return inner
+ def send_confirmation_email(self, subject, email, page, extra_context):
+ message = page.render_to_string(extra_context=extra_context)
+ from_email = 'noreply@%s' % Site.objects.get_current().domain
+ send_mail(subject, message, from_email, [email])
+
+ def password_reset(self, request, node=None, extra_context=None):
+ pass
+
+ def password_reset_confirm(self, request, node=None, extra_context=None):
+ pass
+
+ def register(self, request, node=None, extra_context=None, token_generator=default_token_generator):
+ if request.user.is_authenticated():
+ return HttpResponseRedirect(node.get_absolute_url())
+
+ if request.method == 'POST':
+ form = RegistrationForm(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
+ }
+ 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('')
+ 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)
+
+ def register_confirm(self, request, node=None, extra_context=None, uidb36=None, token=None):
+ """
+ Checks that a given hash in a registration link is valid and activates
+ the given account. If so, log them in and redirect to
+ self.post_register_confirm_redirect.
+ """
+ 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 default_token_generator.check_token(user, token):
+ user.is_active = True
+ true_password = user.password
+ 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, ABSOLUTELY make sure that the true password is restored.
+ user.password = true_password
+ user.save()
+ return self.post_register_confirm_redirect(request, node)
+
+ raise Http404
+
+ def post_register_confirm_redirect(self, request, node):
+ return HttpResponseRedirect(node.get_absolute_url())
+
class Meta:
abstract = True
to include in the account, and fields from the account profile to use in
the account.
"""
- manage_account_page = models.ForeignKey(Page, related_name='classifieds_manage_account_page')
+ 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
inner = self.login_required(inner)
return inner
+ def post_register_confirm_redirect(self, request, node):
+ messages.add_message(request, messages.INFO, 'Welcome! Please fill in some more information.')
+ return HttpResponseRedirect('/%s/%s/' % (node.get_absolute_url().strip('/'), reverse('account', urlconf=self).strip('/')))
+
class Meta:
abstract = True
\ No newline at end of file