From 1adf0c2f703f5f31f2566fe23267bb70b3b6b6e9 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 21 Aug 2023 12:24:01 +0530 Subject: [PATCH] resolve bugs --- base/constants.py | 2 +- base/messages.py | 6 +++++- guardian/views.py | 6 +++++- junior/views.py | 19 +++++++++++++++---- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/base/constants.py b/base/constants.py index 9688c0f..d376971 100644 --- a/base/constants.py +++ b/base/constants.py @@ -27,7 +27,7 @@ NUMBER = { 'ninety_nine': 99, 'hundred': 100, 'thirty_six_hundred': 3600 } - +none = "none" # Super Admin string constant for 'role' SUPER_ADMIN = "Super Admin" diff --git a/base/messages.py b/base/messages.py index cbcd559..d87b48e 100644 --- a/base/messages.py +++ b/base/messages.py @@ -101,7 +101,11 @@ ERROR_CODE = { "2072": "You can not approve or reject this task because junior does not exist in the system", "2073": "You can not approve or reject this junior because junior does not exist in the system", "2074": "You can not complete this task because you does not exist in the system", - "2075": "Your account is deactivated. Please contact with admin" + "2075": "Your account is deactivated. Please contact with admin", + "2076": "This junior already associate with you", + "2077": "You can not add guardian", + "2078": "This junior is not associate with you", + } """Success message code""" SUCCESS_CODE = { diff --git a/guardian/views.py b/guardian/views.py index 9b9a0aa..37b39cc 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -164,6 +164,10 @@ class CreateTaskAPIView(viewsets.ModelViewSet): try: image = request.data['default_image'] junior = request.data['junior'] + junior_id = Junior.objects.filter(id=junior).last() + guardian_data = Guardian.objects.filter(user=request.user).last() + if guardian_data.guardian_code in junior_id.guardian_code: + return custom_error_response(ERROR_CODE['2078'], response_status=status.HTTP_400_BAD_REQUEST) allowed_extensions = ['.jpg', '.jpeg', '.png'] if not any(extension in str(image) for extension in allowed_extensions): return custom_error_response(ERROR_CODE['2048'], response_status=status.HTTP_400_BAD_REQUEST) @@ -185,7 +189,7 @@ class CreateTaskAPIView(viewsets.ModelViewSet): if serializer.is_valid(): # save serializer task = serializer.save() - junior_id = Junior.objects.filter(id=junior).last() + send_notification_to_junior.delay(TASK_ASSIGNED, request.auth.payload['user_id'], junior_id.auth.id, {'task_id': task.id}) return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK) diff --git a/junior/views.py b/junior/views.py index 1fdf2be..8df8a09 100644 --- a/junior/views.py +++ b/junior/views.py @@ -40,7 +40,7 @@ from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, Ad from guardian.models import Guardian, JuniorTask from guardian.serializers import TaskDetailsSerializer, TaskDetailsjuniorSerializer from base.messages import ERROR_CODE, SUCCESS_CODE -from base.constants import NUMBER, ARTICLE_STATUS +from base.constants import NUMBER, ARTICLE_STATUS, none from account.utils import custom_response, custom_error_response from guardian.utils import upload_image_to_alibaba from .utils import update_positions_based_on_points @@ -171,7 +171,11 @@ class AddJuniorAPIView(viewsets.ModelViewSet): image_url = upload_image_to_alibaba(profile_image, filename) info_data.update({"image": image_url}) if user := User.objects.filter(username=request.data['email']).first(): - self.associate_guardian(user) + data = self.associate_guardian(user) + if data == none: + return custom_error_response(ERROR_CODE['2077'], response_status=status.HTTP_400_BAD_REQUEST) + elif not data: + return custom_error_response(ERROR_CODE['2076'], response_status=status.HTTP_400_BAD_REQUEST) return custom_response(SUCCESS_CODE['3021'], response_status=status.HTTP_200_OK) # use AddJuniorSerializer serializer serializer = AddJuniorSerializer(data=request.data, context=info_data) @@ -184,9 +188,16 @@ class AddJuniorAPIView(viewsets.ModelViewSet): return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST) def associate_guardian(self, user): - junior = Junior.objects.filter(auth=user).first() + junior = Junior.objects.filter(auth__email=self.request.data['email']).first() guardian = Guardian.objects.filter(user=self.request.user).first() - junior.guardian_code = [guardian.guardian_code] + if not junior: + return none + if guardian.guardian_code in junior.guardian_code: + return False + if type(junior.guardian_code) is list: + junior.guardian_code.append(guardian.guardian_code) + else: + junior.guardian_code = [guardian.guardian_code] junior.guardian_code_status = str(NUMBER['two']) junior.save() JuniorGuardianRelationship.objects.get_or_create(guardian=guardian, junior=junior,