From 9e95da5e031a99d2be04083662dd67ca20d8931c Mon Sep 17 00:00:00 2001 From: uttamthummala Date: Fri, 20 Oct 2023 19:33:13 +0530 Subject: [PATCH] Fixed error in timezone --- CDC_Backend/APIs/adminViews.py | 8 ++++++-- CDC_Backend/APIs/studentViews.py | 4 ++-- CDC_Backend/APIs/tests/test_adminView.py | 6 +++--- CDC_Backend/APIs/utils.py | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CDC_Backend/APIs/adminViews.py b/CDC_Backend/APIs/adminViews.py index d04a980..2f4a4b6 100644 --- a/CDC_Backend/APIs/adminViews.py +++ b/CDC_Backend/APIs/adminViews.py @@ -4,6 +4,7 @@ from rest_framework.decorators import api_view from .serializers import * from .utils import * +import pytz @api_view(['POST']) @@ -124,7 +125,10 @@ def updateDeadline(request, id, email, user_type): opening.deadline_datetime = datetime.datetime.strptime(data[DEADLINE_DATETIME], '%Y-%m-%d %H:%M:%S %z') opening.changed_by = get_object_or_404(User, id=id) opening.save() - deadline=opening.deadline_datetime.strftime('%Y-%m-%d %H:%M:%S') + deadline=opening.deadline_datetime + itz=pytz.timezone('Asia/Kolkata') + deadline=deadline.astimezone(itz) + deadline=deadline.strftime('%Y-%m-%d %H:%M:%S') send_opening_to_notifications_service(id=opening.id,name=opening.company_name,deadline=deadline,role=opening.designation) return Response({'action': "Update Deadline", 'message': "Deadline Updated"}, status=status.HTTP_200_OK) @@ -151,7 +155,7 @@ def updateOfferAccepted(request, id, email, user_type): if DEADLINE_DATETIME in data: deadline_datetime = datetime.datetime.strptime(data[DEADLINE_DATETIME], '%Y-%m-%d %H:%M:%S %z') else: - deadline_datetime = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) + datetime.timedelta(days=2) + deadline_datetime = timezone.localtime(timezone.now()).replace(hour=0, minute=0, second=0, microsecond=0) + datetime.timedelta(days=2) if opening_type == "Internship": opening = get_object_or_404(Internship, pk=data[OPENING_ID]) else: diff --git a/CDC_Backend/APIs/studentViews.py b/CDC_Backend/APIs/studentViews.py index f373ca8..a324afc 100644 --- a/CDC_Backend/APIs/studentViews.py +++ b/CDC_Backend/APIs/studentViews.py @@ -109,7 +109,7 @@ def getDashboard(request, id, email, user_type): placementsdata = PlacementSerializerForStudent(filtered_placements, many=True).data - placementApplications = PlacementApplication.objects.filter(student_id=id) + placementApplications = PlacementApplication.objects.filter(student_id=id).order_by('-updated_at') placementApplications = PlacementApplicationSerializer(placementApplications, many=True).data internships = Internship.objects.filter(allowed_batch__contains=[studentDetails.batch], allowed_branch__contains=[studentDetails.branch], @@ -119,7 +119,7 @@ def getDashboard(request, id, email, user_type): filtered_internships = internship_eligibility_filters(studentDetails, internships) internshipsdata = InternshipSerializerForStudent(filtered_internships, many=True).data - internshipApplications = InternshipApplication.objects.filter(student_id=id) + internshipApplications = InternshipApplication.objects.filter(student_id=id).order_by('-updated_at') internshipApplications = InternshipApplicationSerializer(internshipApplications, many=True).data return Response( {'action': "Get Dashboard - Student", 'message': "Data Found", "placements": placementsdata, diff --git a/CDC_Backend/APIs/tests/test_adminView.py b/CDC_Backend/APIs/tests/test_adminView.py index 9f455ab..d9be51a 100644 --- a/CDC_Backend/APIs/tests/test_adminView.py +++ b/CDC_Backend/APIs/tests/test_adminView.py @@ -721,7 +721,7 @@ class AdminView(APITestCase): self.assertEqual(Placement.objects.get( id=self.placement1.id).offer_accepted, True) self.assertEqual(Placement.objects.get( - id=self.placement1.id).deadline_datetime, timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)+timezone.timedelta(days=2)) + id=self.placement1.id).deadline_datetime, timezone.localtime(timezone.now()).replace(hour=0, minute=0, second=0, microsecond=0)+timezone.timedelta(days=2)) def test_offerAccepted_withDeadline(self): url = reverse("Update Offer Accepted") @@ -729,7 +729,7 @@ class AdminView(APITestCase): "opening_type": "Placement", "opening_id": self.placement3.id, "offer_accepted": "true", - "deadline_datetime": (timezone.now().replace( + "deadline_datetime": (timezone.localtime(timezone.now()).replace( hour=0, minute=0, second=0, microsecond=0)+timezone.timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S %z') } self.admin.user_type = ["s_admin"] @@ -739,7 +739,7 @@ class AdminView(APITestCase): data), content_type='application/json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(Placement.objects.get( - id=self.placement3.id).deadline_datetime, timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)+timezone.timedelta(days=1)) + id=self.placement3.id).deadline_datetime, timezone.localtime(timezone.now()).replace(hour=0, minute=0, second=0, microsecond=0)+timezone.timedelta(days=1)) self.assertEqual(response.data['message'], 'Offer Accepted Updated') def test_offerAccepted_wrongOpening(self): diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index a439233..ff54a8a 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -481,11 +481,11 @@ def get_eligible_emails(opening_id, opening_type=PLACEMENT): emails.append(student_user.email) except Exception as e: logger.warning('Utils - send_opening_notifications: For Loop' + str(e)) - return False + return False, [] return True, emails except: logger.warning('Utils - send_opening_notifications: ' + str(sys.exc_info())) - return False + return False, [] def exception_email(opening): opening = opening.dict()