Ejemplo n.º 1
0
    def random(self, request, *args, **kwargs):
        player = get_player(request)

        query = Q(is_available_publicly=True)
        if player:
            query |= Q(owner=player)

        images = self.get_queryset().order_by('random_order').filter(query)
        images = images.reverse() if random.random() < .5 else images

        try:
            size = int(request.query_params.get('size'))
            size = size if size > 0 else self.default_random_size
        except (TypeError, ValueError):
            size = self.default_random_size

        paginator = Paginator(images, size * 2, readahead=100)
        # need to access first page before page_range works
        paginator.page(1)
        page_range = list(paginator.page_range)
        random.shuffle(page_range)

        image_list = []

        for page_num in page_range:
            image_list.extend(paginator.page(page_num))
            if len(image_list) >= size:
                break

        random.shuffle(image_list)

        serializer = self.get_serializer(instance=image_list[:size], many=True)
        return Response(serializer.data)
Ejemplo n.º 2
0
    def test_empty_page(self):
        paginator = Paginator(TestUser.objects.all().order_by("-first_name"), 1, allow_empty_first_page=False)
        with self.assertRaises(django_paginator.EmptyPage):
            self.assertEqual(paginator.page(5).object_list, [])

        paginator = Paginator(TestUser.objects.all().order_by("-first_name"), 1)
        self.assertEqual(paginator.page(5).object_list, [])
Ejemplo n.º 3
0
    def test_count_reads_ahead(self):
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 1, readahead=2)

        paginator.page(1)
        self.assertEqual(3, paginator.count)

        paginator.page(3)
        self.assertEqual(4, paginator.count)
Ejemplo n.º 4
0
    def test_that_marker_is_read(self):
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 1, readahead=1)
        paginator.page(2)

        with sleuth.watch("djangae.contrib.pagination.paginator._get_marker") as get_marker:
            paginator.page(4)

            self.assertTrue(get_marker.called)
            self.assertIsNotNone(get_marker.call_returns[0][0])
            self.assertEqual(1, get_marker.call_returns[0][1])
Ejemplo n.º 5
0
    def test_ordering_required_exception_is_thrown_when_no_order_specified(self):
        # The exception should not be thrown when an order is specified
        query_set = SimpleModelWithoutOrdering.objects.order_by("name")
        paginator = Paginator(query_set, 25, readahead=10)

        # The exception should not be thrown when the model has a default order
        query_set = SimpleModelWithOrdering.objects.all()
        paginator = Paginator(query_set, 25, readahead=10)

        # The exception should be thrown when no order is specified on the model or in the query set
        query_set = SimpleModelWithoutOrdering.objects.all()
        with self.assertRaises(PaginationOrderingRequired):
            paginator = Paginator(query_set, 25, readahead=10)
    def test_that_readahead_stores_markers(self):
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 1, readahead=4)

        expected_markers = [ None ] + list(TestUser.objects.all().order_by("first_name").values_list(paginator.field_required, flat=True))[:3]

        paginator.page(1)

        query_id = queryset_identifier(paginator.object_list)

        actual_markers = []
        for i in xrange(1, 5):
            actual_markers.append(_get_marker(query_id, i)[0])

        self.assertEqual(expected_markers, actual_markers)

        # Now change the per page number
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 2, readahead=4)

        all_markers = list(TestUser.objects.all().order_by("first_name").values_list(paginator.field_required, flat=True))
        expected_markers = [ None, all_markers[1] ]

        paginator.page(1)

        query_id = queryset_identifier(paginator.object_list)

        actual_markers = []
        for i in xrange(1, 3):
            actual_markers.append(_get_marker(query_id, i)[0])

        self.assertEqual(expected_markers, actual_markers)
    def test_count_up_to(self):
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 1, readahead=2)
        paginator.page(1)
        self.assertEqual(3, paginator.count)

        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 1, readahead=10)
        paginator.page(1)
        self.assertEqual(4, paginator.count)
    def test_ordering(self):
        paginator = Paginator(TestUser.objects.all().order_by("created"), 1, readahead=2)
        paginator.page(1)
        self.assertEqual([self.u1, self.u2, self.u3, self.u4], list(paginator.object_list))

        paginator = Paginator(TestUser.objects.all().order_by("-created"), 1, readahead=2)
        paginator.page(1)
        self.assertEqual([self.u4, self.u3, self.u2, self.u1], list(paginator.object_list))
    def test_empty_page(self):
        paginator = Paginator(TestUser.objects.all().order_by("-first_name"), 1, allow_empty_first_page=False)
        with self.assertRaises(django_paginator.EmptyPage):
            self.assertEqual(paginator.page(5).object_list, [])

        paginator = Paginator(TestUser.objects.all().order_by("-first_name"), 1)
        self.assertEqual(paginator.page(5).object_list, [])
Ejemplo n.º 10
0
    def test_that_readahead_stores_markers(self):
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 1, readahead=4)

        expected_markers = [ None ] + list(TestUser.objects.all().order_by("first_name").values_list(paginator.field_required, flat=True))[:3]

        paginator.page(1)

        query_id = queryset_identifier(paginator.object_list)

        actual_markers = []
        for i in xrange(1, 5):
            actual_markers.append(_get_marker(query_id, i)[0])

        self.assertEqual(expected_markers, actual_markers)

        # Now change the per page number
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 2, readahead=4)

        all_markers = list(TestUser.objects.all().order_by("first_name").values_list(paginator.field_required, flat=True))
        expected_markers = [ None, all_markers[1] ]

        paginator.page(1)

        query_id = queryset_identifier(paginator.object_list)

        actual_markers = []
        for i in xrange(1, 3):
            actual_markers.append(_get_marker(query_id, i)[0])

        self.assertEqual(expected_markers, actual_markers)
Ejemplo n.º 11
0
    def test_ordering(self):
        paginator = Paginator(TestUser.objects.all().order_by("created"), 1, readahead=2)
        paginator.page(1)
        self.assertEqual([self.u1, self.u2, self.u3, self.u4], list(paginator.object_list))

        paginator = Paginator(TestUser.objects.all().order_by("-created"), 1, readahead=2)
        paginator.page(1)
        self.assertEqual([self.u4, self.u3, self.u2, self.u1], list(paginator.object_list))
Ejemplo n.º 12
0
 def test_page_jump_updates_count_correctly(self):
     paginator = Paginator(TestUser.objects.all().order_by("first_name"),
                           1,
                           readahead=1)
     paginator.page(1)
     self.assertEqual(2, paginator.count)
     paginator.page(3)
     self.assertEqual(4, paginator.count)
    def test_that_marker_is_read(self):
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 1, readahead=1)
        paginator.page(2)

        with sleuth.watch("djangae.contrib.pagination.paginator._get_marker") as get_marker:
            paginator.page(4)

            self.assertTrue(get_marker.called)
            self.assertIsNotNone(get_marker.call_returns[0][0])
            self.assertEqual(1, get_marker.call_returns[0][1])
Ejemplo n.º 14
0
    def test_pages_correct(self):
        paginator = Paginator(TestUser.objects.all().order_by("first_name"),
                              1)  # 1 item per page

        self.assertEqual("A", paginator.page(1).object_list[0].first_name)
        self.assertEqual("A", paginator.page(2).object_list[0].first_name)
        self.assertEqual("B", paginator.page(3).object_list[0].first_name)
        self.assertEqual("B", paginator.page(4).object_list[0].first_name)

        paginator = Paginator(TestUser.objects.all().order_by(
            "first_name", "last_name"), 1)  # 1 item per page
        self.assertEqual(self.u1, paginator.page(1).object_list[0])
        self.assertEqual(self.u2, paginator.page(2).object_list[0])
        self.assertEqual(self.u3, paginator.page(3).object_list[0])
        self.assertEqual(self.u4, paginator.page(4).object_list[0])

        paginator = Paginator(TestUser.objects.all().order_by(
            "first_name", "-last_name"), 1)  # 1 item per page
        self.assertEqual(self.u2, paginator.page(1).object_list[0])
        self.assertEqual(self.u1, paginator.page(2).object_list[0])
        self.assertEqual(self.u4, paginator.page(3).object_list[0])
        self.assertEqual(self.u3, paginator.page(4).object_list[0])

        paginator = Paginator(TestUser.objects.all().order_by("-first_name"),
                              1)  # 1 item per page
        self.assertEqual(self.u4, paginator.page(1).object_list[0])
        self.assertEqual(self.u3, paginator.page(2).object_list[0])
        self.assertEqual(self.u2, paginator.page(3).object_list[0])
        self.assertEqual(self.u1, paginator.page(4).object_list[0])

        with self.assertRaises(PaginationOrderingRequired):
            paginator = Paginator(TestUser.objects.all().order_by(
                "-first_name", "last_name"), 1)  # 1 item per page
            list(paginator.page(1).object_list)

        # test paging when last page has less than per_page objects
        paginator = Paginator(TestUser.objects.all().order_by("first_name"),
                              3)  # 3 items per page
        self.assertEqual(sorted([self.u1.pk, self.u2.pk, self.u3.pk]),
                         sorted([x.pk for x in paginator.page(1).object_list]))
        self.assertEqual(sorted([self.u4.pk]),
                         sorted([x.pk for x in paginator.page(2).object_list]))
Ejemplo n.º 15
0
 def test_page_jump_updates_count_correctly(self):
     paginator = Paginator(TestUser.objects.all().order_by("first_name"), 1, readahead=1)
     paginator.page(1)
     self.assertEqual(2, paginator.count)
     paginator.page(3)
     self.assertEqual(4, paginator.count)
Ejemplo n.º 16
0
 def test_default_ordering(self):
     """ Tests that pagination works using just the model's default ordering. """
     paginator = Paginator(TestUser.objects.all(), 1, readahead=2)
     paginator.page(1)
     self.assertEqual([self.u1, self.u2, self.u3, self.u4], list(paginator.object_list))
Ejemplo n.º 17
0
 def test_default_ordering(self):
     """ Tests that pagination works using just the model's default ordering. """
     paginator = Paginator(TestUser.objects.all(), 1, readahead=2)
     paginator.page(1)
     self.assertEqual([self.u1, self.u2, self.u3, self.u4],
                      list(paginator.object_list))
Ejemplo n.º 18
0
    def test_pages_correct(self):
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 1) # 1 item per page

        self.assertEqual("A", paginator.page(1).object_list[0].first_name)
        self.assertEqual("A", paginator.page(2).object_list[0].first_name)
        self.assertEqual("B", paginator.page(3).object_list[0].first_name)
        self.assertEqual("B", paginator.page(4).object_list[0].first_name)

        paginator = Paginator(TestUser.objects.all().order_by("first_name", "last_name"), 1) # 1 item per page
        self.assertEqual(self.u1, paginator.page(1).object_list[0])
        self.assertEqual(self.u2, paginator.page(2).object_list[0])
        self.assertEqual(self.u3, paginator.page(3).object_list[0])
        self.assertEqual(self.u4, paginator.page(4).object_list[0])

        paginator = Paginator(TestUser.objects.all().order_by("first_name", "-last_name"), 1) # 1 item per page
        self.assertEqual(self.u2, paginator.page(1).object_list[0])
        self.assertEqual(self.u1, paginator.page(2).object_list[0])
        self.assertEqual(self.u4, paginator.page(3).object_list[0])
        self.assertEqual(self.u3, paginator.page(4).object_list[0])

        paginator = Paginator(TestUser.objects.all().order_by("-first_name"), 1) # 1 item per page
        self.assertEqual(self.u4, paginator.page(1).object_list[0])
        self.assertEqual(self.u3, paginator.page(2).object_list[0])
        self.assertEqual(self.u2, paginator.page(3).object_list[0])
        self.assertEqual(self.u1, paginator.page(4).object_list[0])

        with self.assertRaises(PaginationOrderingRequired):
            paginator = Paginator(TestUser.objects.all().order_by("-first_name", "last_name"), 1) # 1 item per page
            list(paginator.page(1).object_list)

        # test paging when last page has less than per_page objects
        paginator = Paginator(TestUser.objects.all().order_by("first_name"), 3) # 3 items per page
        self.assertEqual(
            sorted([self.u1.pk, self.u2.pk, self.u3.pk]),
            sorted([x.pk for x in paginator.page(1).object_list])
        )
        self.assertEqual(
            sorted([self.u4.pk]),
            sorted([x.pk for x in paginator.page(2).object_list])
        )