Skip to content

Instantly share code, notes, and snippets.

@tiliv
Last active December 20, 2015 20:38

Revisions

  1. tiliv revised this gist Aug 9, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions mixins.py
    Original file line number Diff line number Diff line change
    @@ -26,6 +26,11 @@ def post(self, request, *args, **kwargs):
    return super(PinAuthenticatedMixin, self).post(request, *args, **kwargs)

    return HttpResponseForbidden()

    def get_context_data(self, **kwargs):
    context = super(PinAuthenticatedMixin, self).get_context_data(**kwargs)
    context['pin_form'] = PinForm()
    return context


    # In action:
  2. tiliv created this gist Aug 9, 2013.
    33 changes: 33 additions & 0 deletions mixins.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    from django import forms
    from django.http import HttpResponseForbidden

    from employees.models import Employee

    class PinForm(forms.Form):
    number = forms.CharField(max_length=4)
    pin = forms.CharField(max_length=4)

    def check_authentication(self):
    """ Assumes the form is baseline valid, and tries to look up an Employee object. """
    try:
    Employee.objects.get(**self.cleaned_data)
    except Employee.DoesNotExist:
    return False
    else:
    return True


    class PinAuthenticatedMixin(object):
    """ Receives posted PIN login data and decides if it matches an Employee. """

    def post(self, request, *args, **kwargs):
    form = PinForm(request.POST)
    if form.is_valid() and form.check_authentication():
    return super(PinAuthenticatedMixin, self).post(request, *args, **kwargs)

    return HttpResponseForbidden()


    # In action:
    class MyView(PinAuthenticatedMixin, TemplateView):
    template_name = "something.html"