Beispiel #1
0
    def tags(self, request, pk=None):

        publicacion = self.get_object()
        if request.method == 'GET':
            serialized = TagSerializer(publicacion.tags, many=True)
            return Response(status=status.HTTP_200_OK, data=serialized.data)

        if request.method == 'POST':
            tag = {
                "nombre": request.data['nombre'],
                "publicaciones": [publicacion.id]
            }
            serialized = TagSerializer(data=tag)
            if serialized.is_valid():
                serialized.save()
                return Response(status=status.HTTP_200_OK,
                                data=serialized.data)
            else:
                return Response(status=status.HTTP_200_OK,
                                data=serialized.errors)

        if request.method == 'DELETE':
            tag_id = request.data['id']
            tag = Tag.objects.get(id=tag_id)
            tag.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
 def put(self, request, pk, format=None):
     tag = self.get_object(pk=pk)
     serializer = TagSerializer(tag, data=request.data)
     if serializer.is_valid():
         if request.user.is_superuser:
             serializer.save()
             return Response(serializer.data, status=status.HTTP_200_OK)
         else:
             return Response(status=status.HTTP_401_UNAUTHORIZED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
 def post(self, request, format=None):
     serializer = TagSerializer(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)
Beispiel #4
0
    def perform_create(self, serializer: TagSerializer):
        serializer.save(user=self.request.user)

        return super(TagViewSet, self).perform_create(serializer)