From 7b75a3233ca657a7cd4489f969df64f0a9d4867c Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 4 Sep 2023 12:52:04 +0530 Subject: [PATCH 1/3] add current page and total page in task list API --- account/utils.py | 5 +++-- guardian/views.py | 7 ++++++- junior/views.py | 7 ++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/account/utils.py b/account/utils.py index 60a5c44..f77e8e5 100644 --- a/account/utils.py +++ b/account/utils.py @@ -186,12 +186,13 @@ def send_support_email(name, sender, subject, message): return name -def custom_response(detail, data=None, response_status=status.HTTP_200_OK, count=None): +def custom_response(detail, data=None, total_pages=None, current_page=None, response_status=status.HTTP_200_OK, count=None): """Custom response code""" if not data: """when data is none""" data = None - return Response({"data": data, "message": detail, "status": "success", "code": response_status, "count": count}) + return Response({"data": data, "message": detail, "total_pages":total_pages, "current_page":current_page, + "status": "success", "code": response_status, "count": count}) def custom_error_response(detail, response_status): diff --git a/guardian/views.py b/guardian/views.py index b212faf..e115065 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -1,4 +1,5 @@ """Views of Guardian""" +import math # django imports # Import IsAuthenticated @@ -154,15 +155,19 @@ class TaskListAPIView(viewsets.ModelViewSet): def list(self, request, *args, **kwargs): """Create guardian profile""" status_value = self.request.GET.get('status') + current_page = self.request.GET.get('page') queryset = self.get_queryset() if status_value and status_value != '0': queryset = queryset.filter(task_status=status_value) paginator = self.pagination_class() + total_count = len(queryset) + total_pages = math.ceil(total_count/10) # use Pagination paginated_queryset = paginator.paginate_queryset(queryset, request) # use TaskDetailsSerializer serializer serializer = self.serializer_class(paginated_queryset, many=True) - return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + return custom_response(None, serializer.data, total_pages=total_pages, current_page=current_page, + response_status=status.HTTP_200_OK) class CreateTaskAPIView(viewsets.ModelViewSet): diff --git a/junior/views.py b/junior/views.py index 3e9eeed..1d2cac8 100644 --- a/junior/views.py +++ b/junior/views.py @@ -12,6 +12,7 @@ import datetime import requests from rest_framework.viewsets import GenericViewSet, mixins +import math """Django app import""" from drf_yasg.utils import swagger_auto_schema from drf_yasg import openapi @@ -366,15 +367,19 @@ class JuniorTaskListAPIView(viewsets.ModelViewSet): page=1""" try: status_value = self.request.GET.get('status') + current_page = self.request.GET.get('page') queryset = self.get_queryset() if status_value and status_value != '0': queryset = queryset.filter(task_status=status_value) paginator = self.pagination_class() + total_count = len(queryset) + total_pages = math.ceil(total_count / 10) # use Pagination paginated_queryset = paginator.paginate_queryset(queryset, request) # use TaskDetails juniorSerializer serializer serializer = self.serializer_class(paginated_queryset, many=True) - return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + return custom_response(None, serializer.data, total_pages=total_pages, current_page=current_page, + response_status=status.HTTP_200_OK) except Exception as e: return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST) From 20fa6e43dadcd7f4640aa58a39595604bd5bf94f Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 4 Sep 2023 15:02:47 +0530 Subject: [PATCH 2/3] changes in task list API and add special character in password for added junior --- account/utils.py | 26 +++++++++++++++++++++++++- guardian/views.py | 15 ++++++++++++--- junior/serializers.py | 8 ++++---- junior/views.py | 9 +++++++-- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/account/utils.py b/account/utils.py index f77e8e5..63dfc17 100644 --- a/account/utils.py +++ b/account/utils.py @@ -1,6 +1,6 @@ """Account utils""" from celery import shared_task - +import random """Import django""" from django.conf import settings from rest_framework import viewsets, status @@ -289,3 +289,27 @@ def get_user_full_name(user_obj): to get user's full name """ return f"{user_obj.first_name} {user_obj.last_name}" if user_obj.first_name or user_obj.last_name else "User" + +def make_special_password(length=10): + # Define character sets + lowercase_letters = string.ascii_lowercase + uppercase_letters = string.ascii_uppercase + digits = string.digits + special_characters = '!@#$%^&*()_-+=<>?/[]{}|' + + # Combine character sets + all_characters = lowercase_letters + uppercase_letters + digits + special_characters + + # Create a password with random characters + password = ( + random.choice(lowercase_letters) + + random.choice(uppercase_letters) + + random.choice(digits) + + random.choice(special_characters) + + ''.join(random.choice(all_characters) for _ in range(length - 4)) + ) + + # Shuffle the characters to make it more random + password_list = list(password) + random.shuffle(password_list) + return ''.join(password_list) diff --git a/guardian/views.py b/guardian/views.py index e115065..66d65e6 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -136,7 +136,8 @@ class TaskListAPIView(viewsets.ModelViewSet): Params status search - page""" + page + junior""" serializer_class = TaskDetailsSerializer permission_classes = [IsAuthenticated] filter_backends = (SearchFilter,) @@ -156,9 +157,17 @@ class TaskListAPIView(viewsets.ModelViewSet): """Create guardian profile""" status_value = self.request.GET.get('status') current_page = self.request.GET.get('page') + junior = self.request.GET.get('junior') queryset = self.get_queryset() - if status_value and status_value != '0': - queryset = queryset.filter(task_status=status_value) + task_status = ['1'] + if str(status_value) == '2': + task_status = ['2', '4'] + elif str(status_value) == '3': + task_status = ['3', '5', '6'] + if status_value and not junior: + queryset = queryset.filter(task_status__in=task_status) + elif status_value and junior: + queryset = queryset.filter(task_status__in=task_status,junior=int(junior)) paginator = self.pagination_class() total_count = len(queryset) total_pages = math.ceil(total_count/10) diff --git a/junior/serializers.py b/junior/serializers.py index de5f671..aba9b12 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -11,7 +11,7 @@ from django.utils import timezone from rest_framework_simplejwt.tokens import RefreshToken # local imports -from account.utils import send_otp_email, generate_code +from account.utils import send_otp_email, generate_code, make_special_password from junior.models import Junior, JuniorPoints, JuniorGuardianRelationship, JuniorArticlePoints, FAQ from guardian.tasks import generate_otp from base.messages import ERROR_CODE, SUCCESS_CODE @@ -297,8 +297,8 @@ class AddJuniorSerializer(serializers.ModelSerializer): user_data = User.objects.create(username=email, email=email, first_name=self.context['first_name'], last_name=self.context['last_name']) - password = User.objects.make_random_password() - user_data.set_password(password) + special_password = make_special_password() + user_data.set_password(special_password) user_data.save() junior_data = Junior.objects.create(auth=user_data, gender=validated_data.get('gender'), image=profile_image, @@ -321,7 +321,7 @@ class AddJuniorSerializer(serializers.ModelSerializer): # add push notification UserNotification.objects.get_or_create(user=user_data) """Notification email""" - junior_notification_email.delay(email, full_name, email, password) + junior_notification_email.delay(email, full_name, email, special_password) # push notification send_notification.delay(ASSOCIATE_JUNIOR, None, None, junior_data.auth.id, {}) return junior_data diff --git a/junior/views.py b/junior/views.py index 1d2cac8..8d1390c 100644 --- a/junior/views.py +++ b/junior/views.py @@ -369,8 +369,13 @@ class JuniorTaskListAPIView(viewsets.ModelViewSet): status_value = self.request.GET.get('status') current_page = self.request.GET.get('page') queryset = self.get_queryset() - if status_value and status_value != '0': - queryset = queryset.filter(task_status=status_value) + task_status = ['1'] + if str(status_value) == '2': + task_status = ['2', '4'] + elif str(status_value) == '3': + task_status = ['3', '5', '6'] + if status_value: + queryset = queryset.filter(task_status__in=task_status) paginator = self.pagination_class() total_count = len(queryset) total_pages = math.ceil(total_count / 10) From 116fb003588f18f36f2f1896c14b5f4add75b27e Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 4 Sep 2023 15:22:15 +0530 Subject: [PATCH 3/3] optimized task status code --- account/utils.py | 9 +++++++++ guardian/views.py | 8 ++------ junior/views.py | 8 ++------ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/account/utils.py b/account/utils.py index 63dfc17..353042a 100644 --- a/account/utils.py +++ b/account/utils.py @@ -313,3 +313,12 @@ def make_special_password(length=10): password_list = list(password) random.shuffle(password_list) return ''.join(password_list) + +def task_status_fun(status_value): + """task status""" + task_status_value = ['1'] + if str(status_value) == '2': + task_status_value = ['2', '4'] + elif str(status_value) == '3': + task_status_value = ['3', '5', '6'] + return task_status_value diff --git a/guardian/views.py b/guardian/views.py index 66d65e6..082a9d7 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -37,7 +37,7 @@ from .models import Guardian, JuniorTask from junior.models import Junior, JuniorPoints, JuniorGuardianRelationship from account.models import UserEmailOtp, UserNotification, UserDeviceDetails from .tasks import generate_otp -from account.utils import custom_response, custom_error_response, OTP_EXPIRY, send_otp_email +from account.utils import custom_response, custom_error_response, send_otp_email, task_status_fun from base.messages import ERROR_CODE, SUCCESS_CODE from base.constants import NUMBER, GUARDIAN_CODE_STATUS, GUARDIAN from .utils import upload_image_to_alibaba @@ -159,11 +159,7 @@ class TaskListAPIView(viewsets.ModelViewSet): current_page = self.request.GET.get('page') junior = self.request.GET.get('junior') queryset = self.get_queryset() - task_status = ['1'] - if str(status_value) == '2': - task_status = ['2', '4'] - elif str(status_value) == '3': - task_status = ['3', '5', '6'] + task_status = task_status_fun(status_value) if status_value and not junior: queryset = queryset.filter(task_status__in=task_status) elif status_value and junior: diff --git a/junior/views.py b/junior/views.py index 8d1390c..fd917d2 100644 --- a/junior/views.py +++ b/junior/views.py @@ -44,7 +44,7 @@ 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, none, GUARDIAN -from account.utils import custom_response, custom_error_response +from account.utils import custom_response, custom_error_response, task_status_fun from guardian.utils import upload_image_to_alibaba from .utils import update_positions_based_on_points from notifications.utils import send_notification @@ -369,11 +369,7 @@ class JuniorTaskListAPIView(viewsets.ModelViewSet): status_value = self.request.GET.get('status') current_page = self.request.GET.get('page') queryset = self.get_queryset() - task_status = ['1'] - if str(status_value) == '2': - task_status = ['2', '4'] - elif str(status_value) == '3': - task_status = ['3', '5', '6'] + task_status = task_status_fun(status_value) if status_value: queryset = queryset.filter(task_status__in=task_status) paginator = self.pagination_class()