def theft_detail(request, pk): try: theft_report = TheftReport.objects.get(pk=pk) except TheftReport.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = TheftReportSerializer(theft_report) return Response(serializer.data) elif request.method == 'PUT': serializer = TheftReportSerializer(theft_report, data=request.data) if serializer.is_valid(): serializer.save(reporter=request.user) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': TheftReport.delete(theft_report) return Response(status=status.HTTP_204_NO_CONTENT)
def theft_report(request): if request.method == 'GET': theft_reports = TheftReport.objects.filter(reporter=request.user) serializer = TheftReportSerializer(theft_reports, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = TheftReportSerializer(data=request.data) if serializer.is_valid(): #check if vehicle exists in db vehicles_student = StudentVehicle.objects.filter(vehicle_registration_number=serializer.data['vehicle_pass_no']) vehicles_emp = EmployeeVehicle.objects.filter(vehicle_registration_number=serializer.data['vehicle_pass_no']) if(len(vehicles_student) > 0): serializer.save(reporter=request.user, stud_vehicle=vehicles_student[0]) elif(len(vehicles_emp) > 0): serializer.save(reporter=request.user, emp_vehicle=vehicles_emp[0]) else: #vehicle does not exist stud_vehicle and emp_vehicle are left blank serializer.save(reporter=request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)