Skip to main content

A reimplementation of django.contrib.auth.views as class based views.

Project description

A reimplementation of django.contrib.auth.views as class based views. Hopefully at some point this project or something similar will make it into django proper.

Currently only LoginView is implemented. The others will follow.

Installation

pip install django-class-based-auth-views

Basic usage

Instead of including django.contrib.auth.login into your urls.py, just use the one provided by this project. urls.py:

from class_based_auth_views.views import LoginView
urlpatterns = patterns('',
    url(r'^login/$', LoginView.as_view(form_class=EmailAsUsernameAuthenticationForm), name="login"),
)

Extending LoginView Example

Now that LoginView is based on generic class based views it is much easier to extend. Say you need to implement a 2 step login procedure with a one time password:

from django.contrib.auth import login

class PhaseOneLoginView(LoginView):
    def form_valid(self, form):
        """
        Forces superusers to login in a 2 step process (One Time Password). Other users are logged in normally
        """
        user = form.get_user()
        if user.is_superuser:
            self.save_user(user)
            return HttpResponseRedirect(self.get_phase_two_url())
        else:
            login(self.request, user)
            return HttpResponseRedirect(self.get_success_url())

    def get_phase_two_url(self):
        return reverse('phase_two_login')

    def save_user(self, user):
        self.request.session['otp_user'] = user


class PhaseTwoLoginView(FormView):
    form_class = OTPTokenForm

    def get_user(self):
        return self.request.session.get('otp_user', None)

    def clean_user(self):
        if 'otp_user' in self.request.session:
            del self.request.session['otp_user']

    def form_valid(self, form):
        code = form.cleaned_data.get('code')
        user = self.get_user()
        login(request, user)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-class-based-auth-views-0.1a1.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file django-class-based-auth-views-0.1a1.tar.gz.

File metadata

File hashes

Hashes for django-class-based-auth-views-0.1a1.tar.gz
Algorithm Hash digest
SHA256 58bca16129bf81131a7234c3cce3e106f22a5adf59a84709af3dbc147ecb3ded
MD5 9f009d5ffcd8450904c9a52fef6fbce2
BLAKE2b-256 578eb5880a45de3eecf241c60dcdeb22b976fda2cb03e78fb1bb76f6999882d5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page