Exemplo n.º 1
0
 def put(self, request, pk):
     shop = self.get_object(pk)
     serializer = ShopModelSerializer(instance=shop, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def post(self, request):
     city_id = request.data.get("city_id")
     city = City.objects.get(id=city_id)
     serializer = ShopModelSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save(city=city)
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
def shop_detail(request, pk):
    try:
        shop = Shop.objects.get(id=pk)
    except Shop.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ShopModelSerializer(shop)
        return Response(serializer.data)
    elif request.method == 'PUT':
        serializer = ShopModelSerializer(instance=shop, data=request.data)
        if serializer.is_valid():
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
    elif request.method == 'DELETE':
        shop.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 4
0
def shop_list(request):
    if request.method == 'GET':
        shops = Shop.objects.all()
        serializer = ShopModelSerializer(shops, many=True)
        context = {'shops': shops}
        return render(request, 'shop.html', context)

    elif request.method == 'POST':
        serializer = ShopModelSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
Exemplo n.º 5
0
 def get(self, request, pk):
     shop = self.get_object(pk)
     serializer = ShopModelSerializer(shop)
     return Response(serializer.data)
Exemplo n.º 6
0
 def post(self, request):
     serializer = ShopModelSerializer(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)
Exemplo n.º 7
0
 def get(self, request):
     shop = Shop.objects.all()
     serializer = ShopModelSerializer(shop, many=True)
     return Response(serializer.data)