Пример #1
0
 def put(self, request, pk, format=None):
     customer = self.get_object(pk)
     serializer = CustomerSerializer(customer, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
 def put(self, request, customer_id):
     customer = self.get_object(customer_id)
     serializer = CustomerSerializer(instance=customer, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response({'error': serializer.errors})
Пример #3
0
 def put(self, request, pk, format=None):
     customer = self.get_object(pk)
     serializer = CustomerSerializer(customer, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #4
0
def customer_details(request, pk):

    try:
        customer = Customer.objects.get(pk=pk)
    except Customer.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)

    # GET customer details
    if request.method == 'GET':
        customer_serializer = CustomerSerializer(customer)
        return JsonResponse(customer_serializer.data)

    # PUT existing customer
    elif request.method == 'PUT':
        customer_data = JSONParser().parse(request)
        customer_serializer = CustomerSerializer(customer, data=customer_data)
        if customer_serializer.is_valid():
            customer_serializer.save()
            return JsonResponse(customer_serializer.data)
        return JsonResponse(customer_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    # DELETE existing customer
    elif request.method == 'DELETE':
        customer.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT)
Пример #5
0
 def put(self, request, pk):
     customer = self.get_object(pk)
     serializer = CustomerSerializer(instance=customer, 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_500_INTERNAL_SERVER_ERROR)
Пример #6
0
 def post(self, request, version='v1', format='json'):
     serialized = CustomerSerializer(data=request.data)
     errors = None
     if serialized.is_valid():
         serialized.save()
         return self.get(request)
     else:
         errors = serialized.errors
     return Response(errors, status=status.HTTP_400_BAD_REQUEST)
Пример #7
0
def customer_list(request):
    """
    List all code snippets, or create a new customer.
    """
    if request.method == 'GET':
        customers = Customer.objects.all()
        serializer = CustomerSerializer(customers, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = CustomerSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Пример #8
0
def customers(request):
    try:
        customers = Customer.objects.all()
    except Customer.DoesNotExist as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        serializer = CustomerSerializer(customers, many=True)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = CustomerSerializer(instance=customers, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response({'error': serializer.errors})
Пример #9
0
    def create(self, request):
        product_id = request.data.get('product_id')
        if product_id:
            product_id_status = utils.check_product_id(
                request.data.get('product_id'))
            # using false to make it explicit
            if product_id_status is False:
                return Response({"product_id": "This product does not exist."},
                                status.HTTP_400_BAD_REQUEST)

        fields = ('name', 'email', 'url')
        serializer = CustomerSerializer(data=request.data,
                                        context={'request': request},
                                        fields=fields)
        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)
Пример #10
0
    def partial_update(self, request, pk, format=None):
        favorites = request.data.get('favorites')
        product_id_statuses = [
            utils.check_product_id(product_status.get("product_id"))
            for product_status in favorites
        ]

        if not any(product_id_statuses):
            return Response({"product_id": "This product does not exist."},
                            status.HTTP_400_BAD_REQUEST)

        fields = ('name', 'email', 'favorites')
        customer = self.get_object(pk)
        serializer = CustomerSerializer(customer,
                                        data=request.data,
                                        context={'request': request},
                                        fields=fields)
        if serializer.is_valid():
            serializer.save()
            data_response = self.get_data_response(
                serializer_data=serializer.data)
            return Response(data_response)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #11
0
def customer_list(request):

    # GET all customers
    if request.method == 'GET':
        customers = Customer.objects.all()
        customers_serializer = CustomerSerializer(customers, many=True)
        return JsonResponse(customers_serializer.data, safe=False)

    # POST new customer
    elif request.method == 'POST':
        customer_data = JSONParser().parse(request)
        customer_serializer = CustomerSerializer(data=customer_data)
        if customer_serializer.is_valid():
            customer_serializer.save()
            return JsonResponse(customer_serializer.data,
                                status=status.HTTP_201_CREATED)
        return JsonResponse(customer_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    # DELETE all customers
    elif request.method == 'DELETE':
        Customer.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT)
Пример #12
0
def customer_detail(request, pk):
    """
    Retrieve, update or delete a customer detail.
    """
    try:
        customer = Customer.objects.get(pk=pk)
    except Customer.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = CustomerSerializer(customer)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = CustomerSerializer(customer, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        customer.delete()
        return HttpResponse(status=204)