decorators.py 833 B

123456789101112131415161718192021
  1. from django.shortcuts import get_object_or_404
  2. from django.core.exceptions import PermissionDenied
  3. from django.contrib.auth.decorators import login_required
  4. from functools import wraps
  5. from .models import TaskList
  6. def allowed_tasklist_required(view_func):
  7. def wrapped_view(request, *args, **kwargs):
  8. if not request.user.is_authenticated:
  9. return login_required(view_func)(request, *args, **kwargs)
  10. tasklist_slug = kwargs.pop('tasklist_slug')
  11. tasklist = get_object_or_404(TaskList, slug=tasklist_slug)
  12. if not request.user.is_superuser \
  13. and not request.user.groups.all().intersection(tasklist.groups.all()):
  14. raise PermissionDenied
  15. kwargs['tasklist'] = tasklist
  16. return view_func(request, **kwargs)
  17. return wraps(view_func)(wrapped_view)