Ejemplo n.º 1
0
    def get(self, request):
        """Returns JSON feed of first 10000 results

        This feels like a duplication of the front-page dashboard search
        logic, but it's separate which allows us to handle multiple
        values.

        """
        search = models.ResponseMappingType.search()
        f = F()

        if "happy" in request.GET:
            happy = {"0": False, "1": True}.get(request.GET["happy"], None)
            if happy is not None:
                f &= F(happy=happy)

        if "platforms" in request.GET:
            platforms = request.GET["platforms"].split(",")
            if platforms:
                f &= F(platform__in=platforms)

        if "locales" in request.GET:
            locales = request.GET["locales"].split(",")
            if locales:
                f &= F(locale__in=locales)

        if "products" in request.GET:
            products = request.GET["products"].split(",")
            if products:
                f &= F(product__in=products)

                if "versions" in request.GET:
                    versions = request.GET["versions"].split(",")
                    if versions:
                        f &= F(version__in=versions)

        date_start = smart_date(request.GET.get("date_start", None))
        date_end = smart_date(request.GET.get("date_end", None))
        delta = smart_timedelta(request.GET.get("date_delta", None))

        if delta is not None:
            if date_end is not None:
                date_start = date_end - delta
            elif date_start is not None:
                date_end = date_start + delta
            else:
                date_end = date.today()
                date_start = date_end - delta

        # We restrict public API access to the last 6 months.
        six_months_ago = date.today() - timedelta(days=180)
        if date_start:
            date_start = max(six_months_ago, date_start)
            f &= F(created__gte=date_start)
        if date_end:
            date_end = max(six_months_ago, date_end)
            f &= F(created__lte=date_end)

        search = search.filter(f)

        search_query = request.GET.get("q", None)
        if search_query is not None:
            search = search.query(description__sqs=search_query)

        # FIXME: Probably want to make this specifyable
        search = search.order_by("-created")

        # Explicitly include only publicly visible fields
        search = search.values_dict(models.ResponseMappingType.public_fields())

        maximum = smart_int(request.GET.get("max", None))
        maximum = maximum or 1000
        maximum = min(max(1, maximum), 10000)

        responses = models.ResponseMappingType.reshape(search[:maximum])
        return rest_framework.response.Response({"count": len(responses), "results": list(responses)})
Ejemplo n.º 2
0
 def test_valid(self):
     eq_(smart_timedelta('1d'), datetime.timedelta(days=1))
     eq_(smart_timedelta('14d'), datetime.timedelta(days=14))
Ejemplo n.º 3
0
 def test_invalid(self):
     eq_(smart_timedelta('0d', 'fallback'), 'fallback')
     eq_(smart_timedelta('foo', 'fallback'), 'fallback')
     eq_(smart_timedelta('d', 'fallback'), 'fallback')
Ejemplo n.º 4
0
    def get(self, request):
        """Returns JSON feed of first 1000 results

        This feels like a duplication of the front-page dashboard search
        logic, but it's separate which allows us to handle multiple
        values.

        """
        search = models.ResponseMappingType.search()
        f = F()

        if 'happy' in request.GET:
            happy = {'0': False, '1': True}.get(request.GET['happy'], None)
            if happy is not None:
                f &= F(happy=happy)

        if 'platforms' in request.GET:
            platforms = request.GET['platforms'].split(',')
            if platforms:
                f &= F(platform__in=platforms)

        if 'locales' in request.GET:
            locales = request.GET['locales'].split(',')
            if locales:
                f &= F(locale__in=locales)

        if 'products' in request.GET:
            products = request.GET['products'].split(',')
            if products:
                f &= F(product__in=products)

                if 'versions' in request.GET:
                    versions = request.GET['versions'].split(',')
                    if versions:
                        f &= F(version__in=versions)

        date_start = smart_date(request.GET.get('date_start', None))
        date_end = smart_date(request.GET.get('date_end', None))
        delta = smart_timedelta(request.GET.get('date_delta', None))

        if delta is not None:
            if date_end is not None:
                date_start = date_end - delta
            elif date_start is not None:
                date_end = date_start + delta
            else:
                date_end = date.today()
                date_start = date_end - delta

        if date_start:
            f &= F(created__gte=date_start)
        if date_end:
            f &= F(created__lte=date_end)

        search = search.filter(f)

        search_query = request.GET.get('q', None)
        if search_query is not None:
            search = search.query(description__sqs=search_query)

        # FIXME: Probably want to make this specifyable
        search = search.order_by('-created')

        # Explicitly include only publicly visible fields
        search = search.values_dict(models.ResponseMappingType.public_fields())

        # FIXME: We're omitting paging here for now. We might want to
        # add that at some point.
        responses = search[:1000]
        return rest_framework.response.Response({
            'count': len(responses),
            'results': list(responses)
        })