Hi,

>> To make things a bit easier, I'm about to write my own mixin for that so
>> I only have to provide a method that checks if credentials are OK.
> 
> As you've noticed, you can override the dispatch to decorate the view
> as required, and if you have a common authentication pattern, you can
> put that logic into a mixin.


For future reference, this is what I use now:


class LoginMixin(object):
  def get_test_func(self):
    return getattr(self, 'test_func', lambda u: u.is_authenticated())
  def get_login_url(self):
    return getattr(self, 'login_url', None)
  def get_redirect_field_name(self):
    return getattr(self, 'redirect_field_name', None)
  def dispatch(self, request, *args, **kwargs):
    from django.contrib.auth.decorators import user_passes_test
    return user_passes_test(
      self.get_test_func(),
      login_url = self.get_login_url(),
      redirect_field_name = self.get_redirect_field_name()
    )(super(LoginMixin, self).dispatch
    )(request, *args, **kwargs)


class DashboardView(LoginMixin, TemplateView):
  login_url = '/base/login'
  template_name = 'dashboard.html'



LoginMixin *must* be the first base class, otherwise it could not
override View.dispatch in the other base classes.


-- 
Pascal Germroth

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to