From 7376fc555bc60fe29a4110b7d15b9f9d15f6f4fb Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 30 Jun 2023 15:21:00 +0530 Subject: [PATCH 01/34] jira-5 google login --- account/serializers.py | 7 +++++ account/urls.py | 5 +-- account/views.py | 69 +++++++++++++++++++++++++++++++++++++++++- zod_bank/settings.py | 16 ++++++++++ 4 files changed, 94 insertions(+), 3 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index 36571dc..e7a9e33 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -29,6 +29,13 @@ class GoogleSignInSerializer(serializers.Serializer): email=self.validated_data['email']) return instance +class GoogleLoginSerializer1(serializers.Serializer): + access_token = serializers.CharField(max_length=5000, required=True) + + class Meta: + """meta class""" + fields = ('access_token',) + class UpdateGuardianImageSerializer(serializers.ModelSerializer): """Reset Password after verification""" class Meta(object): diff --git a/account/urls.py b/account/urls.py index b9501e6..010398a 100644 --- a/account/urls.py +++ b/account/urls.py @@ -5,14 +5,15 @@ from rest_framework.decorators import api_view """Third party import""" from rest_framework import routers from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp, - ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage) + ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage, + GoogleLoginViewSet1) """Router""" router = routers.SimpleRouter() """API End points with router""" router.register('user', UserLogin, basename='user') router.register('admin', UserLogin, basename='admin') -# router.register('google-login', GoogleLoginAPIViewset, basename='admin') +router.register('google-login', GoogleLoginViewSet1, basename='admin') router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp') router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification') router.register('user-email-verification', UserEmailVerification, basename='user-email-verification') diff --git a/account/views.py b/account/views.py index 067b147..4335b67 100644 --- a/account/views.py +++ b/account/views.py @@ -9,7 +9,7 @@ from account.models import UserProfile, UserPhoneOtp, UserEmailOtp from django.contrib.auth.models import User from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer, ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, - GoogleSignInSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer) + GoogleLoginSerializer1, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer) from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp @@ -22,6 +22,73 @@ from rest_framework.response import Response from rest_framework.permissions import IsAuthenticated from templated_email import send_templated_mail +import google.oauth2.credentials +import google.auth.transport.requests +from rest_framework import status +from rest_framework.response import Response +import requests +from django.conf import settings +# from apps.accounts.utility import get_token + + +class GoogleLoginMixin: + def google_login(self, request): + access_token = request.data.get('access_token') + user_type = request.data.get('user_type') + if not access_token: + return Response({'error': 'Access token is required.'}, status=status.HTTP_400_BAD_REQUEST) + + try: + # Validate the access token and obtain the user's email and name + credentials = google.oauth2.credentials.Credentials.from_authorized_user_info( + info={ + 'access_token': access_token, + 'token_uri': 'https://oauth2.googleapis.com/token', + # 'token_uri': 'https://auth.googleapis.com/token', + 'client_id': settings.GOOGLE_CLIENT_ID, + 'client_secret': settings.GOOGLE_CLIENT_SECRET, + 'refresh_token': None, + } + ) + print("credentials===>",credentials, '===>',credentials.token) + user_info_endpoint = f'https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}' + # user_info_endpoint = f'https://www.googleapis.com/auth/userinfo?access_token={access_token}' + headers = {'Authorization': f'Bearer {credentials.token}'} + response = requests.get(user_info_endpoint, headers=headers) + response.raise_for_status() + user_info = response.json() + email = user_info['email'] + name = user_info['name'] + profile_picture = user_info['picture'] + except Exception as e: + return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST) + + # Check if the user exists in your database or create a new user + # ... + if User.objects.filter(email__iexact=email).exists(): + print("00000000000") + return custom_response(SUCCESS_CODE['3003'], response_status=status.HTTP_200_OK) + + if not User.objects.filter(email__iexact=email).exists(): + print("999999999999999") + user_obj = User.objects.create(username=email, + email=email) + if str(user_type) == '1': + Junior.objects.create(auth=user_obj) + + + # Return a JSON response with the user's email and name + return Response({'token': "get_token()", 'name': name, 'email': email, 'profile_picture': profile_picture}) + +class GoogleLoginViewSet1(GoogleLoginMixin, viewsets.GenericViewSet): + serializer_class = GoogleLoginSerializer1 + + def create(self, request): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + print("88888888888888888888888888") + return self.google_login(request) + # class GoogleLoginAPIViewset(viewsets.ModelViewSet): # """Google Login""" # serializer_class = GoogleSignInSerializer diff --git a/zod_bank/settings.py b/zod_bank/settings.py index 840de8e..27d33b6 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -56,6 +56,7 @@ INSTALLED_APPS = [ 'account', 'junior', 'guardian', + # 'social_django' ] MIDDLEWARE = [ @@ -171,6 +172,21 @@ CORS_ALLOW_HEADERS = ( """Static files (CSS, JavaScript, Images) https://docs.djangoproject.com/en/3.0/howto/static-files/""" +AUTHENTICATION_BACKENDS = [ + 'social_core.backends.google.GoogleOAuth2', + 'django.contrib.auth.backends.ModelBackend', +] + +LOGIN_URL = 'login' +LOGIN_REDIRECT_URL = 'home' +LOGOUT_URL = 'logout' +LOGOUT_REDIRECT_URL = 'login' + +# SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '' +# SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '' + +GOOGLE_CLIENT_ID = "182276566528-hlbjncs19fo502jposod6kft2p9k4grk.apps.googleusercontent.com" +GOOGLE_CLIENT_SECRET = "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" # EMAIL_BACKEND = os.getenv('EMAIL_BACKEND') # EMAIL_HOST = os.getenv('EMAIL_HOST') From 79ac140ddd03fec4025dc2e861f893b147a27488 Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 30 Jun 2023 16:22:41 +0530 Subject: [PATCH 02/34] jira-3 google login --- account/views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/account/views.py b/account/views.py index 4335b67..93cf17f 100644 --- a/account/views.py +++ b/account/views.py @@ -74,7 +74,9 @@ class GoogleLoginMixin: user_obj = User.objects.create(username=email, email=email) if str(user_type) == '1': - Junior.objects.create(auth=user_obj) + Junior.objects.create(auth=user_obj, is_verified=True) + if str(user_type) == '2': + Guardian.objects.create(user=user_obj, is_verified=True) # Return a JSON response with the user's email and name From d2498f82ad3862ef76e06fa16bae69135a89cb64 Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 30 Jun 2023 21:25:43 +0530 Subject: [PATCH 03/34] jira-5 google login and apple login --- account/serializers.py | 21 +++++----- account/urls.py | 7 ++-- account/utils.py | 57 ++++++++++++++++++++++++++- account/views.py | 88 +++++++++++++++++++++++++++--------------- zod_bank/settings.py | 20 +++++----- 5 files changed, 139 insertions(+), 54 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index e7a9e33..f25510d 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -13,7 +13,7 @@ from rest_framework.decorators import action from django.contrib.auth import authenticate, login from rest_framework_simplejwt.tokens import RefreshToken from guardian.utils import upload_image_to_alibaba - +from .utils import get_token class GoogleSignInSerializer(serializers.Serializer): """Google login Serializer""" email = serializers.EmailField() @@ -139,11 +139,13 @@ class GuardianSerializer(serializers.ModelSerializer): first_name = serializers.SerializerMethodField('get_first_name') last_name = serializers.SerializerMethodField('get_last_name') auth_token = serializers.SerializerMethodField('get_auth_token') + refresh_token = serializers.SerializerMethodField('get_refresh_token') def get_auth_token(self, obj): - refresh = RefreshToken.for_user(obj.user) - access_token = str(refresh.access_token) - return access_token + return get_token()['access'] + def get_refresh_token(self, obj): + return get_token()['refresh'] + def get_user_type(self, obj): """user type""" @@ -164,7 +166,7 @@ class GuardianSerializer(serializers.ModelSerializer): class Meta(object): """Meta info""" model = Guardian - fields = ['auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name', 'gender', 'dob', + fields = ['auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name', 'gender', 'dob', 'referral_code', 'is_active', 'is_complete_profile', 'passcode', 'created_at', 'updated_at', 'user_type'] @@ -176,11 +178,12 @@ class JuniorSerializer(serializers.ModelSerializer): first_name = serializers.SerializerMethodField('get_first_name') last_name = serializers.SerializerMethodField('get_last_name') auth_token = serializers.SerializerMethodField('get_auth_token') + refresh_token = serializers.SerializerMethodField('get_refresh_token') def get_auth_token(self, obj): - refresh = RefreshToken.for_user(obj.auth) - access_token = str(refresh.access_token) - return access_token + return get_token()['access'] + def get_refresh_token(self, obj): + return get_token()['refresh'] def get_user_type(self, obj): return JUNIOR @@ -197,7 +200,7 @@ class JuniorSerializer(serializers.ModelSerializer): class Meta(object): """Meta info""" model = Junior - fields = ['auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', + fields = ['auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'updated_at', 'user_type'] diff --git a/account/urls.py b/account/urls.py index 010398a..e0e1c6c 100644 --- a/account/urls.py +++ b/account/urls.py @@ -6,14 +6,14 @@ from rest_framework.decorators import api_view from rest_framework import routers from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp, ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage, - GoogleLoginViewSet1) + GoogleLoginViewSet, SigninWithApple) """Router""" router = routers.SimpleRouter() """API End points with router""" router.register('user', UserLogin, basename='user') router.register('admin', UserLogin, basename='admin') -router.register('google-login', GoogleLoginViewSet1, basename='admin') +router.register('google-login', GoogleLoginViewSet, basename='admin') router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp') router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification') router.register('user-email-verification', UserEmailVerification, basename='user-email-verification') @@ -23,5 +23,6 @@ urlpatterns = [ path('api/v1/forgot-password/', ForgotPasswordAPIView.as_view()), path('api/v1/reset-password/', ResetPasswordAPIView.as_view()), path('api/v1/change-password/', ChangePasswordAPIView.as_view()), - path('api/v1/update-profile-image/', UpdateProfileImage.as_view()) + path('api/v1/update-profile-image/', UpdateProfileImage.as_view()), + path('api/v1/apple-login/', SigninWithApple.as_view(), name='signup_with_apple'), ] diff --git a/account/utils.py b/account/utils.py index bfc2399..0848c03 100644 --- a/account/utils.py +++ b/account/utils.py @@ -3,8 +3,13 @@ from django.conf import settings from rest_framework import viewsets, status from rest_framework.response import Response - from templated_email import send_templated_mail +import jwt +from datetime import datetime +from calendar import timegm +from uuid import uuid4 +import secrets + def send_otp_email(recipient_email, otp): from_email = settings.EMAIL_FROM_ADDRESS recipient_list = [recipient_email] @@ -36,3 +41,53 @@ def custom_error_response(detail, response_status): if not detail: detail = {} return Response({"error": detail, "status": "failed", "code": response_status}) + + +def get_user_data(attrs): + """ + used to decode token + """ + user_data = jwt.decode(jwt=attrs['token'], options={'verify_signature': False}, + algorithms=['RS256']) + return user_data + + +def generate_jwt_token(token_type: str, now_time: int, data: dict = dict): + """ + used to generate jwt token + """ + if type(data) == type: + data = {} + data.update({ + 'token_type': token_type, + 'iss': 'your_site_url', + 'iat': timegm(datetime.utcnow().utctimetuple()), + 'jti': uuid4().hex + }) + TOKEN_TYPE = ["access", "refresh"] + if token_type == TOKEN_TYPE[1]: + exp = now_time + settings.SIMPLE_JWT['REFRESH_TOKEN_LIFETIME'] + else: + exp = now_time + settings.SIMPLE_JWT['ACCESS_TOKEN_LIFETIME'] + + data.update({ + "exp": timegm(exp.utctimetuple()) + }) + + + signing_key = secrets.token_hex(32) + + return jwt.encode(payload=data, key=signing_key, + algorithm='HS256') + + +def get_token(data: dict = dict): + """ create access and refresh token """ + now_time = datetime.utcnow() + access = generate_jwt_token('access', now_time, data) + refresh = generate_jwt_token('refresh', now_time, data) + + return { + 'access': access, + 'refresh': refresh + } \ No newline at end of file diff --git a/account/views.py b/account/views.py index 93cf17f..106dfce 100644 --- a/account/views.py +++ b/account/views.py @@ -2,6 +2,7 @@ from rest_framework import viewsets, status, views from rest_framework.decorators import action import random import logging +import jwt from django.contrib.auth import authenticate, login from guardian.models import Guardian from junior.models import Junior @@ -13,22 +14,17 @@ from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSeriali from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp -from django.conf import settings from account.utils import send_otp_email from account.utils import custom_response, custom_error_response -from django.core.mail import EmailMessage -from django.core.mail import send_mail -from rest_framework.response import Response from rest_framework.permissions import IsAuthenticated from templated_email import send_templated_mail - import google.oauth2.credentials import google.auth.transport.requests from rest_framework import status from rest_framework.response import Response import requests from django.conf import settings -# from apps.accounts.utility import get_token +from .utils import get_token class GoogleLoginMixin: @@ -65,9 +61,18 @@ class GoogleLoginMixin: # Check if the user exists in your database or create a new user # ... - if User.objects.filter(email__iexact=email).exists(): + user_data = User.objects.filter(email__iexact=email) + if user_data.exists(): print("00000000000") - return custom_response(SUCCESS_CODE['3003'], response_status=status.HTTP_200_OK) + if str(user_type) == '1': + junior_query = Junior.objects.filter(auth=user_data.last()).last() + serializer = JuniorSerializer(junior_query) + if str(user_type) == '2': + guardian_query = Guardian.objects.filter(user=user_data.last()).last() + print("guardian_query==>",guardian_query,'====>',type(guardian_query)) + serializer = GuardianSerializer(guardian_query) + return custom_response(SUCCESS_CODE['3003'], serializer.data, + response_status=status.HTTP_200_OK) if not User.objects.filter(email__iexact=email).exists(): print("999999999999999") @@ -80,9 +85,11 @@ class GoogleLoginMixin: # Return a JSON response with the user's email and name - return Response({'token': "get_token()", 'name': name, 'email': email, 'profile_picture': profile_picture}) + return custom_response(SUCCESS_CODE['3003'], {'auth_token': get_token(), 'name': name, 'email': email, + 'profile_picture': profile_picture, "user_type":user_type}, + response_status=status.HTTP_200_OK) -class GoogleLoginViewSet1(GoogleLoginMixin, viewsets.GenericViewSet): +class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet): serializer_class = GoogleLoginSerializer1 def create(self, request): @@ -91,27 +98,46 @@ class GoogleLoginViewSet1(GoogleLoginMixin, viewsets.GenericViewSet): print("88888888888888888888888888") return self.google_login(request) -# class GoogleLoginAPIViewset(viewsets.ModelViewSet): -# """Google Login""" -# serializer_class = GoogleSignInSerializer -# -# def create(self, request, *args, **kwargs): -# """ -# Override default behaviour of create method -# """ -# provider_type = [] -# serializer = self.get_serializer(data=request.data) -# if serializer.is_valid(raise_exception=True): -# # provider = self.get_provider_view(request.data.get('provider')) -# # if User is not authenticated then send error message -# # if not provider.is_authenticated(request): -# # return custom_error_response({}, status.HTTP_400_BAD_REQUEST) -# -# user = serializer.save() -# if User.objects.filter(email__iexact=user.email).exists(): -# print("ppppppppppppp") -# return custom_response(SUCCESS_CODE["3003"], response_status=status.HTTP_200_OK) -# return custom_response(ERROR_CODE["2002"], response_status=status.HTTP_400_BAD_REQUEST) +class SigninWithApple(views.APIView): + """This API is for sign in with Apple for app.""" + def post(self, request): + token = request.data.get("identityToken") + user_type = request.data.get("user_type") + if not token: + return Response({"message": "data should contain `identityToken`"}) + decoded_data = jwt.decode(token, options={"verify_signature": False}) + print("decoded_data===>",decoded_data) + user_data = {"email": decoded_data.get('email'),"username": decoded_data.get('email'), + "first_name": request.data.get("fullName").get("givenName"),"is_active": True, + "last_name": request.data.get("fullName").get("familyName"),} + if user_data['email'] and not user_data['first_name']: + user_data['first_name'] = user_data['email'].split("@")[0] + user_data['last_name'] = user_data['email'].split("@")[0] + if decoded_data.get("email"): + try: + user = User.objects.get(email=decoded_data.get("email")) + if str(user_type) == '1': + junior_query = Junior.objects.filter(auth=user).last() + print("junior_query==>", junior_query, '====>', type(junior_query)) + serializer = JuniorSerializer(junior_query) + if str(user_type) == '2': + guardian_query = Guardian.objects.filter(user=user).last() + print("guardian_query==>", guardian_query, '====>', type(guardian_query)) + serializer = GuardianSerializer(guardian_query) + return custom_response(SUCCESS_CODE['3003'], serializer.data, + response_status=status.HTTP_200_OK) + + except User.DoesNotExist: + user = User.objects.create(**user_data) + if str(user_type) == '1': + junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True) + serializer = JuniorSerializer(junior_query) + if str(user_type) == '2': + guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True) + serializer = GuardianSerializer(guardian_query) + return custom_response(SUCCESS_CODE['3003'], serializer.data, + response_status=status.HTTP_200_OK) + class UpdateProfileImage(views.APIView): permission_classes = [IsAuthenticated] diff --git a/zod_bank/settings.py b/zod_bank/settings.py index 27d33b6..fc88b1c 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -96,7 +96,7 @@ REST_FRAMEWORK = { ] } SIMPLE_JWT = { - 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=15), + 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=50), 'REFRESH_TOKEN_LIFETIME': timedelta(days=7), } # Database @@ -172,15 +172,15 @@ CORS_ALLOW_HEADERS = ( """Static files (CSS, JavaScript, Images) https://docs.djangoproject.com/en/3.0/howto/static-files/""" -AUTHENTICATION_BACKENDS = [ - 'social_core.backends.google.GoogleOAuth2', - 'django.contrib.auth.backends.ModelBackend', -] - -LOGIN_URL = 'login' -LOGIN_REDIRECT_URL = 'home' -LOGOUT_URL = 'logout' -LOGOUT_REDIRECT_URL = 'login' +# AUTHENTICATION_BACKENDS = [ +# 'social_core.backends.google.GoogleOAuth2', +# 'django.contrib.auth.backends.ModelBackend', +# ] +# +# LOGIN_URL = 'login' +# LOGIN_REDIRECT_URL = 'home' +# LOGOUT_URL = 'logout' +# LOGOUT_REDIRECT_URL = 'login' # SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '' # SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '' From 014b5fe183cb58ea6f57ac4b549d8bb831d67999 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 3 Jul 2023 12:58:06 +0530 Subject: [PATCH 04/34] jira-5 apple login with first and last name --- account/views.py | 95 +++++++++++++++++++++++++----------------------- base/messages.py | 11 +++--- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/account/views.py b/account/views.py index 106dfce..f645080 100644 --- a/account/views.py +++ b/account/views.py @@ -31,6 +31,7 @@ class GoogleLoginMixin: def google_login(self, request): access_token = request.data.get('access_token') user_type = request.data.get('user_type') + # decoded_data = jwt.decode(access_token, options={"verify_signature": False}) if not access_token: return Response({'error': 'Access token is required.'}, status=status.HTTP_400_BAD_REQUEST) @@ -40,7 +41,6 @@ class GoogleLoginMixin: info={ 'access_token': access_token, 'token_uri': 'https://oauth2.googleapis.com/token', - # 'token_uri': 'https://auth.googleapis.com/token', 'client_id': settings.GOOGLE_CLIENT_ID, 'client_secret': settings.GOOGLE_CLIENT_SECRET, 'refresh_token': None, @@ -48,16 +48,17 @@ class GoogleLoginMixin: ) print("credentials===>",credentials, '===>',credentials.token) user_info_endpoint = f'https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}' - # user_info_endpoint = f'https://www.googleapis.com/auth/userinfo?access_token={access_token}' headers = {'Authorization': f'Bearer {credentials.token}'} response = requests.get(user_info_endpoint, headers=headers) response.raise_for_status() user_info = response.json() + print("user_info===>",user_info,'==>',type(user_info)) email = user_info['email'] - name = user_info['name'] + first_name = user_info['given_name'] + last_name = user_info['family_name'] profile_picture = user_info['picture'] except Exception as e: - return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST) + return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST) # Check if the user exists in your database or create a new user # ... @@ -76,19 +77,20 @@ class GoogleLoginMixin: if not User.objects.filter(email__iexact=email).exists(): print("999999999999999") - user_obj = User.objects.create(username=email, - email=email) + user_obj = User.objects.create(username=email, email=email, first_name=first_name, last_name=last_name) if str(user_type) == '1': - Junior.objects.create(auth=user_obj, is_verified=True) + junior_query = Junior.objects.create(auth=user_obj, is_verified=True, is_active=True, + image=profile_picture) + serializer = JuniorSerializer(junior_query) if str(user_type) == '2': - Guardian.objects.create(user=user_obj, is_verified=True) - - + guardian_query = Guardian.objects.create(user=user_obj, is_verified=True, is_active=True, + image=profile_picture) + serializer = GuardianSerializer(guardian_query) # Return a JSON response with the user's email and name - return custom_response(SUCCESS_CODE['3003'], {'auth_token': get_token(), 'name': name, 'email': email, - 'profile_picture': profile_picture, "user_type":user_type}, + return custom_response(SUCCESS_CODE['3003'], serializer.data, response_status=status.HTTP_200_OK) + class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet): serializer_class = GoogleLoginSerializer1 @@ -101,42 +103,45 @@ class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet): class SigninWithApple(views.APIView): """This API is for sign in with Apple for app.""" def post(self, request): - token = request.data.get("identityToken") + token = request.data.get("access_token") user_type = request.data.get("user_type") if not token: - return Response({"message": "data should contain `identityToken`"}) - decoded_data = jwt.decode(token, options={"verify_signature": False}) - print("decoded_data===>",decoded_data) - user_data = {"email": decoded_data.get('email'),"username": decoded_data.get('email'), - "first_name": request.data.get("fullName").get("givenName"),"is_active": True, - "last_name": request.data.get("fullName").get("familyName"),} - if user_data['email'] and not user_data['first_name']: - user_data['first_name'] = user_data['email'].split("@")[0] - user_data['last_name'] = user_data['email'].split("@")[0] - if decoded_data.get("email"): - try: - user = User.objects.get(email=decoded_data.get("email")) - if str(user_type) == '1': - junior_query = Junior.objects.filter(auth=user).last() - print("junior_query==>", junior_query, '====>', type(junior_query)) - serializer = JuniorSerializer(junior_query) - if str(user_type) == '2': - guardian_query = Guardian.objects.filter(user=user).last() - print("guardian_query==>", guardian_query, '====>', type(guardian_query)) - serializer = GuardianSerializer(guardian_query) - return custom_response(SUCCESS_CODE['3003'], serializer.data, - response_status=status.HTTP_200_OK) + return custom_error_response(ERROR_CODE['2027'], response_status=status.HTTP_400_BAD_REQUEST) + try: + decoded_data = jwt.decode(token, options={"verify_signature": False}) + print("decoded_data===>",decoded_data) + user_data = {"email": decoded_data.get('email'),"username": decoded_data.get('email'), + "first_name": request.data.get("fullName").get("givenName"),"is_active": True, + "last_name": request.data.get("fullName").get("familyName"),} + if user_data['email'] and not user_data['first_name']: + user_data['first_name'] = user_data['email'].split("@")[0] + user_data['last_name'] = user_data['email'].split("@")[0] + if decoded_data.get("email"): + try: + user = User.objects.get(email=decoded_data.get("email")) + if str(user_type) == '1': + junior_query = Junior.objects.filter(auth=user).last() + print("junior_query==>", junior_query, '====>', type(junior_query)) + serializer = JuniorSerializer(junior_query) + if str(user_type) == '2': + guardian_query = Guardian.objects.filter(user=user).last() + print("guardian_query==>", guardian_query, '====>', type(guardian_query)) + serializer = GuardianSerializer(guardian_query) + return custom_response(SUCCESS_CODE['3003'], serializer.data, + response_status=status.HTTP_200_OK) - except User.DoesNotExist: - user = User.objects.create(**user_data) - if str(user_type) == '1': - junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True) - serializer = JuniorSerializer(junior_query) - if str(user_type) == '2': - guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True) - serializer = GuardianSerializer(guardian_query) - return custom_response(SUCCESS_CODE['3003'], serializer.data, - response_status=status.HTTP_200_OK) + except User.DoesNotExist: + user = User.objects.create(**user_data) + if str(user_type) == '1': + junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True) + serializer = JuniorSerializer(junior_query) + if str(user_type) == '2': + guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True) + serializer = GuardianSerializer(guardian_query) + return custom_response(SUCCESS_CODE['3003'], serializer.data, + response_status=status.HTTP_200_OK) + except Exception as e: + logging.error(e) class UpdateProfileImage(views.APIView): diff --git a/base/messages.py b/base/messages.py index 7d48f97..83e221b 100644 --- a/base/messages.py +++ b/base/messages.py @@ -45,11 +45,12 @@ ERROR_CODE = { "2019": "Either File extension or File size doesn't meet the requirements", "2020": "Enter valid mobile number", "2021": "Already register", - "2022":"Invalid Guardian code", - "2023":"Invalid user", - "2024":"Email not verified", - "2025":"Invalid input. Expected a list of strings.", - "2026" : "New password should not same as old password" + "2022": "Invalid Guardian code", + "2023": "Invalid user", + "2024": "Email not verified", + "2025": "Invalid input. Expected a list of strings.", + "2026": "New password should not same as old password", + "2027": "data should contain `identityToken`" } SUCCESS_CODE = { # Success code for password From 28cbdf9cfa689e22f7a5274132db2c8a19865b3e Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 3 Jul 2023 14:57:07 +0530 Subject: [PATCH 05/34] jira-5 social login --- account/serializers.py | 4 ++-- account/views.py | 18 ++---------------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index f25510d..2388b5f 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -167,7 +167,7 @@ class GuardianSerializer(serializers.ModelSerializer): """Meta info""" model = Guardian fields = ['auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name', 'gender', 'dob', - 'referral_code', 'is_active', 'is_complete_profile', 'passcode', + 'referral_code', 'is_active', 'is_complete_profile', 'passcode', 'image', 'created_at', 'updated_at', 'user_type'] @@ -201,7 +201,7 @@ class JuniorSerializer(serializers.ModelSerializer): """Meta info""" model = Junior fields = ['auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', - 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', + 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', 'updated_at', 'user_type'] class EmailVerificationSerializer(serializers.ModelSerializer): diff --git a/account/views.py b/account/views.py index f645080..37bc7c8 100644 --- a/account/views.py +++ b/account/views.py @@ -31,7 +31,6 @@ class GoogleLoginMixin: def google_login(self, request): access_token = request.data.get('access_token') user_type = request.data.get('user_type') - # decoded_data = jwt.decode(access_token, options={"verify_signature": False}) if not access_token: return Response({'error': 'Access token is required.'}, status=status.HTTP_400_BAD_REQUEST) @@ -46,13 +45,11 @@ class GoogleLoginMixin: 'refresh_token': None, } ) - print("credentials===>",credentials, '===>',credentials.token) user_info_endpoint = f'https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}' headers = {'Authorization': f'Bearer {credentials.token}'} response = requests.get(user_info_endpoint, headers=headers) response.raise_for_status() user_info = response.json() - print("user_info===>",user_info,'==>',type(user_info)) email = user_info['email'] first_name = user_info['given_name'] last_name = user_info['family_name'] @@ -64,19 +61,16 @@ class GoogleLoginMixin: # ... user_data = User.objects.filter(email__iexact=email) if user_data.exists(): - print("00000000000") if str(user_type) == '1': junior_query = Junior.objects.filter(auth=user_data.last()).last() serializer = JuniorSerializer(junior_query) if str(user_type) == '2': guardian_query = Guardian.objects.filter(user=user_data.last()).last() - print("guardian_query==>",guardian_query,'====>',type(guardian_query)) serializer = GuardianSerializer(guardian_query) return custom_response(SUCCESS_CODE['3003'], serializer.data, response_status=status.HTTP_200_OK) if not User.objects.filter(email__iexact=email).exists(): - print("999999999999999") user_obj = User.objects.create(username=email, email=email, first_name=first_name, last_name=last_name) if str(user_type) == '1': junior_query = Junior.objects.create(auth=user_obj, is_verified=True, is_active=True, @@ -97,7 +91,6 @@ class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet): def create(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) - print("88888888888888888888888888") return self.google_login(request) class SigninWithApple(views.APIView): @@ -109,23 +102,15 @@ class SigninWithApple(views.APIView): return custom_error_response(ERROR_CODE['2027'], response_status=status.HTTP_400_BAD_REQUEST) try: decoded_data = jwt.decode(token, options={"verify_signature": False}) - print("decoded_data===>",decoded_data) - user_data = {"email": decoded_data.get('email'),"username": decoded_data.get('email'), - "first_name": request.data.get("fullName").get("givenName"),"is_active": True, - "last_name": request.data.get("fullName").get("familyName"),} - if user_data['email'] and not user_data['first_name']: - user_data['first_name'] = user_data['email'].split("@")[0] - user_data['last_name'] = user_data['email'].split("@")[0] + user_data = {"email": decoded_data.get('email'), "username": decoded_data.get('email'), "is_active": True} if decoded_data.get("email"): try: user = User.objects.get(email=decoded_data.get("email")) if str(user_type) == '1': junior_query = Junior.objects.filter(auth=user).last() - print("junior_query==>", junior_query, '====>', type(junior_query)) serializer = JuniorSerializer(junior_query) if str(user_type) == '2': guardian_query = Guardian.objects.filter(user=user).last() - print("guardian_query==>", guardian_query, '====>', type(guardian_query)) serializer = GuardianSerializer(guardian_query) return custom_response(SUCCESS_CODE['3003'], serializer.data, response_status=status.HTTP_200_OK) @@ -142,6 +127,7 @@ class SigninWithApple(views.APIView): response_status=status.HTTP_200_OK) except Exception as e: logging.error(e) + return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST) class UpdateProfileImage(views.APIView): From a27c486c4368057a7156e0a6cd9ae37a3509d6e0 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 3 Jul 2023 15:53:25 +0530 Subject: [PATCH 06/34] remove unused code --- account/serializers.py | 15 +-------------- account/views.py | 4 ++-- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index 2388b5f..e2aee49 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -14,22 +14,9 @@ from django.contrib.auth import authenticate, login from rest_framework_simplejwt.tokens import RefreshToken from guardian.utils import upload_image_to_alibaba from .utils import get_token -class GoogleSignInSerializer(serializers.Serializer): - """Google login Serializer""" - email = serializers.EmailField() - def create(self, validated_data): - """Create or update user model""" - with transaction.atomic(): - if User.objects.filter(email__iexact=self.validated_data['email']).exists(): - return User.objects.get(email__iexact=self.validated_data['email']) - if not User.objects.filter(email__iexact=self.validated_data['email']).exists(): - instance = User.objects.create(username=self.validated_data['email'], - email=self.validated_data['email']) - return instance - -class GoogleLoginSerializer1(serializers.Serializer): +class GoogleLoginSerializer(serializers.Serializer): access_token = serializers.CharField(max_length=5000, required=True) class Meta: diff --git a/account/views.py b/account/views.py index 37bc7c8..f992a05 100644 --- a/account/views.py +++ b/account/views.py @@ -10,7 +10,7 @@ from account.models import UserProfile, UserPhoneOtp, UserEmailOtp from django.contrib.auth.models import User from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer, ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, - GoogleLoginSerializer1, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer) + GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer) from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp @@ -86,7 +86,7 @@ class GoogleLoginMixin: class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet): - serializer_class = GoogleLoginSerializer1 + serializer_class = GoogleLoginSerializer def create(self, request): serializer = self.get_serializer(data=request.data) From 22f1d01b9478e5cb5df1a7e1e41a88f8bc4d5390 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 3 Jul 2023 15:59:37 +0530 Subject: [PATCH 07/34] requirement.txt file --- requirements.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/requirements.txt b/requirements.txt index 2175525..1103c6a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ async-timeout==4.0.2 billiard==4.1.0 boto3==1.26.157 botocore==1.29.157 +cachetools==5.3.1 celery==5.3.1 certifi==2023.5.7 cffi==1.15.1 @@ -38,6 +39,7 @@ django-timezone-field==5.1 djangorestframework==3.14.0 djangorestframework-simplejwt==5.2.2 drf-yasg==1.21.6 +google-auth==2.21.0 gunicorn==20.1.0 idna==3.4 inflection==0.5.1 @@ -50,6 +52,8 @@ phonenumbers==8.13.15 Pillow==9.5.0 prompt-toolkit==3.0.38 psycopg==3.1.9 +pyasn1==0.5.0 +pyasn1-modules==0.3.0 pycparser==2.21 pycryptodome==3.18.0 PyJWT==2.7.0 @@ -60,6 +64,7 @@ pytz==2023.3 PyYAML==6.0 redis==4.5.5 requests==2.31.0 +rsa==4.9 s3transfer==0.6.1 six==1.16.0 sqlparse==0.4.4 From 1c7d6bac85ce0891a1c75ca9d0fccaef9c1baa08 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 3 Jul 2023 17:17:58 +0530 Subject: [PATCH 08/34] changes in settings and env file --- zod_bank/settings.py | 64 ++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/zod_bank/settings.py b/zod_bank/settings.py index fc88b1c..6930469 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -172,49 +172,37 @@ CORS_ALLOW_HEADERS = ( """Static files (CSS, JavaScript, Images) https://docs.djangoproject.com/en/3.0/howto/static-files/""" -# AUTHENTICATION_BACKENDS = [ -# 'social_core.backends.google.GoogleOAuth2', -# 'django.contrib.auth.backends.ModelBackend', -# ] -# -# LOGIN_URL = 'login' -# LOGIN_REDIRECT_URL = 'home' -# LOGOUT_URL = 'logout' -# LOGOUT_REDIRECT_URL = 'login' -# SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '' -# SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '' +GOOGLE_CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID') +GOOGLE_CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET') -GOOGLE_CLIENT_ID = "182276566528-hlbjncs19fo502jposod6kft2p9k4grk.apps.googleusercontent.com" -GOOGLE_CLIENT_SECRET = "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" +EMAIL_BACKEND = os.getenv('EMAIL_BACKEND') +EMAIL_HOST = os.getenv('EMAIL_HOST') +EMAIL_PORT = os.getenv('EMAIL_PORT') +EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS') +EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER') # Replace with your Gmail email address +EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD') # Replace with your Gmail email password or App password +EMAIL_FROM_ADDRESS = os.getenv('EMAIL_FROM_ADDRESS') -# EMAIL_BACKEND = os.getenv('EMAIL_BACKEND') -# EMAIL_HOST = os.getenv('EMAIL_HOST') -# EMAIL_PORT = os.getenv('EMAIL_PORT') -# EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS') -# EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER') # Replace with your Gmail email address -# EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD') # Replace with your Gmail email password or App password -# EMAIL_FROM_ADDRESS = os.getenv('EMAIL_FROM_ADDRESS') +# EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend" +# EMAIL_HOST="smtp.sendgrid.net" +# EMAIL_PORT="587" +# EMAIL_USE_TLS="True" +# EMAIL_HOST_USER="apikey" # Replace with your Gmail email address +# EMAIL_HOST_PASSWORD="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" +# EMAIL_FROM_ADDRESS="zodbank@yopmail.com" -EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend" -EMAIL_HOST="smtp.sendgrid.net" -EMAIL_PORT="587" -EMAIL_USE_TLS="True" -EMAIL_HOST_USER="apikey" # Replace with your Gmail email address -EMAIL_HOST_PASSWORD="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" -EMAIL_FROM_ADDRESS="zodbank@yopmail.com" +ALIYUN_OSS_ACCESS_KEY_ID = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID') +ALIYUN_OSS_ACCESS_KEY_SECRET = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET') +ALIYUN_OSS_BUCKET_NAME = os.getenv('ALIYUN_OSS_BUCKET_NAME') +ALIYUN_OSS_ENDPOINT = os.getenv('ALIYUN_OSS_ENDPOINT') +ALIYUN_OSS_REGION = os.getenv('ALIYUN_OSS_REGION') -# ALIYUN_OSS_ACCESS_KEY_ID = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID') -# ALIYUN_OSS_ACCESS_KEY_SECRET = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET') -# ALIYUN_OSS_BUCKET_NAME = os.getenv('ALIYUN_OSS_BUCKET_NAME') -# ALIYUN_OSS_ENDPOINT = os.getenv('ALIYUN_OSS_ENDPOINT') -# ALIYUN_OSS_REGION = os.getenv('ALIYUN_OSS_REGION') - -ALIYUN_OSS_ACCESS_KEY_ID="LTAI5t7w1gq1CswJtvxtEZTd" -ALIYUN_OSS_ACCESS_KEY_SECRET="6yknAFpP2gVMhCWAJwbAjCEw2eehpf" -ALIYUN_OSS_BUCKET_NAME="zod-dev" -ALIYUN_OSS_ENDPOINT="oss-me-central-1.aliyuncs.com" -ALIYUN_OSS_REGION="Global" +# ALIYUN_OSS_ACCESS_KEY_ID="LTAI5t7w1gq1CswJtvxtEZTd" +# ALIYUN_OSS_ACCESS_KEY_SECRET="6yknAFpP2gVMhCWAJwbAjCEw2eehpf" +# ALIYUN_OSS_BUCKET_NAME="zod-dev" +# ALIYUN_OSS_ENDPOINT="oss-me-central-1.aliyuncs.com" +# ALIYUN_OSS_REGION="Global" STATIC_URL = 'static/' STATIC_ROOT = 'static' From 376bb89f20ea83ba40218b1adfcdb98e47e2c5db Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 3 Jul 2023 18:10:53 +0530 Subject: [PATCH 09/34] changes in setting --- zod_bank/settings.py | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/zod_bank/settings.py b/zod_bank/settings.py index 6930469..71b561c 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -176,21 +176,21 @@ https://docs.djangoproject.com/en/3.0/howto/static-files/""" GOOGLE_CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID') GOOGLE_CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET') -EMAIL_BACKEND = os.getenv('EMAIL_BACKEND') -EMAIL_HOST = os.getenv('EMAIL_HOST') -EMAIL_PORT = os.getenv('EMAIL_PORT') -EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS') -EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER') # Replace with your Gmail email address -EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD') # Replace with your Gmail email password or App password -EMAIL_FROM_ADDRESS = os.getenv('EMAIL_FROM_ADDRESS') +# EMAIL_BACKEND = os.getenv('EMAIL_BACKEND') +# EMAIL_HOST = os.getenv('EMAIL_HOST') +# EMAIL_PORT = os.getenv('EMAIL_PORT') +# EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS') +# EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER') # Replace with your Gmail email address +# EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD') # Replace with your Gmail email password or App password +# EMAIL_FROM_ADDRESS = os.getenv('EMAIL_FROM_ADDRESS') -# EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend" -# EMAIL_HOST="smtp.sendgrid.net" -# EMAIL_PORT="587" -# EMAIL_USE_TLS="True" -# EMAIL_HOST_USER="apikey" # Replace with your Gmail email address -# EMAIL_HOST_PASSWORD="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" -# EMAIL_FROM_ADDRESS="zodbank@yopmail.com" +EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend" +EMAIL_HOST="smtp.sendgrid.net" +EMAIL_PORT="587" +EMAIL_USE_TLS="True" +EMAIL_HOST_USER="apikey" # Replace with your Gmail email address +EMAIL_HOST_PASSWORD="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" +EMAIL_FROM_ADDRESS="zodbank@yopmail.com" ALIYUN_OSS_ACCESS_KEY_ID = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID') ALIYUN_OSS_ACCESS_KEY_SECRET = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET') @@ -198,11 +198,6 @@ ALIYUN_OSS_BUCKET_NAME = os.getenv('ALIYUN_OSS_BUCKET_NAME') ALIYUN_OSS_ENDPOINT = os.getenv('ALIYUN_OSS_ENDPOINT') ALIYUN_OSS_REGION = os.getenv('ALIYUN_OSS_REGION') -# ALIYUN_OSS_ACCESS_KEY_ID="LTAI5t7w1gq1CswJtvxtEZTd" -# ALIYUN_OSS_ACCESS_KEY_SECRET="6yknAFpP2gVMhCWAJwbAjCEw2eehpf" -# ALIYUN_OSS_BUCKET_NAME="zod-dev" -# ALIYUN_OSS_ENDPOINT="oss-me-central-1.aliyuncs.com" -# ALIYUN_OSS_REGION="Global" STATIC_URL = 'static/' STATIC_ROOT = 'static' From 5e5161a77ed00bae811778fea65a091a3dd28987 Mon Sep 17 00:00:00 2001 From: jain Date: Tue, 4 Jul 2023 18:26:00 +0530 Subject: [PATCH 10/34] list of the task and task table --- account/serializers.py | 19 +++--- base/constants.py | 10 +++ base/messages.py | 6 +- guardian/admin.py | 11 +++- guardian/migrations/0008_juniortask.py | 36 +++++++++++ guardian/models.py | 29 ++++++++- guardian/serializers.py | 84 +++++++++++++++++++++++++- guardian/urls.py | 6 +- guardian/views.py | 56 +++++++++++++---- junior/serializers.py | 58 ++++++++++++++++++ junior/urls.py | 4 +- junior/views.py | 15 ++++- zod_bank/settings.py | 1 + 13 files changed, 304 insertions(+), 31 deletions(-) create mode 100644 guardian/migrations/0008_juniortask.py diff --git a/account/serializers.py b/account/serializers.py index e2aee49..c328e37 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -126,12 +126,11 @@ class GuardianSerializer(serializers.ModelSerializer): first_name = serializers.SerializerMethodField('get_first_name') last_name = serializers.SerializerMethodField('get_last_name') auth_token = serializers.SerializerMethodField('get_auth_token') - refresh_token = serializers.SerializerMethodField('get_refresh_token') def get_auth_token(self, obj): - return get_token()['access'] - def get_refresh_token(self, obj): - return get_token()['refresh'] + refresh = RefreshToken.for_user(obj.user) + access_token = str(refresh.access_token) + return access_token def get_user_type(self, obj): @@ -153,7 +152,7 @@ class GuardianSerializer(serializers.ModelSerializer): class Meta(object): """Meta info""" model = Guardian - fields = ['auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name', 'gender', 'dob', + fields = ['id', 'auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name', 'gender', 'dob', 'referral_code', 'is_active', 'is_complete_profile', 'passcode', 'image', 'created_at', 'updated_at', 'user_type'] @@ -165,12 +164,12 @@ class JuniorSerializer(serializers.ModelSerializer): first_name = serializers.SerializerMethodField('get_first_name') last_name = serializers.SerializerMethodField('get_last_name') auth_token = serializers.SerializerMethodField('get_auth_token') - refresh_token = serializers.SerializerMethodField('get_refresh_token') def get_auth_token(self, obj): - return get_token()['access'] - def get_refresh_token(self, obj): - return get_token()['refresh'] + print("obj===>",obj,'===>',type(obj)) + refresh = RefreshToken.for_user(obj.auth) + access_token = str(refresh.access_token) + return access_token def get_user_type(self, obj): return JUNIOR @@ -187,7 +186,7 @@ class JuniorSerializer(serializers.ModelSerializer): class Meta(object): """Meta info""" model = Junior - fields = ['auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', + fields = ['id', 'auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', 'updated_at', 'user_type'] diff --git a/base/constants.py b/base/constants.py index b66ed5f..4e7bdcd 100644 --- a/base/constants.py +++ b/base/constants.py @@ -35,6 +35,16 @@ GENDERS = ( ('1', 'Male'), ('2', 'Female') ) +TASK_STATUS = ( + ('1', 'pending'), + ('2', 'in-progress'), + ('3', 'rejected'), + ('4', 'requested'), + ('5', 'completed') +) + +PENDING = 'pending' +TASK_POINTS = 5 # duplicate name used defined in constant PROJECT_NAME PROJECT_NAME = 'Zod Bank' GUARDIAN = 'guardian' diff --git a/base/messages.py b/base/messages.py index ffdc1c5..b6fd487 100644 --- a/base/messages.py +++ b/base/messages.py @@ -50,7 +50,8 @@ ERROR_CODE = { "2024": "Email not verified", "2025": "Invalid input. Expected a list of strings.", "2026": "New password should not same as old password", - "2027": "data should contain `identityToken`" + "2027": "data should contain `identityToken`", + "2028": "You are not authorized person to sign up on this platform" } SUCCESS_CODE = { # Success code for password @@ -79,7 +80,8 @@ SUCCESS_CODE = { "3014": "Password has been updated successfully.", "3015": "Verification code sent on your email.", "3016": "Send otp on your Email successfully", - "3017": "Profile image update successfully" + "3017": "Profile image update successfully", + "3018": "Created task successfully" } STATUS_CODE_ERROR = { diff --git a/guardian/admin.py b/guardian/admin.py index a3bd40c..6ece960 100644 --- a/guardian/admin.py +++ b/guardian/admin.py @@ -2,7 +2,7 @@ """Third party Django app""" from django.contrib import admin """Import Django app""" -from .models import Guardian +from .models import Guardian, JuniorTask # Register your models here. @admin.register(Guardian) class GuardianAdmin(admin.ModelAdmin): @@ -12,3 +12,12 @@ class GuardianAdmin(admin.ModelAdmin): def __str__(self): """Return email id""" return self.user__email + +@admin.register(JuniorTask) +class TaskAdmin(admin.ModelAdmin): + """Junior Admin""" + list_display = ['task_name', 'task_status'] + + def __str__(self): + """Return email id""" + return str(self.task_name) + str(self.points) diff --git a/guardian/migrations/0008_juniortask.py b/guardian/migrations/0008_juniortask.py new file mode 100644 index 0000000..44a10d7 --- /dev/null +++ b/guardian/migrations/0008_juniortask.py @@ -0,0 +1,36 @@ +# Generated by Django 4.2.2 on 2023-07-04 09:24 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('junior', '0006_alter_junior_country_name'), + ('guardian', '0007_alter_guardian_country_name'), + ] + + operations = [ + migrations.CreateModel( + name='JuniorTask', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('task_name', models.CharField(max_length=100)), + ('task_description', models.CharField(max_length=500)), + ('points', models.IntegerField(default=5)), + ('due_date', models.DateField(blank=True, null=True)), + ('image', models.ImageField(blank=True, default=None, null=True, upload_to='')), + ('task_status', models.CharField(blank=True, choices=[('1', 'pending'), ('2', 'in-progress'), ('3', 'rejected'), ('4', 'requested'), ('5', 'completed')], default='pending', max_length=15, null=True)), + ('is_active', models.BooleanField(default=True)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('guardian', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='guardian', to='guardian.guardian', verbose_name='Guardian')), + ('junior', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='junior', to='junior.junior', verbose_name='Junior')), + ], + options={ + 'verbose_name': 'Junior Task', + 'db_table': 'junior_task', + }, + ), + ] diff --git a/guardian/models.py b/guardian/models.py index 1ecf47f..501e080 100644 --- a/guardian/models.py +++ b/guardian/models.py @@ -3,7 +3,8 @@ from django.db import models from django.contrib.auth import get_user_model """Import Django app""" -from base.constants import GENDERS +from base.constants import GENDERS, TASK_STATUS, PENDING, TASK_POINTS +from junior.models import Junior User = get_user_model() # Create your models here. @@ -41,3 +42,29 @@ class Guardian(models.Model): def __str__(self): """Return email id""" return f'{self.user}' + +class JuniorTask(models.Model): + """Guardian model""" + guardian = models.ForeignKey(Guardian, on_delete=models.CASCADE, related_name='guardian', verbose_name='Guardian') + task_name = models.CharField(max_length=100) + task_description = models.CharField(max_length=500) + points = models.IntegerField(default=TASK_POINTS) + due_date = models.DateField(auto_now_add=False, null=True, blank=True) + image = models.ImageField(null=True, blank=True, default=None) + junior = models.ForeignKey(Junior, on_delete=models.CASCADE, related_name='junior', verbose_name='Junior') + task_status = models.CharField(choices=TASK_STATUS, max_length=15, null=True, blank=True, default=PENDING) + is_active = models.BooleanField(default=True) + """Profile created and updated time""" + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta(object): + """ Meta class """ + db_table = 'junior_task' + verbose_name = 'Junior Task' + + def __str__(self): + """Return email id""" + return f'{self.task_name}' + + diff --git a/guardian/serializers.py b/guardian/serializers.py index ba72ef5..d8429d1 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -6,9 +6,13 @@ from rest_framework import serializers from rest_framework_simplejwt.tokens import RefreshToken from django.db import transaction from django.contrib.auth.models import User +from django.core.validators import URLValidator +from django.core.exceptions import ValidationError """Import Django app""" -from .models import Guardian +from .models import Guardian, JuniorTask from account.models import UserProfile, UserEmailOtp +from account.serializers import JuniorSerializer +from junior.serializers import JuniorDetailSerializer from base.messages import ERROR_CODE, SUCCESS_CODE from .utils import upload_image_to_alibaba from junior.models import Junior @@ -134,3 +138,81 @@ class CreateGuardianSerializer(serializers.ModelSerializer): with transaction.atomic(): instance = super().save(**kwargs) return instance + + + + +class ImageField(serializers.Field): + def to_representation(self, value): + return value + def to_internal_value(self, data): + # If data is a valid URL, return it as is + if validators.URLValidator()(data): + return data + # else: + # raise serializers.ValidationError("Enter a valid URL.") + + + +class TaskSerializer(serializers.ModelSerializer): + class Meta(object): + model = JuniorTask + fields = ['task_name','task_description','points', 'due_date', 'junior', 'image'] + def validate_image(self, value): + if 'http' in str(value): + print("999999999999") + return value + else: + print("00000000000000000") + filename = f"images/{value.name}" + image_url = upload_image_to_alibaba(value, filename) + return image_url + def create(self, validated_data): + print("validated_data NOW===>", validated_data, '===>', type(validated_data)) + validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last() + # images = self.context['image'] + # if 'http' in str(images): + # print("999999999999") + # validated_data['image'] = images + # else: + # print("00000000000000000") + # filename = f"images/{images.name}" + # image_url = upload_image_to_alibaba(images, filename) + # validated_data['image'] = image_url + + print("validated_data NOW===>", validated_data, '===>', type(validated_data)) + instance = JuniorTask.objects.create(**validated_data) + return instance + +class GuardianDetailSerializer(serializers.ModelSerializer): + """junior serializer""" + + email = serializers.SerializerMethodField('get_auth') + first_name = serializers.SerializerMethodField('get_first_name') + last_name = serializers.SerializerMethodField('get_last_name') + + def get_auth(self, obj): + """user email address""" + return obj.user.username + + def get_first_name(self, obj): + """user first name""" + return obj.user.first_name + + def get_last_name(self, obj): + """user last name""" + return obj.user.last_name + class Meta(object): + """Meta info""" + model = Guardian + fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', + 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', + 'updated_at'] +class TaskDetailsSerializer(serializers.ModelSerializer): + + guardian = GuardianDetailSerializer() + junior = JuniorDetailSerializer() + class Meta(object): + model = JuniorTask + fields = ['id', 'guardian', 'task_name', 'task_description', 'points', 'due_date', 'image','junior', + 'task_status', 'is_active'] diff --git a/guardian/urls.py b/guardian/urls.py index 5399f2b..54470cb 100644 --- a/guardian/urls.py +++ b/guardian/urls.py @@ -1,7 +1,7 @@ """ Urls files""" """Django import""" from django.urls import path, include -from .views import SignupViewset, UpdateGuardianProfile +from .views import SignupViewset, UpdateGuardianProfile, TaskListAPIView, CreateTaskAPIView """Third party import""" from rest_framework import routers @@ -13,6 +13,10 @@ router = routers.SimpleRouter() router.register('sign-up', SignupViewset, basename='sign-up') """Create guardian profile API""" router.register('create-guardian-profile', UpdateGuardianProfile, basename='update-guardian-profile') +"""Create Task API""" +router.register('create-task', CreateTaskAPIView, basename='create-task') +"""Task list API""" +router.register('task-list', TaskListAPIView, basename='task-list') """Define Url pattern""" urlpatterns = [ path('api/v1/', include(router.urls)), diff --git a/guardian/views.py b/guardian/views.py index e8dda1e..f764d36 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -3,8 +3,8 @@ from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets, status """Import Django app""" -from .serializers import UserSerializer, CreateGuardianSerializer -from .models import Guardian +from .serializers import UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer +from .models import Guardian, JuniorTask from junior.models import Junior from account.models import UserEmailOtp from .tasks import generate_otp @@ -18,17 +18,20 @@ class SignupViewset(viewsets.ModelViewSet): def create(self, request, *args, **kwargs): """Create user profile""" - serializer = UserSerializer(context=request.data['user_type'], data=request.data) - if serializer.is_valid(): - serializer.save() - """Generate otp""" - otp = generate_otp() - UserEmailOtp.objects.create(email=request.data['email'], otp=otp, user_type=str(request.data['user_type'])) - """Send email to the register user""" - send_otp_email(request.data['email'], otp) - return custom_response(SUCCESS_CODE['3001'], {"email_otp": otp}, - response_status=status.HTTP_200_OK) - return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) + if request.data['user_type'] in ['1', '2']: + serializer = UserSerializer(context=request.data['user_type'], data=request.data) + if serializer.is_valid(): + serializer.save() + """Generate otp""" + otp = generate_otp() + UserEmailOtp.objects.create(email=request.data['email'], otp=otp, user_type=str(request.data['user_type'])) + """Send email to the register user""" + send_otp_email(request.data['email'], otp) + return custom_response(SUCCESS_CODE['3001'], {"email_otp": otp}, + response_status=status.HTTP_200_OK) + return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) + else: + return custom_error_response(ERROR_CODE['2028'], response_status=status.HTTP_400_BAD_REQUEST) class UpdateGuardianProfile(viewsets.ViewSet): """Update guardian profile""" @@ -46,3 +49,30 @@ class UpdateGuardianProfile(viewsets.ViewSet): serializer.save() return custom_response(None, serializer.data,response_status=status.HTTP_200_OK) return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) + + +class TaskListAPIView(viewsets.ModelViewSet): + """Update guardian profile""" + serializer_class = TaskDetailsSerializer + permission_classes = [IsAuthenticated] + + def list(self, request, *args, **kwargs): + """Create guardian profile""" + queryset = JuniorTask.objects.filter(guardian__user=request.user) + print("queryset==>",queryset) + serializer = TaskDetailsSerializer(queryset, many=True) + print("serializer.data===>",serializer.data) + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + +class CreateTaskAPIView(viewsets.ModelViewSet): + """create task for junior""" + serializer_class = TaskSerializer + + def create(self, request, *args, **kwargs): + print("request.data===>",request.data) + image = request.data['image'] + serializer = TaskSerializer(context={"user":request.user, "image":image}, data=request.data) + if serializer.is_valid(): + serializer.save() + return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK) + return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) diff --git a/junior/serializers.py b/junior/serializers.py index 34596e9..b8c6864 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -59,6 +59,11 @@ class CreateJuniorSerializer(serializers.ModelSerializer): def create(self, validated_data): """Create junior profile""" image = validated_data.get('image', None) + guardian_code = validated_data.get('guardian_code',None) + print("guardian_code===>",guardian_code,'==>',type(guardian_code)) + + + # phone_number = validated_data.get('phone', None) # guardian_data = Guardian.objects.filter(phone=phone_number) # junior_data = Junior.objects.filter(phone=phone_number) @@ -82,6 +87,9 @@ class CreateJuniorSerializer(serializers.ModelSerializer): junior.gender = validated_data.get('gender',junior.gender) """Update guardian code""" junior.guardian_code = validated_data.get('guardian_code', junior.guardian_code) + """condition for guardian code""" + if guardian_code: + junior.guardian_code = guardian_code junior.dob = validated_data.get('dob',junior.dob) junior.passcode = validated_data.get('passcode', junior.passcode) junior.country_name = validated_data.get('country_name', junior.country_name) @@ -107,3 +115,53 @@ class CreateJuniorSerializer(serializers.ModelSerializer): with transaction.atomic(): instance = super().save(**kwargs) return instance + +class JuniorDetailSerializer(serializers.ModelSerializer): + """junior serializer""" + email = serializers.SerializerMethodField('get_auth') + first_name = serializers.SerializerMethodField('get_first_name') + last_name = serializers.SerializerMethodField('get_last_name') + + def get_auth(self, obj): + """user email address""" + return obj.auth.username + + def get_first_name(self, obj): + """user first name""" + return obj.auth.first_name + + def get_last_name(self, obj): + """user last name""" + return obj.auth.last_name + + class Meta(object): + """Meta info""" + model = Junior + fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', + 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', + 'updated_at'] + +class JuniorDetailListSerializer(serializers.ModelSerializer): + """junior serializer""" + + email = serializers.SerializerMethodField('get_auth') + first_name = serializers.SerializerMethodField('get_first_name') + last_name = serializers.SerializerMethodField('get_last_name') + + + + def get_auth(self, obj): + return obj.auth.username + + def get_first_name(self, obj): + return obj.auth.first_name + + def get_last_name(self, obj): + return obj.auth.last_name + + class Meta(object): + """Meta info""" + model = Junior + fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', + 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', + 'updated_at'] \ No newline at end of file diff --git a/junior/urls.py b/junior/urls.py index 2b64fe4..5eb0f4c 100644 --- a/junior/urls.py +++ b/junior/urls.py @@ -1,7 +1,7 @@ """ Urls files""" """Django import""" from django.urls import path, include -from .views import UpdateJuniorProfile, ValidateGuardianCode +from .views import UpdateJuniorProfile, ValidateGuardianCode, JuniorListAPIView """Third party import""" from rest_framework import routers @@ -13,6 +13,8 @@ router = routers.SimpleRouter() router.register('create-junior-profile', UpdateJuniorProfile, basename='profile-update') """validate guardian code API""" router.register('validate-guardian-code', ValidateGuardianCode, basename='validate-guardian-code') +"""junior list API""" +router.register('junior-list', JuniorListAPIView, basename='junior-list') """Define url pattern""" urlpatterns = [ path('api/v1/', include(router.urls)), diff --git a/junior/views.py b/junior/views.py index 153bb41..fd2555e 100644 --- a/junior/views.py +++ b/junior/views.py @@ -3,7 +3,7 @@ from rest_framework import viewsets, status from rest_framework.permissions import IsAuthenticated """Django app import""" from junior.models import Junior -from .serializers import CreateJuniorSerializer +from .serializers import CreateJuniorSerializer, JuniorDetailListSerializer from guardian.models import Guardian from base.messages import ERROR_CODE, SUCCESS_CODE from account.utils import custom_response, custom_error_response @@ -38,3 +38,16 @@ class ValidateGuardianCode(viewsets.ViewSet): return custom_response(SUCCESS_CODE['3013'], response_status=status.HTTP_200_OK) else: return custom_error_response(ERROR_CODE["2022"], response_status=status.HTTP_400_BAD_REQUEST) + +class JuniorListAPIView(viewsets.ModelViewSet): + """Junior list of assosicated guardian""" + + serializer_class = JuniorDetailListSerializer + + def list(self, request, *args, **kwargs): + """ junior list""" + guardian_data = Guardian.objects.filter(user__email=request.user).last() + queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) + print("queryset===>",queryset) + serializer = JuniorDetailListSerializer(queryset, many=True) + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) diff --git a/zod_bank/settings.py b/zod_bank/settings.py index 71b561c..7a05e1a 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -201,3 +201,4 @@ ALIYUN_OSS_REGION = os.getenv('ALIYUN_OSS_REGION') STATIC_URL = 'static/' STATIC_ROOT = 'static' + From 0d143a39ac062327d990daa9b7b0d3641f5431ee Mon Sep 17 00:00:00 2001 From: jain Date: Tue, 4 Jul 2023 19:25:51 +0530 Subject: [PATCH 11/34] jira-20 create task --- .../migrations/0009_alter_juniortask_image.py | 18 +++++++++++ .../0010_alter_juniortask_task_status.py | 18 +++++++++++ guardian/models.py | 4 +-- guardian/serializers.py | 32 +++---------------- guardian/urls.py | 6 ++-- guardian/views.py | 27 ++++++++++++++-- 6 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 guardian/migrations/0009_alter_juniortask_image.py create mode 100644 guardian/migrations/0010_alter_juniortask_task_status.py diff --git a/guardian/migrations/0009_alter_juniortask_image.py b/guardian/migrations/0009_alter_juniortask_image.py new file mode 100644 index 0000000..496fee1 --- /dev/null +++ b/guardian/migrations/0009_alter_juniortask_image.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-07-04 12:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('guardian', '0008_juniortask'), + ] + + operations = [ + migrations.AlterField( + model_name='juniortask', + name='image', + field=models.URLField(blank=True, default=None, null=True), + ), + ] diff --git a/guardian/migrations/0010_alter_juniortask_task_status.py b/guardian/migrations/0010_alter_juniortask_task_status.py new file mode 100644 index 0000000..da84f2c --- /dev/null +++ b/guardian/migrations/0010_alter_juniortask_task_status.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-07-04 13:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('guardian', '0009_alter_juniortask_image'), + ] + + operations = [ + migrations.AlterField( + model_name='juniortask', + name='task_status', + field=models.CharField(choices=[('1', 'pending'), ('2', 'in-progress'), ('3', 'rejected'), ('4', 'requested'), ('5', 'completed')], default='pending', max_length=15), + ), + ] diff --git a/guardian/models.py b/guardian/models.py index 501e080..3752b02 100644 --- a/guardian/models.py +++ b/guardian/models.py @@ -50,9 +50,9 @@ class JuniorTask(models.Model): task_description = models.CharField(max_length=500) points = models.IntegerField(default=TASK_POINTS) due_date = models.DateField(auto_now_add=False, null=True, blank=True) - image = models.ImageField(null=True, blank=True, default=None) + image = models.URLField(null=True, blank=True, default=None) junior = models.ForeignKey(Junior, on_delete=models.CASCADE, related_name='junior', verbose_name='Junior') - task_status = models.CharField(choices=TASK_STATUS, max_length=15, null=True, blank=True, default=PENDING) + task_status = models.CharField(choices=TASK_STATUS, max_length=15, default=PENDING) is_active = models.BooleanField(default=True) """Profile created and updated time""" created_at = models.DateTimeField(auto_now_add=True) diff --git a/guardian/serializers.py b/guardian/serializers.py index d8429d1..be0272d 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -141,46 +141,22 @@ class CreateGuardianSerializer(serializers.ModelSerializer): - -class ImageField(serializers.Field): - def to_representation(self, value): - return value - def to_internal_value(self, data): - # If data is a valid URL, return it as is - if validators.URLValidator()(data): - return data - # else: - # raise serializers.ValidationError("Enter a valid URL.") - - - class TaskSerializer(serializers.ModelSerializer): class Meta(object): model = JuniorTask fields = ['task_name','task_description','points', 'due_date', 'junior', 'image'] - def validate_image(self, value): - if 'http' in str(value): - print("999999999999") - return value - else: - print("00000000000000000") - filename = f"images/{value.name}" - image_url = upload_image_to_alibaba(value, filename) - return image_url def create(self, validated_data): - print("validated_data NOW===>", validated_data, '===>', type(validated_data)) validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last() - # images = self.context['image'] + images = self.context['image'] + validated_data['image'] = images + print("images===>",images) + print("validated_data===>",validated_data) # if 'http' in str(images): - # print("999999999999") # validated_data['image'] = images # else: - # print("00000000000000000") # filename = f"images/{images.name}" # image_url = upload_image_to_alibaba(images, filename) # validated_data['image'] = image_url - - print("validated_data NOW===>", validated_data, '===>', type(validated_data)) instance = JuniorTask.objects.create(**validated_data) return instance diff --git a/guardian/urls.py b/guardian/urls.py index 54470cb..6acd172 100644 --- a/guardian/urls.py +++ b/guardian/urls.py @@ -1,7 +1,7 @@ """ Urls files""" """Django import""" from django.urls import path, include -from .views import SignupViewset, UpdateGuardianProfile, TaskListAPIView, CreateTaskAPIView +from .views import SignupViewset, UpdateGuardianProfile, AllTaskListAPIView, CreateTaskAPIView, TaskListAPIView """Third party import""" from rest_framework import routers @@ -15,7 +15,9 @@ router.register('sign-up', SignupViewset, basename='sign-up') router.register('create-guardian-profile', UpdateGuardianProfile, basename='update-guardian-profile') """Create Task API""" router.register('create-task', CreateTaskAPIView, basename='create-task') -"""Task list API""" +"""All Task list API""" +router.register('all-task-list', AllTaskListAPIView, basename='all-task-list') +"""Task list bases on the status API""" router.register('task-list', TaskListAPIView, basename='task-list') """Define Url pattern""" urlpatterns = [ diff --git a/guardian/views.py b/guardian/views.py index f764d36..22ffaa0 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -11,6 +11,7 @@ from .tasks import generate_otp from account.utils import send_otp_email from account.utils import custom_response, custom_error_response from base.messages import ERROR_CODE, SUCCESS_CODE +from .utils import upload_image_to_alibaba # Create your views here. class SignupViewset(viewsets.ModelViewSet): """Signup view set""" @@ -51,7 +52,7 @@ class UpdateGuardianProfile(viewsets.ViewSet): return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) -class TaskListAPIView(viewsets.ModelViewSet): +class AllTaskListAPIView(viewsets.ModelViewSet): """Update guardian profile""" serializer_class = TaskDetailsSerializer permission_classes = [IsAuthenticated] @@ -64,14 +65,34 @@ class TaskListAPIView(viewsets.ModelViewSet): print("serializer.data===>",serializer.data) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) +class TaskListAPIView(viewsets.ModelViewSet): + """Update guardian profile""" + serializer_class = TaskDetailsSerializer + permission_classes = [IsAuthenticated] + + def list(self, request, *args, **kwargs): + """Create guardian profile""" + print("request.GET.get(status)==>",self.request.GET.get('status')) + queryset = JuniorTask.objects.filter(guardian__user=request.user, task_status=self.request.GET.get('status')) + print("queryset==>",queryset) + serializer = TaskDetailsSerializer(queryset, many=True) + print("serializer.data===>",serializer.data) + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) class CreateTaskAPIView(viewsets.ModelViewSet): """create task for junior""" serializer_class = TaskSerializer def create(self, request, *args, **kwargs): - print("request.data===>",request.data) image = request.data['image'] - serializer = TaskSerializer(context={"user":request.user, "image":image}, data=request.data) + data = request.data + if 'https' in str(image): + image_data = image + else: + filename = f"images/{image}" + image_url = upload_image_to_alibaba(image, filename) + image_data = image_url + data.pop('image') + serializer = TaskSerializer(context={"user":request.user, "image":image_data}, data=data) if serializer.is_valid(): serializer.save() return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK) From a80f603614813007a362cee43ca08c6bf128658d Mon Sep 17 00:00:00 2001 From: jain Date: Thu, 6 Jul 2023 07:57:53 +0530 Subject: [PATCH 12/34] jira-15 top junior API and default images field --- base/constants.py | 6 +- guardian/admin.py | 2 +- ...t_image_juniortask_is_approved_and_more.py | 28 ++++++ guardian/models.py | 2 + guardian/serializers.py | 70 +++++++++++++++ guardian/urls.py | 7 +- guardian/utils.py | 1 + guardian/views.py | 90 ++++++++++++++++++- junior/serializers.py | 43 ++++++++- zod_bank/settings.py | 3 +- 10 files changed, 243 insertions(+), 9 deletions(-) create mode 100644 guardian/migrations/0011_juniortask_default_image_juniortask_is_approved_and_more.py diff --git a/base/constants.py b/base/constants.py index 4e7bdcd..f455316 100644 --- a/base/constants.py +++ b/base/constants.py @@ -43,7 +43,11 @@ TASK_STATUS = ( ('5', 'completed') ) -PENDING = 'pending' +PENDING = 1 +IN_PROGRESS = 2 +REJECTED = 3 +REQUESTED = 4 +COMPLETED = 5 TASK_POINTS = 5 # duplicate name used defined in constant PROJECT_NAME PROJECT_NAME = 'Zod Bank' diff --git a/guardian/admin.py b/guardian/admin.py index 6ece960..45f5c95 100644 --- a/guardian/admin.py +++ b/guardian/admin.py @@ -16,7 +16,7 @@ class GuardianAdmin(admin.ModelAdmin): @admin.register(JuniorTask) class TaskAdmin(admin.ModelAdmin): """Junior Admin""" - list_display = ['task_name', 'task_status'] + list_display = ['task_name', 'task_status', 'junior', 'points'] def __str__(self): """Return email id""" diff --git a/guardian/migrations/0011_juniortask_default_image_juniortask_is_approved_and_more.py b/guardian/migrations/0011_juniortask_default_image_juniortask_is_approved_and_more.py new file mode 100644 index 0000000..7d10d3c --- /dev/null +++ b/guardian/migrations/0011_juniortask_default_image_juniortask_is_approved_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.2 on 2023-07-05 11:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('guardian', '0010_alter_juniortask_task_status'), + ] + + operations = [ + migrations.AddField( + model_name='juniortask', + name='default_image', + field=models.ImageField(blank=True, default=None, null=True, upload_to=''), + ), + migrations.AddField( + model_name='juniortask', + name='is_approved', + field=models.BooleanField(default=False), + ), + migrations.AlterField( + model_name='juniortask', + name='task_status', + field=models.CharField(choices=[('1', 'pending'), ('2', 'in-progress'), ('3', 'rejected'), ('4', 'requested'), ('5', 'completed')], default=1, max_length=15), + ), + ] diff --git a/guardian/models.py b/guardian/models.py index 3752b02..c2ac60b 100644 --- a/guardian/models.py +++ b/guardian/models.py @@ -50,10 +50,12 @@ class JuniorTask(models.Model): task_description = models.CharField(max_length=500) points = models.IntegerField(default=TASK_POINTS) due_date = models.DateField(auto_now_add=False, null=True, blank=True) + default_image = models.ImageField(null=True, blank=True, default=None) image = models.URLField(null=True, blank=True, default=None) junior = models.ForeignKey(Junior, on_delete=models.CASCADE, related_name='junior', verbose_name='Junior') task_status = models.CharField(choices=TASK_STATUS, max_length=15, default=PENDING) is_active = models.BooleanField(default=True) + is_approved = models.BooleanField(default=False) """Profile created and updated time""" created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) diff --git a/guardian/serializers.py b/guardian/serializers.py index be0272d..fb091fc 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -129,7 +129,9 @@ class CreateGuardianSerializer(serializers.ModelSerializer): if image: filename = f"images/{image.name}" image_url = upload_image_to_alibaba(image, filename) + print("image_url=====>",image_url) guardian.image = image_url + print("guardian.image=====>", guardian.image) guardian.save() return guardian @@ -192,3 +194,71 @@ class TaskDetailsSerializer(serializers.ModelSerializer): model = JuniorTask fields = ['id', 'guardian', 'task_name', 'task_description', 'points', 'due_date', 'image','junior', 'task_status', 'is_active'] + + + + + +class TopJuniorSerializer(serializers.ModelSerializer): + total_points = serializers.SerializerMethodField() + + email = serializers.SerializerMethodField('get_auth') + first_name = serializers.SerializerMethodField('get_first_name') + last_name = serializers.SerializerMethodField('get_last_name') + + def get_auth(self, obj): + return obj.auth.username + + def get_first_name(self, obj): + return obj.auth.first_name + + def get_last_name(self, obj): + return obj.auth.last_name + + class Meta: + model = Junior + fields = ['id', 'email', 'first_name', 'last_name', 'phone', 'country_code', 'country_name', 'gender', 'dob', 'image', 'junior_code', 'guardian_code', 'referral_code', 'referral_code_used', 'is_active', 'is_complete_profile', 'passcode', 'is_verified', 'created_at', 'updated_at', 'total_points'] + + def get_total_points(self, obj): + junior_ids_with_total_points = self.context.get('junior_ids_with_total_points') + if junior_ids_with_total_points: + junior_id = obj.id + for item in junior_ids_with_total_points: + if item['junior'] == junior_id: + return item['total_points'] + return 0 + +# +# class TopJuniorSerializer(serializers.ModelSerializer): +# total_points = serializers.SerializerMethodField() +# +# email = serializers.SerializerMethodField('get_auth') +# first_name = serializers.SerializerMethodField('get_first_name') +# last_name = serializers.SerializerMethodField('get_last_name') +# +# +# def get_auth(self, obj): +# return obj.auth.username +# +# def get_first_name(self, obj): +# return obj.auth.first_name +# +# def get_last_name(self, obj): +# print("onbj==>",obj) +# return obj.auth.last_name +# +# class Meta: +# model = Junior +# fields = ['id', 'email', 'first_name', 'last_name', 'phone', 'country_code', 'country_name', 'gender', 'dob', 'image', 'junior_code', 'guardian_code', 'referral_code', 'referral_code_used', 'is_active', 'is_complete_profile', 'passcode', 'is_verified', 'created_at', 'updated_at', 'total_points'] +# +# def get_total_points(self, obj): +# total_highest_points = self.context.get('total_highest_points') +# if total_highest_points: +# print("total_highest_points==>",total_highest_points) +# junior_id = obj.id +# print("junior_id==>", junior_id) +# total_points = next((item['total_points'] for item in total_highest_points if item['junior'] == junior_id), 0) +# print("total_points==>", total_points) +# return total_points +# return 0 +# diff --git a/guardian/urls.py b/guardian/urls.py index 6acd172..b69b041 100644 --- a/guardian/urls.py +++ b/guardian/urls.py @@ -1,7 +1,8 @@ """ Urls files""" """Django import""" from django.urls import path, include -from .views import SignupViewset, UpdateGuardianProfile, AllTaskListAPIView, CreateTaskAPIView, TaskListAPIView +from .views import (SignupViewset, UpdateGuardianProfile, AllTaskListAPIView, CreateTaskAPIView, TaskListAPIView, + SearchTaskListAPIView, TopJuniorListAPIView) """Third party import""" from rest_framework import routers @@ -19,6 +20,10 @@ router.register('create-task', CreateTaskAPIView, basename='create-task') router.register('all-task-list', AllTaskListAPIView, basename='all-task-list') """Task list bases on the status API""" router.register('task-list', TaskListAPIView, basename='task-list') +"""Leaderboard API""" +router.register('top-junior', TopJuniorListAPIView, basename='top-junior') +"""Search Task list on the bases of status, due date, and task title API""" +router.register('filter-task', SearchTaskListAPIView, basename='filter-task') """Define Url pattern""" urlpatterns = [ path('api/v1/', include(router.urls)), diff --git a/guardian/utils.py b/guardian/utils.py index 0e46c6c..a4e1ffc 100644 --- a/guardian/utils.py +++ b/guardian/utils.py @@ -11,3 +11,4 @@ def upload_image_to_alibaba(image, filename): # Upload the temporary file to Alibaba OSS bucket.put_object_from_file(filename, temp_file.name) return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{filename}" + diff --git a/guardian/views.py b/guardian/views.py index 22ffaa0..9a4ab78 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -2,16 +2,20 @@ """Third party Django app""" from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets, status +from django.db.models import Max """Import Django app""" -from .serializers import UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer +from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer, + TopJuniorSerializer) from .models import Guardian, JuniorTask from junior.models import Junior +from junior.serializers import JuniorDetailSerializer from account.models import UserEmailOtp from .tasks import generate_otp from account.utils import send_otp_email from account.utils import custom_response, custom_error_response from base.messages import ERROR_CODE, SUCCESS_CODE from .utils import upload_image_to_alibaba +from django.db.models import Sum # Create your views here. class SignupViewset(viewsets.ModelViewSet): """Signup view set""" @@ -73,7 +77,12 @@ class TaskListAPIView(viewsets.ModelViewSet): def list(self, request, *args, **kwargs): """Create guardian profile""" print("request.GET.get(status)==>",self.request.GET.get('status')) - queryset = JuniorTask.objects.filter(guardian__user=request.user, task_status=self.request.GET.get('status')) + status_value = self.request.GET.get('status') + if status_value == 0: + queryset = JuniorTask.objects.filter(guardian__user=request.user) + else: + queryset = JuniorTask.objects.filter(guardian__user=request.user, + task_status=status_value) print("queryset==>",queryset) serializer = TaskDetailsSerializer(queryset, many=True) print("serializer.data===>",serializer.data) @@ -97,3 +106,80 @@ class CreateTaskAPIView(viewsets.ModelViewSet): serializer.save() return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK) return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) + + +class SearchTaskListAPIView(viewsets.ModelViewSet): + """Update guardian profile""" + serializer_class = TaskDetailsSerializer + permission_classes = [IsAuthenticated] + + def list(self, request, *args, **kwargs): + """Create guardian profile""" + due_date = self.request.GET.get('due_date') + print("request.GET.get(status)==>",self.request.GET.get('status')) + if self.request.GET.get('status'): + queryset = JuniorTask.objects.filter(guardian__user=request.user, + task_status=self.request.GET.get('status')).order_by('due_date') + if due_date: + queryset = JuniorTask.objects.filter(guardian__user=request.user, + due_date=due_date).order_by('due_date').order_by('created_at') + + print("queryset==>",queryset) + serializer = TaskDetailsSerializer(queryset, many=True) + # print("serializer.data===>",serializer.data) + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + + +class TopJuniorListAPIView(viewsets.ModelViewSet): + """Top juniors list""" + serializer_class = TopJuniorSerializer + # permission_classes = [IsAuthenticated] + + def list(self, request, *args, **kwargs): + """fetch junior list those complete their task""" + junior_ids_with_total_points = JuniorTask.objects.filter(task_status=1) \ + .values('junior') \ + .annotate(total_points=Sum('points')) \ + .order_by('-total_points') + + junior_ids = [item['junior'] for item in junior_ids_with_total_points] + + juniors = Junior.objects.filter(id__in=junior_ids) \ + .annotate(max_points=Max('junior_task__points')) \ + .order_by('-max_points', 'id') + + serializer = self.get_serializer(juniors, many=True, context={'junior_ids_with_total_points': junior_ids_with_total_points}) + return custom_response(serializer.data, response_status=status.HTTP_200_OK) + +# +# class TopJuniorListAPIView(viewsets.ModelViewSet): +# """Top juniors list""" +# serializer_class = TopJuniorSerializer +# # permission_classes = [IsAuthenticated] +# +# def list(self, request, *args, **kwargs): +# """fetch junior list those complete their task""" +# total_highest_points = list(JuniorTask.objects.filter(task_status=1) +# .values('junior') +# .annotate(total_points=Sum('points')) +# .order_by('-total_points')) +# print("total_highest_points===>",total_highest_points,'===>',type(total_highest_points)) +# junior_ids = [item['junior'] for item in total_highest_points] +# print("junior_ids====>", junior_ids) +# juniors = Junior.objects.filter(id__in=junior_ids) +# # a = [] +# # for i in junior_ids: +# # juniors = Junior.objects.filter(id=i) +# # a.append(juniors) +# print("juniors====>", juniors) +# # print('a===>',a,'==>',type(a)) +# serializer = self.get_serializer(juniors, context={'total_highest_points': total_highest_points}, many=True) +# print("serializer====>",type(serializer.data)) +# # Find the junior with the highest points +# # highest_points_junior = max(serializer.data, key=lambda x: x['total_points']) +# +# return custom_response(serializer.data, response_status=status.HTTP_200_OK) +# # serializer = self.get_serializer(total_highest_points, many=True) +# # return custom_response(None, serializer.data,response_status=status.HTTP_200_OK) + + diff --git a/junior/serializers.py b/junior/serializers.py index b8c6864..8e871cd 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -8,7 +8,8 @@ import random from junior.models import Junior from guardian.utils import upload_image_to_alibaba from base.messages import ERROR_CODE, SUCCESS_CODE -from guardian.models import Guardian +from guardian.models import Guardian, JuniorTask +from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED class ListCharField(serializers.ListField): """Serializer for Array field""" @@ -147,7 +148,13 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): email = serializers.SerializerMethodField('get_auth') first_name = serializers.SerializerMethodField('get_first_name') last_name = serializers.SerializerMethodField('get_last_name') - + assigned_task = serializers.SerializerMethodField('get_assigned_task') + points = serializers.SerializerMethodField('get_points') + in_progress_task = serializers.SerializerMethodField('get_in_progress_task') + completed_task = serializers.SerializerMethodField('get_completed_task') + requested_task = serializers.SerializerMethodField('get_requested_task') + rejected_task = serializers.SerializerMethodField('get_rejected_task') + pending_task = serializers.SerializerMethodField('get_pending_task') def get_auth(self, obj): @@ -159,9 +166,39 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): def get_last_name(self, obj): return obj.auth.last_name + def get_assigned_task(self, obj): + print("obj===>",obj,'type==>',type(obj)) + data = JuniorTask.objects.filter(junior=obj).count() + return data + + def get_points(self, obj): + data = sum(JuniorTask.objects.filter(junior=obj, task_status=COMPLETED).values_list('points', flat=True)) + return data + + def get_in_progress_task(self, obj): + data = JuniorTask.objects.filter(junior=obj, task_status=IN_PROGRESS).count() + return data + + def get_completed_task(self, obj): + data = JuniorTask.objects.filter(junior=obj, task_status=COMPLETED).count() + return data + + + def get_requested_task(self, obj): + data = JuniorTask.objects.filter(junior=obj, task_status=REQUESTED).count() + return data + + def get_rejected_task(self, obj): + data = JuniorTask.objects.filter(junior=obj, task_status=REJECTED).count() + return data + + def get_pending_task(self, obj): + data = JuniorTask.objects.filter(junior=obj, task_status=PENDING).count() + return data class Meta(object): """Meta info""" model = Junior fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', - 'updated_at'] \ No newline at end of file + 'updated_at', 'assigned_task','points', 'pending_task', 'in_progress_task', 'completed_task', + 'requested_task', 'rejected_task'] diff --git a/zod_bank/settings.py b/zod_bank/settings.py index 7a05e1a..60c3e70 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -190,7 +190,8 @@ EMAIL_PORT="587" EMAIL_USE_TLS="True" EMAIL_HOST_USER="apikey" # Replace with your Gmail email address EMAIL_HOST_PASSWORD="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" -EMAIL_FROM_ADDRESS="zodbank@yopmail.com" +EMAIL_FROM_ADDRESS="support@zodbank.com" +# EMAIL_FROM_ADDRESS="zodbank@yopmail.com" ALIYUN_OSS_ACCESS_KEY_ID = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID') ALIYUN_OSS_ACCESS_KEY_SECRET = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET') From cf966a52500a186385ff83b78177bdc01241adf8 Mon Sep 17 00:00:00 2001 From: jain Date: Thu, 6 Jul 2023 12:21:54 +0530 Subject: [PATCH 13/34] jira-15 dashboard API --- account/serializers.py | 2 +- base/messages.py | 2 +- guardian/admin.py | 2 +- .../0012_alter_juniortask_default_image.py | 18 +++++++ .../migrations/0013_alter_guardian_image.py | 18 +++++++ guardian/models.py | 4 +- guardian/serializers.py | 14 +++--- guardian/utils.py | 6 ++- guardian/views.py | 47 +++++++++---------- zod_bank/settings.py | 5 +- 10 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 guardian/migrations/0012_alter_juniortask_default_image.py create mode 100644 guardian/migrations/0013_alter_guardian_image.py diff --git a/account/serializers.py b/account/serializers.py index c328e37..70808f3 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -87,7 +87,7 @@ class ChangePasswordSerializer(serializers.Serializer): if self.context.password not in ('', None): if user.check_password(value): return value - raise serializers.ValidationError({"details":ERROR_CODE['2015']}) + raise serializers.ValidationError(ERROR_CODE['2015']) def create(self, validated_data): new_password = validated_data.pop('new_password') current_password = validated_data.pop('current_password') diff --git a/base/messages.py b/base/messages.py index b6fd487..dc6392a 100644 --- a/base/messages.py +++ b/base/messages.py @@ -81,7 +81,7 @@ SUCCESS_CODE = { "3015": "Verification code sent on your email.", "3016": "Send otp on your Email successfully", "3017": "Profile image update successfully", - "3018": "Created task successfully" + "3018": "Task created successfully" } STATUS_CODE_ERROR = { diff --git a/guardian/admin.py b/guardian/admin.py index 45f5c95..97edbdb 100644 --- a/guardian/admin.py +++ b/guardian/admin.py @@ -16,7 +16,7 @@ class GuardianAdmin(admin.ModelAdmin): @admin.register(JuniorTask) class TaskAdmin(admin.ModelAdmin): """Junior Admin""" - list_display = ['task_name', 'task_status', 'junior', 'points'] + list_display = ['id', 'task_name', 'task_status', 'junior', 'due_date', 'points', 'created_at', 'updated_at'] def __str__(self): """Return email id""" diff --git a/guardian/migrations/0012_alter_juniortask_default_image.py b/guardian/migrations/0012_alter_juniortask_default_image.py new file mode 100644 index 0000000..eefae1b --- /dev/null +++ b/guardian/migrations/0012_alter_juniortask_default_image.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-07-06 05:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('guardian', '0011_juniortask_default_image_juniortask_is_approved_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='juniortask', + name='default_image', + field=models.URLField(blank=True, default=None, null=True), + ), + ] diff --git a/guardian/migrations/0013_alter_guardian_image.py b/guardian/migrations/0013_alter_guardian_image.py new file mode 100644 index 0000000..0a16630 --- /dev/null +++ b/guardian/migrations/0013_alter_guardian_image.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-07-06 06:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('guardian', '0012_alter_juniortask_default_image'), + ] + + operations = [ + migrations.AlterField( + model_name='guardian', + name='image', + field=models.URLField(blank=True, default=None, null=True), + ), + ] diff --git a/guardian/models.py b/guardian/models.py index c2ac60b..00c5d42 100644 --- a/guardian/models.py +++ b/guardian/models.py @@ -16,7 +16,7 @@ class Guardian(models.Model): phone = models.CharField(max_length=31, null=True, blank=True, default=None) country_name = models.CharField(max_length=100, null=True, blank=True, default=None) """Image info""" - image = models.ImageField(null=True, blank=True, default=None) + image = models.URLField(null=True, blank=True, default=None) """Personal info""" family_name = models.CharField(max_length=50, null=True, blank=True, default=None) gender = models.CharField(choices=GENDERS, max_length=15, null=True, blank=True, default=None) @@ -50,7 +50,7 @@ class JuniorTask(models.Model): task_description = models.CharField(max_length=500) points = models.IntegerField(default=TASK_POINTS) due_date = models.DateField(auto_now_add=False, null=True, blank=True) - default_image = models.ImageField(null=True, blank=True, default=None) + default_image = models.URLField(null=True, blank=True, default=None) image = models.URLField(null=True, blank=True, default=None) junior = models.ForeignKey(Junior, on_delete=models.CASCADE, related_name='junior', verbose_name='Junior') task_status = models.CharField(choices=TASK_STATUS, max_length=15, default=PENDING) diff --git a/guardian/serializers.py b/guardian/serializers.py index fb091fc..a55c320 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -129,9 +129,9 @@ class CreateGuardianSerializer(serializers.ModelSerializer): if image: filename = f"images/{image.name}" image_url = upload_image_to_alibaba(image, filename) - print("image_url=====>",image_url) + print("image_url=====>",image_url,'===>',type(image_url)) guardian.image = image_url - print("guardian.image=====>", guardian.image) + print("guardian.image=====>", guardian.image,'===>',type(guardian.image)) guardian.save() return guardian @@ -146,13 +146,11 @@ class CreateGuardianSerializer(serializers.ModelSerializer): class TaskSerializer(serializers.ModelSerializer): class Meta(object): model = JuniorTask - fields = ['task_name','task_description','points', 'due_date', 'junior', 'image'] + fields = ['task_name','task_description','points', 'due_date', 'junior', 'default_image'] def create(self, validated_data): validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last() images = self.context['image'] - validated_data['image'] = images - print("images===>",images) - print("validated_data===>",validated_data) + validated_data['default_image'] = images # if 'http' in str(images): # validated_data['image'] = images # else: @@ -192,8 +190,8 @@ class TaskDetailsSerializer(serializers.ModelSerializer): junior = JuniorDetailSerializer() class Meta(object): model = JuniorTask - fields = ['id', 'guardian', 'task_name', 'task_description', 'points', 'due_date', 'image','junior', - 'task_status', 'is_active'] + fields = ['id', 'guardian', 'task_name', 'task_description', 'points', 'due_date','default_image', 'image', + 'junior', 'task_status', 'is_active', 'created_at','updated_at'] diff --git a/guardian/utils.py b/guardian/utils.py index a4e1ffc..46a8071 100644 --- a/guardian/utils.py +++ b/guardian/utils.py @@ -10,5 +10,9 @@ def upload_image_to_alibaba(image, filename): bucket = oss2.Bucket(auth, settings.ALIYUN_OSS_ENDPOINT, settings.ALIYUN_OSS_BUCKET_NAME) # Upload the temporary file to Alibaba OSS bucket.put_object_from_file(filename, temp_file.name) - return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{filename}" + print("filename====>",filename,'===>',type(filename)) + new_filename = filename.replace(' ', '%20') + print() + print("filename====>", filename, '===>', type(filename)) + return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}" diff --git a/guardian/views.py b/guardian/views.py index 9a4ab78..900734e 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -2,6 +2,7 @@ """Third party Django app""" from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets, status +from rest_framework.pagination import PageNumberPagination from django.db.models import Max """Import Django app""" from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer, @@ -64,35 +65,34 @@ class AllTaskListAPIView(viewsets.ModelViewSet): def list(self, request, *args, **kwargs): """Create guardian profile""" queryset = JuniorTask.objects.filter(guardian__user=request.user) - print("queryset==>",queryset) serializer = TaskDetailsSerializer(queryset, many=True) - print("serializer.data===>",serializer.data) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) class TaskListAPIView(viewsets.ModelViewSet): """Update guardian profile""" serializer_class = TaskDetailsSerializer permission_classes = [IsAuthenticated] + pagination_class = PageNumberPagination def list(self, request, *args, **kwargs): """Create guardian profile""" - print("request.GET.get(status)==>",self.request.GET.get('status')) status_value = self.request.GET.get('status') if status_value == 0: - queryset = JuniorTask.objects.filter(guardian__user=request.user) + queryset = JuniorTask.objects.filter(guardian__user=request.user).order_by('created_at') else: queryset = JuniorTask.objects.filter(guardian__user=request.user, - task_status=status_value) - print("queryset==>",queryset) - serializer = TaskDetailsSerializer(queryset, many=True) - print("serializer.data===>",serializer.data) + task_status=status_value).order_by('due_date','created_at') + paginator = self.pagination_class() + paginated_queryset = paginator.paginate_queryset(queryset, request) + serializer = TaskDetailsSerializer(paginated_queryset, many=True) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + class CreateTaskAPIView(viewsets.ModelViewSet): """create task for junior""" serializer_class = TaskSerializer def create(self, request, *args, **kwargs): - image = request.data['image'] + image = request.data['default_image'] data = request.data if 'https' in str(image): image_data = image @@ -100,7 +100,7 @@ class CreateTaskAPIView(viewsets.ModelViewSet): filename = f"images/{image}" image_url = upload_image_to_alibaba(image, filename) image_data = image_url - data.pop('image') + data.pop('default_image') serializer = TaskSerializer(context={"user":request.user, "image":image_data}, data=data) if serializer.is_valid(): serializer.save() @@ -112,21 +112,17 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): """Update guardian profile""" serializer_class = TaskDetailsSerializer permission_classes = [IsAuthenticated] + pagination_class = PageNumberPagination def list(self, request, *args, **kwargs): """Create guardian profile""" - due_date = self.request.GET.get('due_date') - print("request.GET.get(status)==>",self.request.GET.get('status')) - if self.request.GET.get('status'): - queryset = JuniorTask.objects.filter(guardian__user=request.user, - task_status=self.request.GET.get('status')).order_by('due_date') - if due_date: - queryset = JuniorTask.objects.filter(guardian__user=request.user, - due_date=due_date).order_by('due_date').order_by('created_at') - - print("queryset==>",queryset) - serializer = TaskDetailsSerializer(queryset, many=True) - # print("serializer.data===>",serializer.data) + title = self.request.GET.get('title') + if title: + queryset = JuniorTask.objects.filter(guardian__user=request.user, task_name__icontains=title)\ + .order_by('due_date','created_at') + paginator = self.pagination_class() + paginated_queryset = paginator.paginate_queryset(queryset, request) + serializer = TaskDetailsSerializer(paginated_queryset, many=True) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) @@ -144,11 +140,10 @@ class TopJuniorListAPIView(viewsets.ModelViewSet): junior_ids = [item['junior'] for item in junior_ids_with_total_points] - juniors = Junior.objects.filter(id__in=junior_ids) \ - .annotate(max_points=Max('junior_task__points')) \ - .order_by('-max_points', 'id') + juniors = Junior.objects.filter(id__in=junior_ids) - serializer = self.get_serializer(juniors, many=True, context={'junior_ids_with_total_points': junior_ids_with_total_points}) + serializer = self.get_serializer(juniors, many=True, context={'junior_ids_with_total_points': + junior_ids_with_total_points}) return custom_response(serializer.data, response_status=status.HTTP_200_OK) # diff --git a/zod_bank/settings.py b/zod_bank/settings.py index 60c3e70..3a897cb 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -92,8 +92,9 @@ REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ # 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', - 'rest_framework_simplejwt.authentication.JWTAuthentication', -] + 'rest_framework_simplejwt.authentication.JWTAuthentication',], + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'PAGE_SIZE': 5, # Set the default pagination size } SIMPLE_JWT = { 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=50), From 2edd8c4e0065e0ce20226774fa73c0f06fd51772 Mon Sep 17 00:00:00 2001 From: jain Date: Thu, 6 Jul 2023 15:52:30 +0530 Subject: [PATCH 14/34] jira-15 and email otp validation --- account/serializers.py | 10 ++++++++-- account/urls.py | 4 ++-- account/views.py | 26 ++++++++++++++++++++++++++ base/messages.py | 3 ++- guardian/views.py | 6 +++++- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index 70808f3..9380ba8 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -135,7 +135,10 @@ class GuardianSerializer(serializers.ModelSerializer): def get_user_type(self, obj): """user type""" - return GUARDIAN + email_verified = UserEmailOtp.objects.filter(email=obj.user.username).last() + if email_verified and email_verified.user_type != None: + return email_verified.user_type + return '2' def get_auth(self, obj): """user email address""" @@ -172,7 +175,10 @@ class JuniorSerializer(serializers.ModelSerializer): return access_token def get_user_type(self, obj): - return JUNIOR + email_verified = UserEmailOtp.objects.filter(email=obj.auth.username).last() + if email_verified and email_verified.user_type != None: + return email_verified.user_type + return '1' def get_auth(self, obj): return obj.auth.username diff --git a/account/urls.py b/account/urls.py index e0e1c6c..74098c2 100644 --- a/account/urls.py +++ b/account/urls.py @@ -14,8 +14,8 @@ router = routers.SimpleRouter() router.register('user', UserLogin, basename='user') router.register('admin', UserLogin, basename='admin') router.register('google-login', GoogleLoginViewSet, basename='admin') -router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp') -router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification') +# router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp') +# router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification') router.register('user-email-verification', UserEmailVerification, basename='user-email-verification') router.register('resend-email-otp', ReSendEmailOtp, basename='resend-email-otp') urlpatterns = [ diff --git a/account/views.py b/account/views.py index f992a05..3c006d7 100644 --- a/account/views.py +++ b/account/views.py @@ -1,3 +1,5 @@ +from datetime import datetime, timedelta + from rest_framework import viewsets, status, views from rest_framework.decorators import action import random @@ -182,9 +184,16 @@ class ForgotPasswordAPIView(views.APIView): 'verification_code': verification_code } ) + + expiry = datetime.today() + timedelta(days=1) + print("expiry===>", expiry, '===>', type(expiry)) user_data, created = UserEmailOtp.objects.get_or_create(email=email) + if created: + user_data.expired_at = expiry + user_data.save() if user_data: user_data.otp = verification_code + user_data.expired_at = expiry user_data.save() return custom_response(SUCCESS_CODE['3015'], response_status=status.HTTP_200_OK) @@ -246,6 +255,7 @@ class UserLogin(viewsets.ViewSet): email_verified = UserEmailOtp.objects.filter(email=username).last() refresh = RefreshToken.for_user(user) access_token = str(refresh.access_token) + print("email_verified.user_type==>",email_verified.user_type) data = {"auth_token":access_token, "is_profile_complete": False, "user_type": email_verified.user_type, } @@ -292,6 +302,15 @@ class UserEmailVerification(viewsets.ModelViewSet): email_data = UserEmailOtp.objects.filter(email=self.request.GET.get('email'), otp=self.request.GET.get('otp')).last() if email_data: + input_datetime_str = str(email_data.expired_at) + input_format = "%Y-%m-%d %H:%M:%S.%f%z" + output_format = "%Y-%m-%d %H:%M:%S.%f" + input_datetime = datetime.strptime(input_datetime_str, input_format) + output_datetime_str = input_datetime.strftime(output_format) + format_str = "%Y-%m-%d %H:%M:%S.%f" + datetime_obj = datetime.strptime(output_datetime_str, format_str) + if datetime.today() > datetime_obj: + return custom_error_response(ERROR_CODE["2029"], response_status=status.HTTP_400_BAD_REQUEST) email_data.is_verified = True email_data.save() if email_data.user_type == '1': @@ -315,12 +334,19 @@ class UserEmailVerification(viewsets.ModelViewSet): class ReSendEmailOtp(viewsets.ModelViewSet): """Send otp on phone""" + serializer_class = EmailVerificationSerializer def create(self, request, *args, **kwargs): otp = generate_otp() if User.objects.filter(email=request.data['email']): + expiry = datetime.today() + timedelta(days=1) + print("expiry===>", expiry, '===>', type(expiry)) email_data, created = UserEmailOtp.objects.get_or_create(email=request.data['email']) + if created: + email_data.expired_at = expiry + email_data.save() if email_data: email_data.otp = otp + email_data.expired_at = expiry email_data.save() send_otp_email(request.data['email'], otp) return custom_response(SUCCESS_CODE['3016'], response_status=status.HTTP_200_OK) diff --git a/base/messages.py b/base/messages.py index dc6392a..f024a3d 100644 --- a/base/messages.py +++ b/base/messages.py @@ -51,7 +51,8 @@ ERROR_CODE = { "2025": "Invalid input. Expected a list of strings.", "2026": "New password should not same as old password", "2027": "data should contain `identityToken`", - "2028": "You are not authorized person to sign up on this platform" + "2028": "You are not authorized person to sign up on this platform", + "2029": "Validity of otp verification is expired" } SUCCESS_CODE = { # Success code for password diff --git a/guardian/views.py b/guardian/views.py index 900734e..6b7bf9b 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -4,6 +4,7 @@ from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets, status from rest_framework.pagination import PageNumberPagination from django.db.models import Max +from datetime import datetime, timedelta """Import Django app""" from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer, TopJuniorSerializer) @@ -30,7 +31,10 @@ class SignupViewset(viewsets.ModelViewSet): serializer.save() """Generate otp""" otp = generate_otp() - UserEmailOtp.objects.create(email=request.data['email'], otp=otp, user_type=str(request.data['user_type'])) + expiry = datetime.today() + timedelta(days=1) + print("expiry===>", expiry, '===>', type(expiry)) + UserEmailOtp.objects.create(email=request.data['email'], otp=otp, + user_type=str(request.data['user_type']), expired_at=expiry) """Send email to the register user""" send_otp_email(request.data['email'], otp) return custom_response(SUCCESS_CODE['3001'], {"email_otp": otp}, From 3b642fcc5f7717f02a014b6114b18ae3cada9c0c Mon Sep 17 00:00:00 2001 From: jain Date: Thu, 6 Jul 2023 16:08:43 +0530 Subject: [PATCH 15/34] changes in requirement file --- account/serializers.py | 1 - account/views.py | 3 --- guardian/serializers.py | 38 -------------------------------------- guardian/views.py | 33 --------------------------------- junior/serializers.py | 10 ---------- junior/views.py | 1 - requirements.txt | 5 +++++ 7 files changed, 5 insertions(+), 86 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index 9380ba8..5d688b4 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -169,7 +169,6 @@ class JuniorSerializer(serializers.ModelSerializer): auth_token = serializers.SerializerMethodField('get_auth_token') def get_auth_token(self, obj): - print("obj===>",obj,'===>',type(obj)) refresh = RefreshToken.for_user(obj.auth) access_token = str(refresh.access_token) return access_token diff --git a/account/views.py b/account/views.py index 3c006d7..fd0cef2 100644 --- a/account/views.py +++ b/account/views.py @@ -186,7 +186,6 @@ class ForgotPasswordAPIView(views.APIView): ) expiry = datetime.today() + timedelta(days=1) - print("expiry===>", expiry, '===>', type(expiry)) user_data, created = UserEmailOtp.objects.get_or_create(email=email) if created: user_data.expired_at = expiry @@ -255,7 +254,6 @@ class UserLogin(viewsets.ViewSet): email_verified = UserEmailOtp.objects.filter(email=username).last() refresh = RefreshToken.for_user(user) access_token = str(refresh.access_token) - print("email_verified.user_type==>",email_verified.user_type) data = {"auth_token":access_token, "is_profile_complete": False, "user_type": email_verified.user_type, } @@ -339,7 +337,6 @@ class ReSendEmailOtp(viewsets.ModelViewSet): otp = generate_otp() if User.objects.filter(email=request.data['email']): expiry = datetime.today() + timedelta(days=1) - print("expiry===>", expiry, '===>', type(expiry)) email_data, created = UserEmailOtp.objects.get_or_create(email=request.data['email']) if created: email_data.expired_at = expiry diff --git a/guardian/serializers.py b/guardian/serializers.py index a55c320..f85affc 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -6,8 +6,6 @@ from rest_framework import serializers from rest_framework_simplejwt.tokens import RefreshToken from django.db import transaction from django.contrib.auth.models import User -from django.core.validators import URLValidator -from django.core.exceptions import ValidationError """Import Django app""" from .models import Guardian, JuniorTask from account.models import UserProfile, UserEmailOtp @@ -129,9 +127,7 @@ class CreateGuardianSerializer(serializers.ModelSerializer): if image: filename = f"images/{image.name}" image_url = upload_image_to_alibaba(image, filename) - print("image_url=====>",image_url,'===>',type(image_url)) guardian.image = image_url - print("guardian.image=====>", guardian.image,'===>',type(guardian.image)) guardian.save() return guardian @@ -226,37 +222,3 @@ class TopJuniorSerializer(serializers.ModelSerializer): return item['total_points'] return 0 -# -# class TopJuniorSerializer(serializers.ModelSerializer): -# total_points = serializers.SerializerMethodField() -# -# email = serializers.SerializerMethodField('get_auth') -# first_name = serializers.SerializerMethodField('get_first_name') -# last_name = serializers.SerializerMethodField('get_last_name') -# -# -# def get_auth(self, obj): -# return obj.auth.username -# -# def get_first_name(self, obj): -# return obj.auth.first_name -# -# def get_last_name(self, obj): -# print("onbj==>",obj) -# return obj.auth.last_name -# -# class Meta: -# model = Junior -# fields = ['id', 'email', 'first_name', 'last_name', 'phone', 'country_code', 'country_name', 'gender', 'dob', 'image', 'junior_code', 'guardian_code', 'referral_code', 'referral_code_used', 'is_active', 'is_complete_profile', 'passcode', 'is_verified', 'created_at', 'updated_at', 'total_points'] -# -# def get_total_points(self, obj): -# total_highest_points = self.context.get('total_highest_points') -# if total_highest_points: -# print("total_highest_points==>",total_highest_points) -# junior_id = obj.id -# print("junior_id==>", junior_id) -# total_points = next((item['total_points'] for item in total_highest_points if item['junior'] == junior_id), 0) -# print("total_points==>", total_points) -# return total_points -# return 0 -# diff --git a/guardian/views.py b/guardian/views.py index 6b7bf9b..5418565 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -32,7 +32,6 @@ class SignupViewset(viewsets.ModelViewSet): """Generate otp""" otp = generate_otp() expiry = datetime.today() + timedelta(days=1) - print("expiry===>", expiry, '===>', type(expiry)) UserEmailOtp.objects.create(email=request.data['email'], otp=otp, user_type=str(request.data['user_type']), expired_at=expiry) """Send email to the register user""" @@ -150,35 +149,3 @@ class TopJuniorListAPIView(viewsets.ModelViewSet): junior_ids_with_total_points}) return custom_response(serializer.data, response_status=status.HTTP_200_OK) -# -# class TopJuniorListAPIView(viewsets.ModelViewSet): -# """Top juniors list""" -# serializer_class = TopJuniorSerializer -# # permission_classes = [IsAuthenticated] -# -# def list(self, request, *args, **kwargs): -# """fetch junior list those complete their task""" -# total_highest_points = list(JuniorTask.objects.filter(task_status=1) -# .values('junior') -# .annotate(total_points=Sum('points')) -# .order_by('-total_points')) -# print("total_highest_points===>",total_highest_points,'===>',type(total_highest_points)) -# junior_ids = [item['junior'] for item in total_highest_points] -# print("junior_ids====>", junior_ids) -# juniors = Junior.objects.filter(id__in=junior_ids) -# # a = [] -# # for i in junior_ids: -# # juniors = Junior.objects.filter(id=i) -# # a.append(juniors) -# print("juniors====>", juniors) -# # print('a===>',a,'==>',type(a)) -# serializer = self.get_serializer(juniors, context={'total_highest_points': total_highest_points}, many=True) -# print("serializer====>",type(serializer.data)) -# # Find the junior with the highest points -# # highest_points_junior = max(serializer.data, key=lambda x: x['total_points']) -# -# return custom_response(serializer.data, response_status=status.HTTP_200_OK) -# # serializer = self.get_serializer(total_highest_points, many=True) -# # return custom_response(None, serializer.data,response_status=status.HTTP_200_OK) - - diff --git a/junior/serializers.py b/junior/serializers.py index 8e871cd..5802a9c 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -61,15 +61,6 @@ class CreateJuniorSerializer(serializers.ModelSerializer): """Create junior profile""" image = validated_data.get('image', None) guardian_code = validated_data.get('guardian_code',None) - print("guardian_code===>",guardian_code,'==>',type(guardian_code)) - - - - # phone_number = validated_data.get('phone', None) - # guardian_data = Guardian.objects.filter(phone=phone_number) - # junior_data = Junior.objects.filter(phone=phone_number) - # if phone_number and (junior_data or guardian_data): - # raise serializers.ValidationError({"details":ERROR_CODE['2012']}) user = User.objects.filter(username=self.context['user']).last() if user: """Save first and last name of junior""" @@ -167,7 +158,6 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): return obj.auth.last_name def get_assigned_task(self, obj): - print("obj===>",obj,'type==>',type(obj)) data = JuniorTask.objects.filter(junior=obj).count() return data diff --git a/junior/views.py b/junior/views.py index fd2555e..89c177f 100644 --- a/junior/views.py +++ b/junior/views.py @@ -48,6 +48,5 @@ class JuniorListAPIView(viewsets.ModelViewSet): """ junior list""" guardian_data = Guardian.objects.filter(user__email=request.user).last() queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) - print("queryset===>",queryset) serializer = JuniorDetailListSerializer(queryset, many=True) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) diff --git a/requirements.txt b/requirements.txt index 1103c6a..5dd7f76 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,6 +19,8 @@ click==8.1.3 click-didyoumean==0.3.0 click-plugins==1.1.1 click-repl==0.3.0 +coreapi==2.3.3 +coreschema==0.0.4 crcmod==1.7 cron-descriptor==1.4.0 cryptography==41.0.1 @@ -43,8 +45,11 @@ google-auth==2.21.0 gunicorn==20.1.0 idna==3.4 inflection==0.5.1 +itypes==1.2.0 +Jinja2==3.1.2 jmespath==0.10.0 kombu==5.3.1 +MarkupSafe==2.1.3 msgpack==1.0.5 oss2==2.18.0 packaging==23.1 From c7a5bf68a0853ff654f08de0f14778a7857dd9dc Mon Sep 17 00:00:00 2001 From: jain Date: Thu, 6 Jul 2023 16:17:56 +0530 Subject: [PATCH 16/34] jira-15 remove commented code --- guardian/serializers.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/guardian/serializers.py b/guardian/serializers.py index f85affc..0241874 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -147,12 +147,6 @@ class TaskSerializer(serializers.ModelSerializer): validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last() images = self.context['image'] validated_data['default_image'] = images - # if 'http' in str(images): - # validated_data['image'] = images - # else: - # filename = f"images/{images.name}" - # image_url = upload_image_to_alibaba(images, filename) - # validated_data['image'] = image_url instance = JuniorTask.objects.create(**validated_data) return instance From 34034dcdb84ded897b42aadbfeecdf4cf11241c0 Mon Sep 17 00:00:00 2001 From: jain Date: Thu, 6 Jul 2023 16:20:07 +0530 Subject: [PATCH 17/34] jira-15 remove commented code --- guardian/utils.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/guardian/utils.py b/guardian/utils.py index 46a8071..66a36ca 100644 --- a/guardian/utils.py +++ b/guardian/utils.py @@ -10,9 +10,6 @@ def upload_image_to_alibaba(image, filename): bucket = oss2.Bucket(auth, settings.ALIYUN_OSS_ENDPOINT, settings.ALIYUN_OSS_BUCKET_NAME) # Upload the temporary file to Alibaba OSS bucket.put_object_from_file(filename, temp_file.name) - print("filename====>",filename,'===>',type(filename)) new_filename = filename.replace(' ', '%20') - print() - print("filename====>", filename, '===>', type(filename)) return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}" From 2b783512a5e4e19e4dd15ebb8e88e48e70597342 Mon Sep 17 00:00:00 2001 From: jain Date: Thu, 6 Jul 2023 19:53:20 +0530 Subject: [PATCH 18/34] update profile --- account/serializers.py | 32 +++++++++-------- account/views.py | 24 +++++++++---- guardian/views.py | 37 +++++++++++++++----- junior/migrations/0007_alter_junior_image.py | 18 ++++++++++ junior/models.py | 2 +- 5 files changed, 83 insertions(+), 30 deletions(-) create mode 100644 junior/migrations/0007_alter_junior_image.py diff --git a/account/serializers.py b/account/serializers.py index 5d688b4..2074c25 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -23,12 +23,28 @@ class GoogleLoginSerializer(serializers.Serializer): """meta class""" fields = ('access_token',) + +class UpdateJuniorProfileImageSerializer(serializers.ModelSerializer): + """Reset Password after verification""" + class Meta(object): + """Meta info""" + model = Junior + fields = ['image'] + + def update(self, instance, validated_data): + """update image """ + junior_image = validated_data.get('image', instance.image) + instance.image = junior_image + instance.save() + return instance + + class UpdateGuardianImageSerializer(serializers.ModelSerializer): """Reset Password after verification""" class Meta(object): """Meta info""" model = Guardian - fields = '__all__' + fields = ['image'] def update(self, instance, validated_data): """update image """ @@ -36,21 +52,7 @@ class UpdateGuardianImageSerializer(serializers.ModelSerializer): instance.save() return instance -class UpdateJuniorProfileImageSerializer(serializers.ModelSerializer): - """Reset Password after verification""" - class Meta(object): - """Meta info""" - model = Junior - fields = '__all__' - def update(self, instance, validated_data): - """update image """ - image = validated_data.get('image', instance.image) - filename = f"images/{image.name}" - image_url = upload_image_to_alibaba(image, filename) - instance.image = image_url - instance.save() - return instance class ResetPasswordSerializer(serializers.Serializer): """Reset Password after verification""" verification_code = serializers.CharField(max_length=10) diff --git a/account/views.py b/account/views.py index fd0cef2..33ebbbc 100644 --- a/account/views.py +++ b/account/views.py @@ -4,7 +4,9 @@ from rest_framework import viewsets, status, views from rest_framework.decorators import action import random import logging +from django.utils import timezone import jwt +from guardian.utils import upload_image_to_alibaba from django.contrib.auth import authenticate, login from guardian.models import Guardian from junior.models import Junior @@ -135,12 +137,22 @@ class SigninWithApple(views.APIView): class UpdateProfileImage(views.APIView): permission_classes = [IsAuthenticated] def put(self, request, format=None): - if request.data['user_type'] == '1': + if str(request.data['user_type']) == '1': junior_query = Junior.objects.filter(auth=request.user).last() - serializer = UpdateJuniorProfileImageSerializer(junior_query, data=request.data, partial=True) - else: + image = request.data['image'] + filename = f"images/{image.name}" + image_url = upload_image_to_alibaba(image, filename) + image_data = image_url + serializer = UpdateJuniorProfileImageSerializer(junior_query, + data={'image':image_data}, partial=True) + if str(request.data['user_type']) == '2': guardian_query = Guardian.objects.filter(user=request.user).last() - serializer = UpdateGuardianImageSerializer(guardian_query, data=request.data, partial=True) + image = request.data['image'] + filename = f"images/{image.name}" + image_url = upload_image_to_alibaba(image, filename) + image_data = image_url + serializer = UpdateGuardianImageSerializer(guardian_query, + data={'image':image_data}, partial=True) if serializer.is_valid(): serializer.save() return custom_response(SUCCESS_CODE['3017'], serializer.data, response_status=status.HTTP_200_OK) @@ -185,7 +197,7 @@ class ForgotPasswordAPIView(views.APIView): } ) - expiry = datetime.today() + timedelta(days=1) + expiry = timezone.now() + timezone.timedelta(days=1) user_data, created = UserEmailOtp.objects.get_or_create(email=email) if created: user_data.expired_at = expiry @@ -336,7 +348,7 @@ class ReSendEmailOtp(viewsets.ModelViewSet): def create(self, request, *args, **kwargs): otp = generate_otp() if User.objects.filter(email=request.data['email']): - expiry = datetime.today() + timedelta(days=1) + expiry = timezone.now() + timezone.timedelta(days=1) email_data, created = UserEmailOtp.objects.get_or_create(email=request.data['email']) if created: email_data.expired_at = expiry diff --git a/guardian/views.py b/guardian/views.py index 5418565..3238cdd 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -3,7 +3,8 @@ from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets, status from rest_framework.pagination import PageNumberPagination -from django.db.models import Max +from django.contrib.auth.models import User +from django.utils import timezone from datetime import datetime, timedelta """Import Django app""" from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer, @@ -22,7 +23,6 @@ from django.db.models import Sum class SignupViewset(viewsets.ModelViewSet): """Signup view set""" serializer_class = UserSerializer - def create(self, request, *args, **kwargs): """Create user profile""" if request.data['user_type'] in ['1', '2']: @@ -31,7 +31,7 @@ class SignupViewset(viewsets.ModelViewSet): serializer.save() """Generate otp""" otp = generate_otp() - expiry = datetime.today() + timedelta(days=1) + expiry = timezone.now() + timezone.timedelta(days=1) UserEmailOtp.objects.create(email=request.data['email'], otp=otp, user_type=str(request.data['user_type']), expired_at=expiry) """Send email to the register user""" @@ -110,24 +110,45 @@ class CreateTaskAPIView(viewsets.ModelViewSet): return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK) return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) - class SearchTaskListAPIView(viewsets.ModelViewSet): """Update guardian profile""" serializer_class = TaskDetailsSerializer permission_classes = [IsAuthenticated] pagination_class = PageNumberPagination + def get_queryset(self): + """Get the queryset for the view""" + title = self.request.GET.get('title') + junior_queryset = JuniorTask.objects.filter(guardian__user=self.request.user, task_name__icontains=title)\ + .order_by('due_date', 'created_at') + return junior_queryset + def list(self, request, *args, **kwargs): """Create guardian profile""" - title = self.request.GET.get('title') - if title: - queryset = JuniorTask.objects.filter(guardian__user=request.user, task_name__icontains=title)\ - .order_by('due_date','created_at') + queryset = self.get_queryset() + paginator = self.pagination_class() paginated_queryset = paginator.paginate_queryset(queryset, request) + serializer = TaskDetailsSerializer(paginated_queryset, many=True) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) +# class SearchTaskListAPIView(viewsets.ModelViewSet): +# """Update guardian profile""" +# serializer_class = TaskDetailsSerializer +# permission_classes = [IsAuthenticated] +# pagination_class = PageNumberPagination +# +# def list(self, request, *args, **kwargs): +# """Create guardian profile""" +# title = self.request.GET.get('title') +# junior_queryset = JuniorTask.objects.filter(guardian__user=request.user, task_name__icontains=title)\ +# .order_by('due_date','created_at') +# paginator = self.pagination_class() +# queryset = paginator.paginate_queryset(junior_queryset, request) +# serializer = TaskDetailsSerializer(queryset, many=True) +# return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" diff --git a/junior/migrations/0007_alter_junior_image.py b/junior/migrations/0007_alter_junior_image.py new file mode 100644 index 0000000..be695a0 --- /dev/null +++ b/junior/migrations/0007_alter_junior_image.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-07-06 12:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('junior', '0006_alter_junior_country_name'), + ] + + operations = [ + migrations.AlterField( + model_name='junior', + name='image', + field=models.URLField(blank=True, default=None, null=True), + ), + ] diff --git a/junior/models.py b/junior/models.py index 331673f..2534b50 100644 --- a/junior/models.py +++ b/junior/models.py @@ -18,7 +18,7 @@ class Junior(models.Model): """Personal info""" gender = models.CharField(max_length=10, choices=GENDERS, null=True, blank=True, default=None) dob = models.DateField(max_length=15, null=True, blank=True, default=None) - image = models.ImageField(null=True, blank=True, default=None) + image = models.URLField(null=True, blank=True, default=None) """Codes""" junior_code = models.CharField(max_length=10, null=True, blank=True, default=None) guardian_code = ArrayField(models.CharField(max_length=10, null=True, blank=True, default=None),null=True) From c6ffb1f039751c65eb6ba1ab15361bebe4afd4e3 Mon Sep 17 00:00:00 2001 From: jain Date: Thu, 6 Jul 2023 19:59:03 +0530 Subject: [PATCH 19/34] update profile --- account/serializers.py | 4 ++-- guardian/views.py | 17 ----------------- 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index 2074c25..28a58b2 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -25,7 +25,7 @@ class GoogleLoginSerializer(serializers.Serializer): class UpdateJuniorProfileImageSerializer(serializers.ModelSerializer): - """Reset Password after verification""" + """update junior image""" class Meta(object): """Meta info""" model = Junior @@ -40,7 +40,7 @@ class UpdateJuniorProfileImageSerializer(serializers.ModelSerializer): class UpdateGuardianImageSerializer(serializers.ModelSerializer): - """Reset Password after verification""" + """update guardian image""" class Meta(object): """Meta info""" model = Guardian diff --git a/guardian/views.py b/guardian/views.py index 3238cdd..2a3eeaf 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -133,23 +133,6 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): serializer = TaskDetailsSerializer(paginated_queryset, many=True) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) -# class SearchTaskListAPIView(viewsets.ModelViewSet): -# """Update guardian profile""" -# serializer_class = TaskDetailsSerializer -# permission_classes = [IsAuthenticated] -# pagination_class = PageNumberPagination -# -# def list(self, request, *args, **kwargs): -# """Create guardian profile""" -# title = self.request.GET.get('title') -# junior_queryset = JuniorTask.objects.filter(guardian__user=request.user, task_name__icontains=title)\ -# .order_by('due_date','created_at') -# paginator = self.pagination_class() -# queryset = paginator.paginate_queryset(junior_queryset, request) -# serializer = TaskDetailsSerializer(queryset, many=True) -# return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) - - class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" serializer_class = TopJuniorSerializer From ab435ef384b9704daf9f3094d5939f3258a0ae37 Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 7 Jul 2023 16:33:51 +0530 Subject: [PATCH 20/34] jira-15 dashboard api profile, default image, top leaderboard API --- account/admin.py | 11 +++- account/migrations/0003_defaulttaskimages.py | 26 +++++++++ account/models.py | 16 ++++++ account/serializers.py | 20 ++++++- account/urls.py | 6 ++- account/views.py | 57 ++++++++++++++++++-- guardian/serializers.py | 37 ++++++++++--- guardian/views.py | 11 ++-- junior/serializers.py | 30 +++++++++++ junior/views.py | 4 +- 10 files changed, 200 insertions(+), 18 deletions(-) create mode 100644 account/migrations/0003_defaulttaskimages.py diff --git a/account/admin.py b/account/admin.py index 7dbf869..96c2e4d 100644 --- a/account/admin.py +++ b/account/admin.py @@ -2,8 +2,17 @@ from django.contrib import admin """Import django app""" -from .models import UserProfile, UserEmailOtp, UserPhoneOtp +from .models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages # Register your models here. + +@admin.register(DefaultTaskImages) +class DefaultTaskImagesAdmin(admin.ModelAdmin): + """User profile admin""" + list_display = ['task_name', 'image_url'] + + def __str__(self): + return self.image_url + @admin.register(UserProfile) class UserProfileAdmin(admin.ModelAdmin): """User profile admin""" diff --git a/account/migrations/0003_defaulttaskimages.py b/account/migrations/0003_defaulttaskimages.py new file mode 100644 index 0000000..ec2e030 --- /dev/null +++ b/account/migrations/0003_defaulttaskimages.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2.2 on 2023-07-07 10:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0002_useremailotp_user_type'), + ] + + operations = [ + migrations.CreateModel( + name='DefaultTaskImages', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('task_name', models.CharField(max_length=15)), + ('image_url', models.URLField(blank=True, default=None, null=True)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ], + options={ + 'db_table': 'default_task_image', + }, + ), + ] diff --git a/account/models.py b/account/models.py index 43ab6e5..3eedfde 100644 --- a/account/models.py +++ b/account/models.py @@ -74,3 +74,19 @@ class UserEmailOtp(models.Model): def __str__(self): """return phone as an object""" return self.email + +class DefaultTaskImages(models.Model): + """Default images upload in oss bucket""" + + task_name = models.CharField(max_length=15) + image_url = models.URLField(null=True, blank=True, default=None) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta(object): + """ Meta information """ + db_table = 'default_task_image' + + def __str__(self): + """return phone as an object""" + return self.task_name diff --git a/account/serializers.py b/account/serializers.py index 28a58b2..b4b6899 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -2,7 +2,7 @@ from rest_framework import serializers from django.contrib.auth.models import User from guardian.models import Guardian from junior.models import Junior -from account.models import UserProfile, UserEmailOtp, UserPhoneOtp +from account.models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages from base.constants import GUARDIAN, JUNIOR, SUPERUSER from django.db import transaction from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR @@ -204,3 +204,21 @@ class EmailVerificationSerializer(serializers.ModelSerializer): model = UserEmailOtp fields = '__all__' + + +class DefaultTaskImagesSerializer(serializers.ModelSerializer): + """Update Password after verification""" + class Meta(object): + """Meta info""" + model = DefaultTaskImages + fields = ['task_name', 'image_url'] + def create(self, validated_data): + data = DefaultTaskImages.objects.create(**validated_data) + return data + +class DefaultTaskImagesDetailsSerializer(serializers.ModelSerializer): + """Update Password after verification""" + class Meta(object): + """Meta info""" + model = DefaultTaskImages + fields = '__all__' diff --git a/account/urls.py b/account/urls.py index 74098c2..c9929be 100644 --- a/account/urls.py +++ b/account/urls.py @@ -6,7 +6,8 @@ from rest_framework.decorators import api_view from rest_framework import routers from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp, ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage, - GoogleLoginViewSet, SigninWithApple) + GoogleLoginViewSet, SigninWithApple, ProfileAPIViewSet, UploadImageAPIViewSet, + DefaultImageAPIViewSet) """Router""" router = routers.SimpleRouter() @@ -18,6 +19,9 @@ router.register('google-login', GoogleLoginViewSet, basename='admin') # router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification') router.register('user-email-verification', UserEmailVerification, basename='user-email-verification') router.register('resend-email-otp', ReSendEmailOtp, basename='resend-email-otp') +router.register('profile', ProfileAPIViewSet, basename='profile') +router.register('upload-default-task-image', UploadImageAPIViewSet, basename='upload-default-task-image') +router.register('default-task-image', DefaultImageAPIViewSet, basename='default-task-image') urlpatterns = [ path('api/v1/', include(router.urls)), path('api/v1/forgot-password/', ForgotPasswordAPIView.as_view()), diff --git a/account/views.py b/account/views.py index 33ebbbc..15b8cff 100644 --- a/account/views.py +++ b/account/views.py @@ -10,11 +10,12 @@ from guardian.utils import upload_image_to_alibaba from django.contrib.auth import authenticate, login from guardian.models import Guardian from junior.models import Junior -from account.models import UserProfile, UserPhoneOtp, UserEmailOtp +from account.models import UserProfile, UserPhoneOtp, UserEmailOtp, DefaultTaskImages from django.contrib.auth.models import User from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer, ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, - GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer) + GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer, + DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer) from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp @@ -29,7 +30,8 @@ from rest_framework.response import Response import requests from django.conf import settings from .utils import get_token - +from junior.serializers import JuniorProfileSerializer +from guardian.serializers import GuardianProfileSerializer class GoogleLoginMixin: def google_login(self, request): @@ -159,6 +161,7 @@ class UpdateProfileImage(views.APIView): return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) class ChangePasswordAPIView(views.APIView): + serializer_class = ChangePasswordSerializer permission_classes = [IsAuthenticated] def post(self, request): serializer = ChangePasswordSerializer(context=request.user, data=request.data) @@ -212,6 +215,7 @@ class ForgotPasswordAPIView(views.APIView): class SendPhoneOtp(viewsets.ModelViewSet): """Send otp on phone""" + queryset = UserPhoneOtp.objects.all() def create(self, request, *args, **kwargs): otp = generate_otp() phone_number = self.request.data['phone'] @@ -227,6 +231,7 @@ class SendPhoneOtp(viewsets.ModelViewSet): class UserPhoneVerification(viewsets.ModelViewSet): """Send otp on phone""" + queryset = UserPhoneOtp.objects.all() def list(self, request, *args, **kwargs): try: phone_data = UserPhoneOtp.objects.filter(phone=self.request.GET.get('phone'), @@ -305,6 +310,7 @@ class UserLogin(viewsets.ViewSet): class UserEmailVerification(viewsets.ModelViewSet): """User Email verification""" serializer_class = EmailVerificationSerializer + queryset = UserEmailOtp.objects.all() def list(self, request, *args, **kwargs): try: @@ -344,6 +350,7 @@ class UserEmailVerification(viewsets.ModelViewSet): class ReSendEmailOtp(viewsets.ModelViewSet): """Send otp on phone""" + queryset = UserEmailOtp.objects.all() serializer_class = EmailVerificationSerializer def create(self, request, *args, **kwargs): otp = generate_otp() @@ -361,3 +368,47 @@ class ReSendEmailOtp(viewsets.ModelViewSet): return custom_response(SUCCESS_CODE['3016'], response_status=status.HTTP_200_OK) else: return custom_error_response(ERROR_CODE["2023"], response_status=status.HTTP_400_BAD_REQUEST) + +class ProfileAPIViewSet(viewsets.ModelViewSet): + """Profile viewset""" + queryset = User.objects.all() + serializer_class = [JuniorProfileSerializer, GuardianProfileSerializer] + def list(self, request, *args, **kwargs): + """profile view""" + if str(self.request.GET.get('user_type')) == '1': + junior_data = Junior.objects.filter(auth=self.request.user).last() + if junior_data: + serializer = JuniorProfileSerializer(junior_data) + if str(self.request.GET.get('user_type')) == '2': + guardian_data = Guardian.objects.filter(user=self.request.user).last() + if guardian_data: + serializer = GuardianProfileSerializer(guardian_data) + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + + +class UploadImageAPIViewSet(viewsets.ModelViewSet): + """Profile viewset""" + queryset = DefaultTaskImages.objects.all() + serializer_class = DefaultTaskImagesSerializer + def create(self, request, *args, **kwargs): + """profile view""" + image_data = request.data['image_url'] + filename = f"default_task_images/{image_data.name}" + image = upload_image_to_alibaba(image_data, filename) + image_data = image + request.data['image_url'] = image_data + serializer = DefaultTaskImagesSerializer(data=request.data) + if serializer.is_valid(): + serializer.save() + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + return custom_error_response(serializer.error, response_status=status.HTTP_400_BAD_REQUEST) + +class DefaultImageAPIViewSet(viewsets.ModelViewSet): + """Profile viewset""" + queryset = DefaultTaskImages.objects.all() + serializer_class = DefaultTaskImagesDetailsSerializer + def list(self, request, *args, **kwargs): + """profile view""" + queryset = DefaultTaskImages.objects.all() + serializer = DefaultTaskImagesSerializer(queryset, many=True) + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) diff --git a/guardian/serializers.py b/guardian/serializers.py index 0241874..0a01cd1 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -38,9 +38,11 @@ class UserSerializer(serializers.ModelSerializer): user = User.objects.create_user(username=email, email=email, password=password) UserProfile.objects.create(user=user, user_type=user_type) if user_type == '1': - Junior.objects.create(auth=user) + Junior.objects.create(auth=user, junior_code=''.join([str(random.randrange(9)) for _ in range(6)]), + referral_code=''.join([str(random.randrange(9)) for _ in range(6)])) if user_type == '2': - Guardian.objects.create(user=user) + Guardian.objects.create(user=user, guardian_code=''.join([str(random.randrange(9)) for _ in range(6)]), + referral_code=''.join([str(random.randrange(9)) for _ in range(6)])) return user except Exception as e: """Error handling""" @@ -88,11 +90,6 @@ class CreateGuardianSerializer(serializers.ModelSerializer): def create(self, validated_data): """Create guardian profile""" - # phone_number = validated_data.get('phone', None) - # guardian_data = Guardian.objects.filter(phone=phone_number) - # junior_data = Junior.objects.filter(phone=phone_number) - # if phone_number and (guardian_data or junior_data): - # raise serializers.ValidationError({"details": ERROR_CODE['2012']}) user = User.objects.filter(username=self.context['user']).last() if user: """Save first and last name of guardian""" @@ -176,7 +173,7 @@ class GuardianDetailSerializer(serializers.ModelSerializer): 'updated_at'] class TaskDetailsSerializer(serializers.ModelSerializer): - guardian = GuardianDetailSerializer() + # guardian = GuardianDetailSerializer() junior = JuniorDetailSerializer() class Meta(object): model = JuniorTask @@ -216,3 +213,27 @@ class TopJuniorSerializer(serializers.ModelSerializer): return item['total_points'] return 0 +class GuardianProfileSerializer(serializers.ModelSerializer): + """junior serializer""" + + email = serializers.SerializerMethodField('get_auth') + first_name = serializers.SerializerMethodField('get_first_name') + last_name = serializers.SerializerMethodField('get_last_name') + + def get_auth(self, obj): + """user email address""" + return obj.user.username + + def get_first_name(self, obj): + """user first name""" + return obj.user.first_name + + def get_last_name(self, obj): + """user last name""" + return obj.user.last_name + class Meta(object): + """Meta info""" + model = Guardian + fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', + 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', + 'updated_at'] \ No newline at end of file diff --git a/guardian/views.py b/guardian/views.py index 2a3eeaf..b6fc025 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -22,6 +22,7 @@ from django.db.models import Sum # Create your views here. class SignupViewset(viewsets.ModelViewSet): """Signup view set""" + queryset = User.objects.all() serializer_class = UserSerializer def create(self, request, *args, **kwargs): """Create user profile""" @@ -44,6 +45,7 @@ class SignupViewset(viewsets.ModelViewSet): class UpdateGuardianProfile(viewsets.ViewSet): """Update guardian profile""" + queryset = Guardian.objects.all() serializer_class = CreateGuardianSerializer permission_classes = [IsAuthenticated] @@ -63,6 +65,7 @@ class UpdateGuardianProfile(viewsets.ViewSet): class AllTaskListAPIView(viewsets.ModelViewSet): """Update guardian profile""" serializer_class = TaskDetailsSerializer + queryset = JuniorTask.objects.all() permission_classes = [IsAuthenticated] def list(self, request, *args, **kwargs): @@ -76,6 +79,7 @@ class TaskListAPIView(viewsets.ModelViewSet): serializer_class = TaskDetailsSerializer permission_classes = [IsAuthenticated] pagination_class = PageNumberPagination + queryset = JuniorTask.objects.all() def list(self, request, *args, **kwargs): """Create guardian profile""" @@ -93,6 +97,7 @@ class TaskListAPIView(viewsets.ModelViewSet): class CreateTaskAPIView(viewsets.ModelViewSet): """create task for junior""" serializer_class = TaskSerializer + queryset = JuniorTask.objects.all() def create(self, request, *args, **kwargs): image = request.data['default_image'] @@ -103,7 +108,6 @@ class CreateTaskAPIView(viewsets.ModelViewSet): filename = f"images/{image}" image_url = upload_image_to_alibaba(image, filename) image_data = image_url - data.pop('default_image') serializer = TaskSerializer(context={"user":request.user, "image":image_data}, data=data) if serializer.is_valid(): serializer.save() @@ -115,6 +119,7 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): serializer_class = TaskDetailsSerializer permission_classes = [IsAuthenticated] pagination_class = PageNumberPagination + queryset = JuniorTask.objects.all() def get_queryset(self): """Get the queryset for the view""" @@ -136,8 +141,8 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" serializer_class = TopJuniorSerializer - # permission_classes = [IsAuthenticated] - + permission_classes = [IsAuthenticated] + queryset = JuniorTask.objects.all() def list(self, request, *args, **kwargs): """fetch junior list those complete their task""" junior_ids_with_total_points = JuniorTask.objects.filter(task_status=1) \ diff --git a/junior/serializers.py b/junior/serializers.py index 5802a9c..a62e7ae 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -192,3 +192,33 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', 'updated_at', 'assigned_task','points', 'pending_task', 'in_progress_task', 'completed_task', 'requested_task', 'rejected_task'] + +class JuniorProfileSerializer(serializers.ModelSerializer): + """junior serializer""" + email = serializers.SerializerMethodField('get_auth') + first_name = serializers.SerializerMethodField('get_first_name') + last_name = serializers.SerializerMethodField('get_last_name') + notification_count = serializers.SerializerMethodField('get_notification_count') + + def get_auth(self, obj): + """user email address""" + return obj.auth.username + + def get_first_name(self, obj): + """user first name""" + return obj.auth.first_name + + def get_last_name(self, obj): + """user last name""" + return obj.auth.last_name + + def get_notification_count(self, obj): + """user email address""" + return 0 + + class Meta(object): + """Meta info""" + model = Junior + fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', + 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', + 'updated_at', 'notification_count'] diff --git a/junior/views.py b/junior/views.py index 89c177f..0658b2c 100644 --- a/junior/views.py +++ b/junior/views.py @@ -10,6 +10,7 @@ from account.utils import custom_response, custom_error_response # Create your views here. class UpdateJuniorProfile(viewsets.ViewSet): """Update junior profile""" + queryset = Junior.objects.all() serializer_class = CreateJuniorSerializer permission_classes = [IsAuthenticated] @@ -27,6 +28,7 @@ class UpdateJuniorProfile(viewsets.ViewSet): class ValidateGuardianCode(viewsets.ViewSet): """Check guardian code exist or not""" + queryset = Guardian.objects.all() permission_classes = [IsAuthenticated] def list(self, request, *args, **kwargs): @@ -43,7 +45,7 @@ class JuniorListAPIView(viewsets.ModelViewSet): """Junior list of assosicated guardian""" serializer_class = JuniorDetailListSerializer - + queryset = Junior.objects.all() def list(self, request, *args, **kwargs): """ junior list""" guardian_data = Guardian.objects.filter(user__email=request.user).last() From 9e8cde3516025a4e36d998a994446a3055eeacb2 Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 7 Jul 2023 17:02:48 +0530 Subject: [PATCH 21/34] swagger --- zod_bank/settings.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/zod_bank/settings.py b/zod_bank/settings.py index 3a897cb..6960233 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -112,6 +112,30 @@ DATABASES = { 'PORT':os.getenv('DB_PORT'), } } + +SWAGGER_SETTINGS = { + "exclude_namespaces": [], + "api_version": '0.1', + "api_path": "", + "enabled_methods": [ + 'get', + 'post', + 'put', + 'patch', + 'delete' + ], + "api_key": '', + "is_authenticated": True, + "is_superuser": False, + + 'SECURITY_DEFINITIONS': { + "api_key": { + "type": "apiKey", + "name": "Authorization", + "in": "header", + }, + }, +} # Password validation # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators From 413434496c64faf42be621a842374bb30f724c10 Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 7 Jul 2023 17:57:29 +0530 Subject: [PATCH 22/34] jira-15 country_name --- account/serializers.py | 8 ++++---- guardian/serializers.py | 2 +- junior/serializers.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index b4b6899..b04df3e 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -157,9 +157,9 @@ class GuardianSerializer(serializers.ModelSerializer): class Meta(object): """Meta info""" model = Guardian - fields = ['id', 'auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name', 'gender', 'dob', - 'referral_code', 'is_active', 'is_complete_profile', 'passcode', 'image', - 'created_at', 'updated_at', 'user_type'] + fields = ['id', 'auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name', + 'gender', 'dob', 'referral_code', 'is_active', 'is_complete_profile', 'passcode', 'image', + 'created_at', 'updated_at', 'user_type', 'country_name'] class JuniorSerializer(serializers.ModelSerializer): @@ -195,7 +195,7 @@ class JuniorSerializer(serializers.ModelSerializer): model = Junior fields = ['id', 'auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', - 'updated_at', 'user_type'] + 'updated_at', 'user_type', 'country_name'] class EmailVerificationSerializer(serializers.ModelSerializer): """Email verification serializer""" diff --git a/guardian/serializers.py b/guardian/serializers.py index 0a01cd1..6df46c6 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -234,6 +234,6 @@ class GuardianProfileSerializer(serializers.ModelSerializer): class Meta(object): """Meta info""" model = Guardian - fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', + fields = ['id', 'email', 'first_name', 'last_name', 'country_name','country_code', 'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', 'updated_at'] \ No newline at end of file diff --git a/junior/serializers.py b/junior/serializers.py index a62e7ae..7506f81 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -219,6 +219,6 @@ class JuniorProfileSerializer(serializers.ModelSerializer): class Meta(object): """Meta info""" model = Junior - fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', + fields = ['id', 'email', 'first_name', 'last_name', 'country_name', 'country_code', 'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', 'updated_at', 'notification_count'] From 3723e46be1e91a5e6024ed8a4125a681d3b77320 Mon Sep 17 00:00:00 2001 From: jain Date: Sun, 9 Jul 2023 17:56:47 +0530 Subject: [PATCH 23/34] jira-15 changes in profile API --- guardian/serializers.py | 22 +++++++++++++++++++++- guardian/views.py | 3 ++- junior/serializers.py | 16 ++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/guardian/serializers.py b/guardian/serializers.py index 6df46c6..1b6fa7e 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -219,6 +219,9 @@ class GuardianProfileSerializer(serializers.ModelSerializer): email = serializers.SerializerMethodField('get_auth') first_name = serializers.SerializerMethodField('get_first_name') last_name = serializers.SerializerMethodField('get_last_name') + total_count = serializers.SerializerMethodField('get_total_count') + complete_field_count = serializers.SerializerMethodField('get_complete_field_count') + notification_count = serializers.SerializerMethodField('get_notification_count') def get_auth(self, obj): """user email address""" @@ -231,9 +234,26 @@ class GuardianProfileSerializer(serializers.ModelSerializer): def get_last_name(self, obj): """user last name""" return obj.user.last_name + + def get_total_count(self, obj): + """total fields count""" + return 9 + + def get_complete_field_count(self, obj): + """total filled fields count""" + total_field_list = [obj.user.first_name, obj.user.last_name, obj.user.email, obj.country_name, obj.country_code, + obj.phone, obj.gender, obj.dob, obj.image] + total_complete_field = [data for data in total_field_list if data != '' and data is not None] + return len(total_complete_field) + + def get_notification_count(self, obj): + """total notification count""" + return 0 + class Meta(object): """Meta info""" model = Guardian fields = ['id', 'email', 'first_name', 'last_name', 'country_name','country_code', 'phone', 'gender', 'dob', - 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', + 'guardian_code', 'notification_count', 'total_count', 'complete_field_count', 'referral_code', + 'is_active', 'is_complete_profile', 'created_at', 'image', 'updated_at'] \ No newline at end of file diff --git a/guardian/views.py b/guardian/views.py index b6fc025..eba6248 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -84,7 +84,8 @@ class TaskListAPIView(viewsets.ModelViewSet): def list(self, request, *args, **kwargs): """Create guardian profile""" status_value = self.request.GET.get('status') - if status_value == 0: + print("status_value==>",status_value,'===>',type(status_value)) + if str(status_value) == '0': queryset = JuniorTask.objects.filter(guardian__user=request.user).order_by('created_at') else: queryset = JuniorTask.objects.filter(guardian__user=request.user, diff --git a/junior/serializers.py b/junior/serializers.py index 7506f81..b87c799 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -199,6 +199,8 @@ class JuniorProfileSerializer(serializers.ModelSerializer): first_name = serializers.SerializerMethodField('get_first_name') last_name = serializers.SerializerMethodField('get_last_name') notification_count = serializers.SerializerMethodField('get_notification_count') + total_count = serializers.SerializerMethodField('get_total_count') + complete_field_count = serializers.SerializerMethodField('get_complete_field_count') def get_auth(self, obj): """user email address""" @@ -213,12 +215,22 @@ class JuniorProfileSerializer(serializers.ModelSerializer): return obj.auth.last_name def get_notification_count(self, obj): - """user email address""" + """total notification count""" return 0 + def get_total_count(self, obj): + """total fields count""" + return 9 + + def get_complete_field_count(self, obj): + """total filled fields count""" + field_list = [obj.auth.first_name, obj.auth.last_name, obj.auth.email, obj.country_name, obj.country_code, + obj.phone, obj.gender, obj.dob, obj.image] + complete_field = [data for data in field_list if data is not None and data != ''] + return len(complete_field) class Meta(object): """Meta info""" model = Junior fields = ['id', 'email', 'first_name', 'last_name', 'country_name', 'country_code', 'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', - 'updated_at', 'notification_count'] + 'updated_at', 'notification_count', 'total_count', 'complete_field_count'] From 1b60d4344e03e6be6086b2ba34d64041db99e6e6 Mon Sep 17 00:00:00 2001 From: jain Date: Sun, 9 Jul 2023 19:13:05 +0530 Subject: [PATCH 24/34] jira-15 top leadership API --- guardian/serializers.py | 32 ++++---------------------- guardian/views.py | 18 ++++----------- junior/admin.py | 11 ++++++++- junior/migrations/0008_juniorpoints.py | 28 ++++++++++++++++++++++ junior/models.py | 18 +++++++++++++++ 5 files changed, 65 insertions(+), 42 deletions(-) create mode 100644 junior/migrations/0008_juniorpoints.py diff --git a/guardian/serializers.py b/guardian/serializers.py index 1b6fa7e..a0a85a6 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -13,7 +13,7 @@ from account.serializers import JuniorSerializer from junior.serializers import JuniorDetailSerializer from base.messages import ERROR_CODE, SUCCESS_CODE from .utils import upload_image_to_alibaba -from junior.models import Junior +from junior.models import Junior, JuniorPoints class UserSerializer(serializers.ModelSerializer): """User serializer""" auth_token = serializers.SerializerMethodField('get_auth_token') @@ -181,37 +181,13 @@ class TaskDetailsSerializer(serializers.ModelSerializer): 'junior', 'task_status', 'is_active', 'created_at','updated_at'] - - - class TopJuniorSerializer(serializers.ModelSerializer): - total_points = serializers.SerializerMethodField() - - email = serializers.SerializerMethodField('get_auth') - first_name = serializers.SerializerMethodField('get_first_name') - last_name = serializers.SerializerMethodField('get_last_name') - - def get_auth(self, obj): - return obj.auth.username - - def get_first_name(self, obj): - return obj.auth.first_name - - def get_last_name(self, obj): - return obj.auth.last_name + junior = JuniorDetailSerializer() class Meta: - model = Junior - fields = ['id', 'email', 'first_name', 'last_name', 'phone', 'country_code', 'country_name', 'gender', 'dob', 'image', 'junior_code', 'guardian_code', 'referral_code', 'referral_code_used', 'is_active', 'is_complete_profile', 'passcode', 'is_verified', 'created_at', 'updated_at', 'total_points'] + model = JuniorPoints + fields = ['id', 'junior', 'total_task_points', 'created_at', 'updated_at'] - def get_total_points(self, obj): - junior_ids_with_total_points = self.context.get('junior_ids_with_total_points') - if junior_ids_with_total_points: - junior_id = obj.id - for item in junior_ids_with_total_points: - if item['junior'] == junior_id: - return item['total_points'] - return 0 class GuardianProfileSerializer(serializers.ModelSerializer): """junior serializer""" diff --git a/guardian/views.py b/guardian/views.py index eba6248..e0f2bef 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -10,7 +10,7 @@ from datetime import datetime, timedelta from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer, TopJuniorSerializer) from .models import Guardian, JuniorTask -from junior.models import Junior +from junior.models import Junior, JuniorPoints from junior.serializers import JuniorDetailSerializer from account.models import UserEmailOtp from .tasks import generate_otp @@ -139,23 +139,15 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): serializer = TaskDetailsSerializer(paginated_queryset, many=True) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" serializer_class = TopJuniorSerializer permission_classes = [IsAuthenticated] - queryset = JuniorTask.objects.all() + queryset = JuniorPoints.objects.all() def list(self, request, *args, **kwargs): """fetch junior list those complete their task""" - junior_ids_with_total_points = JuniorTask.objects.filter(task_status=1) \ - .values('junior') \ - .annotate(total_points=Sum('points')) \ - .order_by('-total_points') - - junior_ids = [item['junior'] for item in junior_ids_with_total_points] - - juniors = Junior.objects.filter(id__in=junior_ids) - - serializer = self.get_serializer(juniors, many=True, context={'junior_ids_with_total_points': - junior_ids_with_total_points}) + junior_total_points = self.get_queryset().order_by('-total_task_points') + serializer = self.get_serializer(junior_total_points, many=True) return custom_response(serializer.data, response_status=status.HTTP_200_OK) diff --git a/junior/admin.py b/junior/admin.py index 87cd7d8..bb9ea49 100644 --- a/junior/admin.py +++ b/junior/admin.py @@ -2,7 +2,7 @@ """Third party Django app""" from django.contrib import admin """Import Django app""" -from .models import Junior +from .models import Junior, JuniorPoints # Register your models here. @admin.register(Junior) class JuniorAdmin(admin.ModelAdmin): @@ -12,3 +12,12 @@ class JuniorAdmin(admin.ModelAdmin): def __str__(self): """Return email id""" return self.auth__email + +@admin.register(JuniorPoints) +class JuniorPointsAdmin(admin.ModelAdmin): + """Junior Points Admin""" + list_display = ['junior', 'total_task_points'] + + def __str__(self): + """Return email id""" + return self.junior.auth.email diff --git a/junior/migrations/0008_juniorpoints.py b/junior/migrations/0008_juniorpoints.py new file mode 100644 index 0000000..d16b7e2 --- /dev/null +++ b/junior/migrations/0008_juniorpoints.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.2 on 2023-07-09 12:40 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('junior', '0007_alter_junior_image'), + ] + + operations = [ + migrations.CreateModel( + name='JuniorPoints', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('total_task_points', models.IntegerField(blank=True, default=0, null=True)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('junior', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='junior_points', to='junior.junior')), + ], + options={ + 'verbose_name': 'Junior Task Points', + 'db_table': 'junior_task_points', + }, + ), + ] diff --git a/junior/models.py b/junior/models.py index 2534b50..167983f 100644 --- a/junior/models.py +++ b/junior/models.py @@ -41,3 +41,21 @@ class Junior(models.Model): def __str__(self): """Return email id""" return f'{self.auth}' + +class JuniorPoints(models.Model): + """Junior model""" + junior = models.OneToOneField(Junior, on_delete=models.CASCADE, related_name='junior_points') + """Contact details""" + total_task_points = models.IntegerField(blank=True, null=True, default=0) + """Profile created and updated time""" + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta(object): + """ Meta class """ + db_table = 'junior_task_points' + verbose_name = 'Junior Task Points' + + def __str__(self): + """Return email id""" + return f'{self.junior.auth}' From 3098de36b8129c6346bc7f873316ed41564d4bc9 Mon Sep 17 00:00:00 2001 From: jain Date: Sun, 9 Jul 2023 19:25:59 +0530 Subject: [PATCH 25/34] jira-15 top leadership API --- guardian/serializers.py | 8 +++++++- guardian/views.py | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/guardian/serializers.py b/guardian/serializers.py index a0a85a6..845f331 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -183,10 +183,16 @@ class TaskDetailsSerializer(serializers.ModelSerializer): class TopJuniorSerializer(serializers.ModelSerializer): junior = JuniorDetailSerializer() + position = serializers.SerializerMethodField() class Meta: model = JuniorPoints - fields = ['id', 'junior', 'total_task_points', 'created_at', 'updated_at'] + fields = ['id', 'junior', 'total_task_points', 'position', 'created_at', 'updated_at'] + + def get_position(self, obj): + queryset = self.context['view'].get_queryset() + position = list(queryset).index(obj) + 1 + return position class GuardianProfileSerializer(serializers.ModelSerializer): diff --git a/guardian/views.py b/guardian/views.py index e0f2bef..ff6e332 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -143,11 +143,18 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" serializer_class = TopJuniorSerializer - permission_classes = [IsAuthenticated] + # permission_classes = [IsAuthenticated] queryset = JuniorPoints.objects.all() + + def get_serializer_context(self): + context = super().get_serializer_context() + context.update({'view': self}) + return context + def list(self, request, *args, **kwargs): """fetch junior list those complete their task""" junior_total_points = self.get_queryset().order_by('-total_task_points') serializer = self.get_serializer(junior_total_points, many=True) return custom_response(serializer.data, response_status=status.HTTP_200_OK) + From 026bfb6da71a74b64102b3f5712320c6a6cf8e71 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 12:10:15 +0530 Subject: [PATCH 26/34] sonar fixes --- account/apps.py | 3 +++ account/models.py | 4 +++- account/serializers.py | 22 +++++++++------------- account/tests.py | 2 ++ account/urls.py | 14 +++++++++++--- account/utils.py | 15 ++++++++++++--- account/views.py | 11 ++++++++--- guardian/models.py | 9 ++++++++- guardian/serializers.py | 25 ++++++++++++++++--------- guardian/utils.py | 7 +++++++ guardian/views.py | 2 +- junior/serializers.py | 13 +++++++------ manage.py | 3 +++ zod_bank/settings.py | 15 ++++----------- 14 files changed, 94 insertions(+), 51 deletions(-) diff --git a/account/apps.py b/account/apps.py index 2b08f1a..dc67efe 100644 --- a/account/apps.py +++ b/account/apps.py @@ -1,6 +1,9 @@ +"""Account app file""" +"""Import Django""" from django.apps import AppConfig class AccountConfig(AppConfig): + """default configurations""" default_auto_field = 'django.db.models.BigAutoField' name = 'account' diff --git a/account/models.py b/account/models.py index 3eedfde..ac23e79 100644 --- a/account/models.py +++ b/account/models.py @@ -1,6 +1,8 @@ +"""Account module file""" +"""Django import""" from django.db import models -import random from django.contrib.auth.models import User +"""App import""" from base.constants import USER_TYPE # Create your models here. diff --git a/account/serializers.py b/account/serializers.py index b04df3e..db11233 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -1,25 +1,21 @@ +"""Account serializer""" +"""Django Imoprt""" from rest_framework import serializers from django.contrib.auth.models import User +from rest_framework_simplejwt.tokens import RefreshToken +"""App import""" from guardian.models import Guardian from junior.models import Junior from account.models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages from base.constants import GUARDIAN, JUNIOR, SUPERUSER -from django.db import transaction from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR -from django.core.exceptions import ObjectDoesNotExist -from django.contrib.auth import authenticate -from rest_framework import viewsets, status -from rest_framework.decorators import action -from django.contrib.auth import authenticate, login -from rest_framework_simplejwt.tokens import RefreshToken -from guardian.utils import upload_image_to_alibaba -from .utils import get_token class GoogleLoginSerializer(serializers.Serializer): + """google login serializer""" access_token = serializers.CharField(max_length=5000, required=True) - class Meta: + class Meta(object): """meta class""" fields = ('access_token',) @@ -86,9 +82,8 @@ class ChangePasswordSerializer(serializers.Serializer): def validate_current_password(self, value): user = self.context - if self.context.password not in ('', None): - if user.check_password(value): - return value + if self.context.password not in ('', None) and user.check_password(value): + return value raise serializers.ValidationError(ERROR_CODE['2015']) def create(self, validated_data): new_password = validated_data.pop('new_password') @@ -109,6 +104,7 @@ class ForgotPasswordSerializer(serializers.Serializer): email = serializers.EmailField() class SuperUserSerializer(serializers.ModelSerializer): + """Super admin serializer""" user_type = serializers.SerializerMethodField('get_user_type') def get_user_type(self, obj): diff --git a/account/tests.py b/account/tests.py index 7ce503c..f2435b9 100644 --- a/account/tests.py +++ b/account/tests.py @@ -1,3 +1,5 @@ +"""Test cases file of account""" +"""Django import""" from django.test import TestCase # Create your tests here. diff --git a/account/urls.py b/account/urls.py index c9929be..ffb2db6 100644 --- a/account/urls.py +++ b/account/urls.py @@ -1,9 +1,9 @@ """ Urls files""" """Django import""" from django.urls import path, include -from rest_framework.decorators import api_view """Third party import""" from rest_framework import routers +"""Import view functions""" from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp, ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage, GoogleLoginViewSet, SigninWithApple, ProfileAPIViewSet, UploadImageAPIViewSet, @@ -13,15 +13,23 @@ router = routers.SimpleRouter() """API End points with router""" router.register('user', UserLogin, basename='user') +"""super admin login""" router.register('admin', UserLogin, basename='admin') +"""google login end point""" router.register('google-login', GoogleLoginViewSet, basename='admin') -# router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp') -# router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification') +router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp') +router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification') +"""email verification end point""" router.register('user-email-verification', UserEmailVerification, basename='user-email-verification') +"""Resend email otp end point""" router.register('resend-email-otp', ReSendEmailOtp, basename='resend-email-otp') +"""Profile end point""" router.register('profile', ProfileAPIViewSet, basename='profile') +"""Upload default task image end point""" router.register('upload-default-task-image', UploadImageAPIViewSet, basename='upload-default-task-image') +"""Fetch default task image end point""" router.register('default-task-image', DefaultImageAPIViewSet, basename='default-task-image') +"""Define url pattern""" urlpatterns = [ path('api/v1/', include(router.urls)), path('api/v1/forgot-password/', ForgotPasswordAPIView.as_view()), diff --git a/account/utils.py b/account/utils.py index 0848c03..4412e62 100644 --- a/account/utils.py +++ b/account/utils.py @@ -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 - } \ No newline at end of file + } diff --git a/account/views.py b/account/views.py index 15b8cff..1324869 100644 --- a/account/views.py +++ b/account/views.py @@ -1,17 +1,20 @@ +"""Account view """ +"""Django import""" from datetime import datetime, timedelta - from rest_framework import viewsets, status, views from rest_framework.decorators import action import random import logging from django.utils import timezone import jwt +"""App Import""" from guardian.utils import upload_image_to_alibaba from django.contrib.auth import authenticate, login from guardian.models import Guardian from junior.models import Junior from account.models import UserProfile, UserPhoneOtp, UserEmailOtp, DefaultTaskImages from django.contrib.auth.models import User +"""Account serializer""" from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer, ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer, @@ -29,12 +32,13 @@ from rest_framework import status from rest_framework.response import Response import requests from django.conf import settings -from .utils import get_token from junior.serializers import JuniorProfileSerializer from guardian.serializers import GuardianProfileSerializer class GoogleLoginMixin: + """google login mixin""" def google_login(self, request): + """google login function""" access_token = request.data.get('access_token') user_type = request.data.get('user_type') if not access_token: @@ -341,7 +345,8 @@ class UserEmailVerification(viewsets.ModelViewSet): guardian_data.save() refresh = RefreshToken.for_user(user_obj) access_token = str(refresh.access_token) - return custom_response(SUCCESS_CODE['3011'], {"auth_token":access_token}, response_status=status.HTTP_200_OK) + return custom_response(SUCCESS_CODE['3011'], {"auth_token":access_token}, + response_status=status.HTTP_200_OK) else: return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST) except Exception as e: diff --git a/guardian/models.py b/guardian/models.py index 00c5d42..1bb4d2c 100644 --- a/guardian/models.py +++ b/guardian/models.py @@ -5,6 +5,7 @@ from django.contrib.auth import get_user_model """Import Django app""" from base.constants import GENDERS, TASK_STATUS, PENDING, TASK_POINTS from junior.models import Junior +"""Add user model""" User = get_user_model() # Create your models here. @@ -44,16 +45,22 @@ class Guardian(models.Model): return f'{self.user}' class JuniorTask(models.Model): - """Guardian model""" + """Junior Task details model""" guardian = models.ForeignKey(Guardian, on_delete=models.CASCADE, related_name='guardian', verbose_name='Guardian') + """task details""" task_name = models.CharField(max_length=100) task_description = models.CharField(max_length=500) + """points of the task""" points = models.IntegerField(default=TASK_POINTS) due_date = models.DateField(auto_now_add=False, null=True, blank=True) + """Images of task""" default_image = models.URLField(null=True, blank=True, default=None) image = models.URLField(null=True, blank=True, default=None) + """associated junior with the task""" junior = models.ForeignKey(Junior, on_delete=models.CASCADE, related_name='junior', verbose_name='Junior') + """task status""" task_status = models.CharField(choices=TASK_STATUS, max_length=15, default=PENDING) + """task stage""" is_active = models.BooleanField(default=True) is_approved = models.BooleanField(default=False) """Profile created and updated time""" diff --git a/guardian/serializers.py b/guardian/serializers.py index 845f331..08c2861 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -114,17 +114,18 @@ class CreateGuardianSerializer(serializers.ModelSerializer): guardian.passcode = validated_data.get('passcode', guardian.passcode) guardian.country_name = validated_data.get('country_name', guardian.country_name) guardian.referral_code_used = validated_data.get('referral_code_used', guardian.referral_code_used) - """Complete profile of the junior if below all data are filled""" - complete_profile_field = all([guardian.phone, guardian.gender, guardian.family_name, guardian.country_name, - guardian.dob, guardian.country_code, user.first_name, user.last_name]) - guardian.is_complete_profile = False - if complete_profile_field: - guardian.is_complete_profile = True image = validated_data.pop('image', None) if image: filename = f"images/{image.name}" image_url = upload_image_to_alibaba(image, filename) guardian.image = image_url + """Complete profile of the junior if below all data are filled""" + complete_profile_field = all([guardian.phone, guardian.gender, guardian.family_name, guardian.country_name, + guardian.dob, guardian.country_code, user.first_name, user.last_name, + user.email, guardian.image]) + guardian.is_complete_profile = False + if complete_profile_field: + guardian.is_complete_profile = True guardian.save() return guardian @@ -137,10 +138,13 @@ class CreateGuardianSerializer(serializers.ModelSerializer): class TaskSerializer(serializers.ModelSerializer): + """Task serializer""" class Meta(object): + """Meta info""" model = JuniorTask fields = ['task_name','task_description','points', 'due_date', 'junior', 'default_image'] def create(self, validated_data): + """create default task image data""" validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last() images = self.context['image'] validated_data['default_image'] = images @@ -173,23 +177,26 @@ class GuardianDetailSerializer(serializers.ModelSerializer): 'updated_at'] class TaskDetailsSerializer(serializers.ModelSerializer): - # guardian = GuardianDetailSerializer() junior = JuniorDetailSerializer() class Meta(object): + """Meta info""" model = JuniorTask fields = ['id', 'guardian', 'task_name', 'task_description', 'points', 'due_date','default_image', 'image', 'junior', 'task_status', 'is_active', 'created_at','updated_at'] class TopJuniorSerializer(serializers.ModelSerializer): + """Top junior serializer""" junior = JuniorDetailSerializer() position = serializers.SerializerMethodField() - class Meta: + class Meta(object): + """Meta info""" model = JuniorPoints fields = ['id', 'junior', 'total_task_points', 'position', 'created_at', 'updated_at'] def get_position(self, obj): + """get position of junior""" queryset = self.context['view'].get_queryset() position = list(queryset).index(obj) + 1 return position @@ -238,4 +245,4 @@ class GuardianProfileSerializer(serializers.ModelSerializer): fields = ['id', 'email', 'first_name', 'last_name', 'country_name','country_code', 'phone', 'gender', 'dob', 'guardian_code', 'notification_count', 'total_count', 'complete_field_count', 'referral_code', 'is_active', 'is_complete_profile', 'created_at', 'image', - 'updated_at'] \ No newline at end of file + 'updated_at'] diff --git a/guardian/utils.py b/guardian/utils.py index 66a36ca..ff8472c 100644 --- a/guardian/utils.py +++ b/guardian/utils.py @@ -1,15 +1,22 @@ +"""Utiles file of guardian""" +"""Django import""" import oss2 from django.conf import settings import tempfile def upload_image_to_alibaba(image, filename): + """upload image on oss alibaba bucket""" # Save the image object to a temporary file with tempfile.NamedTemporaryFile(delete=False) as temp_file: + """write image in temporary file""" temp_file.write(image.read()) + """auth of bucket""" auth = oss2.Auth(settings.ALIYUN_OSS_ACCESS_KEY_ID, settings.ALIYUN_OSS_ACCESS_KEY_SECRET) + """fetch bucket details""" bucket = oss2.Bucket(auth, settings.ALIYUN_OSS_ENDPOINT, settings.ALIYUN_OSS_BUCKET_NAME) # Upload the temporary file to Alibaba OSS bucket.put_object_from_file(filename, temp_file.name) + """create perfect url for image""" new_filename = filename.replace(' ', '%20') return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}" diff --git a/guardian/views.py b/guardian/views.py index ff6e332..9c43628 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -143,7 +143,7 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" serializer_class = TopJuniorSerializer - # permission_classes = [IsAuthenticated] + permission_classes = [IsAuthenticated] queryset = JuniorPoints.objects.all() def get_serializer_context(self): diff --git a/junior/serializers.py b/junior/serializers.py index b87c799..81ea0cb 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -89,16 +89,17 @@ class CreateJuniorSerializer(serializers.ModelSerializer): junior.phone = validated_data.get('phone', junior.phone) junior.country_code = validated_data.get('country_code', junior.country_code) junior.referral_code_used = validated_data.get('referral_code_used', junior.referral_code_used) - """Complete profile of the junior if below all data are filled""" - complete_profile_field = all([junior.phone, junior.gender, junior.country_name, - junior.dob, junior.country_code, user.first_name, user.last_name]) - junior.is_complete_profile = False - if complete_profile_field: - junior.is_complete_profile = True if image: filename = f"images/{image.name}" image_url = upload_image_to_alibaba(image, filename) junior.image = image_url + """Complete profile of the junior if below all data are filled""" + complete_profile_field = all([junior.phone, junior.gender, junior.country_name, junior.image, + junior.dob, junior.country_code, user.first_name, user.last_name, + user.email]) + junior.is_complete_profile = False + if complete_profile_field: + junior.is_complete_profile = True junior.save() return junior diff --git a/manage.py b/manage.py index fe9e065..a19d33b 100755 --- a/manage.py +++ b/manage.py @@ -1,7 +1,9 @@ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" """Django import""" +"""Import OS module""" import os +"""Import sys module""" import sys @@ -18,6 +20,7 @@ def main(): "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc + """execute command line function""" execute_from_command_line(sys.argv) diff --git a/zod_bank/settings.py b/zod_bank/settings.py index 6960233..6f43fdc 100644 --- a/zod_bank/settings.py +++ b/zod_bank/settings.py @@ -93,8 +93,8 @@ REST_FRAMEWORK = { # 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', 'rest_framework_simplejwt.authentication.JWTAuthentication',], - 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', - 'PAGE_SIZE': 5, # Set the default pagination size + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'PAGE_SIZE': 5, } SIMPLE_JWT = { 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=50), @@ -201,22 +201,15 @@ https://docs.djangoproject.com/en/3.0/howto/static-files/""" GOOGLE_CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID') GOOGLE_CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET') -# EMAIL_BACKEND = os.getenv('EMAIL_BACKEND') -# EMAIL_HOST = os.getenv('EMAIL_HOST') -# EMAIL_PORT = os.getenv('EMAIL_PORT') -# EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS') -# EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER') # Replace with your Gmail email address -# EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD') # Replace with your Gmail email password or App password -# EMAIL_FROM_ADDRESS = os.getenv('EMAIL_FROM_ADDRESS') EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend" EMAIL_HOST="smtp.sendgrid.net" EMAIL_PORT="587" EMAIL_USE_TLS="True" -EMAIL_HOST_USER="apikey" # Replace with your Gmail email address +EMAIL_HOST_USER="apikey" EMAIL_HOST_PASSWORD="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" EMAIL_FROM_ADDRESS="support@zodbank.com" -# EMAIL_FROM_ADDRESS="zodbank@yopmail.com" + ALIYUN_OSS_ACCESS_KEY_ID = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID') ALIYUN_OSS_ACCESS_KEY_SECRET = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET') From c8925aad8eff38c70bd5aaa239b76a11faf073cb Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 14:53:43 +0530 Subject: [PATCH 27/34] jira-15 leader board API and profile API --- guardian/serializers.py | 16 ++++++++-------- guardian/views.py | 10 ++++++++-- junior/admin.py | 2 +- .../migrations/0009_juniorpoints_position.py | 18 ++++++++++++++++++ junior/models.py | 2 ++ junior/serializers.py | 10 ++++++++-- 6 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 junior/migrations/0009_juniorpoints_position.py diff --git a/guardian/serializers.py b/guardian/serializers.py index 08c2861..f5f8588 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -120,7 +120,7 @@ class CreateGuardianSerializer(serializers.ModelSerializer): image_url = upload_image_to_alibaba(image, filename) guardian.image = image_url """Complete profile of the junior if below all data are filled""" - complete_profile_field = all([guardian.phone, guardian.gender, guardian.family_name, guardian.country_name, + complete_profile_field = all([guardian.phone, guardian.gender, guardian.country_name, guardian.dob, guardian.country_code, user.first_name, user.last_name, user.email, guardian.image]) guardian.is_complete_profile = False @@ -188,18 +188,18 @@ class TaskDetailsSerializer(serializers.ModelSerializer): class TopJuniorSerializer(serializers.ModelSerializer): """Top junior serializer""" junior = JuniorDetailSerializer() - position = serializers.SerializerMethodField() + position = serializers.IntegerField() - class Meta(object): + class Meta: """Meta info""" model = JuniorPoints fields = ['id', 'junior', 'total_task_points', 'position', 'created_at', 'updated_at'] - def get_position(self, obj): - """get position of junior""" - queryset = self.context['view'].get_queryset() - position = list(queryset).index(obj) + 1 - return position + def to_representation(self, instance): + """Convert instance to representation""" + representation = super().to_representation(instance) + representation['position'] = instance.position + return representation class GuardianProfileSerializer(serializers.ModelSerializer): diff --git a/guardian/views.py b/guardian/views.py index 9c43628..360f77d 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -143,7 +143,7 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" serializer_class = TopJuniorSerializer - permission_classes = [IsAuthenticated] + # permission_classes = [IsAuthenticated] queryset = JuniorPoints.objects.all() def get_serializer_context(self): @@ -152,8 +152,14 @@ class TopJuniorListAPIView(viewsets.ModelViewSet): return context def list(self, request, *args, **kwargs): - """fetch junior list those complete their task""" + """Fetch junior list of those who complete their tasks""" junior_total_points = self.get_queryset().order_by('-total_task_points') + + # Update the position field for each JuniorPoints object + for index, junior in enumerate(junior_total_points): + junior.position = index + 1 + junior.save() + serializer = self.get_serializer(junior_total_points, many=True) return custom_response(serializer.data, response_status=status.HTTP_200_OK) diff --git a/junior/admin.py b/junior/admin.py index bb9ea49..a2acfb6 100644 --- a/junior/admin.py +++ b/junior/admin.py @@ -16,7 +16,7 @@ class JuniorAdmin(admin.ModelAdmin): @admin.register(JuniorPoints) class JuniorPointsAdmin(admin.ModelAdmin): """Junior Points Admin""" - list_display = ['junior', 'total_task_points'] + list_display = ['junior', 'total_task_points', 'position'] def __str__(self): """Return email id""" diff --git a/junior/migrations/0009_juniorpoints_position.py b/junior/migrations/0009_juniorpoints_position.py new file mode 100644 index 0000000..2f5b31d --- /dev/null +++ b/junior/migrations/0009_juniorpoints_position.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-07-10 07:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('junior', '0008_juniorpoints'), + ] + + operations = [ + migrations.AddField( + model_name='juniorpoints', + name='position', + field=models.IntegerField(blank=True, default=99999, null=True), + ), + ] diff --git a/junior/models.py b/junior/models.py index 167983f..505a144 100644 --- a/junior/models.py +++ b/junior/models.py @@ -47,6 +47,8 @@ class JuniorPoints(models.Model): junior = models.OneToOneField(Junior, on_delete=models.CASCADE, related_name='junior_points') """Contact details""" total_task_points = models.IntegerField(blank=True, null=True, default=0) + """position of the junior""" + position = models.IntegerField(blank=True, null=True, default=99999) """Profile created and updated time""" created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) diff --git a/junior/serializers.py b/junior/serializers.py index 81ea0cb..12e522f 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -5,7 +5,7 @@ from django.contrib.auth.models import User from django.db import transaction import random """Import django app""" -from junior.models import Junior +from junior.models import Junior, JuniorPoints from guardian.utils import upload_image_to_alibaba from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.models import Guardian, JuniorTask @@ -147,6 +147,7 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): requested_task = serializers.SerializerMethodField('get_requested_task') rejected_task = serializers.SerializerMethodField('get_rejected_task') pending_task = serializers.SerializerMethodField('get_pending_task') + position = serializers.SerializerMethodField('get_position') def get_auth(self, obj): @@ -162,6 +163,11 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): data = JuniorTask.objects.filter(junior=obj).count() return data + def get_position(self, obj): + data = JuniorPoints.objects.filter(junior=obj).last() + if data: + return data.position + return 99999 def get_points(self, obj): data = sum(JuniorTask.objects.filter(junior=obj, task_status=COMPLETED).values_list('points', flat=True)) return data @@ -192,7 +198,7 @@ class JuniorDetailListSerializer(serializers.ModelSerializer): fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob', 'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image', 'updated_at', 'assigned_task','points', 'pending_task', 'in_progress_task', 'completed_task', - 'requested_task', 'rejected_task'] + 'requested_task', 'rejected_task', 'position'] class JuniorProfileSerializer(serializers.ModelSerializer): """junior serializer""" From 0d4c9aae78afd1035f976d8ebc99db1e6cbb394e Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 16:50:50 +0530 Subject: [PATCH 28/34] delete user account API --- account/migrations/0004_userdelete.py | 31 ++++++++++++++++++ account/models.py | 21 ++++++++++++ account/serializers.py | 47 ++++++++++++++++++++++++++- account/urls.py | 4 ++- account/views.py | 19 ++++++++++- base/messages.py | 5 +-- junior/admin.py | 2 +- 7 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 account/migrations/0004_userdelete.py diff --git a/account/migrations/0004_userdelete.py b/account/migrations/0004_userdelete.py new file mode 100644 index 0000000..10bd2aa --- /dev/null +++ b/account/migrations/0004_userdelete.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.2 on 2023-07-10 09:24 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('account', '0003_defaulttaskimages'), + ] + + operations = [ + migrations.CreateModel( + name='UserDelete', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('old_email', models.EmailField(blank=True, max_length=254, null=True, verbose_name='Original Email')), + ('d_email', models.EmailField(blank=True, max_length=254, null=True, verbose_name='Dummy Email')), + ('is_active', models.BooleanField(default=True)), + ('reason', models.TextField(blank=True, max_length=500, null=True, verbose_name='Reason for Leaving')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='delete_information_set', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'db_table': 'user_delete_information', + }, + ), + ] diff --git a/account/models.py b/account/models.py index ac23e79..31c592a 100644 --- a/account/models.py +++ b/account/models.py @@ -92,3 +92,24 @@ class DefaultTaskImages(models.Model): def __str__(self): """return phone as an object""" return self.task_name + +class UserDelete(models.Model): + """ + User delete information + """ + user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='delete_information_set') + """Old email""" + old_email = models.EmailField(blank=True, null=True, verbose_name='Original Email') + """Dummy email""" + d_email = models.EmailField(blank=True, null=True, verbose_name='Dummy Email') + is_active = models.BooleanField(default=True) + """reason for leaving""" + reason = models.TextField(max_length=500, blank=True, null=True, verbose_name='Reason for Leaving') + created_at = models.DateTimeField(auto_now_add=True) + + class Meta(object): + """ Meta information """ + db_table = 'user_delete_information' + + def __str__(self): + return self.user.email diff --git a/account/serializers.py b/account/serializers.py index db11233..4007e98 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -1,12 +1,13 @@ """Account serializer""" """Django Imoprt""" +import random from rest_framework import serializers from django.contrib.auth.models import User from rest_framework_simplejwt.tokens import RefreshToken """App import""" from guardian.models import Guardian from junior.models import Junior -from account.models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages +from account.models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserDelete from base.constants import GUARDIAN, JUNIOR, SUPERUSER from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR @@ -218,3 +219,47 @@ class DefaultTaskImagesDetailsSerializer(serializers.ModelSerializer): """Meta info""" model = DefaultTaskImages fields = '__all__' + +class UserDeleteSerializer(serializers.ModelSerializer): + """User Delete Serializer""" + class Meta(object): + """Meta Information""" + model = UserDelete + fields = ['reason'] + def create(self, validated_data): + user = self.context['user'] + user_type = str(self.context['user_type']) + data = validated_data.get('reason') + random_num = random.randint(0,10000) + user_tb = User.objects.filter(id=user.id).last() + user_type_data = UserEmailOtp.objects.filter(email=user.email).last() + if user_type == '1' and user_type_data.user_type == '1': + junior_data = Junior.objects.filter(auth__email=user_tb.email).first() + if junior_data: + junior_data.is_active = False + junior_data.is_verified = False + junior_data.guardian_code = '{}' + junior_data.save() + elif user_type == '2' and user_type_data.user_type == '2': + guardian_data = Guardian.objects.filter(user__email=user_tb.email).first() + if guardian_data: + guardian_data.is_active = False + guardian_data.is_verified = False + guardian_data.save() + jun_data = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) + for data in jun_data: + data.guardian_code.remove(guardian_data.guardian_code) + data.save() + else: + raise serializers.ValidationError({"details":ERROR_CODE['2030'],"code":"400", "status":"failed"}) + + user_tb.email = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower() + user_tb.username = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower() + user_tb.password = 'None' + d_email = user_tb.email + o_mail = user.email + instance = UserDelete.objects.create(user=user_tb, d_email=d_email, old_email=o_mail, + is_active=True, reason=data) + user_tb.save() + + return instance diff --git a/account/urls.py b/account/urls.py index ffb2db6..b64f99d 100644 --- a/account/urls.py +++ b/account/urls.py @@ -7,7 +7,7 @@ from rest_framework import routers from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp, ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage, GoogleLoginViewSet, SigninWithApple, ProfileAPIViewSet, UploadImageAPIViewSet, - DefaultImageAPIViewSet) + DefaultImageAPIViewSet, DeleteUserProfileAPIViewSet) """Router""" router = routers.SimpleRouter() @@ -29,6 +29,8 @@ router.register('profile', ProfileAPIViewSet, basename='profile') router.register('upload-default-task-image', UploadImageAPIViewSet, basename='upload-default-task-image') """Fetch default task image end point""" router.register('default-task-image', DefaultImageAPIViewSet, basename='default-task-image') +"""Delete user account""" +router.register('delete', DeleteUserProfileAPIViewSet, basename='delete') """Define url pattern""" urlpatterns = [ path('api/v1/', include(router.urls)), diff --git a/account/views.py b/account/views.py index 1324869..5657b4a 100644 --- a/account/views.py +++ b/account/views.py @@ -18,7 +18,7 @@ from django.contrib.auth.models import User from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer, ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer, - DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer) + DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer, UserDeleteSerializer) from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp @@ -417,3 +417,20 @@ class DefaultImageAPIViewSet(viewsets.ModelViewSet): queryset = DefaultTaskImages.objects.all() serializer = DefaultTaskImagesSerializer(queryset, many=True) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + + +class DeleteUserProfileAPIViewSet(viewsets.GenericViewSet): + """ Delete user API view set """ + + @action(detail=False, methods=['POST'], url_path='user-account',serializer_class=UserDeleteSerializer, + permission_classes=[IsAuthenticated]) + def account(self, request): + print("request.data===>",request.data) + print("request.user===>", request.user) + user_type = str(request.data['user_type']) + serializer = self.get_serializer(data=request.data, context={'request': request, 'user': request.user, + 'user_type':user_type}) + if serializer.is_valid(): + serializer.save() + return custom_response(SUCCESS_CODE['3005'], response_status=status.HTTP_200_OK) + return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) diff --git a/base/messages.py b/base/messages.py index f024a3d..b00bfbf 100644 --- a/base/messages.py +++ b/base/messages.py @@ -52,7 +52,8 @@ ERROR_CODE = { "2026": "New password should not same as old password", "2027": "data should contain `identityToken`", "2028": "You are not authorized person to sign up on this platform", - "2029": "Validity of otp verification is expired" + "2029": "Validity of otp verification is expired", + "2030": "Use correct user type and token" } SUCCESS_CODE = { # Success code for password @@ -64,7 +65,7 @@ SUCCESS_CODE = { # Success code for password reset "3004": "Password reset link has been sent to your email address", # Success code for link verified - "3005": "Your link has been verified, it's valid", + "3005": "Your account is deleted successfully.", # Success code for password reset "3006": "Your password has been reset successfully.", # Success code for password update diff --git a/junior/admin.py b/junior/admin.py index a2acfb6..3764f48 100644 --- a/junior/admin.py +++ b/junior/admin.py @@ -7,7 +7,7 @@ from .models import Junior, JuniorPoints @admin.register(Junior) class JuniorAdmin(admin.ModelAdmin): """Junior Admin""" - list_display = ['auth'] + list_display = ['auth', 'guardian_code'] def __str__(self): """Return email id""" From 4fb7a2fc6c6e369f455c2fcc8ee4d6aac3e81934 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 17:51:30 +0530 Subject: [PATCH 29/34] jira-14 dashboard API --- guardian/serializers.py | 21 +++++++++++---------- guardian/views.py | 14 +++++++++++--- junior/serializers.py | 20 +++++++++++--------- junior/views.py | 17 +++++++++++++---- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/guardian/serializers.py b/guardian/serializers.py index f5f8588..14c4a6d 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -67,7 +67,7 @@ class CreateGuardianSerializer(serializers.ModelSerializer): family_name = serializers.CharField(max_length=100, required=False) dob = serializers.DateField(required=False) referral_code = serializers.CharField(max_length=100, required=False) - image = serializers.ImageField(required=False) + image = serializers.URLField(required=False) class Meta(object): """Meta info""" @@ -93,10 +93,15 @@ class CreateGuardianSerializer(serializers.ModelSerializer): user = User.objects.filter(username=self.context['user']).last() if user: """Save first and last name of guardian""" - if self.context.get('first_name') != '' and self.context.get('last_name') != '': - user.first_name = self.context.get('first_name', user.first_name) - user.last_name = self.context.get('last_name', user.last_name) - user.save() + if self.context.get('first_name') != '' and self.context.get('first_name') is not None: + user.first_name = self.context.get('first_name') + else: + user.first_name = user.first_name + if self.context.get('last_name') != '' and self.context.get('last_name') is not None: + user.last_name = self.context.get('last_name') + else: + user.last_name = user.last_name + user.save() """Create guardian data""" guardian, created = Guardian.objects.get_or_create(user=self.context['user']) if created: @@ -114,11 +119,7 @@ class CreateGuardianSerializer(serializers.ModelSerializer): guardian.passcode = validated_data.get('passcode', guardian.passcode) guardian.country_name = validated_data.get('country_name', guardian.country_name) guardian.referral_code_used = validated_data.get('referral_code_used', guardian.referral_code_used) - image = validated_data.pop('image', None) - if image: - filename = f"images/{image.name}" - image_url = upload_image_to_alibaba(image, filename) - guardian.image = image_url + guardian.image = validated_data.get('image', guardian.image) """Complete profile of the junior if below all data are filled""" complete_profile_field = all([guardian.phone, guardian.gender, guardian.country_name, guardian.dob, guardian.country_code, user.first_name, user.last_name, diff --git a/guardian/views.py b/guardian/views.py index 360f77d..4d9517f 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -51,10 +51,18 @@ class UpdateGuardianProfile(viewsets.ViewSet): def create(self, request, *args, **kwargs): """Create guardian profile""" + data = request.data + image = request.data.get('image') + image_url = '' + if image: + filename = f"images/{image.name}" + image_url = upload_image_to_alibaba(image, filename) + data = {"image":image_url} serializer = CreateGuardianSerializer(context={"user":request.user, - "first_name":request.data.get('first_name', ''), - "last_name": request.data.get('last_name',' ')}, - data=request.data) + "first_name":request.data.get('first_name'), + "last_name": request.data.get('last_name'), + "image":image_url}, + data=data) if serializer.is_valid(): """save serializer""" serializer.save() diff --git a/junior/serializers.py b/junior/serializers.py index 12e522f..28bbdc4 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -36,7 +36,7 @@ class CreateJuniorSerializer(serializers.ModelSerializer): dob = serializers.DateField(required=False) referral_code = serializers.CharField(max_length=100, required=False) guardian_code = ListCharField(required=False) - image = serializers.ImageField(required=False) + image = serializers.URLField(required=False) class Meta(object): """Meta info""" @@ -64,10 +64,15 @@ class CreateJuniorSerializer(serializers.ModelSerializer): user = User.objects.filter(username=self.context['user']).last() if user: """Save first and last name of junior""" - if self.context.get('first_name') != '' and self.context.get('last_name') != '': - user.first_name = self.context.get('first_name', user.first_name) - user.last_name = self.context.get('last_name', user.last_name) - user.save() + if self.context.get('first_name') != '' and self.context.get('first_name') is not None: + user.first_name = self.context.get('first_name') + else: + user.first_name = user.first_name + if self.context.get('last_name') != '' and self.context.get('last_name') is not None: + user.last_name = self.context.get('last_name') + else: + user.last_name = user.last_name + user.save() """Create junior data""" junior, created = Junior.objects.get_or_create(auth=self.context['user']) if created: @@ -89,10 +94,7 @@ class CreateJuniorSerializer(serializers.ModelSerializer): junior.phone = validated_data.get('phone', junior.phone) junior.country_code = validated_data.get('country_code', junior.country_code) junior.referral_code_used = validated_data.get('referral_code_used', junior.referral_code_used) - if image: - filename = f"images/{image.name}" - image_url = upload_image_to_alibaba(image, filename) - junior.image = image_url + junior.image = validated_data.get('image', junior.image) """Complete profile of the junior if below all data are filled""" complete_profile_field = all([junior.phone, junior.gender, junior.country_name, junior.image, junior.dob, junior.country_code, user.first_name, user.last_name, diff --git a/junior/views.py b/junior/views.py index 0658b2c..fa3b57c 100644 --- a/junior/views.py +++ b/junior/views.py @@ -7,6 +7,7 @@ from .serializers import CreateJuniorSerializer, JuniorDetailListSerializer from guardian.models import Guardian from base.messages import ERROR_CODE, SUCCESS_CODE from account.utils import custom_response, custom_error_response +from guardian.utils import upload_image_to_alibaba # Create your views here. class UpdateJuniorProfile(viewsets.ViewSet): """Update junior profile""" @@ -16,10 +17,18 @@ class UpdateJuniorProfile(viewsets.ViewSet): def create(self, request, *args, **kwargs): """Use CreateJuniorSerializer""" - serializer = CreateJuniorSerializer(context={"user":request.user, - "first_name":request.data.get('first_name', ''), - "last_name": request.data.get('last_name',' ')}, - data=request.data) + request_data = request.data + image = request.data.get('image') + image_url = '' + if image: + filename = f"images/{image.name}" + image_url = upload_image_to_alibaba(image, filename) + request_data = {"image": image_url} + serializer = CreateJuniorSerializer(context={"user":request.user, "image":image_url, + "first_name": request.data.get('first_name'), + "last_name": request.data.get('last_name') + }, + data=request_data) if serializer.is_valid(): """save serializer""" serializer.save() From 03fb774a7b41dc51131f634ab2289113d5dc52de Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 18:01:14 +0530 Subject: [PATCH 30/34] jira-25 setting API --- guardian/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/guardian/views.py b/guardian/views.py index 4d9517f..94ff8f4 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -117,6 +117,7 @@ class CreateTaskAPIView(viewsets.ModelViewSet): filename = f"images/{image}" image_url = upload_image_to_alibaba(image, filename) image_data = image_url + data.pop('default_image') serializer = TaskSerializer(context={"user":request.user, "image":image_data}, data=data) if serializer.is_valid(): serializer.save() From 1ae81d41349d3a234a20863794a51af9ec03ae4f Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 19:16:45 +0530 Subject: [PATCH 31/34] jira-25 setting API --- account/admin.py | 17 ++--- account/migrations/0005_usernotification.py | 31 ++++++++ account/models.py | 27 +++++++ account/serializers.py | 84 ++++++++++++++------- account/urls.py | 7 +- account/views.py | 36 +++++++-- base/messages.py | 3 +- guardian/serializers.py | 4 +- guardian/views.py | 3 +- 9 files changed, 164 insertions(+), 48 deletions(-) create mode 100644 account/migrations/0005_usernotification.py diff --git a/account/admin.py b/account/admin.py index 96c2e4d..2614bdb 100644 --- a/account/admin.py +++ b/account/admin.py @@ -2,9 +2,16 @@ from django.contrib import admin """Import django app""" -from .models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages +from .models import UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserNotification # Register your models here. +@admin.register(UserNotification) +class UserNotificationAdmin(admin.ModelAdmin): + """User profile admin""" + list_display = ['user', 'push_notification', 'email_notification', 'sms_notification'] + + def __str__(self): + return self.image_url @admin.register(DefaultTaskImages) class DefaultTaskImagesAdmin(admin.ModelAdmin): """User profile admin""" @@ -13,14 +20,6 @@ class DefaultTaskImagesAdmin(admin.ModelAdmin): def __str__(self): return self.image_url -@admin.register(UserProfile) -class UserProfileAdmin(admin.ModelAdmin): - """User profile admin""" - list_display = ['user'] - - def __str__(self): - return self.user__email - @admin.register(UserEmailOtp) class UserEmailOtpAdmin(admin.ModelAdmin): """User Email otp admin""" diff --git a/account/migrations/0005_usernotification.py b/account/migrations/0005_usernotification.py new file mode 100644 index 0000000..397b633 --- /dev/null +++ b/account/migrations/0005_usernotification.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.2 on 2023-07-10 12:40 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('account', '0004_userdelete'), + ] + + operations = [ + migrations.CreateModel( + name='UserNotification', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('push_notification', models.BooleanField(default=True)), + ('email_notification', models.BooleanField(default=True)), + ('sms_notification', models.BooleanField(default=True)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='user_notification', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'db_table': 'user_notification', + }, + ), + ] diff --git a/account/models.py b/account/models.py index 31c592a..36b2286 100644 --- a/account/models.py +++ b/account/models.py @@ -72,6 +72,8 @@ class UserEmailOtp(models.Model): class Meta(object): """ Meta information """ db_table = 'user_email_otp' + verbose_name = 'User Email OTP' + verbose_name_plural = 'User Email OTP' def __str__(self): """return phone as an object""" @@ -113,3 +115,28 @@ class UserDelete(models.Model): def __str__(self): return self.user.email + + +class UserNotification(models.Model): + """ + User notification details + """ + user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_notification') + """Push Notification""" + push_notification = models.BooleanField(default=True) + """Email Notification""" + email_notification = models.BooleanField(default=True) + """SMS Notification""" + sms_notification = models.BooleanField(default=True) + + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta(object): + """ Meta information """ + db_table = 'user_notification' + verbose_name = 'User Notification' + verbose_name_plural = 'User Notification' + + def __str__(self): + return self.user.email diff --git a/account/serializers.py b/account/serializers.py index 4007e98..e4ad809 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -7,7 +7,7 @@ from rest_framework_simplejwt.tokens import RefreshToken """App import""" from guardian.models import Guardian from junior.models import Junior -from account.models import UserProfile, UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserDelete +from account.models import UserEmailOtp, DefaultTaskImages, UserDelete, UserNotification from base.constants import GUARDIAN, JUNIOR, SUPERUSER from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR @@ -230,36 +230,64 @@ class UserDeleteSerializer(serializers.ModelSerializer): user = self.context['user'] user_type = str(self.context['user_type']) data = validated_data.get('reason') + passwd = self.context['password'] random_num = random.randint(0,10000) user_tb = User.objects.filter(id=user.id).last() - user_type_data = UserEmailOtp.objects.filter(email=user.email).last() - if user_type == '1' and user_type_data.user_type == '1': - junior_data = Junior.objects.filter(auth__email=user_tb.email).first() - if junior_data: - junior_data.is_active = False - junior_data.is_verified = False - junior_data.guardian_code = '{}' - junior_data.save() - elif user_type == '2' and user_type_data.user_type == '2': - guardian_data = Guardian.objects.filter(user__email=user_tb.email).first() - if guardian_data: - guardian_data.is_active = False - guardian_data.is_verified = False - guardian_data.save() - jun_data = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) - for data in jun_data: - data.guardian_code.remove(guardian_data.guardian_code) - data.save() + if user_tb.check_password(passwd): + user_type_data = UserEmailOtp.objects.filter(email=user.email).last() + if user_type == '1' and user_type_data.user_type == '1': + junior_data = Junior.objects.filter(auth__email=user_tb.email).first() + if junior_data: + junior_data.is_active = False + junior_data.is_verified = False + junior_data.guardian_code = '{}' + junior_data.save() + elif user_type == '2' and user_type_data.user_type == '2': + guardian_data = Guardian.objects.filter(user__email=user_tb.email).first() + if guardian_data: + guardian_data.is_active = False + guardian_data.is_verified = False + guardian_data.save() + jun_data = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) + for data in jun_data: + data.guardian_code.remove(guardian_data.guardian_code) + data.save() + else: + raise serializers.ValidationError({"details":ERROR_CODE['2030'],"code":"400", "status":"failed"}) + + user_tb.email = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower() + user_tb.username = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower() + user_tb.password = 'None' + d_email = user_tb.email + o_mail = user.email + instance = UserDelete.objects.create(user=user_tb, d_email=d_email, old_email=o_mail, + is_active=True, reason=data) + user_tb.save() + + return instance else: - raise serializers.ValidationError({"details":ERROR_CODE['2030'],"code":"400", "status":"failed"}) + raise serializers.ValidationError({"details": ERROR_CODE['2031'], "code": "400", "status": "failed"}) - user_tb.email = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower() - user_tb.username = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower() - user_tb.password = 'None' - d_email = user_tb.email - o_mail = user.email - instance = UserDelete.objects.create(user=user_tb, d_email=d_email, old_email=o_mail, - is_active=True, reason=data) - user_tb.save() +class UserNotificationSerializer(serializers.ModelSerializer): + """User Notification serializer""" + class Meta(object): + """Meta info""" + model = UserNotification + fields = '__all__' + + +class UpdateUserNotificationSerializer(serializers.ModelSerializer): + """Update User Notification serializer""" + class Meta(object): + """Meta info""" + model = UserNotification + fields = ['push_notification', 'email_notification', 'sms_notification'] + + def create(self, validated_data): + instance = UserNotification.objects.filter(user=self.context).last() + instance.push_notification = validated_data.get('push_notification',instance.push_notification) + instance.email_notification = validated_data.get('email_notification', instance.email_notification) + instance.sms_notification = validated_data.get('sms_notification', instance.sms_notification) + instance.save() return instance diff --git a/account/urls.py b/account/urls.py index b64f99d..6bfc8dd 100644 --- a/account/urls.py +++ b/account/urls.py @@ -7,7 +7,8 @@ from rest_framework import routers from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp, ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage, GoogleLoginViewSet, SigninWithApple, ProfileAPIViewSet, UploadImageAPIViewSet, - DefaultImageAPIViewSet, DeleteUserProfileAPIViewSet) + DefaultImageAPIViewSet, DeleteUserProfileAPIViewSet, UserNotificationAPIViewSet, + UpdateUserNotificationAPIViewSet) """Router""" router = routers.SimpleRouter() @@ -31,6 +32,10 @@ router.register('upload-default-task-image', UploadImageAPIViewSet, basename='up router.register('default-task-image', DefaultImageAPIViewSet, basename='default-task-image') """Delete user account""" router.register('delete', DeleteUserProfileAPIViewSet, basename='delete') +"""user account notification""" +router.register('user-notification', UserNotificationAPIViewSet, basename='user-notification') +"""update user account notification""" +router.register('update-user-notification', UpdateUserNotificationAPIViewSet, basename='update-user-notification') """Define url pattern""" urlpatterns = [ path('api/v1/', include(router.urls)), diff --git a/account/views.py b/account/views.py index 5657b4a..1ad142d 100644 --- a/account/views.py +++ b/account/views.py @@ -12,13 +12,14 @@ from guardian.utils import upload_image_to_alibaba from django.contrib.auth import authenticate, login from guardian.models import Guardian from junior.models import Junior -from account.models import UserProfile, UserPhoneOtp, UserEmailOtp, DefaultTaskImages +from account.models import UserProfile, UserPhoneOtp, UserEmailOtp, DefaultTaskImages, UserNotification from django.contrib.auth.models import User """Account serializer""" from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer, ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer, - DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer, UserDeleteSerializer) + DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer, UserDeleteSerializer, + UserNotificationSerializer, UpdateUserNotificationSerializer) from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp @@ -425,12 +426,37 @@ class DeleteUserProfileAPIViewSet(viewsets.GenericViewSet): @action(detail=False, methods=['POST'], url_path='user-account',serializer_class=UserDeleteSerializer, permission_classes=[IsAuthenticated]) def account(self, request): - print("request.data===>",request.data) - print("request.user===>", request.user) user_type = str(request.data['user_type']) + password = request.data['password'] serializer = self.get_serializer(data=request.data, context={'request': request, 'user': request.user, - 'user_type':user_type}) + 'user_type':user_type, + 'password':password}) if serializer.is_valid(): serializer.save() return custom_response(SUCCESS_CODE['3005'], response_status=status.HTTP_200_OK) return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) + + +class UserNotificationAPIViewSet(viewsets.ModelViewSet): + """Profile viewset""" + queryset = UserNotification.objects.all() + serializer_class = UserNotificationSerializer + def list(self, request, *args, **kwargs): + """profile view""" + queryset = self.queryset.filter(user=request.user) + serializer = UserNotificationSerializer(queryset, many=True) + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + + +class UpdateUserNotificationAPIViewSet(viewsets.ModelViewSet): + """Profile viewset""" + serializer_class = UpdateUserNotificationSerializer + + def create(self, request, *args, **kwargs): + """profile view""" + serializer = UpdateUserNotificationSerializer(data=request.data, + context=request.user) + if serializer.is_valid(): + serializer.save() + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + return custom_error_response(serializer.error, response_status=status.HTTP_400_BAD_REQUEST) diff --git a/base/messages.py b/base/messages.py index b00bfbf..788c7bc 100644 --- a/base/messages.py +++ b/base/messages.py @@ -53,7 +53,8 @@ ERROR_CODE = { "2027": "data should contain `identityToken`", "2028": "You are not authorized person to sign up on this platform", "2029": "Validity of otp verification is expired", - "2030": "Use correct user type and token" + "2030": "Use correct user type and token", + "2031":"Invalid password" } SUCCESS_CODE = { # Success code for password diff --git a/guardian/serializers.py b/guardian/serializers.py index 14c4a6d..dd79ed7 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -8,7 +8,7 @@ from django.db import transaction from django.contrib.auth.models import User """Import Django app""" from .models import Guardian, JuniorTask -from account.models import UserProfile, UserEmailOtp +from account.models import UserProfile, UserEmailOtp, UserNotification from account.serializers import JuniorSerializer from junior.serializers import JuniorDetailSerializer from base.messages import ERROR_CODE, SUCCESS_CODE @@ -36,7 +36,7 @@ class UserSerializer(serializers.ModelSerializer): try: """Create user profile""" user = User.objects.create_user(username=email, email=email, password=password) - UserProfile.objects.create(user=user, user_type=user_type) + UserNotification.objects.create(user=user) if user_type == '1': Junior.objects.create(auth=user, junior_code=''.join([str(random.randrange(9)) for _ in range(6)]), referral_code=''.join([str(random.randrange(9)) for _ in range(6)])) diff --git a/guardian/views.py b/guardian/views.py index 94ff8f4..e7f10bf 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -12,7 +12,7 @@ from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializ from .models import Guardian, JuniorTask from junior.models import Junior, JuniorPoints from junior.serializers import JuniorDetailSerializer -from account.models import UserEmailOtp +from account.models import UserEmailOtp, UserNotification from .tasks import generate_otp from account.utils import send_otp_email from account.utils import custom_response, custom_error_response @@ -92,7 +92,6 @@ class TaskListAPIView(viewsets.ModelViewSet): def list(self, request, *args, **kwargs): """Create guardian profile""" status_value = self.request.GET.get('status') - print("status_value==>",status_value,'===>',type(status_value)) if str(status_value) == '0': queryset = JuniorTask.objects.filter(guardian__user=request.user).order_by('created_at') else: From c0f20f732dae06848da21a4cd72892f715b42d02 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 10 Jul 2023 21:18:22 +0530 Subject: [PATCH 32/34] swagger points --- account/serializers.py | 10 ++++++++-- account/views.py | 11 +++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index e4ad809..47472c0 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -7,7 +7,7 @@ from rest_framework_simplejwt.tokens import RefreshToken """App import""" from guardian.models import Guardian from junior.models import Junior -from account.models import UserEmailOtp, DefaultTaskImages, UserDelete, UserNotification +from account.models import UserEmailOtp, DefaultTaskImages, UserDelete, UserNotification, UserPhoneOtp from base.constants import GUARDIAN, JUNIOR, SUPERUSER from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR @@ -233,7 +233,7 @@ class UserDeleteSerializer(serializers.ModelSerializer): passwd = self.context['password'] random_num = random.randint(0,10000) user_tb = User.objects.filter(id=user.id).last() - if user_tb.check_password(passwd): + if user_tb and user_tb.check_password(passwd): user_type_data = UserEmailOtp.objects.filter(email=user.email).last() if user_type == '1' and user_type_data.user_type == '1': junior_data = Junior.objects.filter(auth__email=user_tb.email).first() @@ -291,3 +291,9 @@ class UpdateUserNotificationSerializer(serializers.ModelSerializer): instance.sms_notification = validated_data.get('sms_notification', instance.sms_notification) instance.save() return instance + +class UserPhoneOtpSerializer(serializers.ModelSerializer): + """User Phone serializers""" + class Meta(object): + model = UserPhoneOtp + fields = '__all__' diff --git a/account/views.py b/account/views.py index 1ad142d..ded9a74 100644 --- a/account/views.py +++ b/account/views.py @@ -19,7 +19,7 @@ from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSeriali ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer, GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer, DefaultTaskImagesSerializer, DefaultTaskImagesDetailsSerializer, UserDeleteSerializer, - UserNotificationSerializer, UpdateUserNotificationSerializer) + UserNotificationSerializer, UpdateUserNotificationSerializer, UserPhoneOtpSerializer) from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp @@ -221,6 +221,7 @@ class ForgotPasswordAPIView(views.APIView): class SendPhoneOtp(viewsets.ModelViewSet): """Send otp on phone""" queryset = UserPhoneOtp.objects.all() + serializer_class = UserPhoneOtpSerializer def create(self, request, *args, **kwargs): otp = generate_otp() phone_number = self.request.data['phone'] @@ -237,6 +238,7 @@ class SendPhoneOtp(viewsets.ModelViewSet): class UserPhoneVerification(viewsets.ModelViewSet): """Send otp on phone""" queryset = UserPhoneOtp.objects.all() + serializer_class = UserPhoneOtpSerializer def list(self, request, *args, **kwargs): try: phone_data = UserPhoneOtp.objects.filter(phone=self.request.GET.get('phone'), @@ -378,7 +380,7 @@ class ReSendEmailOtp(viewsets.ModelViewSet): class ProfileAPIViewSet(viewsets.ModelViewSet): """Profile viewset""" queryset = User.objects.all() - serializer_class = [JuniorProfileSerializer, GuardianProfileSerializer] + serializer_class = JuniorProfileSerializer def list(self, request, *args, **kwargs): """profile view""" if str(self.request.GET.get('user_type')) == '1': @@ -438,7 +440,7 @@ class DeleteUserProfileAPIViewSet(viewsets.GenericViewSet): class UserNotificationAPIViewSet(viewsets.ModelViewSet): - """Profile viewset""" + """notification viewset""" queryset = UserNotification.objects.all() serializer_class = UserNotificationSerializer def list(self, request, *args, **kwargs): @@ -449,7 +451,8 @@ class UserNotificationAPIViewSet(viewsets.ModelViewSet): class UpdateUserNotificationAPIViewSet(viewsets.ModelViewSet): - """Profile viewset""" + """Update notification viewset""" + queryset = UserNotification.objects.all() serializer_class = UpdateUserNotificationSerializer def create(self, request, *args, **kwargs): From 41761f22e82c2ba9a205450aaa62340f30229cef Mon Sep 17 00:00:00 2001 From: jain Date: Tue, 11 Jul 2023 11:52:07 +0530 Subject: [PATCH 33/34] sonar issue --- account/admin.py | 9 ++++++++- account/serializers.py | 21 ++++----------------- account/utils.py | 21 +++++++++++++++++++++ account/views.py | 2 -- guardian/serializers.py | 4 ---- junior/serializers.py | 5 ----- 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/account/admin.py b/account/admin.py index 2614bdb..e1decdd 100644 --- a/account/admin.py +++ b/account/admin.py @@ -2,9 +2,16 @@ from django.contrib import admin """Import django app""" -from .models import UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserNotification +from .models import UserEmailOtp, UserPhoneOtp, DefaultTaskImages, UserNotification, UserDelete # Register your models here. +@admin.register(UserDelete) +class UserDeleteAdmin(admin.ModelAdmin): + """User profile admin""" + list_display = ['user', 'old_email', 'd_email'] + + def __str__(self): + return self.user @admin.register(UserNotification) class UserNotificationAdmin(admin.ModelAdmin): """User profile admin""" diff --git a/account/serializers.py b/account/serializers.py index 47472c0..21a8d41 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -10,7 +10,7 @@ from junior.models import Junior from account.models import UserEmailOtp, DefaultTaskImages, UserDelete, UserNotification, UserPhoneOtp from base.constants import GUARDIAN, JUNIOR, SUPERUSER from base.messages import ERROR_CODE_REQUIRED, ERROR_CODE, SUCCESS_CODE, STATUS_CODE_ERROR - +from .utils import junior_account_update, guardian_account_update class GoogleLoginSerializer(serializers.Serializer): """google login serializer""" @@ -236,25 +236,11 @@ class UserDeleteSerializer(serializers.ModelSerializer): if user_tb and user_tb.check_password(passwd): user_type_data = UserEmailOtp.objects.filter(email=user.email).last() if user_type == '1' and user_type_data.user_type == '1': - junior_data = Junior.objects.filter(auth__email=user_tb.email).first() - if junior_data: - junior_data.is_active = False - junior_data.is_verified = False - junior_data.guardian_code = '{}' - junior_data.save() + junior_account_update(user_tb) elif user_type == '2' and user_type_data.user_type == '2': - guardian_data = Guardian.objects.filter(user__email=user_tb.email).first() - if guardian_data: - guardian_data.is_active = False - guardian_data.is_verified = False - guardian_data.save() - jun_data = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) - for data in jun_data: - data.guardian_code.remove(guardian_data.guardian_code) - data.save() + guardian_account_update(user_tb) else: raise serializers.ValidationError({"details":ERROR_CODE['2030'],"code":"400", "status":"failed"}) - user_tb.email = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower() user_tb.username = str(random_num) + str('@D_') + '{}'.format(user_tb.username).lower() user_tb.password = 'None' @@ -295,5 +281,6 @@ class UpdateUserNotificationSerializer(serializers.ModelSerializer): class UserPhoneOtpSerializer(serializers.ModelSerializer): """User Phone serializers""" class Meta(object): + """Meta info""" model = UserPhoneOtp fields = '__all__' diff --git a/account/utils.py b/account/utils.py index 4412e62..af9f86b 100644 --- a/account/utils.py +++ b/account/utils.py @@ -10,7 +10,28 @@ from datetime import datetime from calendar import timegm from uuid import uuid4 import secrets +from junior.models import Junior +from guardian.models import Guardian +def junior_account_update(user_tb): + """junior account delete""" + junior_data = Junior.objects.filter(auth__email=user_tb.email).first() + if junior_data: + junior_data.is_active = False + junior_data.is_verified = False + junior_data.guardian_code = '{}' + junior_data.save() +def guardian_account_update(user_tb): + """update guardian account after delete the user account""" + guardian_data = Guardian.objects.filter(user__email=user_tb.email).first() + if guardian_data: + guardian_data.is_active = False + guardian_data.is_verified = False + guardian_data.save() + jun_data = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) + for data in jun_data: + data.guardian_code.remove(guardian_data.guardian_code) + data.save() def send_otp_email(recipient_email, otp): """Send otp on email with template""" from_email = settings.EMAIL_FROM_ADDRESS diff --git a/account/views.py b/account/views.py index ded9a74..b5c0d26 100644 --- a/account/views.py +++ b/account/views.py @@ -109,8 +109,6 @@ class SigninWithApple(views.APIView): def post(self, request): token = request.data.get("access_token") user_type = request.data.get("user_type") - if not token: - return custom_error_response(ERROR_CODE['2027'], response_status=status.HTTP_400_BAD_REQUEST) try: decoded_data = jwt.decode(token, options={"verify_signature": False}) user_data = {"email": decoded_data.get('email'), "username": decoded_data.get('email'), "is_active": True} diff --git a/guardian/serializers.py b/guardian/serializers.py index dd79ed7..2c1c3dc 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -95,12 +95,8 @@ class CreateGuardianSerializer(serializers.ModelSerializer): """Save first and last name of guardian""" if self.context.get('first_name') != '' and self.context.get('first_name') is not None: user.first_name = self.context.get('first_name') - else: - user.first_name = user.first_name if self.context.get('last_name') != '' and self.context.get('last_name') is not None: user.last_name = self.context.get('last_name') - else: - user.last_name = user.last_name user.save() """Create guardian data""" guardian, created = Guardian.objects.get_or_create(user=self.context['user']) diff --git a/junior/serializers.py b/junior/serializers.py index 28bbdc4..df7a40a 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -59,19 +59,14 @@ class CreateJuniorSerializer(serializers.ModelSerializer): def create(self, validated_data): """Create junior profile""" - image = validated_data.get('image', None) guardian_code = validated_data.get('guardian_code',None) user = User.objects.filter(username=self.context['user']).last() if user: """Save first and last name of junior""" if self.context.get('first_name') != '' and self.context.get('first_name') is not None: user.first_name = self.context.get('first_name') - else: - user.first_name = user.first_name if self.context.get('last_name') != '' and self.context.get('last_name') is not None: user.last_name = self.context.get('last_name') - else: - user.last_name = user.last_name user.save() """Create junior data""" junior, created = Junior.objects.get_or_create(auth=self.context['user']) From 8b4db3e2e931e2ce68aaa3e569cdc1c3683cb401 Mon Sep 17 00:00:00 2001 From: jain Date: Tue, 11 Jul 2023 13:14:20 +0530 Subject: [PATCH 34/34] jira-26 changes in notification API --- account/serializers.py | 11 +++++++---- guardian/views.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index 21a8d41..82b957e 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -272,10 +272,13 @@ class UpdateUserNotificationSerializer(serializers.ModelSerializer): def create(self, validated_data): instance = UserNotification.objects.filter(user=self.context).last() - instance.push_notification = validated_data.get('push_notification',instance.push_notification) - instance.email_notification = validated_data.get('email_notification', instance.email_notification) - instance.sms_notification = validated_data.get('sms_notification', instance.sms_notification) - instance.save() + if instance: + instance.push_notification = validated_data.get('push_notification',instance.push_notification) + instance.email_notification = validated_data.get('email_notification', instance.email_notification) + instance.sms_notification = validated_data.get('sms_notification', instance.sms_notification) + instance.save() + else: + instance = UserNotification.objects.create(user=self.context) return instance class UserPhoneOtpSerializer(serializers.ModelSerializer): diff --git a/guardian/views.py b/guardian/views.py index e7f10bf..f55dd47 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -151,7 +151,7 @@ class SearchTaskListAPIView(viewsets.ModelViewSet): class TopJuniorListAPIView(viewsets.ModelViewSet): """Top juniors list""" serializer_class = TopJuniorSerializer - # permission_classes = [IsAuthenticated] + permission_classes = [IsAuthenticated] queryset = JuniorPoints.objects.all() def get_serializer_context(self):