예제 #1
0
    def test_format_paginated_url(self):
        request = lambda url: cast(Any, RequestFactory().get(url))

        self.assertEqual(
            format_paginated_url(request("/api/some_url"),
                                 offset=0,
                                 page_size=10),
            "http://testserver/api/some_url?offset=10",
        )
        self.assertEqual(
            format_paginated_url(request("/api/some_url?offset=0"),
                                 offset=0,
                                 page_size=10), "api/some_url?offset=10")
        self.assertEqual(
            format_paginated_url(request("/api/some_url?offset=0"),
                                 offset=0,
                                 page_size=10,
                                 mode=PaginationMode.previous),
            None,
        )
        self.assertEqual(
            format_paginated_url(request("/api/some_url?offset=0"),
                                 offset=20,
                                 page_size=10,
                                 mode=PaginationMode.previous),
            "api/some_url?offset=0",
        )
예제 #2
0
    def trend(self, request: request.Request, *args: Any, **kwargs: Any) -> Response:
        try:
            serializer = TrendSerializer(request=request)
            serializer.is_valid(raise_exception=True)
        except Exception as e:
            capture_exception(e)

        result = self.calculate_trends(request)
        filter = Filter(request=request, team=self.team)
        next = (
            format_paginated_url(request, filter.offset, BREAKDOWN_VALUES_LIMIT)
            if len(result["result"]) >= BREAKDOWN_VALUES_LIMIT
            else None
        )
        return Response({**result, "next": next})
예제 #3
0
파일: insight.py 프로젝트: PostHog/posthog
    def trend(self, request: request.Request, *args: Any, **kwargs: Any):
        try:
            serializer = TrendSerializer(request=request)
            serializer.is_valid(raise_exception=True)
        except Exception as e:
            capture_exception(e)

        result = self.calculate_trends(request)
        filter = Filter(request=request, team=self.team)
        next = (format_paginated_url(request, filter.offset,
                                     BREAKDOWN_VALUES_LIMIT)
                if len(result["result"]) >= BREAKDOWN_VALUES_LIMIT else None)
        if self.request.accepted_renderer.format == "csv":
            csvexport = []
            for item in result["result"]:
                line = {"series": item["label"]}
                for index, data in enumerate(item["data"]):
                    line[item["labels"][index]] = data
                csvexport.append(line)
            renderer = csvrenderers.CSVRenderer()
            renderer.header = csvexport[0].keys()
            export = renderer.render(csvexport)
            if request.GET.get("export_insight_id"):
                export = "{}/insights/{}/\n".format(
                    SITE_URL,
                    request.GET["export_insight_id"]).encode() + export

            response = HttpResponse(export)
            response[
                "Content-Disposition"] = 'attachment; filename="{name} ({date_from} {date_to}) from PostHog.csv"'.format(
                    name=slugify(request.GET.get("export_name", "export")),
                    date_from=filter.date_from.strftime("%Y-%m-%d -")
                    if filter.date_from else "up until",
                    date_to=filter.date_to.strftime("%Y-%m-%d"),
                )
            return response
        return Response({**result, "next": next})
예제 #4
0
def paginated_result(
    entites: Union[List[Dict[str, Any]], ReturnDict], request: request.Request, offset: int = 0,
) -> Optional[str]:
    return format_paginated_url(request, offset, 100) if len(entites) > 99 else None