Created
May 22, 2014 14:21
-
-
Save ssebastianj/5cd45293fd6da6cddd41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from django.shortcuts import redirect | |
def redirect_if_logged(f=None, redirect_to_url=None): | |
def _decorator(view_func): | |
def _wrapped_view(request, *args, **kwargs): | |
if request.user.is_authenticated(): | |
redirect_url = redirect_to_url | |
if redirect_to_url is None: | |
get_redirect_1 = request.GET.get('next') | |
get_redirect_2 = request.META.get('HTTP_REFERER') | |
# ¿La URL tiene el argumento 'next'? | |
if get_redirect_1 is not None: | |
redirect_url = get_redirect_1 | |
elif get_redirect_2 is not None and get_redirect_2 != request.path: | |
# Redirigir hacia el referer | |
redirect_url = get_redirect_2 | |
else: | |
# Redirigir hacia el home | |
redirect_url = 'home' | |
return redirect(redirect_url, *args, **kwargs) | |
else: | |
return view_func(request, *args, **kwargs) | |
return _wrapped_view | |
if f is None: | |
return _decorator | |
else: | |
return _decorator(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment