update position of the junior

This commit is contained in:
jain
2023-07-24 11:00:22 +05:30
parent d86f7d3436
commit a2b4f3b758
2 changed files with 18 additions and 23 deletions

View File

@ -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

View File

@ -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:
@ -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)