コード例 #1
0
def employees_detail(request, pk):

    try:
        emp = employee.objects.get(pk=pk)
    except employee.DoesNotExist:
        return JsonResponse({'message': 'The employee does not exist'},
                            status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        employees_serializer = EmployeeSerializer(emp)
        return JsonResponse(employees_serializer.data)

    elif request.method == 'PUT':
        employee_data = JSONParser().parse(request)
        employees_serializer = EmployeeSerializer(emp, data=employee_data)
        if employees_serializer.is_valid():
            employees_serializer.save()
            return JsonResponse(employees_serializer.data)
        return JsonResponse(employees_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        emp.delete()
        return JsonResponse({'message': 'Tutorial was deleted successfully!'},
                            status=status.HTTP_204_NO_CONTENT)
コード例 #2
0
ファイル: views.py プロジェクト: shankarchari/PyRainbow
 def put(self, request, pk, format=None):
     employee = self.get_object(pk)
     serializer = EmployeeSerializer(employee, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
コード例 #3
0
def employees_list(request):
    if request.method == 'GET':
        employees = employee.objects.all()
        firstname = request.GET.get('firstname', None)
        if firstname is not None:
            employees = employees.filter(firstname__icontains=firstname)

        employees_serializer = EmployeeSerializer(employees, many=True)
        return JsonResponse(employees_serializer.data, safe=False)

    elif request.method == 'POST':
        employee_data = JSONParser().parse(request)
        employees_serializer = EmployeeSerializer(data=employee_data)
        if employees_serializer.is_valid():
            employees_serializer.save()
            return JsonResponse(employees_serializer.data,
                                status=status.HTTP_201_CREATED)
        return JsonResponse(employees_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
コード例 #4
0
 def put(self, request, *args, **kwargs):
     serializer = EmployeeSerializer(self.get_object(), data=request.data)
     if serializer.is_valid(raise_exception=True):
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
コード例 #5
0
 def create(self, request, *args, **kwargs):
     serializer = EmployeeSerializer(data=request.data)
     if serializer.is_valid(raise_exception=True):
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
コード例 #6
0
ファイル: views.py プロジェクト: shankarchari/PyRainbow
 def post(self, request, format=None):
     serializer = EmployeeSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status = status.HTTP_201_CREATED)
     return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)