def test_page_range_window(self): paginator = Paginator(range(100), 5) self.assertEqual(utils.page_range_window(paginator.page(1), 5), [1, 2, 3, 4, 5, 6, 7, 8, 9]) self.assertEqual(utils.page_range_window(paginator.page(5), 5), [1, 2, 3, 4, 5, 6, 7, 8, 9]) self.assertEqual(utils.page_range_window(paginator.page(9), 5), [5, 6, 7, 8, 9, 10, 11, 12, 13]) self.assertEqual(utils.page_range_window(paginator.page(20), 5), [12, 13, 14, 15, 16, 17, 18, 19, 20]) paginator = Paginator(range(3), 5) self.assertEqual(utils.page_range_window(paginator.page(1), 5), [1]) paginator = Paginator(range(3), 3) self.assertEqual(utils.page_range_window(paginator.page(1), 5), [1]) paginator = Paginator(range(4), 3) self.assertEqual(utils.page_range_window(paginator.page(1), 5), [1, 2])
def get(self, request, conversation): """ Render the messages sent & received for this conversation. Takes the following query parameters: :param str direction: Either 'inbound' or 'outbound', defaults to 'inbound' :param int page: The page to display for the pagination. :param str query: The query string to search messages for in the batch's inbound messages. """ direction = request.GET.get('direction', 'inbound') page = request.GET.get('p', 1) query = request.GET.get('q', None) token = None batch_id = conversation.batch.key # Paginator starts counting at 1 so 0 would also be invalid inbound_message_paginator = Paginator( PagedMessageCache(conversation.count_replies(), lambda start, stop: conversation.received_messages_in_cache( start, stop)), 20) outbound_message_paginator = Paginator( PagedMessageCache(conversation.count_sent_messages(), lambda start, stop: conversation.sent_messages_in_cache( start, stop)), 20) tag_context = { 'batch_id': batch_id, 'conversation': conversation, 'inbound_message_paginator': inbound_message_paginator, 'outbound_message_paginator': outbound_message_paginator, 'inbound_uniques_count': conversation.count_inbound_uniques(), 'outbound_uniques_count': conversation.count_outbound_uniques(), 'message_direction': direction, } # If we're doing a query we can shortcut the results as we don't # need all the message paginator stuff since we're loading the results # asynchronously with JavaScript. client = ms_client.Client(settings.MESSAGE_STORE_API_URL) if query and not token: token = client.match(batch_id, direction, [{ 'key': 'msg.content', 'pattern': re.escape(query), 'flags': 'i', }]) tag_context.update({ 'query': query, 'token': token, }) return tag_context elif query and token: match_result = ms_client.MatchResult(client, batch_id, direction, token, page=int(page), page_size=20) message_paginator = match_result.paginator tag_context.update({ 'token': token, 'query': query, }) elif direction == 'inbound': message_paginator = inbound_message_paginator else: message_paginator = outbound_message_paginator try: message_page = message_paginator.page(page) except PageNotAnInteger: message_page = message_paginator.page(1) except EmptyPage: message_page = message_paginator.page(message_paginator.num_pages) tag_context.update({ 'message_page': message_page, 'message_page_range': page_range_window(message_page, 5), }) return self.render_to_response(tag_context)