Beispiel #1
0
 def post(self, request, format=None):
     serializer = ShopSerializer(data=request.data)
     if serializer.is_valid():
         print('true')
         serializer.save(owner=request.user)
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
 def put(self, request, pk, format=None):
     shop_instance = get_object_or_404(Shop, pk=pk)
     serializer = ShopSerializer(shop_instance, data=request.data)
     if serializer.is_valid():
         print('true')
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
 def create(self, request, *args, **kwargs):
     serializer = ShopSerializer(data={
         'name': request.data['name'],
         'shop_owner_id': request.user.id
     })
     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)
Beispiel #4
0
    def create(self, request):
        shop = request.data.get('shop')
        serializer = ShopSerializer(data=shop)

        if serializer.is_valid(raise_exception=True):
            Shop.objects.create(title=shop['title'])

            return Response(serializer.data)

        else:
            data = {'error': 'Error Creating Shop', 'status_code': 400}
            error_serializer = ErrorSerializer(data=data)
            error_serializer.is_valid(True)

            return Response(error_serializer.data)
Beispiel #5
0
    def update(self, request, pk=None):
        shop_to_update = request.data.get('shop')
        serializer = ShopSerializer(data=shop_to_update)

        if serializer.is_valid(raise_exception=True):
            queryset = Shop.objects.all()
            shop = get_object_or_404(queryset, pk=pk)
            shop.title = shop_to_update['title']
            shop.save()

            serializer_to_return = ShopSerializer(shop)

            return Response(serializer_to_return.data)

        else:
            data = {'error': 'Error Updating Shop', 'status_code': 400}
            error_serializer = ErrorSerializer(data=data)
            error_serializer.is_valid(True)

            return Response(error_serializer.data)