forked from danfairs/django-lazysignup
-
Notifications
You must be signed in to change notification settings - Fork 3
/
decorators.py
73 lines (62 loc) · 2.89 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from functools import wraps
from django.conf import settings
from django.contrib.auth import SESSION_KEY
from django.contrib.auth import authenticate
from django.contrib.auth import get_user
from django.contrib.auth import login
from django.shortcuts import redirect
from functools import WRAPPER_ASSIGNMENTS
from lazysignup.constants import USER_AGENT_BLACKLIST
from lazysignup.utils import is_lazy_user
ALLOW_LAZY_REGISTRY = {}
def allow_lazy_user(func):
def wrapped(request, *args, **kwargs):
assert hasattr(request, 'session'), ("You need to have the session "
"app installed")
if getattr(settings, 'LAZYSIGNUP_ENABLE', True):
# If the user agent is one we ignore, bail early
ignore = False
request_user_agent = request.META.get('HTTP_USER_AGENT', '')
for user_agent in USER_AGENT_BLACKLIST:
if user_agent.search(request_user_agent):
ignore = True
break
# If there's already a key in the session for a valid user, then
# we don't need to do anything. If the user isn't valid, then
# get_user will return an anonymous user
if get_user(request).is_anonymous and not ignore:
# If not, then we have to create a user, and log them in.
from lazysignup.models import LazyUser
user, username = LazyUser.objects.create_lazy_user()
request.user = None
user = authenticate(username=username)
assert user, ("Lazy user creation and authentication "
"failed. Have you got "
"lazysignup.backends.LazySignupBackend in "
"AUTHENTICATION_BACKENDS?")
# Set the user id in the session here to prevent the login
# call cycling the session key.
request.session[SESSION_KEY] = user.id
login(request, user)
return func(request, *args, **kwargs)
return wraps(func)(wrapped)
def require_lazy_user(*redirect_args, **redirect_kwargs):
def decorator(func):
@wraps(func, assigned=WRAPPER_ASSIGNMENTS)
def inner(request, *args, **kwargs):
if is_lazy_user(request.user):
return func(request, *args, **kwargs)
else:
return redirect(*redirect_args, **redirect_kwargs)
return inner
return decorator
def require_nonlazy_user(*redirect_args, **redirect_kwargs):
def decorator(func):
@wraps(func, assigned=WRAPPER_ASSIGNMENTS)
def inner(request, *args, **kwargs):
if not is_lazy_user(request.user):
return func(request, *args, **kwargs)
else:
return redirect(*redirect_args, **redirect_kwargs)
return inner
return decorator