Account Control part 1

This is the first in a series of videos on creating a product which utilizes other services to help your users stay informed. We start the series with getting our users setup with an account, and giving them the ability to log in and out.

resources

How to log a user in

Authenticating Users

How to log a user out

Starting Jinja2

Deferred Tasks and Scheduled Jobs with Celery 3.1, Django 1.7 and Redis

accounts/urls.py

from django.conf.urls import patterns, url, include

from .views import (
    AccountRegistrationView, LoginView, LogoutView, DashboardView,
    PasswordRecoveryView, SettingsView)

urlpatterns = patterns(
    '',
    url(r'^dashboard/$', DashboardView.as_view(), name="dashboard"),
    url(r'^register/$', AccountRegistrationView.as_view(), name='register'),
    url(r'^login/$', LoginView.as_view(), name='login'),
    url(r'^logout/$', LogoutView.as_view(), name='logout'),

    url(r'^password_recovery/$', PasswordRecoveryView.as_view(),
        name='password_recovery'),
    url(r'^settings/$', SettingsView.as_view(), name="settings"),
)

accounts/views.py

from django.http import HttpResponseRedirect
from django.views.generic import FormView, RedirectView, TemplateView
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import (
    login as auth_login, logout as auth_logout, authenticate)

from braces.views import LoginRequiredMixin

from .forms import UserCreateForm


class DashboardView(LoginRequiredMixin, TemplateView):
    template_name = 'accounts/dashboard.jinja'


class AccountRegistrationView(FormView):
    template_name = 'accounts/register.jinja'
    form_class = UserCreateForm
    success_url = reverse_lazy('dashboard')

    def form_valid(self, form):
        saved_user = form.save()
        user = authenticate(
            username=saved_user.username,
            password=form.cleaned_data['password1'])
        auth_login(self.request, user)
        return HttpResponseRedirect(self.get_success_url())


class LoginView(FormView):
    template_name = 'accounts/login.jinja'
    form_class = AuthenticationForm
    success_url = reverse_lazy('dashboard')

    def form_valid(self, form):
        user = form.get_user()
        auth_login(self.request, user)
        return super(LoginView, self).form_valid(form)


class LogoutView(LoginRequiredMixin, RedirectView):
    permanent = False
    url = reverse_lazy('home')

    def get(self, request, *args, **kwargs):
        auth_logout(request)
        return super(LogoutView, self).get(request, args, kwargs)


class PasswordRecoveryView(FormView):
    pass

class SettingsView(FormView):
    pass

accounts/forms.py

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms

class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')

    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        user.email = self.cleaned_data['email']

        if commit:
            user.save()

        return user
comments powered by Disqus