Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decorators.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from functools import wraps
  2. from urllib.parse import urlparse
  3. from django.conf import settings
  4. from django.contrib.auth import REDIRECT_FIELD_NAME
  5. from django.core.exceptions import PermissionDenied
  6. from django.shortcuts import resolve_url
  7. def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
  8. """
  9. Decorator for views that checks that the user passes the given test,
  10. redirecting to the log-in page if necessary. The test should be a callable
  11. that takes the user object and returns True if the user passes.
  12. """
  13. def decorator(view_func):
  14. @wraps(view_func)
  15. def _wrapped_view(request, *args, **kwargs):
  16. if test_func(request.user):
  17. return view_func(request, *args, **kwargs)
  18. path = request.build_absolute_uri()
  19. resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
  20. # If the login url is the same scheme and net location then just
  21. # use the path as the "next" url.
  22. login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
  23. current_scheme, current_netloc = urlparse(path)[:2]
  24. if ((not login_scheme or login_scheme == current_scheme) and
  25. (not login_netloc or login_netloc == current_netloc)):
  26. path = request.get_full_path()
  27. from django.contrib.auth.views import redirect_to_login
  28. return redirect_to_login(
  29. path, resolved_login_url, redirect_field_name)
  30. return _wrapped_view
  31. return decorator
  32. def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
  33. """
  34. Decorator for views that checks that the user is logged in, redirecting
  35. to the log-in page if necessary.
  36. """
  37. actual_decorator = user_passes_test(
  38. lambda u: u.is_authenticated,
  39. login_url=login_url,
  40. redirect_field_name=redirect_field_name
  41. )
  42. if function:
  43. return actual_decorator(function)
  44. return actual_decorator
  45. def permission_required(perm, login_url=None, raise_exception=False):
  46. """
  47. Decorator for views that checks whether a user has a particular permission
  48. enabled, redirecting to the log-in page if necessary.
  49. If the raise_exception parameter is given the PermissionDenied exception
  50. is raised.
  51. """
  52. def check_perms(user):
  53. if isinstance(perm, str):
  54. perms = (perm,)
  55. else:
  56. perms = perm
  57. # First check if the user has the permission (even anon users)
  58. if user.has_perms(perms):
  59. return True
  60. # In case the 403 handler should be called raise the exception
  61. if raise_exception:
  62. raise PermissionDenied
  63. # As the last resort, show the login form
  64. return False
  65. return user_passes_test(check_perms, login_url=login_url)