Beispiel #1
0
    def create(self, request, **kwargs):
        data = request.data

        serializer = CategorySerializer(data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=status.HTTP_201_CREATED)
Beispiel #2
0
    def partial_update(self, request, pk=None, **kwargs):
        category = get_object_or_404(self.queryset, pk=pk)

        serializer = CategorySerializer(instance=category,
                                        data=request.data,
                                        partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(status=status.HTTP_200_OK)
Beispiel #3
0
def category_list(request, format=None):
    """List all code categories, or create a new category."""
    if request.method == 'GET':
        categories = Category.get_active()
        serializer = CategorySerializer(categories, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = CategorySerializer(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 category_detail(request, pk, format=None):
    """Retrieve, update or delete a code category."""
    try:
        category = Category.objects.get(pk=pk)
    except Category.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = CategorySerializer(category)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = CategorySerializer(category,
                                        data=request.data,
                                        partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        category.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Beispiel #5
0
 def post(self, request):
     serializer = CategorySerializer(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 #6
0
    def perform_create(self, serializer: CategorySerializer):

        serializer.save(user=self.request.user)

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