I'm trying to decorate the login_required Django decorator. I've been looking at this question decorating decorators: try to get my head around understanding it. However, this case is a bit different because I don't want to modify the login_required decorator rather add further functionality to it.
To be clear, I'm trying to create a decorator that decorates a view function (which first argument is request as normal) and that checks for a user parameter. If possible I don't want to create my own version of the decorator as proposed here Django: Tweaking @login_required decorator because I think this is a security critical method that might be modified in the future.
This is what I've got so far but it isn't working.
from django.conf import settings
from django.contrib.auth.decorators import login_required, REDIRECT_FIELD_NAME
from django.http.response import HttpResponseRedirect
from django.utils.decorators import available_attrs
from functools import wraps
def login_and_def_pass_required(view_function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
def decorate_view(view_function):
@wraps(view_function, assigned=available_attrs(view_function))
def _double_wrapped_view(request, *args, **kwargs):
actual_decorator = login_required(view_function, redirect_field_name, login_url)
if request.user.temporary_password:
return HttpResponseRedirect(settings.SET_PERMANENT_PASSWORD_URL)
return actual_decorator
return _double_wrapped_view
return decorate_view
This raises the exception "'function' object has no attribute 'get'".
I'm using Django 1.8.4 Python 3.4