From 158851f6e4476da860baf7b381e849e7ae33b103 Mon Sep 17 00:00:00 2001 From: karthikmurakonda Date: Thu, 19 Oct 2023 06:30:46 +0530 Subject: [PATCH] create new endpoint to expose eligible students for an opeining id --- CDC_Backend/APIs/adminUrls.py | 1 + CDC_Backend/APIs/adminViews.py | 25 +++++++++++++++++ CDC_Backend/APIs/constants.py | 1 + CDC_Backend/APIs/utils.py | 51 ++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/CDC_Backend/APIs/adminUrls.py b/CDC_Backend/APIs/adminUrls.py index da8acd6..5afb5c7 100644 --- a/CDC_Backend/APIs/adminUrls.py +++ b/CDC_Backend/APIs/adminUrls.py @@ -16,4 +16,5 @@ urlpatterns = [ path('addPPO/', adminViews.addPPO, name="Add PPO"), path('getStudentApplication/', adminViews.getStudentApplication, name="Get student application"), path('getStats/', adminViews.getStats, name="Get Stats"), + path('getEligibleStudents/', adminViews.get_eligible_students, name="Get Eligible Students"), ] diff --git a/CDC_Backend/APIs/adminViews.py b/CDC_Backend/APIs/adminViews.py index 429260e..db7f79f 100644 --- a/CDC_Backend/APIs/adminViews.py +++ b/CDC_Backend/APIs/adminViews.py @@ -750,3 +750,28 @@ def getStats(request, id, email, user_type): print(sys.exc_info()) return Response({'action': "Get Stats", 'message': "Something Went Wrong"}, status=status.HTTP_400_BAD_REQUEST) + + +@api_view(['GET']) +@isAuthorizedService() +@precheck(required_data=[OPENING_ID]) +def get_eligible_students(request): + try: + data = request.GET + opening_id = data[OPENING_ID] + if OPENING_TYPE in data: + opening_type= data[OPENING_TYPE] + else: + opening_type= "Placement" + eligible_students=get_eligible_emails(opening_id=opening_id, opening_type=opening_type) + return Response({'action': "Get Eligible Students", 'message': "Eligible Students Fetched", + 'eligible_students': eligible_students}, + status=status.HTTP_200_OK) + except Http404: + return Response({'action': "Get Eligible Students", 'message': "Opening Not Found"}, + status=status.HTTP_404_NOT_FOUND) + except: + logger.warning("Get Eligible Students: " + str(sys.exc_info())) + return Response({'action': "Get Eligible Students", 'message': "Something Went Wrong"}, + status=status.HTTP_400_BAD_REQUEST) + \ No newline at end of file diff --git a/CDC_Backend/APIs/constants.py b/CDC_Backend/APIs/constants.py index 12ac4fb..267d0e9 100644 --- a/CDC_Backend/APIs/constants.py +++ b/CDC_Backend/APIs/constants.py @@ -100,6 +100,7 @@ EMAIL = "email" STUDENT = 'student' ADMIN = 'admin' SUPER_ADMIN = 's_admin' +SERVICE= 'service' COMPANY = 'company' TIER = 'tier' # To be Configured Properly diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index 10f1c0c..36e0471 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -151,6 +151,26 @@ def isAuthorized(allowed_users=None): return decorator +def isAuthorizedService(): + def decorator(view_func): + def wrapper_func(request, *args, **kwargs): + try: + headers = request.META + if 'HTTP_AUTHORIZATION' in headers: + token_id = headers['HTTP_AUTHORIZATION'][7:] + jwt.decode(token_id, os.environ.get("SERVICE_SECRET_KEY"), algorithms="HS256") + return view_func(request, *args, **kwargs) + else: + raise PermissionError("Authorization Header Not Found") + except: + logger.warning("Is Authorized? " + str(sys.exc_info())) + return Response( + {'action': "Is Authorized?", 'message': "Something went wrong. Contact CDC for more details"}, + status=status.HTTP_400_BAD_REQUEST) + return wrapper_func + + return decorator + def generateRandomString(): try: @@ -435,6 +455,37 @@ def send_opening_notifications(opening_id, opening_type=PLACEMENT): logger.warning('Utils - send_opening_notifications: ' + str(sys.exc_info())) return False +def get_eligible_emails(opening_id, opening_type=PLACEMENT): + try: + # print(opening_id, opening_type) + if opening_type == PLACEMENT: + opening = get_object_or_404(Placement, id=opening_id) + else: + opening = get_object_or_404(Internship, id=opening_id) + emails=[] + students = Student.objects.all() + for student in students.iterator(): + if student.branch in opening.allowed_branch: + if student.degree == 'bTech' or opening.rs_eligible is True: + if (isinstance(opening,Placement) and PlacementApplicationConditions(student, opening)[0]) or ( + isinstance(opening,Internship) and InternshipApplicationConditions(student, opening)[0]): + try: + student_user = get_object_or_404(User, id=student.id) + # check if he applied + if opening_type == PLACEMENT: + if PlacementApplication.objects.filter(student=student, placement=opening).exists(): + continue + else: + if InternshipApplication.objects.filter(student=student, internship=opening).exists(): + continue + emails.append(student_user.email) + except Exception as e: + logger.warning('Utils - send_opening_notifications: For Loop' + str(e)) + return False + return True, emails + except: + logger.warning('Utils - send_opening_notifications: ' + str(sys.exc_info())) + return False def exception_email(opening): opening = opening.dict()