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)