本文整理汇总了Python中rest_framework_jwt.settings.api_settings.JWT_ENCODE_HANDLER属性的典型用法代码示例。如果您正苦于以下问题:Python api_settings.JWT_ENCODE_HANDLER属性的具体用法?Python api_settings.JWT_ENCODE_HANDLER怎么用?Python api_settings.JWT_ENCODE_HANDLER使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类rest_framework_jwt.settings.api_settings
的用法示例。
在下文中一共展示了api_settings.JWT_ENCODE_HANDLER属性的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_token
# 需要导入模块: from rest_framework_jwt.settings import api_settings [as 别名]
# 或者: from rest_framework_jwt.settings.api_settings import JWT_ENCODE_HANDLER [as 别名]
def get_token(self, obj):
warnings.warn(
'Support of djangorestframework-jwt will be removed in 3.0.0 version. '
'Use rest_framework_simplejwt instead.',
DeprecationWarning,
)
try:
from rest_framework_jwt.settings import api_settings
except ImportError:
warnings.warn('djangorestframework-jwt must be installed for JWT authentication',
ImportWarning)
raise
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
payload = jwt_payload_handler(self.get_jwt_payload(obj))
token = jwt_encode_handler(payload)
return token
示例2: create
# 需要导入模块: from rest_framework_jwt.settings import api_settings [as 别名]
# 或者: from rest_framework_jwt.settings.api_settings import JWT_ENCODE_HANDLER [as 别名]
def create(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
try:
if serializer.is_valid(raise_exception=True):
student = serializer.create(request.data)
jwt_token = {
'token': jwt_encode_handler(jwt_payload_handler(student.user))
}
return Response(jwt_token, status=status.HTTP_201_CREATED)
except IntegrityError:
serializer.error_messages = {
'Error': 'Student with that username already exists!'}
return Response(serializer.error_messages, status=status.HTTP_400_BAD_REQUEST)
示例3: resolve_token
# 需要导入模块: from rest_framework_jwt.settings import api_settings [as 别名]
# 或者: from rest_framework_jwt.settings.api_settings import JWT_ENCODE_HANDLER [as 别名]
def resolve_token(self, args, context, info):
if (not context or not context.user or self.id != context.user.id) and not getattr(self, 'is_current_user', False):
return None
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
payload = jwt_payload_handler(self)
token = jwt_encode_handler(payload)
return token
示例4: jwt_encode
# 需要导入模块: from rest_framework_jwt.settings import api_settings [as 别名]
# 或者: from rest_framework_jwt.settings.api_settings import JWT_ENCODE_HANDLER [as 别名]
def jwt_encode(user):
try:
from rest_framework_jwt.settings import api_settings
except ImportError:
raise ImportError("djangorestframework_jwt needs to be installed")
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
payload = jwt_payload_handler(user)
return jwt_encode_handler(payload)
示例5: generate_jwt_token
# 需要导入模块: from rest_framework_jwt.settings import api_settings [as 别名]
# 或者: from rest_framework_jwt.settings.api_settings import JWT_ENCODE_HANDLER [as 别名]
def generate_jwt_token(user, data):
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
data['token'] = token
return data