decorators.py 966 B

123456789101112131415161718192021222324252627282930
  1. from functools import wraps
  2. from django.urls import reverse
  3. from social_auth.backends import get_backend
  4. from social_auth.exceptions import WrongBackend
  5. def dsa_view(redirect_name=None):
  6. """Decorate djangos-social-auth views. Will check and retrieve backend
  7. or return HttpResponseServerError if backend is not found.
  8. redirect_name parameter is used to build redirect URL used by backend.
  9. """
  10. def dec(func):
  11. @wraps(func)
  12. def wrapper(request, backend, *args, **kwargs):
  13. if redirect_name:
  14. redirect = reverse(redirect_name, args=(backend,))
  15. else:
  16. redirect = request.path
  17. request.social_auth_backend = get_backend(backend, request, redirect)
  18. if request.social_auth_backend is None:
  19. raise WrongBackend(backend)
  20. return func(request, request.social_auth_backend, *args, **kwargs)
  21. return wrapper
  22. return dec