Skip to content

Instantly share code, notes, and snippets.

@ssebastianj
Created May 22, 2014 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssebastianj/5cd45293fd6da6cddd41 to your computer and use it in GitHub Desktop.
Save ssebastianj/5cd45293fd6da6cddd41 to your computer and use it in GitHub Desktop.
# -*- 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