def put(self, request, pk, format=None): client = self.get_object(pk) serializer = ClientSerializer(client, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def client_detail(request, facebook_id): """ Retrieve, update or delete a code client. """ try: client = Client.objects.get(facebook_id=facebook_id) except Client.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = ClientSerializer(client) log('client_detail', 'detail', 'detail user %s' % facebook_id) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = ClientSerializer(client, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': client.delete() log('client', 'delete', 'delete client %s' % facebook_id) return HttpResponse(status=204)
def post(self, request, format=None): client = ClientSerializer(data=request.data) if client.is_valid(): client = client.save() return Response(data={ 'client_id': client.client_id, }, status=status.HTTP_201_CREATED) return Response(client.errors, status=status.HTTP_400_BAD_REQUEST)
def _handle_put(self, request, *args, **kwargs): """ PUT/PATCH handler for Client Detail Sample Post Data: { "name": "Detroit City Apartments" } """ client = self.get_object(kwargs.get('pk')) serializer = ClientSerializer(client, data=request.DATA) if serializer.is_valid(): serializer.save() return serializer.data return self.raise_bad_request(serializer.errors)
def post(request): # Comprovem que l'usuari no sigui manager if request.data["is_manager"]: return Response("A client can not be manager", status=status.HTTP_400_BAD_REQUEST) # Creem l'usuari user = RegisterSerializer(data=request.data) user.is_valid(raise_exception=True) user.save() # Creem el client amb l'id i l'username de l'usuari client = { "_id": user.data["id"], "username": user.data["username"], "age": request.data.get('age', None), "country": request.data.get('country', None) } serializer = ClientSerializer(data=client) serializer.is_valid(raise_exception=True) serializer.save() return Response(data=serializer.data, status=status.HTTP_201_CREATED)
def post(self, request, format=None): serializer = ClientSerializer(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 post(self, request, format=None): serializer = ClientSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)