-
Notifications
You must be signed in to change notification settings - Fork 1
/
decorators.py
63 lines (50 loc) · 1.78 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
from functools import wraps
from .constants import Messages
from .exceptions import WrongUsage
def login_required(fn):
@wraps(fn)
def wrapper(cls, root, info, **kwargs):
user = info.context.user
if not user.is_authenticated:
return cls(success=False, errors=Messages.UNAUTHENTICATED)
return fn(cls, root, info, **kwargs)
return wrapper
def verification_required(fn):
@wraps(fn)
@login_required
def wrapper(cls, root, info, **kwargs):
user = info.context.user
if not user.status.verified:
return cls(success=False, errors=Messages.NOT_VERIFIED)
return fn(cls, root, info, **kwargs)
return wrapper
def secondary_email_required(fn):
@wraps(fn)
@verification_required
def wrapper(cls, root, info, **kwargs):
user = info.context.user
if not user.status.secondary_email:
return cls(success=False, errors=Messages.SECONDARY_EMAIL_REQUIRED)
return fn(cls, root, info, **kwargs)
return wrapper
def password_confirmation_required(fn):
@wraps(fn)
def wrapper(cls, root, info, **kwargs):
try:
field_name = next(
i for i in kwargs.keys() if i in ["password", "old_password"]
)
password = kwargs[field_name]
except Exception:
raise WrongUsage(
"""
@password_confirmation is supposed to be used on
mutations with 'password' or 'old_password' field required.
"""
)
user = info.context.user
if user.check_password(password):
return fn(cls, root, info, **kwargs)
errors = {field_name: Messages.INVALID_PASSWORD}
return cls(success=False, errors=errors)
return wrapper