This repository has been archived on 2019-09-01. You can view files and clone it, but cannot push or open issues or pull requests.
horrible-todo/tasks/decorators.py

21 lines
655 B
Python

from django.core.exceptions import PermissionDenied
from django.contrib import messages
from django.shortcuts import redirect
from .models import Task
def is_author(function):
"""
Decorator view for checking if the current user owns the task.
"""
def wrap(request, *args, **kwargs):
task = Task.objects.get(pk=kwargs['task_id'])
if task.author == request.user:
return function(request, *args, **kwargs)
else:
messages.error(request, 'What are you doing.')
return redirect('tasks:index')
wrap.__doc__ = function.__doc__
wrap.__name__ = function.__name__
return wrap