From 5ee01af82552f825bcb953467f7f7f2cae66aa78 Mon Sep 17 00:00:00 2001 From: abutalib-kiwi Date: Fri, 21 Jul 2023 11:35:58 +0530 Subject: [PATCH 1/7] article api changes --- notifications/utils.py | 2 -- web_admin/views.py | 48 ++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/notifications/utils.py b/notifications/utils.py index 26d6778..b5114c2 100644 --- a/notifications/utils.py +++ b/notifications/utils.py @@ -51,7 +51,6 @@ def get_basic_detail(notification_type, from_user_id, to_user_id): def send_notification(notification_type, from_user_id, to_user_id, extra_data): """ used to send the push for the given notification type """ (notification_data, from_user, to_user) = get_basic_detail(notification_type, from_user_id, to_user_id) - print(notification_data, to_user) user_notification_type = UserNotification.objects.filter(user=to_user).first() # data = notification_data.data data = notification_data @@ -64,7 +63,6 @@ def send_notification(notification_type, from_user_id, to_user_id, extra_data): def send_push(user, data): """ used to send push notification to specific user """ - # if user.push_notification: notification_data = data.pop('data', None) user.fcmdevice_set.filter(active=True).send_message( Message(notification=FirebaseNotification(data['title'], data['body']), data=notification_data) diff --git a/web_admin/views.py b/web_admin/views.py index 3918f1e..b387a97 100644 --- a/web_admin/views.py +++ b/web_admin/views.py @@ -4,6 +4,7 @@ web_admin views file # django imports from rest_framework.viewsets import GenericViewSet, mixins from rest_framework.response import Response +from rest_framework.filters import OrderingFilter, SearchFilter from rest_framework import status from rest_framework.decorators import action from rest_framework.permissions import IsAuthenticated @@ -22,13 +23,19 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel article api """ serializer_class = ArticleSerializer - # permission_classes = [IsAuthenticated, AdminPermission] - queryset = Article.objects.prefetch_related('article_cards', - 'article_survey', - 'article_survey__survey_options').order_by('-created_at') - + permission_classes = [IsAuthenticated, AdminPermission] + queryset = Article + filter_backends = (OrderingFilter, SearchFilter,) + search_fields = ['title'] http_method_names = ['get', 'post', 'put', 'delete'] + def get_queryset(self): + article = self.queryset.objects.filter(is_deleted=False).prefetch_related( + 'article_cards', 'article_survey', 'article_survey__survey_options' + ).order_by('-created_at') + queryset = self.filter_queryset(article) + return queryset + def create(self, request, *args, **kwargs): """ article create api method @@ -50,7 +57,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel :param kwargs: :return: success message """ - article = self.queryset.filter(id=kwargs['pk']).first() + article = self.get_object() serializer = self.serializer_class(article, data=request.data) serializer.is_valid(raise_exception=True) serializer.save() @@ -64,7 +71,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel :param kwargs: :return: list of article """ - queryset = self.queryset.filter(is_deleted=False) + queryset = self.get_queryset() paginator = self.pagination_class() paginated_queryset = paginator.paginate_queryset(queryset, request) serializer = self.serializer_class(paginated_queryset, many=True) @@ -78,8 +85,8 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel :param kwargs: :return: article detail data """ - queryset = self.queryset.filter(id=kwargs['pk'], is_deleted=False) - serializer = self.serializer_class(queryset, many=True) + queryset = self.get_object() + serializer = self.serializer_class(queryset, many=False) return custom_response(None, data=serializer.data) def destroy(self, request, *args, **kwargs): @@ -90,25 +97,13 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel :param kwargs: :return: success message """ - article = self.queryset.filter(id=kwargs['pk']).update(is_deleted=True) + article = self.get_object() + article.is_deleted = True + article.save() if article: return custom_response(SUCCESS_CODE["3029"]) return custom_error_response(ERROR_CODE["2041"], status.HTTP_400_BAD_REQUEST) - @action(methods=['get'], url_name='', url_path='', detail=False) - def search_article(self, request): - """ - article search api method - :param request: - :return: searched article - """ - search = request.GET.get('search') - queryset = self.queryset.filter(title__icontains=search) - paginator = self.pagination_class() - paginated_queryset = paginator.paginate_queryset(queryset, request) - serializer = self.serializer_class(paginated_queryset, many=True) - return custom_response(None, data=serializer.data) - @action(methods=['get'], url_name='remove_card', url_path='remove_card', detail=True, serializer_class=None) def remove_article_card(self, request, *args, **kwargs): @@ -144,7 +139,10 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel @action(methods=['post'], url_name='test-add-card', url_path='test-add-card', detail=False, serializer_class=ArticleCardSerializer) def add_card(self, request): - print(request.data) + """ + :param request: + :return: + """ serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() From fdd0b88b11dfb9b76861c4e0b94faf1c27420797 Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 21 Jul 2023 14:48:18 +0530 Subject: [PATCH 2/7] add print in junior list --- junior/views.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/junior/views.py b/junior/views.py index 592326a..c951cb5 100644 --- a/junior/views.py +++ b/junior/views.py @@ -97,10 +97,18 @@ class JuniorListAPIView(viewsets.ModelViewSet): def list(self, request, *args, **kwargs): """ junior list""" + print("self.request.META====>",self.request.META) + print() auth_token = self.request.META['HTTP_AUTHORIZATION'] headers_token = { 'Authorization': auth_token } + print("auth_token====>", auth_token) + print("headers_token====>", headers_token) + print("os.getenv('BASE_URL')===>", os.getenv('BASE_URL')) + url = requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers_token) + print("url data====>",url) + requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers_token) guardian_data = Guardian.objects.filter(user__email=request.user).last() # fetch junior object From 8013767d71b7d83b956f9e4fb951c14034c62ee3 Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 21 Jul 2023 14:49:38 +0530 Subject: [PATCH 3/7] add print in junior list --- junior/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/junior/views.py b/junior/views.py index c951cb5..5de49dc 100644 --- a/junior/views.py +++ b/junior/views.py @@ -106,6 +106,7 @@ class JuniorListAPIView(viewsets.ModelViewSet): print("auth_token====>", auth_token) print("headers_token====>", headers_token) print("os.getenv('BASE_URL')===>", os.getenv('BASE_URL')) + print("url====>", os.getenv('BASE_URL') + '/api/v1/top-junior/') url = requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers_token) print("url data====>",url) From 2a225ea79df7619d4501455c6fde1165bd45ae53 Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 21 Jul 2023 16:19:30 +0530 Subject: [PATCH 4/7] add dev url for top junior api --- junior/views.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/junior/views.py b/junior/views.py index 5de49dc..42b1690 100644 --- a/junior/views.py +++ b/junior/views.py @@ -107,10 +107,10 @@ class JuniorListAPIView(viewsets.ModelViewSet): print("headers_token====>", headers_token) print("os.getenv('BASE_URL')===>", os.getenv('BASE_URL')) print("url====>", os.getenv('BASE_URL') + '/api/v1/top-junior/') - url = requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers_token) - print("url data====>",url) + # url = requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers_token) + # print("url data====>",url) - requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers_token) + requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers_token) guardian_data = Guardian.objects.filter(user__email=request.user).last() # fetch junior object queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) @@ -294,7 +294,8 @@ class JuniorPointsListAPIView(viewsets.ModelViewSet): headers = { 'Authorization': token } - requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers) + # requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers) + requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers) serializer = JuniorPointsSerializer(queryset) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) From 0a5eaa233fc31975fa046a7a7ad53b11080d25af Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 21 Jul 2023 17:33:21 +0530 Subject: [PATCH 5/7] add qa top -junior url --- junior/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/junior/views.py b/junior/views.py index 42b1690..9217a0d 100644 --- a/junior/views.py +++ b/junior/views.py @@ -110,7 +110,7 @@ class JuniorListAPIView(viewsets.ModelViewSet): # url = requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers_token) # print("url data====>",url) - requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers_token) + # requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers_token) guardian_data = Guardian.objects.filter(user__email=request.user).last() # fetch junior object queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) @@ -295,7 +295,7 @@ class JuniorPointsListAPIView(viewsets.ModelViewSet): 'Authorization': token } # requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers) - requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers) + # requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers) serializer = JuniorPointsSerializer(queryset) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) From d86f7d3436aeaf74bab3ea2c43afb3a77f7caf57 Mon Sep 17 00:00:00 2001 From: jain Date: Fri, 21 Jul 2023 18:38:16 +0530 Subject: [PATCH 6/7] guardian code --- base/messages.py | 3 ++- junior/views.py | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/base/messages.py b/base/messages.py index 998a5b2..38c345d 100644 --- a/base/messages.py +++ b/base/messages.py @@ -74,7 +74,8 @@ ERROR_CODE = { "2041": "Article with given id doesn't exist.", "2042": "Article Card with given id doesn't exist.", "2043": "Article Survey with given id doesn't exist.", - "2044": "Task does not exist" + "2044": "Task does not exist", + "2045": "Invalid guardian" } """Success message code""" SUCCESS_CODE = { diff --git a/junior/views.py b/junior/views.py index 9217a0d..d67dfde 100644 --- a/junior/views.py +++ b/junior/views.py @@ -113,10 +113,12 @@ class JuniorListAPIView(viewsets.ModelViewSet): # requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers_token) guardian_data = Guardian.objects.filter(user__email=request.user).last() # fetch junior object - queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) - # use JuniorDetailListSerializer serializer - serializer = JuniorDetailListSerializer(queryset, many=True) - return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + if guardian_data: + queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code)) + # use JuniorDetailListSerializer serializer + serializer = JuniorDetailListSerializer(queryset, many=True) + return custom_response(None, serializer.data, response_status=status.HTTP_200_OK) + return custom_error_response(ERROR_CODE['2045'], response_status=status.HTTP_200_OK) class AddJuniorAPIView(viewsets.ModelViewSet): """Add Junior by guardian""" From a2b4f3b75864103d69a0860a267a39e02985192a Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 24 Jul 2023 11:00:22 +0530 Subject: [PATCH 7/7] update position of the junior --- junior/utils.py | 14 +++++++++++++- junior/views.py | 27 +++++---------------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/junior/utils.py b/junior/utils.py index e1fb5df..418f3c2 100644 --- a/junior/utils.py +++ b/junior/utils.py @@ -3,7 +3,8 @@ from django.conf import settings """Third party Django app""" from templated_email import send_templated_mail - +from .models import JuniorPoints +from django.db.models import F # junior notification # email for sending email # when guardian create junior profile @@ -45,3 +46,14 @@ def junior_approval_mail(guardian, full_name): } ) return full_name + +def update_positions_based_on_points(): + # First, retrieve all the JuniorPoints instances ordered by total_points in descending order. + juniors_points = JuniorPoints.objects.order_by('-total_points') + + # Now, iterate through the queryset and update the position field based on the order. + position = 1 + for junior_point in juniors_points: + junior_point.position = position + junior_point.save() + position += 1 diff --git a/junior/views.py b/junior/views.py index d67dfde..a764adf 100644 --- a/junior/views.py +++ b/junior/views.py @@ -19,7 +19,7 @@ import requests # Import account's serializer # Import account's task # import junior serializer -from junior.models import Junior +from junior.models import Junior, JuniorPoints from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, AddJuniorSerializer,\ RemoveJuniorSerializer, CompleteTaskSerializer, JuniorPointsSerializer) from guardian.models import Guardian, JuniorTask @@ -28,6 +28,7 @@ from base.messages import ERROR_CODE, SUCCESS_CODE from base.constants import NUMBER 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 """ Define APIs """ # Define validate guardian code API, @@ -97,20 +98,7 @@ class JuniorListAPIView(viewsets.ModelViewSet): def list(self, request, *args, **kwargs): """ junior list""" - print("self.request.META====>",self.request.META) - print() - auth_token = self.request.META['HTTP_AUTHORIZATION'] - headers_token = { - 'Authorization': auth_token - } - print("auth_token====>", auth_token) - print("headers_token====>", headers_token) - print("os.getenv('BASE_URL')===>", os.getenv('BASE_URL')) - print("url====>", os.getenv('BASE_URL') + '/api/v1/top-junior/') - # url = requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers_token) - # print("url data====>",url) - - # requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers_token) + update_positions_based_on_points() guardian_data = Guardian.objects.filter(user__email=request.user).last() # fetch junior object if guardian_data: @@ -149,7 +137,7 @@ class InvitedJuniorAPIView(viewsets.ModelViewSet): """Get the queryset for the view""" guardian = Guardian.objects.filter(user__email=self.request.user).last() junior_queryset = Junior.objects.filter(guardian_code__icontains=str(guardian.guardian_code), - is_invited=True) + is_invited=True) return junior_queryset def list(self, request, *args, **kwargs): """ junior list""" @@ -292,12 +280,7 @@ class JuniorPointsListAPIView(viewsets.ModelViewSet): """profile view""" queryset = self.get_queryset() # update position of junior - token = self.request.META['HTTP_AUTHORIZATION'] - headers = { - 'Authorization': token - } - # requests.get(os.getenv('BASE_URL') + '/api/v1/top-junior/', headers=headers) - # requests.get('https://dev-api.zodqaapp.com/api/v1/top-junior/', headers=headers) + update_positions_based_on_points() serializer = JuniorPointsSerializer(queryset) return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)