def add_client(request): if request.method == 'POST': client_data = JSONParser().parse(request) client_serializer = ClientSerializer(data=client_data) if client_serializer.is_valid(): # Generate query cursor = connection.cursor() query = 'INSERT INTO clients_client(' data_spc_query = '' data_query = [] attrbs_table = list(client_data.keys()) for a in attrbs_table: if a != attrbs_table[-1]: query += a + ', ' data_spc_query += '%s, ' else: query += a + ') VALUES(' data_spc_query += '%s);' if a != 'password': data_query.append(client_data.get(a)) else: pwd = make_password(client_data.get(a)) data_query.append(pwd) query += data_spc_query print(query) cursor.execute(query, tuple(data_query)) connection.commit() return JsonResponse({ "code": 201, "message": "Client Added" }, status=status.HTTP_201_CREATED) return JsonResponse(client_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
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 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 _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(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)