Example #1
0
    def components(self, request, **kwargs):
        obj = self.get_object()
        if request.method == "POST":
            if not request.user.has_perm("project.edit", obj):
                self.permission_denied(request, "Can not create components")
            with transaction.atomic():
                serializer = ComponentSerializer(
                    data=request.data, context={"request": request, "project": obj}
                )
                serializer.is_valid(raise_exception=True)
                serializer.save()
                serializer.instance.post_create(self.request.user)
                return Response(
                    serializer.data,
                    status=HTTP_201_CREATED,
                    headers={
                        "Location": str(serializer.data[api_settings.URL_FIELD_NAME])
                    },
                )

        queryset = obj.component_set.filter_access(self.request.user).order_by("id")
        page = self.paginate_queryset(queryset)

        serializer = ComponentSerializer(
            page, many=True, context={"request": request}, remove_fields=("project",)
        )

        return self.get_paginated_response(serializer.data)
Example #2
0
    def components(self, request, **kwargs):
        obj = self.get_object()
        if request.method == 'POST':
            if not request.user.has_perm('project.edit', obj):
                self.permission_denied(request,
                                       message='Can not create components')
            with transaction.atomic():
                serializer = ComponentSerializer(data=request.data,
                                                 context={'request': request})
                serializer.is_valid(raise_exception=True)
                serializer.save(project=obj)
                serializer.instance.post_create(self.request.user)
                return Response(
                    serializer.data,
                    status=status.HTTP_201_CREATED,
                    headers={
                        'Location':
                        str(serializer.data[api_settings.URL_FIELD_NAME])
                    },
                )

        queryset = obj.component_set.all().order_by('id')
        page = self.paginate_queryset(queryset)

        serializer = ComponentSerializer(page,
                                         many=True,
                                         context={'request': request},
                                         remove_fields=('project', ))

        return self.get_paginated_response(serializer.data)