This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverridden.py
48 lines (35 loc) · 1.61 KB
/
overridden.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
from __future__ import annotations
from functools import wraps
from typing import Any, Callable, TypeVar, cast
import graphql_jwt.decorators
import graphql_jwt.exceptions
from django.http import HttpRequest
from skole.utils.constants import Errors
C = TypeVar("C", bound=Callable[..., Any])
def login_required(func: C) -> C:
"""
Custom version of `graphql_jwt.decorators.login_required`, behavior is the same.
This allows the wrapped function to know that it was wrapped with this decorator.
This is used in `SkoleObjectTypeMeta` to dynamically add info to the API docs if
logging in is required for that resolver.
"""
@wraps(func)
@graphql_jwt.decorators.context(func)
def wrapper(context: HttpRequest, *args: Any, **kwargs: Any) -> Any:
if context.user.is_authenticated:
return func(*args, **kwargs)
raise graphql_jwt.exceptions.PermissionDenied(Errors.AUTH_REQUIRED)
setattr(wrapper, "login_required", True)
return cast(C, wrapper)
def verification_required(func: C) -> C:
"""Use as a decorator on query resolver to make it require email verification."""
@wraps(func)
@graphql_jwt.decorators.context(func)
@login_required
def wrapper(context: HttpRequest, *args: Any, **kwargs: Any) -> Any:
# Ignore: `@login_required` makes sure that `user` cannot be anonymous.
if context.user.verified: # type: ignore[union-attr]
return func(*args, **kwargs)
raise graphql_jwt.exceptions.PermissionDenied(Errors.VERIFICATION_REQUIRED)
setattr(wrapper, "verification_required", True)
return cast(C, wrapper)