Created
September 3, 2016 17:14
-
-
Save Alir3z4/d0adf83c663e65b77749869aec28c763 to your computer and use it in GitHub Desktop.
Defining ``account`` package custom decorators.
This file contains hidden or 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 -*- | |
""" | |
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