本文整理匯總了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