sonar fixes

This commit is contained in:
jain
2023-07-10 12:10:15 +05:30
parent 3098de36b8
commit 026bfb6da7
14 changed files with 94 additions and 51 deletions

View File

@ -1,8 +1,9 @@
"""Account utils"""
"""Third party Django app"""
"""Import django"""
from django.conf import settings
from rest_framework import viewsets, status
from rest_framework.response import Response
"""Third party Django app"""
from templated_email import send_templated_mail
import jwt
from datetime import datetime
@ -11,6 +12,7 @@ from uuid import uuid4
import secrets
def send_otp_email(recipient_email, otp):
"""Send otp on email with template"""
from_email = settings.EMAIL_FROM_ADDRESS
recipient_list = [recipient_email]
send_templated_mail(
@ -26,8 +28,8 @@ def send_otp_email(recipient_email, otp):
def custom_response(detail, data=None, response_status=status.HTTP_200_OK):
"""Custom response code"""
if not data:
"""when data is none"""
data = None
return Response({"data": data, "message": detail, "status": "success", "code": response_status})
@ -39,6 +41,7 @@ def custom_error_response(detail, response_status):
:return: Json response
"""
if not detail:
"""when details is empty"""
detail = {}
return Response({"error": detail, "status": "failed", "code": response_status})
@ -58,16 +61,20 @@ def generate_jwt_token(token_type: str, now_time: int, data: dict = dict):
"""
if type(data) == type:
data = {}
"""Update data dictionary"""
data.update({
'token_type': token_type,
'iss': 'your_site_url',
'iat': timegm(datetime.utcnow().utctimetuple()),
'jti': uuid4().hex
})
"""Access and Refresh token"""
TOKEN_TYPE = ["access", "refresh"]
if token_type == TOKEN_TYPE[1]:
"""Refresh token"""
exp = now_time + settings.SIMPLE_JWT['REFRESH_TOKEN_LIFETIME']
else:
"""access token"""
exp = now_time + settings.SIMPLE_JWT['ACCESS_TOKEN_LIFETIME']
data.update({
@ -84,10 +91,12 @@ def generate_jwt_token(token_type: str, now_time: int, data: dict = dict):
def get_token(data: dict = dict):
""" create access and refresh token """
now_time = datetime.utcnow()
"""generate access token"""
access = generate_jwt_token('access', now_time, data)
"""generate refresh token"""
refresh = generate_jwt_token('refresh', now_time, data)
return {
'access': access,
'refresh': refresh
}
}