Commit d7a58537 by Bodor Máté

Implement vieset functions

parent 71726083
......@@ -3,17 +3,23 @@ from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework import status
from django.core.exceptions import ObjectDoesNotExist
from image.models import Image
from image.serializers import ImageSerializer
from image.serializers import ImageUpdateSerializer
class ImageViewSet(ViewSet):
def list(self, request):
images = Image.object.all()
serializer = ImageSerializer(images, many=True)
return HttpResponse(serializer.data)
image_list = []
images = Image.objects.all()
for image in images:
image_list.append(image.get())
serializer = ImageSerializer(image_list, many=True)
return Response(serializer.data)
def create(self, request):
serializer = ImageSerializer(data=request.data)
......@@ -30,17 +36,30 @@ class ImageViewSet(ViewSet):
return Response(serializer.data)
def retrieve(self, request, pk=None):
return Response(status=status.HTTP_204_NO_CONTENT)
queryset = Image.objects.all()
image = get_object_or_404(queryset, pk=pk)
serializer = ImageSerializer(instance=image)
image = image.get()
serializer = ImageSerializer(instance=image)
return Response(serializer.data)
def update(self, request, pk=None):
return HttpResponse("update")
serializer = ImageUpdateSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
queryset = Image.objects.all()
image = get_object_or_404(queryset, pk=pk)
image = image.update(serializer.validated_data)
serializer = ImageSerializer(instance=image)
return Response(serializer.data)
def partial_update(self, request, pk=None):
return HttpResponse("patch")
return self.update(request, pk)
def destroy(self, request, pk=None):
queryset = Image.objects.all()
image = get_object_or_404(queryset, pk=pk)
try:
image = Image.objects.get(id=pk)
image.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
except ObjectDoesNotExist:
return Response(status=status.HTTP_204_NO_CONTENT)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment