Пример #1
0
    def publish(self, request, **kwargs):
        """sets the `published` value of the `Content`

        :param request: a WSGI request object
        :param kwargs: keyword arguments (optional)
        :return: `rest_framework.response.Response`
        """
        content = self.get_object()

        if "published" in get_request_data(request):
            if not get_request_data(request)["published"]:
                content.published = None
            else:
                publish_dt = parse_datetime(get_request_data(request)["published"])
                if publish_dt:
                    publish_dt = publish_dt.astimezone(timezone.utc)
                else:
                    publish_dt = None
                content.published = publish_dt
        else:
            content.published = timezone.now()

        content.save()
        LogEntry.objects.log(request.user, content, content.get_status())
        return Response({"status": content.get_status(), "published": content.published})
Пример #2
0
    def create_token(self, request, **kwargs):
        """Create a new obfuscated url info to use for accessing unpublished content.

        :param request: a WSGI request object
        :param kwargs: keyword arguments (optional)
        :return: `rest_framework.response.Response`
        """

        data = {
            "content": self.get_object().id,
            "create_date": get_request_data(request)["create_date"],
            "expire_date": get_request_data(request)["expire_date"]
        }
        serializer = ObfuscatedUrlInfoSerializer(data=data)
        if not serializer.is_valid():
            return Response(
                serializer.errors,
                status=status.HTTP_400_BAD_REQUEST,
                content_type="application/json",
            )
        serializer.save()

        return Response(serializer.data,
                        status=status.HTTP_200_OK,
                        content_type="application/json")
Пример #3
0
    def publish(self, request, **kwargs):
        """sets the `published` value of the `Content`

        :param request: a WSGI request object
        :param kwargs: keyword arguments (optional)
        :return: `rest_framework.response.Response`
        """
        content = self.get_object()

        if "published" in get_request_data(request):
            if not get_request_data(request)["published"]:
                content.published = None
            else:
                publish_dt = parse_datetime(
                    get_request_data(request)["published"])
                if publish_dt:
                    publish_dt = publish_dt.astimezone(timezone.utc)
                else:
                    publish_dt = None
                content.published = publish_dt
        else:
            content.published = timezone.now()

        content.save()
        LogEntry.objects.log(request.user, content, content.get_status())
        return Response({
            "status": content.get_status(),
            "published": content.published
        })
Пример #4
0
    def create(self, request, *args, **kwargs):
        """
        Grabbing the user from request.user, and the rest of the method
        is the same as ModelViewSet.create().

        :param request: a WSGI request object
        :param args: inline arguments (optional)
        :param kwargs: keyword arguments (optional)
        :return: `rest_framework.response.Response`
        :raise: 400
        """
        data = get_request_data(request).copy()
        data["user"] = request.user.id
        serializer = self.get_serializer(data=data, files=request.FILES)

        if serializer.is_valid():
            self.pre_save(serializer.object)
            self.object = serializer.save(force_insert=True)
            self.post_save(self.object, created=True)
            headers = self.get_success_headers(serializer.data)
            return Response(
                serializer.data,
                status=status.HTTP_201_CREATED,
                headers=headers
            )

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #5
0
    def contributions(self, request, **kwargs):
        """gets or adds contributions

        :param request: a WSGI request object
        :param kwargs: keyword arguments (optional)
        :return: `rest_framework.response.Response`
        """
        # Check if the contribution app is installed
        if Contribution not in get_models():
            return Response([])

        content_pk = kwargs.get('pk', None)
        if content_pk is None:
            return Response([], status=status.HTTP_404_NOT_FOUND)

        queryset = Contribution.search_objects.search().filter(
            es_filter.Term(**{'content.id': content_pk})
        )
        if request.method == "POST":
            serializer = ContributionSerializer(
                queryset[:queryset.count()].sort('id')[:25],
                data=get_request_data(request),
                many=True)
            if not serializer.is_valid():
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
            serializer.save()
            return Response(serializer.data)
        else:
            serializer = ContributionSerializer(queryset[:queryset.count()].sort('id'), many=True)
            return Response(serializer.data)
Пример #6
0
    def create(self, request, *args, **kwargs):
        """
        Grabbing the user from request.user, and the rest of the method
        is the same as ModelViewSet.create().

        :param request: a WSGI request object
        :param args: inline arguments (optional)
        :param kwargs: keyword arguments (optional)
        :return: `rest_framework.response.Response`
        :raise: 400
        """
        data = get_request_data(request).copy()
        data["user"] = request.user.id
        serializer = self.get_serializer(data=data, files=request.FILES)

        if serializer.is_valid():
            self.pre_save(serializer.object)
            self.object = serializer.save(force_insert=True)
            self.post_save(self.object, created=True)
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data,
                            status=status.HTTP_201_CREATED,
                            headers=headers)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #7
0
    def contributions(self, request, **kwargs):
        """gets or adds contributions

        :param request: a WSGI request object
        :param kwargs: keyword arguments (optional)
        :return: `rest_framework.response.Response`
        """
        # Check if the contribution app is installed
        if Contribution not in get_models():
            return Response([])

        if request.method == "POST":
            serializer = ContributionSerializer(data=get_request_data(request),
                                                many=True)
            if not serializer.is_valid():
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            serializer.save()
            return Response(serializer.data)
        else:
            content_pk = kwargs.get('pk', None)
            if content_pk is None:
                return Response([], status=status.HTTP_404_NOT_FOUND)
            queryset = Contribution.search_objects.search().filter(
                es_filter.Term(**{'content.id': content_pk}))
            serializer = ContributionSerializer(
                queryset[:queryset.count()].sort('id'), many=True)
            return Response(serializer.data)
Пример #8
0
 def group_count(self, request, **kwargs):
     params = dict(
         groups=[
             dict(get_request_data(request))
         ]
     )
     qs = self.get_filtered_queryset(params, sort_pinned=False)
     return Response(dict(count=qs.count()))
Пример #9
0
    def create_token(self, request, **kwargs):
        """Create a new obfuscated url info to use for accessing unpublished content.

        :param request: a WSGI request object
        :param kwargs: keyword arguments (optional)
        :return: `rest_framework.response.Response`
        """

        data = {
            "content": self.get_object().id,
            "create_date": get_request_data(request)["create_date"],
            "expire_date": get_request_data(request)["expire_date"]
        }
        serializer = ObfuscatedUrlInfoSerializer(data=data)
        if not serializer.is_valid():
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST, content_type="application/json")
        serializer.save()

        return Response(serializer.data, status=status.HTTP_200_OK, content_type="application/json")
Пример #10
0
    def list(self, request, *args, **kwargs):
        """Filter Content with a custom search.
        {
            "query": SEARCH_QUERY
            "preview": true
        }
        "preview" is optional and, when true, will include
        items that would normally be removed due to "excluded_ids".
        """

        queryset = self.get_filtered_queryset(get_request_data(request))
        # Switch between paginated or standard style responses
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)
Пример #11
0
    def list(self, request, *args, **kwargs):
        """Filter Content with a custom search.
        {
            "query": SEARCH_QUERY
            "preview": true
        }
        "preview" is optional and, when true, will include
        items that would normally be removed due to "excluded_ids".
        """

        queryset = self.get_filtered_queryset(get_request_data(request))
        # Switch between paginated or standard style responses
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)
Пример #12
0
    def contributions(self, request, **kwargs):
        """gets or adds contributions

        :param request: a WSGI request object
        :param kwargs: keyword arguments (optional)
        :return: `rest_framework.response.Response`
        """
        # Check if the contribution app is installed
        if Contribution not in get_models():
            return Response([])
        queryset = Contribution.objects.filter(content=self.get_object())
        if request.method == "POST":
            serializer = ContributionSerializer(
                queryset,
                data=get_request_data(request),
                many=True)
            if not serializer.is_valid():
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
            serializer.save()
            return Response(serializer.data)
        else:
            serializer = ContributionSerializer(queryset, many=True)
            return Response(serializer.data)
Пример #13
0
 def count(self, request, **kwargs):
     qs = self.get_filtered_queryset(get_request_data(request), sort_pinned=False)
     return Response(dict(count=qs.count()))
Пример #14
0
 def group_count(self, request, **kwargs):
     params = dict(groups=[dict(get_request_data(request))])
     qs = self.get_filtered_queryset(params, sort_pinned=False)
     return Response(dict(count=qs.count()))
Пример #15
0
 def count(self, request, **kwargs):
     qs = self.get_filtered_queryset(get_request_data(request),
                                     sort_pinned=False)
     return Response(dict(count=qs.count()))