Commit d7a58537 by Bodor Máté

Implement vieset functions

parent 71726083
...@@ -3,17 +3,23 @@ from rest_framework.viewsets import ViewSet ...@@ -3,17 +3,23 @@ from rest_framework.viewsets import ViewSet
from rest_framework.response import Response from rest_framework.response import Response
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import get_object_or_404 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.models import Image
from image.serializers import ImageSerializer from image.serializers import ImageSerializer
from image.serializers import ImageUpdateSerializer
class ImageViewSet(ViewSet): class ImageViewSet(ViewSet):
def list(self, request): def list(self, request):
images = Image.object.all() image_list = []
serializer = ImageSerializer(images, many=True) images = Image.objects.all()
return HttpResponse(serializer.data) for image in images:
image_list.append(image.get())
serializer = ImageSerializer(image_list, many=True)
return Response(serializer.data)
def create(self, request): def create(self, request):
serializer = ImageSerializer(data=request.data) serializer = ImageSerializer(data=request.data)
...@@ -30,17 +36,30 @@ class ImageViewSet(ViewSet): ...@@ -30,17 +36,30 @@ class ImageViewSet(ViewSet):
return Response(serializer.data) return Response(serializer.data)
def retrieve(self, request, pk=None): 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): 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): def partial_update(self, request, pk=None):
return HttpResponse("patch") return self.update(request, pk)
def destroy(self, request, pk=None): def destroy(self, request, pk=None):
queryset = Image.objects.all() try:
image = get_object_or_404(queryset, pk=pk) image = Image.objects.get(id=pk)
image.delete() image.delete()
return Response(status=status.HTTP_204_NO_CONTENT) 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