mirror of
https://git.kotyczka.ch/developers/django.git
synced 2025-04-06 07:45:08 +02:00
52 lines
1.9 KiB
Python
Executable File
52 lines
1.9 KiB
Python
Executable File
from django.shortcuts import render
|
|
|
|
from rest_framework.views import APIView
|
|
from rest_framework.decorators import api_view
|
|
from django.http.response import JsonResponse
|
|
from rest_framework.parsers import JSONParser
|
|
from rest_framework.response import Response
|
|
from rest_framework import status, permissions, viewsets
|
|
from pyapp.models import ShoppingItem, Product
|
|
from .serializers import ShoppingItemSerializer, ProductSerializer
|
|
|
|
@api_view(['GET', 'POST'])
|
|
def item_list(request):
|
|
"""
|
|
List all ShoppingItems, or create a new snippet.
|
|
"""
|
|
if request.method == 'GET':
|
|
ShoppingItems = ShoppingItem.objects.all()
|
|
serializer = ShoppingItemSerializer(ShoppingItems, many=True)
|
|
return Response(serializer.data)
|
|
|
|
elif request.method == 'POST':
|
|
serializer = ShoppingItemSerializer(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)
|
|
|
|
@api_view(['GET', 'PUT', 'DELETE'])
|
|
def item_detail(request, pk):
|
|
"""
|
|
Retrieve, update or delete a code snippet.
|
|
"""
|
|
try:
|
|
ShoppingItems = ShoppingItem.objects.get(pk=pk)
|
|
except ShoppingItem.DoesNotExist:
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
|
|
|
if request.method == 'GET':
|
|
serializer = ShoppingItemSerializer(ShoppingItem)
|
|
return Response(serializer.data)
|
|
|
|
elif request.method == 'PUT':
|
|
serializer = ShoppingItemSerializer(ShoppingItem, data=request.data)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response(serializer.data)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
elif request.method == 'DELETE':
|
|
ShoppingItem.delete()
|
|
return Response(status=status.HTTP_204_NO_CONTENT) |