Exemple #1
0
    def patch(self, request):
        basket = self.model.objects.filter(client_id=request.user.id).first()
        if basket is None:
            return Response(status=status.HTTP_400_BAD_REQUEST)
        record_id = request.data.get('id', None)

        if record_id is None:
            return Response(status=status.HTTP_400_BAD_REQUEST)

        if request.query_params.get('delete', False):
            basket.basket_remedies.filter(id=record_id).delete()

            return Response(BasketSerializer(basket).data, status.HTTP_200_OK)
        if request.query_params.get('replace', False):
            remedy_id = request.data.get('remedy')
            basket_remedy = basket.basket_remedies.filter(id=record_id).first()
            new_remedy = basket.basket_remedies.filter(remedy_id=remedy_id).first()

            if new_remedy is not None:
                new_remedy.amount += 1
                new_remedy.save()
                basket_remedy.delete()
            else:
                basket_remedy.remedy_id = remedy_id
                basket_remedy.save()

            return Response(BasketSerializer(basket).data, status.HTTP_200_OK)
        return Response(status=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
 def get(self, request):
     if request.user.type == 'ADMIN':
         return Response(status.HTTP_403_FORBIDDEN)
     basket = self.model.objects.filter(client_id=request.user.id).first()
     if basket is None:
         basket = self.model.objects.create(client=Client.objects.get(user_id=request.user.id))
     return Response(
         BasketSerializer(basket).data,
         status.HTTP_200_OK,
     )
Exemple #3
0
def remove_basket(request, pk):
    try:
        car = Car.objects.get(id=pk)
    except Exception as e:
        return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
    user = request.user
    try:
        user.basket.cars.remove(car)
        user.basket.save()
    except Exception as e:
        return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    ser = BasketSerializer(user.basket)
    return Response(ser.data, status=status.HTTP_200_OK)
Exemple #4
0
    def post(self, request):
        serializer = AddToBasketSerializer(data=request.data)

        if not serializer.is_valid():
            return Response(status=status.HTTP_400_BAD_REQUEST)

        basket = self.model.objects.filter(client_id=request.user.id).first()
        if basket is None:
            try:
                basket = self.model.objects.create(client=Client.objects.get(user_id=request.user.id))
            except:
                return Response(status=status.HTTP_403_FORBIDDEN)
        remedy_data = request.data.get('remedy', None)
        remedies_data = request.data.get('remedies', None)

        if remedy_data is not None:
            self.add_remedy(basket, remedy_data)

            return Response(BasketSerializer(basket).data, status.HTTP_200_OK)
        if remedies_data is not None:
            for r in remedies_data:
                self.add_remedy(basket, r)
            return Response(BasketSerializer(basket).data, status.HTTP_200_OK)
        return Response(status=status.HTTP_400_BAD_REQUEST)
Exemple #5
0
def add_to_basket(request, pk):
    try:
        car = Car.objects.get(id=pk)
    except Exception as e:
        return Response({'error': e}, status=status.HTTP_400_BAD_REQUEST)
    user = request.user
    try:
        if not user.basket.cars.get(id=car.id) == Car.DoesNotExist:
            return Response({'error': 'This car is already on your basket'},
                            status=status.HTTP_400_BAD_REQUEST)
    except Car.DoesNotExist as e:
        user.basket.cars.add(car)
        user.basket.save()
    except Exception as e:
        return Response({'error': e}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    ser = BasketSerializer(user.basket)
    return Response(ser.data, status=status.HTTP_200_OK)