def test_page_key_to_page_key_naive(self): # The datetime naive version should work too, # but we can't compare the string key because # it vary depending on the platform dt = datetime.datetime( year=2012, month=3, day=9, hour=22, minute=30, second=40, microsecond=123123) self.assertEqual( serializers.page_key( serializers.to_page_key( *serializers.page_key( serializers.to_page_key(value=dt, pk=1)))), (dt, '1'))
def test_page_key_to_page_key_tight_api(self): tz = pytz.timezone('UTC') dt = tz.localize(datetime.datetime( year=2012, month=3, day=9, hour=22, minute=30, second=40, microsecond=123123)) self.assertEqual( serializers.to_page_key( *serializers.page_key( serializers.to_page_key(value=dt, pk=1))), '1331332240.123123-1') self.assertEqual( serializers.to_page_key( *serializers.page_key( serializers.to_page_key(value=None, pk=None))), '')
def pagination_ajax(request): if not request.is_ajax(): return Http404() try: value, pk = serializers.page_key(request.GET.get('p', '')) except serializers.InvalidPage: return Http404() try: page = paginator.paginate(query_set=Article.objects.all(), lookup_field='-date', value=value, pk=pk, per_page=20, move_to=paginator.NEXT_PAGE) except paginator.EmptyPage: data = {'error': "this page is empty"} else: data = { 'articles': [{ 'title': article.title } for article in page], 'has_next': page.has_next(), 'has_prev': page.has_previous(), 'next_objects_left': page.next_objects_left(limit=100), 'prev_objects_left': page.prev_objects_left(limit=100), 'next_pages_left': page.next_pages_left(limit=100), 'prev_pages_left': page.prev_pages_left(limit=100), 'next_page': serializers.to_page_key(**page.next_page()), 'prev_page': serializers.to_page_key(**page.prev_page()) } return HttpResponse(json.dumps(data), content_type="application/json")
def pagination_ajax(request): if not request.is_ajax(): return Http404() try: value, pk = serializers.page_key(request.GET.get('p', '')) except serializers.InvalidPage: return Http404() try: page = paginator.paginate( query_set=Article.objects.all(), lookup_field='-date', value=value, pk=pk, per_page=20, move_to=paginator.NEXT_PAGE) except paginator.EmptyPage: data = {'error': "this page is empty"} else: data = { 'articles': [{'title': article.title} for article in page], 'has_next': page.has_next(), 'has_prev': page.has_previous(), 'next_objects_left': page.next_objects_left(limit=100), 'prev_objects_left': page.prev_objects_left(limit=100), 'next_pages_left': page.next_pages_left(limit=100), 'prev_pages_left': page.prev_pages_left(limit=100), 'next_page': serializers.to_page_key(**page.next_page()), 'prev_page': serializers.to_page_key(**page.prev_page())} return HttpResponse(json.dumps(data), content_type="application/json")
def test_page_key_microseconds(self): tz = pytz.timezone('UTC') dt = tz.localize(datetime.datetime( year=2012, month=3, day=9, hour=22, minute=30, second=40, microsecond=0)) self.assertEqual( serializers.page_key( serializers.to_page_key(value=dt, pk=1)), (dt, '1'))
def test_page_key_to_page_key(self): tz = pytz.timezone('UTC') dt = tz.localize(datetime.datetime( year=2012, month=3, day=9, hour=22, minute=30, second=40, microsecond=123123)) key = serializers.to_page_key(value=dt, pk=1) self.assertEqual(key, '1331332240.123123-1') page_dt, page_key = serializers.page_key(key) self.assertEqual(page_dt, dt) self.assertEqual(page_key, '1')
def test_page_key_to_page_key_buenos_aires(self): tz = pytz.timezone('UTC') dt = tz.localize(datetime.datetime( year=2012, month=3, day=9, hour=22, minute=30, second=40, microsecond=123123)) bs_as = pytz.timezone('America/Argentina/Buenos_Aires') key = serializers.to_page_key(value=dt.astimezone(bs_as), pk=1) self.assertEqual(key, '1331332240.123123-1') page_dt, page_key = serializers.page_key(key) self.assertEqual(page_dt, dt) self.assertEqual(page_key, '1')
def paginate(request, query_set, lookup_field, per_page=15, page_var='value'): # TODO: remove try: value, pk = serializers.page_key( request.GET.get(page_var, '')) except serializers.InvalidPage: return Http404() try: return paginator.paginate( query_set=query_set, lookup_field='-' + lookup_field, value=value, pk=pk, per_page=per_page, move_to=paginator.NEXT_PAGE) except paginator.EmptyPage: raise Http404()