def employeeApi(request, id=0): if request.method == 'GET': employees = Employees.objects.all() employees_serializer = EmployeeSerializer(employees, many=True) return JsonResponse(employees_serializer.data, safe=False) elif request.method == 'POST': employee_data = JSONParser().parse(request) employee_serializer = EmployeeSerializer(data=employee_data) if employee_serializer.is_valid(): employee_serializer.save() return JsonResponse("Added Successfully!", safe=False) return JsonResponse("Failed to add", safe=False) elif request.method == 'PUT': employee_data = JSONParser().parse(request) employee = Employees.objects.get( EmployeeId=employee_data['EmployeeId']) employee_serializer = EmployeeSerializer(employee, data=employee_data) if employee_serializer.is_valid(): employee_serializer.save() return JsonResponse("Updated Successfully!", safe=False) return JsonResponse("Failed to update", safe=False) elif request.method == 'DELETE': employee = Employees.objects.get(EmployeeId=id) employee.delete() return JsonResponse("Deleted Successfully!", safe=False)
def add_employee(request): status1 = {} errors = {} print "ddddddddddd" if request.method == 'PUT': serializer = EmployeeSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.data) elif request.method == 'POST': serializer = EmployeeSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() print "1111111" message = "successfully created" status1['message'] = "successfully created" return Response(status1, status=status.HTTP_201_CREATED) else: print "2222222222" for err in serializer.errors: errors['error'] = err return Response(errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, company_id, pk, format=None): employee = Employee.objects.get(company_id=company_id, pk=pk) serializer = EmployeeSerializer(employee, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): serializer = EmployeeSerializer(data=request.data) if serializer.is_valid(): serializer.save() return response.Response(serializer.data, status=status.HTTP_201_CREATED) return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def list(request): """List all Employee, or create a new Employee.""" if request.method == 'GET': employee = Employee.objects.all() serializer = EmployeeSerializer(employee, many=True) return Response(serializer.data) elif request.method == 'POST': 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)
def employee_list(request): if request.method == 'GET': print(request) logger.debug("inside get method") employee = Employee.objects.all() firstname = request.GET.get('firstname', None) print(firstname) companyname = request.GET.get('companyname', None) if firstname is not None: logger.debug("firstname is {}".format(firstname)) employee = employee.filter(firstname=firstname) else: if companyname is not None: company = Companyinfo.objects.filter( companyname=companyname)[0] employee = Employee.objects.filter(company=company).count() return HttpResponse(employee) employee_serializer = EmployeeSerializer(employee, many=True) return JsonResponse(employee_serializer.data, safe=False) elif request.method == 'POST': logger.debug("inside post method") employee_data = JSONParser().parse(request) employee_serializer = EmployeeSerializer(data=employee_data) if employee_serializer.is_valid(): employee_serializer.save() return JsonResponse({"message": "valid"}) else: return JsonResponse({"message": "not valid"}) elif request.method == 'DELETE': logger.debug("inside delete method") firstname = request.GET.get('firstname', None) employee = Employee.objects.filter(firstname=firstname) if firstname is not None: employee.delete() return JsonResponse({"message": "deleted successfully"}) else: return JsonResponse({"message": "deletion not successful"}) elif request.method == 'PUT': logger.debug("inside put method") employee_data = JSONParser().parse(request) firstname = employee_data.get("firstname") if firstname is not None: employee = Employee.objects.filter(firstname=firstname) employee.update(**employee_data) return JsonResponse({"message": "updated successful"}) else: return JsonResponse({"message": "update failed"})
def participate(request): try: employee = request.data.get('employee') employeeSerializer = EmployeeSerializer(data=employee) if employeeSerializer.is_valid(): employeeSerializer.save() else: return JsonResponse({'error': "Objetos não criados"}, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JsonResponse({'employee': employeeSerializer.data}, safe=False, status=status.HTTP_201_CREATED) except IntegrityError: return bad_request(request) except Exception as e: return JsonResponse({'error': str(e)}, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def post(self, request, company_id): try: group_employee_ids = list( Group.objects.filter(name="employee").values_list('id', flat=True)) request.data["groups"] = group_employee_ids request.data["password"] = make_password( password=request.data["password"]) 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) except Exception as a: print(a) return JsonResponse( {"detail": "An error occurred on the server" + str(a)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)