From patchwork Wed Sep 1 16:57:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 1523323 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; dkim=fail reason="key not found in DNS" header.d=that.guru header.i=@that.guru header.a=rsa-sha256 header.s=x header.b=QsjlWjVm; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H09HS2YBJz9sW8 for ; Thu, 2 Sep 2021 02:58:28 +1000 (AEST) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4H09HS1MZgz2yJG for ; Thu, 2 Sep 2021 02:58:28 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" header.d=that.guru header.i=@that.guru header.a=rsa-sha256 header.s=x header.b=QsjlWjVm; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=that.guru (client-ip=136.175.108.127; helo=mail-108-mta127.mxroute.com; envelope-from=stephen@that.guru; receiver=) Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" header.d=that.guru header.i=@that.guru header.a=rsa-sha256 header.s=x header.b=QsjlWjVm; dkim-atps=neutral Received: from mail-108-mta127.mxroute.com (mail-108-mta127.mxroute.com [136.175.108.127]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4H09H96vlnz2xt3 for ; Thu, 2 Sep 2021 02:58:13 +1000 (AEST) Received: from filter004.mxroute.com ([149.28.56.236] filter004.mxroute.com) (Authenticated sender: mN4UYu2MZsgR) by mail-108-mta127.mxroute.com (ZoneMTA) with ESMTPSA id 17ba24beadb00074ba.001 for (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256); Wed, 01 Sep 2021 16:58:09 +0000 X-Zone-Loop: 108f14f4c50842b3ca0eaac4f90141c24f2c0e845e18 X-Originating-IP: [149.28.56.236] DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=x; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=XzNNE+41gh/IYBj6q+G7fH3LpgUkWTsj+uSDkUPOwSw=; b=QsjlWjVmDPlXEXL6JfV6CFhuNh a8uRp+2FJfO13MtiMKrkA1Hwx8Lj6hqxT6ifegpLzAOqIMuQzhWayiLQl5zRSM7BlN1kDK8/oWqz1 OQIas0AQ4lgLU8XCyGM1cNl6yvG8JyIA5LAP9mvZ4FpFbumaOv664RimD+qGjYaKgGsMgT6yK55Wt 4NKaeR3phFNPdoxveuzvFIxKqmT9B2agU1JbKtWOXK8oRHTq1cV0lC87u5L2fXpuvpWtak3xisSXH vvNOmAkq1dSXaOXR5lrJ0eUeb+aK9UafvPcIM0JhYlkEla5Pjc8XD/clxVmty5/C9/sPSdKIFgH9a wkLWoedQ==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [RFC PATCH v2 04/19] REST: Include 'first', 'last' refs in 'Link' header Date: Wed, 1 Sep 2021 17:57:41 +0100 Message-Id: <20210901165756.181192-5-stephen@that.guru> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210901165756.181192-1-stephen@that.guru> References: <20210901165756.181192-1-stephen@that.guru> MIME-Version: 1.0 X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" I've no idea why this wasn't done from day one, but it's a huge usability win for anyone attempting to do pagination with this header. Signed-off-by: Stephen Finucane --- patchwork/api/base.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git patchwork/api/base.py patchwork/api/base.py index d870a511..7baac275 100644 --- patchwork/api/base.py +++ patchwork/api/base.py @@ -12,6 +12,7 @@ from rest_framework.pagination import PageNumberPagination from rest_framework.response import Response from rest_framework.serializers import HyperlinkedIdentityField from rest_framework.serializers import HyperlinkedModelSerializer +from rest_framework.utils.urls import replace_query_param from patchwork.api import utils @@ -59,19 +60,33 @@ class LinkHeaderPagination(PageNumberPagination): max_page_size = settings.MAX_REST_RESULTS_PER_PAGE page_size_query_param = 'per_page' + def get_first_link(self): + url = self.request.build_absolute_uri() + return replace_query_param(url, self.page_query_param, 1) + + def get_last_link(self): + url = self.request.build_absolute_uri() + page_number = self.page.paginator.num_pages + return replace_query_param(url, self.page_query_param, page_number) + def get_paginated_response(self, data): next_url = self.get_next_link() previous_url = self.get_previous_link() + first_url = self.get_first_link() + last_url = self.get_last_link() + + links = [] + + if next_url is not None: + links.append(f'<{next_url}>; rel="next"') + + if previous_url is not None: + links.append(f'<{previous_url}>; rel="prev"') + + links.append(f'<{first_url}>; rel="first"') + links.append(f'<{last_url}>; rel="last"') - link = '' - if next_url is not None and previous_url is not None: - link = '<{next_url}>; rel="next", <{previous_url}>; rel="prev"' - elif next_url is not None: - link = '<{next_url}>; rel="next"' - elif previous_url is not None: - link = '<{previous_url}>; rel="prev"' - link = link.format(next_url=next_url, previous_url=previous_url) - headers = {'Link': link} if link else {} + headers = {'Link': ', '.join(links)} if links else {} return Response(data, headers=headers)