Skip to content

Instantly share code, notes, and snippets.

@Alir3z4
Created September 3, 2016 17:14
Show Gist options
  • Save Alir3z4/d0adf83c663e65b77749869aec28c763 to your computer and use it in GitHub Desktop.
Save Alir3z4/d0adf83c663e65b77749869aec28c763 to your computer and use it in GitHub Desktop.
Defining ``account`` package custom decorators.
# -*- coding: utf-8 -*-
"""
account.decorators
==================
Defining ``account`` package custom decorators.
Including:
* redirect_if_logged_in
"""
from django.shortcuts import redirect
from functools import wraps
def redirect_if_logged_in(method):
"""
Decorator to check if the current use on our beloved request has been
logged in, if ``True`` the current user will be redirected to
his/her profile page.
:return: The decorated method or Http redirect response if the user
is logged in.
:rtype: bool
"""
@wraps(method)
def wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated():
return redirect(request.user.get_absolute_url())
return method(request, *args, **kwargs)
return wrapped_view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment