示例#1
0
 def post(self, request):
     """
     增加员工信息
     """
     data = JSONParser().parse(request)
     serializer = EmpSerializer(data=data)
     if not serializer.is_valid():
         raise ParseError(serializer.errors)
     serializer.save()
     return JsonResponse(serializer.data, safe=False)
示例#2
0
 def put(self, request, username):
     """
     修改员工信息
     """
     data = JSONParser().parse(request)
     emp = get_object_or_404(Emp, username=username)
     serializer = EmpSerializer(emp, data=data, partial=True)
     if not serializer.is_valid():
         raise ParseError(serializer.errors)
     serializer.save()
     return JsonResponse(serializer.data, safe=False)
示例#3
0
def AddEmployee(request):
    if request.method == 'POST':
        serializer = EmpSerializer(data=request.POST)
        print ""
        print serializer.is_valid()
        print "++++++++++++++"
        if serializer.is_valid():
            serializer.save()
        else:
            print serializer.errors
            return JsonResponse({'error': serializer.errors})
    return JsonResponse({'data': serializer.data})
示例#4
0
def emp_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        emps = Emp.objects.all()
        serializer = EmpSerializer(emps, many=True)
        return JSONResponse(serializer.data)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = EmpSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        else:
            return JSONResponse(serializer.errors, status=400)
示例#5
0
def emp_detail(request,pk):
    """
    Retrieve, update a code snippet.
    """
    try:
        emp = Emp.objects.get(pk=pk)
    except Emp.DoesNotExist:
        return HttpResponse(status=400)
    
    if request.method == 'GET':
        serializer = EmpSerializer(emp)
        return JSONResponse(serializer.data)
    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = EmpSerializer(emp, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        else:
            return JSONResponse(serializer.error,status=400)
    elif request.method == 'DELETE':
        emp.delete()
        return HttpResponse(status=204)