From 85f706828a27d14225249593858e0c0b8f2f604a Mon Sep 17 00:00:00 2001 From: Codewizard-18 Date: Thu, 17 Oct 2024 22:51:33 +0530 Subject: [PATCH 1/2] added api views for placement shedule --- .../placement_cell/api/serializers.py | 9 +- .../applications/placement_cell/api/urls.py | 8 + .../applications/placement_cell/api/views.py | 196 ++++++++++++++++++ .../applications/placement_cell/urls.py | 3 +- 4 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 FusionIIIT/applications/placement_cell/api/urls.py create mode 100644 FusionIIIT/applications/placement_cell/api/views.py diff --git a/FusionIIIT/applications/placement_cell/api/serializers.py b/FusionIIIT/applications/placement_cell/api/serializers.py index e6960adb0..e1238f29f 100644 --- a/FusionIIIT/applications/placement_cell/api/serializers.py +++ b/FusionIIIT/applications/placement_cell/api/serializers.py @@ -4,7 +4,14 @@ from applications.placement_cell.models import (Achievement, Course, Education, Experience, Has, Patent, Project, Publication, Skill, - PlacementStatus, NotifyStudent) + PlacementStatus, NotifyStudent , PlacementSchedule) + + +class PlacementScheduleSerializer(serializers.ModelSerializer): + class Meta: + model = PlacementSchedule + fields = '__all__' + class SkillSerializer(serializers.ModelSerializer): diff --git a/FusionIIIT/applications/placement_cell/api/urls.py b/FusionIIIT/applications/placement_cell/api/urls.py new file mode 100644 index 000000000..d6bc71b5a --- /dev/null +++ b/FusionIIIT/applications/placement_cell/api/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from .views import PlacementScheduleView,BatchStatisticsView + + +urlpatterns = [ + path('placement/', PlacementScheduleView.as_view(), name='placement-list'), + path('statistics/',BatchStatisticsView.as_view()), +] \ No newline at end of file diff --git a/FusionIIIT/applications/placement_cell/api/views.py b/FusionIIIT/applications/placement_cell/api/views.py new file mode 100644 index 000000000..cc180013f --- /dev/null +++ b/FusionIIIT/applications/placement_cell/api/views.py @@ -0,0 +1,196 @@ +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework import status,permissions +from django.contrib.auth.models import User +from django.http import JsonResponse +from django.core.files.storage import FileSystemStorage +from django.views.decorators.csrf import csrf_exempt +from ..models import * +from applications.academic_information.models import Student +from .serializers import PlacementScheduleSerializer, NotifyStudentSerializer +import json + + +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework import permissions +from django.http import JsonResponse + +class PlacementScheduleView(APIView): + permission_classes = [permissions.AllowAny] + + def get(self, request): + combined_data = [] + + notify_students = NotifyStudent.objects.all() + for notify in notify_students: + placements = PlacementSchedule.objects.filter(notify_id=notify.id) + placement_serializer = PlacementScheduleSerializer(placements, many=True) + notify_data = NotifyStudentSerializer(notify).data + + for placement in placement_serializer.data: + combined_entry = {**notify_data, **placement} + combined_data.append(combined_entry) + + return Response(combined_data) + + def post(self, request): + placement_type = request.data.get("placement_type") + company_name = request.data.get("company_name") + ctc = request.data.get("ctc") + description = request.data.get("description") + schedule_at = request.data.get("schedule_at") + date = request.data.get("placement_date") + location = request.data.get("location") + role = request.data.get("role") + resume = request.FILES.get("resume") + + try: + role_create, _ = Role.objects.get_or_create(role=role) + notify = NotifyStudent.objects.create( + placement_type=placement_type, + company_name=company_name, + description=description, + ctc=ctc, + timestamp=schedule_at, + ) + + schedule = PlacementSchedule.objects.create( + notify_id=notify, + title=company_name, + description=description, + placement_date=date, + attached_file=resume, + role=role_create, + location=location, + time=schedule_at, + ) + + return JsonResponse({"message": "Successfully Added Schedule"}, status=201) + + except Exception as e: + return JsonResponse({"error": str(e)}, status=400) + + + + +@csrf_exempt +def placement_schedule_save(request): + permission_classes = [permissions.AllowAny] + if request.method != "POST": + return JsonResponse({"error": "Method Not Allowed"}, status=405) + + placement_type = request.POST.get("placement_type") + company_name = request.POST.get("company_name") + ctc = request.POST.get("ctc") + description = request.POST.get("description") + timestamp = request.POST.get("time_stamp") + title = request.POST.get("title") + location = request.POST.get("location") + role = request.POST.get("role") + + resume = request.FILES.get("resume") + schedule_at = request.POST.get("schedule_at") + date = request.POST.get("placement_date") + + try: + role_create, _ = Role.objects.get_or_create(role=role) + + notify = NotifyStudent.objects.create( + placement_type=placement_type, + company_name=company_name, + description=description, + ctc=ctc, + timestamp=timestamp + ) + + schedule = PlacementSchedule.objects.create( + notify_id=notify, + title=company_name, + description=description, + placement_date=date, + attached_file=resume, + role=role_create, + location=location, + time=schedule_at + ) + + return JsonResponse({"message": "Successfully Added Schedule"}, status=201) + except Exception as e: + return JsonResponse({"error": str(e)}, status=400) + + + +class BatchStatisticsView(APIView): + def get(self, request): + combined_data = [] + student_records = StudentRecord.objects.all() + + if not student_records.exists(): + return Response({"error": "No student records found"}, status=status.HTTP_404_NOT_FOUND) + + for student in student_records: + try: + cur_student = Student.objects.get(id=student.unique_id) + cur_placement = PlacementRecord.objects.get(id=student.record_id) + user = User.objects.get(id=student.unique_id) + + combined_entry = { + "branch": cur_student.branch, + "placement_name": cur_placement.name, + "ctc": cur_placement.ctc, + "year": cur_placement.year, + "first_name": user.first_name + } + + combined_data.append(combined_entry) + + except Student.DoesNotExist: + return Response({"error": f"Student with id {student.unique_id} not found"}, status=status.HTTP_404_NOT_FOUND) + except PlacementRecord.DoesNotExist: + return Response({"error": f"Placement record with id {student.record_id} not found"}, status=status.HTTP_404_NOT_FOUND) + except User.DoesNotExist: + return Response({"error": f"User with id {student.unique_id} not found"}, status=status.HTTP_404_NOT_FOUND) + except Exception as e: + return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST) + + if not combined_data: + return Response({"message": "No combined data found"}, status=status.HTTP_204_NO_CONTENT) + + return Response(combined_data, status=status.HTTP_200_OK) + + # def get(self, request): + # combined_data = [] + + # # Get all student records + # student_records = StudentRecord.objects.all() + + # for student in student_records: + # # Get the current student, placement record, and user + # cur_student = Student.objects.get(id=student.unique_id) + # cur_placement = PlacementRecord.objects.get(id=student.record_id) + # user = User.objects.get(username=student.unique_id) + + # # Combine the required fields into a dictionary + # combined_entry = { + # "branch": cur_student.branch, # Assuming branch is a field in the Student model + # "placement_name": cur_placement.name, # Name field from PlacementRecord + # "ctc": cur_placement.ctc, # CTC field from PlacementRecord + # "year": cur_placement.year, # Year field from PlacementRecord + # "first_name": user.first_name # First name field from User + # } + + # # Append the combined data to the list + # combined_data.append(combined_entry) + + # return Response(combined_data, status=status.HTTP_200_OK) + + + + + + + + + + diff --git a/FusionIIIT/applications/placement_cell/urls.py b/FusionIIIT/applications/placement_cell/urls.py index 190638861..1aa89d001 100644 --- a/FusionIIIT/applications/placement_cell/urls.py +++ b/FusionIIIT/applications/placement_cell/urls.py @@ -1,4 +1,4 @@ -from django.conf.urls import url +from django.conf.urls import url,include from . import views app_name = 'placement' @@ -26,4 +26,5 @@ url(r'^placement_record_save/$', views.placement_record_save, name='placement_record_save'), url(r'^add_placement_visit/$', views.add_placement_visit, name='add_placement_visit'), url(r'^placement_visit_save/$', views.placement_visit_save, name='placement_visit_save'), + url(r'^api/', include('applications.placement_cell.api.urls')), ] From 3302b5ace36d80b6b2e4c0bb9d069b391baa3bb8 Mon Sep 17 00:00:00 2001 From: Codewizard-18 Date: Tue, 22 Oct 2024 22:34:39 +0530 Subject: [PATCH 2/2] batch statistics --- .../applications/placement_cell/api/views.py | 70 +++++++++---------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/FusionIIIT/applications/placement_cell/api/views.py b/FusionIIIT/applications/placement_cell/api/views.py index cc180013f..67b5f8cc5 100644 --- a/FusionIIIT/applications/placement_cell/api/views.py +++ b/FusionIIIT/applications/placement_cell/api/views.py @@ -1,5 +1,6 @@ from rest_framework.views import APIView from rest_framework.response import Response +from django.shortcuts import get_object_or_404, redirect, render from rest_framework import status,permissions from django.contrib.auth.models import User from django.http import JsonResponse @@ -66,6 +67,7 @@ def post(self, request): time=schedule_at, ) + return redirect('placement') return JsonResponse({"message": "Successfully Added Schedule"}, status=201) except Exception as e: @@ -122,6 +124,7 @@ def placement_schedule_save(request): class BatchStatisticsView(APIView): + permission_classes = [permissions.AllowAny] def get(self, request): combined_data = [] student_records = StudentRecord.objects.all() @@ -131,12 +134,13 @@ def get(self, request): for student in student_records: try: - cur_student = Student.objects.get(id=student.unique_id) - cur_placement = PlacementRecord.objects.get(id=student.record_id) - user = User.objects.get(id=student.unique_id) + cur_student = Student.objects.get(id_id=student.unique_id_id) + cur_placement = PlacementRecord.objects.get(id=student.record_id_id) + user = User.objects.get(username=student.unique_id_id) combined_entry = { - "branch": cur_student.branch, + "branch": cur_student.specialization, + "batch" : cur_placement.year, "placement_name": cur_placement.name, "ctc": cur_placement.ctc, "year": cur_placement.year, @@ -159,38 +163,32 @@ def get(self, request): return Response(combined_data, status=status.HTTP_200_OK) - # def get(self, request): - # combined_data = [] - - # # Get all student records - # student_records = StudentRecord.objects.all() - - # for student in student_records: - # # Get the current student, placement record, and user - # cur_student = Student.objects.get(id=student.unique_id) - # cur_placement = PlacementRecord.objects.get(id=student.record_id) - # user = User.objects.get(username=student.unique_id) - - # # Combine the required fields into a dictionary - # combined_entry = { - # "branch": cur_student.branch, # Assuming branch is a field in the Student model - # "placement_name": cur_placement.name, # Name field from PlacementRecord - # "ctc": cur_placement.ctc, # CTC field from PlacementRecord - # "year": cur_placement.year, # Year field from PlacementRecord - # "first_name": user.first_name # First name field from User - # } - - # # Append the combined data to the list - # combined_data.append(combined_entry) - - # return Response(combined_data, status=status.HTTP_200_OK) - - - - - - - + def post(self,request): + placement_type=request.POST.get("placement_type") + company_name=request.POST.get("company_name") + roll_no = request.POST.get("roll_no") + ctc=request.POST.get("ctc") + year=request.POST.get("year") + test_type=request.POST.get("test_type") + test_score=request.POST.get("test_score") + try: + p2 = PlacementRecord.objects.create( + placement_type = placement_type, + name = company_name, + ctc = ctc, + year = year, + test_score = test_score, + test_type = test_type, + ) + p1 = StudentRecord.objects.create( + record_id = p2, + unique_id_id = roll_no, + ) + return JsonResponse({"message": "Successfully Added"}, status=201) + + except Exception as e: + return JsonResponse({"error": str(e)}, status=400) +