consolidated code

This commit is contained in:
uttamthummala 2023-10-03 02:35:33 +05:30
parent ed6f313b68
commit 7ccac2a2f2
1 changed files with 50 additions and 105 deletions

View File

@ -307,136 +307,82 @@ def getApplications(request, id, email, user_type):
def submitApplication(request, id, email, user_type): def submitApplication(request, id, email, user_type):
try: try:
data = request.data data = request.data
if OPENING_TYPE in data: if OPENING_TYPE in data:
opening_type= data[OPENING_TYPE] opening_type= data[OPENING_TYPE]
else: else:
opening_type= "Placement" opening_type= "Placement"
student = get_object_or_404(Student, pk=data[STUDENT_ID])
if opening_type == "Internship": if opening_type == "Internship":
opening = get_object_or_404(Internship, pk=data[OPENING_ID]) opening = get_object_or_404(Internship, pk=data[OPENING_ID])
student_user = get_object_or_404(User, id=student.id)
if data[APPLICATION_ID] == "":
application = InternshipApplication()
application.id = generateRandomString()
application.internship = opening
application.student = student
if data[RESUME_FILE_NAME] in student.resumes:
application.resume = data[RESUME_FILE_NAME]
else:
raise FileNotFoundError(RESUME_FILE_NAME + " Not Found")
additional_info = {}
for i in opening.additional_info:
if i not in data[ADDITIONAL_INFO]:
raise AttributeError(i + " not found in Additional Info")
else:
additional_info[i] = data[ADDITIONAL_INFO][i]
application.additional_info = json.dumps(additional_info)
data = {
"name": student.name,
"company_name": opening.company_name,
"application_type": "Internship",
"additional_info": dict(json.loads(application.additional_info)),
}
subject = STUDENT_APPLICATION_SUBMITTED_TEMPLATE_SUBJECT.format(company_name=opening.company_name)
application.changed_by = get_object_or_404(User, id=id)
application.save()
sendEmail(student_user.email, subject, data, STUDENT_APPLICATION_SUBMITTED_TEMPLATE)
return Response({'action': "Add Student Application", 'message': "Application added For Internship"},
status=status.HTTP_200_OK)
else:
application = get_object_or_404(InternshipApplication, id=data[APPLICATION_ID])
if application:
if data[RESUME_FILE_NAME] in student.resumes:
application.resume = data[RESUME_FILE_NAME]
else:
raise FileNotFoundError(RESUME_FILE_NAME + " Not Found")
application.resume = data[RESUME_FILE_NAME]
additional_info = {}
for i in opening.additional_info:
if i not in data[ADDITIONAL_INFO]:
raise AttributeError(i + " not found in Additional Info")
else:
additional_info[i] = data[ADDITIONAL_INFO][i]
application.additional_info = json.dumps(additional_info)
data = {
"name": student.name,
"company_name": opening.company_name,
"application_type": "Internship",
"resume": application.resume[16:],
"additional_info_items": dict(json.loads(application.additional_info)),
}
subject = STUDENT_APPLICATION_UPDATED_TEMPLATE_SUBJECT.format(company_name=opening.company_name)
application.changed_by = get_object_or_404(User, id=id)
application.save()
sendEmail(student_user.email, subject, data, STUDENT_APPLICATION_UPDATED_TEMPLATE)
return Response({'action': "Add Student Application", 'message': "Application updated For Internship"},
status=status.HTTP_200_OK)
else:
return Response({'action': "Edit Student Application", 'message': "No Application Found For Internship"},
status=status.HTTP_400_BAD_REQUEST)
else: else:
opening = get_object_or_404(Placement, pk=data[OPENING_ID]) opening = get_object_or_404(Placement, pk=data[OPENING_ID])
student_user = get_object_or_404(User, id=student.id) student = get_object_or_404(Student, pk=data[STUDENT_ID])
if data[APPLICATION_ID] == "": # opening = get_object_or_404(Placement, pk=data[OPENING_ID])
application = PlacementApplication() student_user = get_object_or_404(User, id=student.id)
application.id = generateRandomString() if data[APPLICATION_ID] == "":
application = PlacementApplication() if opening_type == "Placement" else InternshipApplication()
application.id = generateRandomString()
if(opening_type == "Placement"):
application.placement = opening application.placement = opening
application.student = student else:
application.internship = opening
application.student = student
if data[RESUME_FILE_NAME] in student.resumes:
application.resume = data[RESUME_FILE_NAME]
else:
raise FileNotFoundError(RESUME_FILE_NAME + " Not Found")
additional_info = {}
for i in opening.additional_info:
if i not in data[ADDITIONAL_INFO]:
raise AttributeError(i + " not found in Additional Info")
else:
additional_info[i] = data[ADDITIONAL_INFO][i]
application.additional_info = json.dumps(additional_info)
data = {
"name": student.name,
"company_name": opening.company_name,
"application_type": "Placement" if opening_type == "Placement" else "Internship",
"additional_info": dict(json.loads(application.additional_info)),
}
subject = STUDENT_APPLICATION_SUBMITTED_TEMPLATE_SUBJECT.format(company_name=opening.company_name)
application.changed_by = get_object_or_404(User, id=id)
application.save()
sendEmail(student_user.email, subject, data, STUDENT_APPLICATION_SUBMITTED_TEMPLATE)
return Response({'action': "Add Student Application", 'message': "Application added"},
status=status.HTTP_200_OK)
else:
if opening_type == "Internship":
application = get_object_or_404(InternshipApplication, id=data[APPLICATION_ID])
else:
application = get_object_or_404(PlacementApplication, id=data[APPLICATION_ID])
if application:
if data[RESUME_FILE_NAME] in student.resumes: if data[RESUME_FILE_NAME] in student.resumes:
application.resume = data[RESUME_FILE_NAME] application.resume = data[RESUME_FILE_NAME]
else: else:
raise FileNotFoundError(RESUME_FILE_NAME + " Not Found") raise FileNotFoundError(RESUME_FILE_NAME + " Not Found")
application.resume = data[RESUME_FILE_NAME]
additional_info = {} additional_info = {}
for i in opening.additional_info: for i in opening.additional_info:
if i not in data[ADDITIONAL_INFO]: if i not in data[ADDITIONAL_INFO]:
raise AttributeError(i + " not found in Additional Info") raise AttributeError(i + " not found in Additional Info")
else: else:
additional_info[i] = data[ADDITIONAL_INFO][i] additional_info[i] = data[ADDITIONAL_INFO][i]
application.additional_info = json.dumps(additional_info) application.additional_info = json.dumps(additional_info)
data = { data = {
"name": student.name, "name": student.name,
"company_name": opening.company_name, "company_name": opening.company_name,
"application_type": "Placement", "application_type": "Placement" if opening_type == "Placement" else "Internship",
"additional_info": dict(json.loads(application.additional_info)), "resume": application.resume[16:],
"additional_info_items": dict(json.loads(application.additional_info)),
} }
subject = STUDENT_APPLICATION_SUBMITTED_TEMPLATE_SUBJECT.format(company_name=opening.company_name) subject = STUDENT_APPLICATION_UPDATED_TEMPLATE_SUBJECT.format(company_name=opening.company_name)
application.changed_by = get_object_or_404(User, id=id) application.changed_by = get_object_or_404(User, id=id)
application.save() application.save()
sendEmail(student_user.email, subject, data, STUDENT_APPLICATION_SUBMITTED_TEMPLATE) sendEmail(student_user.email, subject, data, STUDENT_APPLICATION_UPDATED_TEMPLATE)
return Response({'action': "Add Student Application", 'message': "Application added"}, return Response({'action': "Add Student Application", 'message': "Application updated"},
status=status.HTTP_200_OK)
else:
application = get_object_or_404(PlacementApplication, id=data[APPLICATION_ID])
if application:
if data[RESUME_FILE_NAME] in student.resumes:
application.resume = data[RESUME_FILE_NAME]
else:
raise FileNotFoundError(RESUME_FILE_NAME + " Not Found")
application.resume = data[RESUME_FILE_NAME]
additional_info = {}
for i in opening.additional_info:
if i not in data[ADDITIONAL_INFO]:
raise AttributeError(i + " not found in Additional Info")
else:
additional_info[i] = data[ADDITIONAL_INFO][i]
application.additional_info = json.dumps(additional_info)
data = {
"name": student.name,
"company_name": opening.company_name,
"application_type": "Placement",
"resume": application.resume[16:],
"additional_info_items": dict(json.loads(application.additional_info)),
}
subject = STUDENT_APPLICATION_UPDATED_TEMPLATE_SUBJECT.format(company_name=opening.company_name)
application.changed_by = get_object_or_404(User, id=id)
application.save()
sendEmail(student_user.email, subject, data, STUDENT_APPLICATION_UPDATED_TEMPLATE)
return Response({'action': "Add Student Application", 'message': "Application updated"},
status=status.HTTP_200_OK) status=status.HTTP_200_OK)
else: else:
return Response({'action': "Edit Student Application", 'message': "No Application Found"}, return Response({'action': "Edit Student Application", 'message': "No Application Found"},
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
except Http404 as e: except Http404 as e:
@ -453,7 +399,6 @@ def submitApplication(request, id, email, user_type):
return Response({'action': "Submit Application", 'message': "Something Went Wrong"}, return Response({'action': "Submit Application", 'message': "Something Went Wrong"},
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
@api_view(['POST']) @api_view(['POST'])
@isAuthorized(allowed_users=[ADMIN]) @isAuthorized(allowed_users=[ADMIN])
@precheck(required_data=[OPENING_ID]) @precheck(required_data=[OPENING_ID])