From patchwork Tue Oct 15 21:30:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeremy Cline X-Patchwork-Id: 1177421 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46t7rf57hDz9sPZ for ; Wed, 16 Oct 2019 08:30:42 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 46t7rf3KYYzDr6P for ; Wed, 16 Oct 2019 08:30:42 +1100 (AEDT) X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=redhat.com (client-ip=209.132.183.28; helo=mx1.redhat.com; envelope-from=jcline@redhat.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (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 46t7rX21dlzDr45 for ; Wed, 16 Oct 2019 08:30:35 +1100 (AEDT) Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BA514757C5 for ; Tue, 15 Oct 2019 21:30:31 +0000 (UTC) Received: by mail-qt1-f197.google.com with SMTP id 59so22644758qtc.5 for ; Tue, 15 Oct 2019 14:30:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=XB+eNao9BnUSOaIrGy01UHZ0a31kqXYow9ke3+wQUkA=; b=VbiFgYyXfBjh+UgIW6u6QxbCVDOpxTQJP20SAiwnVihCNQH1P2dUZELtB13bGBJwAI xVWX3qOfex6GFuIxJYXHV9x3EfCRvWJ45+jv/F9UTioqEAAQnobcY24CK8KIZLSWmK87 5QOipp++f/XZbA3iZfxvt3+Jq4b4ZEy+M/K0oHjBiOOxNh7pdjkN/IvBPL53AgzDrbOm Z8IvNwK8hgrAtIBCiiqQ6AaPJLV+9LpqGccIDhapN7+CK/8bbtoteE/04doIC3peUBB1 0Kd7euauboykFLfJgnPSQS0UV1vx7+ZKKJEStKmBp57hyvImb8pTivgG3CXDk98psLRk iTUA== X-Gm-Message-State: APjAAAXNxtI532XsY3Ro3k2f4WVe3MZ+rMp/AaiFwQ8LkldeahMMXuUY v4fE4w3xHZ56l450cQAEdcuDjz7fPW9WAq9gS6c0Fb/zTBKGv2xgQpj+hY3UxsnHys/J4ahufS9 RH27+3c89WMtra+ZJLjZ6dw== X-Received: by 2002:a0c:b35c:: with SMTP id a28mr16730058qvf.238.1571175030592; Tue, 15 Oct 2019 14:30:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqzscL9ApvRGs/neY4LrA4tnes7vzc7PI7l6WAJ1YlQjmrj3SNxvnO4rWXqOLEAJwlI+0HzKfw== X-Received: by 2002:a0c:b35c:: with SMTP id a28mr16730010qvf.238.1571175030180; Tue, 15 Oct 2019 14:30:30 -0700 (PDT) Received: from laptop.jcline.org.com ([136.56.87.133]) by smtp.gmail.com with ESMTPSA id c131sm10739312qke.24.2019.10.15.14.30.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2019 14:30:29 -0700 (PDT) From: Jeremy Cline To: patchwork@lists.ozlabs.org Subject: [PATCH] Allow ordering events by date Date: Tue, 15 Oct 2019 17:30:11 -0400 Message-Id: <20191015213011.17467-1-jcline@redhat.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 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" By default, the events API orders events by date in descending order (newest first). However, it's useful to be able to order the events by oldest events first. For example, when a client is polling the events API for new events since a given date and wishes to process them in chronological order. Signed-off-by: Jeremy Cline --- patchwork/api/event.py | 2 +- patchwork/tests/api/test_event.py | 18 ++++++++++++++++++ ...-order-events-by-date-7484164761c5231b.yaml | 5 +++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/api-order-events-by-date-7484164761c5231b.yaml diff --git a/patchwork/api/event.py b/patchwork/api/event.py index c0d973d..e6d467d 100644 --- a/patchwork/api/event.py +++ b/patchwork/api/event.py @@ -77,7 +77,7 @@ class EventList(ListAPIView): serializer_class = EventSerializer filter_class = filterset_class = EventFilterSet page_size_query_param = None # fixed page size - ordering_fields = () + ordering_fields = ('date',) ordering = '-date' def get_queryset(self): diff --git a/patchwork/tests/api/test_event.py b/patchwork/tests/api/test_event.py index 8816538..bff8f40 100644 --- a/patchwork/tests/api/test_event.py +++ b/patchwork/tests/api/test_event.py @@ -149,6 +149,24 @@ class TestEventAPI(utils.APITestCase): resp = self.client.get(self.api_url(), {'series': 999999}) self.assertEqual(0, len(resp.data)) + def test_order_by_date_default(self): + """Assert the default ordering is by date descending.""" + self._create_events() + + resp = self.client.get(self.api_url()) + events = Event.objects.order_by("-date").all() + for api_event, event in zip(resp.data, events): + self.assertEqual(api_event["id"], event.id) + + def test_order_by_date_ascending(self): + """Assert the default ordering is by date descending.""" + self._create_events() + + resp = self.client.get(self.api_url(), {'order': 'date'}) + events = Event.objects.order_by("date").all() + for api_event, event in zip(resp.data, events): + self.assertEqual(api_event["id"], event.id) + def test_create(self): """Ensure creates aren't allowed""" user = create_maintainer() diff --git a/releasenotes/notes/api-order-events-by-date-7484164761c5231b.yaml b/releasenotes/notes/api-order-events-by-date-7484164761c5231b.yaml new file mode 100644 index 0000000..5d5328d --- /dev/null +++ b/releasenotes/notes/api-order-events-by-date-7484164761c5231b.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Allow ordering events from the events API by date. This can be done by + adding ``order=date`` or ``order=-date`` (the default) parameters.