From patchwork Wed Jun 13 09:35:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 928781 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.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 415M7v6FK3z9rxs for ; Wed, 13 Jun 2018 19:36:11 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="OBUMNAJG"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 415M7v4j84zF09h for ; Wed, 13 Jun 2018 19:36:11 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="OBUMNAJG"; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (mailfrom) smtp.mailfrom=that.guru (client-ip=23.83.209.62; helo=fossa.birch.relay.mailchannels.net; envelope-from=stephen@that.guru; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="OBUMNAJG"; dkim-atps=neutral Received: from fossa.birch.relay.mailchannels.net (fossa.birch.relay.mailchannels.net [23.83.209.62]) (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 415M7q1szbzDr9J for ; Wed, 13 Jun 2018 19:36:06 +1000 (AEST) X-Sender-Id: 5xi41l16bi|x-authuser|stephen@that.guru Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id D0C24681F1A for ; Wed, 13 Jun 2018 09:36:03 +0000 (UTC) Received: from one.mxroute.com (unknown [100.96.26.53]) (Authenticated sender: 5xi41l16bi) by relay.mailchannels.net (Postfix) with ESMTPA id 73C08682507 for ; Wed, 13 Jun 2018 09:36:03 +0000 (UTC) X-Sender-Id: 5xi41l16bi|x-authuser|stephen@that.guru Received: from one.mxroute.com (one-outgoing.mxroute.com [172.19.61.140]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.15.2); Wed, 13 Jun 2018 09:36:03 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: 5xi41l16bi|x-authuser|stephen@that.guru X-MailChannels-Auth-Id: 5xi41l16bi X-Bubble-Glossy: 71ecd4ac25c6c8b0_1528882563698_1659283331 X-MC-Loop-Signature: 1528882563698:485914066 X-MC-Ingress-Time: 1528882563697 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=default; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version :Content-Type:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=3xKv3uArmANuFc09iNkVkkH0kktro7/Hzp7mDIRaWAg=; b=OBUMNAJGCW2WwmjTt62oOHAV9y WySt9bXXOgusrG5qA7H2kiXhjP5h++pPWS5cL5jm77+9Pmx3UIOLmV9AIdkTqK5Q4B+pB87jy0Fod GJB2m2E7HtLPLg2jy/lapj2j9x1vL3RxreWX1aUoE0J6+D8MLZxke/JvKtnZvI4zGR4HOYyNS1tYL 1/VV1S6X7Dj+nQIdFn0Hl3+NS03XvnJROVeZ830x+hFZPQmfzymVs4YEQG39Y34FCYq5JxwnGEQoN p6MmFjscx7Xz6HMbfB7gEuPXErHmansRhxW0tLMA9hUt07SDpm/2HZBtdoBXJRJr9D091PGvdkvc5 0xSqzs1g==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH 1/2] REST: Add missing tests for '/series' Date: Wed, 13 Jun 2018 10:35:56 +0100 Message-Id: <20180613093557.6385-1-stephen@that.guru> X-Mailer: git-send-email 2.17.1 X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" I'm not sure how these were missed but they're definitely needed as we continue adding functionality here. Signed-off-by: Stephen Finucane --- patchwork/tests/api/test_series.py | 148 +++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 patchwork/tests/api/test_series.py diff --git a/patchwork/tests/api/test_series.py b/patchwork/tests/api/test_series.py new file mode 100644 index 00000000..e6f7cdba --- /dev/null +++ b/patchwork/tests/api/test_series.py @@ -0,0 +1,148 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2018 Stephen Finucane +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import unittest + +from django.conf import settings + +from patchwork.compat import reverse +from patchwork.tests.utils import create_cover +from patchwork.tests.utils import create_maintainer +from patchwork.tests.utils import create_project +from patchwork.tests.utils import create_person +from patchwork.tests.utils import create_series +from patchwork.tests.utils import create_series_patch +from patchwork.tests.utils import create_user + +if settings.ENABLE_REST_API: + from rest_framework import status + from rest_framework.test import APITestCase +else: + # stub out APITestCase + from django.test import TestCase + APITestCase = TestCase # noqa + + +@unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API') +class TestSeriesAPI(APITestCase): + fixtures = ['default_tags'] + + @staticmethod + def api_url(item=None, version=None): + kwargs = {} + if version: + kwargs['version'] = version + + if item is None: + return reverse('api-series-list', kwargs=kwargs) + kwargs['pk'] = item + return reverse('api-series-detail', kwargs=kwargs) + + def assertSerialized(self, series_obj, series_json): + self.assertEqual(series_obj.id, series_json['id']) + self.assertEqual(series_obj.name, series_json['name']) + self.assertEqual(series_obj.version, series_json['version']) + self.assertEqual(series_obj.total, series_json['total']) + self.assertEqual(series_obj.received_total, + series_json['received_total']) + self.assertIn(series_obj.get_mbox_url(), series_json['mbox']) + + # nested fields + + self.assertEqual(series_obj.project.id, + series_json['project']['id']) + self.assertEqual(series_obj.submitter.id, + series_json['submitter']['id']) + self.assertEqual(series_obj.cover_letter.id, + series_json['cover_letter']['id']) + self.assertEqual(series_obj.patches.count(), + len(series_json['patches'])) + + def test_list(self): + """Validate we can list series.""" + resp = self.client.get(self.api_url()) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(0, len(resp.data)) + + project_obj = create_project(linkname='myproject') + person_obj = create_person(email='test@example.com') + cover_obj = create_cover() + series_obj = create_series(project=project_obj, submitter=person_obj) + series_obj.add_cover_letter(cover_obj) + create_series_patch(series=series_obj) + + # anonymous users + resp = self.client.get(self.api_url()) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(1, len(resp.data)) + series_rsp = resp.data[0] + self.assertSerialized(series_obj, series_rsp) + + # authenticated user + user = create_user() + self.client.force_authenticate(user=user) + resp = self.client.get(self.api_url()) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(1, len(resp.data)) + series_rsp = resp.data[0] + self.assertSerialized(series_obj, series_rsp) + + # test filtering by project + resp = self.client.get(self.api_url(), {'project': 'myproject'}) + self.assertEqual([series_obj.id], [x['id'] for x in resp.data]) + resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) + self.assertEqual(0, len(resp.data)) + + # test filtering by owner, both ID and email + resp = self.client.get(self.api_url(), {'submitter': person_obj.id}) + self.assertEqual([series_obj.id], [x['id'] for x in resp.data]) + resp = self.client.get(self.api_url(), { + 'submitter': 'test@example.com'}) + self.assertEqual([series_obj.id], [x['id'] for x in resp.data]) + resp = self.client.get(self.api_url(), { + 'submitter': 'test@example.org'}) + self.assertEqual(0, len(resp.data)) + + def test_detail(self): + """Validate we can get a specific series.""" + cover = create_cover() + series = create_series() + series.add_cover_letter(cover) + + resp = self.client.get(self.api_url(series.id)) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertSerialized(series, resp.data) + + def test_create_update_delete(self): + """Ensure creates, updates and deletes aren't allowed""" + user = create_maintainer() + user.is_superuser = True + user.save() + self.client.force_authenticate(user=user) + + resp = self.client.post(self.api_url(), {'name': 'Test'}) + self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code) + + series = create_series() + + resp = self.client.patch(self.api_url(series.id), {'name': 'Test'}) + self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code) + + resp = self.client.delete(self.api_url(series.id)) + self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code) From patchwork Wed Jun 13 09:35:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 928782 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.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 415M8H2VSVz9s2g for ; Wed, 13 Jun 2018 19:36:31 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="JdsdHHwC"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 415M8H0hPszF0Tn for ; Wed, 13 Jun 2018 19:36:31 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="JdsdHHwC"; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (mailfrom) smtp.mailfrom=that.guru (client-ip=23.83.222.34; helo=cheetah.ash.relay.mailchannels.net; envelope-from=stephen@that.guru; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="JdsdHHwC"; dkim-atps=neutral Received: from cheetah.ash.relay.mailchannels.net (cheetah.ash.relay.mailchannels.net [23.83.222.34]) (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 415M7z6x4mzDrq8 for ; Wed, 13 Jun 2018 19:36:15 +1000 (AEST) X-Sender-Id: 5xi41l16bi|x-authuser|stephen@that.guru Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id CC53C5C2AB8 for ; Wed, 13 Jun 2018 09:36:04 +0000 (UTC) Received: from one.mxroute.com (unknown [100.96.33.46]) (Authenticated sender: 5xi41l16bi) by relay.mailchannels.net (Postfix) with ESMTPA id 5181B5C21B0 for ; Wed, 13 Jun 2018 09:36:04 +0000 (UTC) X-Sender-Id: 5xi41l16bi|x-authuser|stephen@that.guru Received: from one.mxroute.com (one-outgoing.mxroute.com [172.18.47.25]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.15.2); Wed, 13 Jun 2018 09:36:04 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: 5xi41l16bi|x-authuser|stephen@that.guru X-MailChannels-Auth-Id: 5xi41l16bi X-Fearful-Inform: 7138d7ef67f3b61d_1528882564682_1907688181 X-MC-Loop-Signature: 1528882564682:1804620405 X-MC-Ingress-Time: 1528882564681 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: 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=LjbgrCS7WuV82jOZ910nQ/3yRoYabWELbod4BCKrwE8=; b=JdsdHHwCrhutRin52CZfZj9Tai Fagxs6vdCj19jzDBN3/IJclMxGbBuNsz0SqF60Xl33VUGAYnuycjrAji3LaoQYGeawnWRbg797mHG aY5h0epf4eunJZTjsFWN3vxgFB+iXjF3zToq4T7VUQ7o3fmNz6vEk1dSbFdaDunASrQqzouKGdf5u FqLOWI8z3HAa4BvMh08fanhXaDq/8ASMHsZ1nZbwVGP4y34hDs/OWE2K6/wS5iON3Ibrys4pPVDmC +elwTDiBlcpJow+RIcCv/tXX7kpVci5w/dGF5CDhsFYYptrZVGtfxyh8YPNQfsDZngIPZ0tIcUxGV L5XEesSw==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH 2/2] REST: Add 'web_url' link to API responses Date: Wed, 13 Jun 2018 10:35:57 +0100 Message-Id: <20180613093557.6385-2-stephen@that.guru> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180613093557.6385-1-stephen@that.guru> References: <20180613093557.6385-1-stephen@that.guru> X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" This provides an easy way for clients to navigate to the web view. The URL is added to four resources: bundles, comments, cover letters and series. We could also extend this to projects and users in the future, but the latter would require renaming an existing property while the latter would require a public "user" page which does not currently exists. Signed-off-by: Stephen Finucane --- Unless anyone complains, I'm probably going to merge this within a day or two and cut 2.1. This particular shortcoming has been a constant annoyance for me and I should have addressed this sooner than I did. --- patchwork/api/bundle.py | 16 +++++++++--- patchwork/api/comment.py | 12 +++++++-- patchwork/api/cover.py | 11 ++++++--- patchwork/api/embedded.py | 39 +++++++++++++++++++++++------- patchwork/api/patch.py | 13 +++++++--- patchwork/api/series.py | 18 ++++++++++---- patchwork/models.py | 12 +++++++++ patchwork/tests/api/test_bundle.py | 29 +++++++++++++++++++--- patchwork/tests/api/test_cover.py | 15 +++++++++--- patchwork/tests/api/test_patch.py | 16 +++++++++++- patchwork/tests/api/test_series.py | 18 ++++++++++++++ 11 files changed, 165 insertions(+), 34 deletions(-) diff --git a/patchwork/api/bundle.py b/patchwork/api/bundle.py index 733e4881..b0005daa 100644 --- a/patchwork/api/bundle.py +++ b/patchwork/api/bundle.py @@ -20,9 +20,9 @@ from django.db.models import Q from rest_framework.generics import ListAPIView from rest_framework.generics import RetrieveAPIView -from rest_framework.serializers import HyperlinkedModelSerializer from rest_framework.serializers import SerializerMethodField +from patchwork.api.base import BaseHyperlinkedModelSerializer from patchwork.api.base import PatchworkPermission from patchwork.api.filters import BundleFilterSet from patchwork.api.embedded import PatchSerializer @@ -32,22 +32,30 @@ from patchwork.compat import is_authenticated from patchwork.models import Bundle -class BundleSerializer(HyperlinkedModelSerializer): +class BundleSerializer(BaseHyperlinkedModelSerializer): + web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) mbox = SerializerMethodField() owner = UserSerializer(read_only=True) patches = PatchSerializer(many=True, read_only=True) + def get_web_url(self, instance): + request = self.context.get('request') + return request.build_absolute_uri(instance.get_absolute_url()) + def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) class Meta: model = Bundle - fields = ('id', 'url', 'project', 'name', 'owner', 'patches', - 'public', 'mbox') + fields = ('id', 'url', 'web_url', 'project', 'name', 'owner', + 'patches', 'public', 'mbox') read_only_fields = ('owner', 'patches', 'mbox') + versioned_fields = { + '1.1': ('web_url', ), + } extra_kwargs = { 'url': {'view_name': 'api-bundle-detail'}, } diff --git a/patchwork/api/comment.py b/patchwork/api/comment.py index fbb7cc83..5a5adb1d 100644 --- a/patchwork/api/comment.py +++ b/patchwork/api/comment.py @@ -30,10 +30,15 @@ from patchwork.models import Comment class CommentListSerializer(BaseHyperlinkedModelSerializer): + web_url = SerializerMethodField() subject = SerializerMethodField() headers = SerializerMethodField() submitter = PersonSerializer(read_only=True) + def get_web_url(self, instance): + request = self.context.get('request') + return request.build_absolute_uri(instance.get_absolute_url()) + def get_subject(self, comment): return email.parser.Parser().parsestr(comment.headers, True).get('Subject', '') @@ -54,9 +59,12 @@ class CommentListSerializer(BaseHyperlinkedModelSerializer): class Meta: model = Comment - fields = ('id', 'msgid', 'date', 'subject', 'submitter', 'content', - 'headers') + fields = ('id', 'web_url', 'msgid', 'date', 'subject', 'submitter', + 'content', 'headers') read_only_fields = fields + versioned_fields = { + '1.1': ('web_url', ), + } class CommentList(ListAPIView): diff --git a/patchwork/api/cover.py b/patchwork/api/cover.py index 246b7cb9..b497fd85 100644 --- a/patchwork/api/cover.py +++ b/patchwork/api/cover.py @@ -34,12 +34,17 @@ from patchwork.models import CoverLetter class CoverLetterListSerializer(BaseHyperlinkedModelSerializer): + web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) submitter = PersonSerializer(read_only=True) mbox = SerializerMethodField() series = SeriesSerializer(many=True, read_only=True) comments = SerializerMethodField() + def get_web_url(self, instance): + request = self.context.get('request') + return request.build_absolute_uri(instance.get_absolute_url()) + def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) @@ -50,11 +55,11 @@ class CoverLetterListSerializer(BaseHyperlinkedModelSerializer): class Meta: model = CoverLetter - fields = ('id', 'url', 'project', 'msgid', 'date', 'name', 'submitter', - 'mbox', 'series', 'comments') + fields = ('id', 'url', 'web_url', 'project', 'msgid', 'date', 'name', + 'submitter', 'mbox', 'series', 'comments') read_only_fields = fields versioned_fields = { - '1.1': ('mbox', 'comments'), + '1.1': ('web_url', 'mbox', 'comments'), } extra_kwargs = { 'url': {'view_name': 'api-cover-detail'}, diff --git a/patchwork/api/embedded.py b/patchwork/api/embedded.py index d79724c4..1d5aba84 100644 --- a/patchwork/api/embedded.py +++ b/patchwork/api/embedded.py @@ -32,7 +32,7 @@ from patchwork import models class MboxMixin(BaseHyperlinkedModelSerializer): - """Embed an link to the mbox URL. + """Embed a link to the mbox URL. This field is just way too useful to leave out of even the embedded serialization. @@ -45,12 +45,25 @@ class MboxMixin(BaseHyperlinkedModelSerializer): return request.build_absolute_uri(instance.get_mbox_url()) -class BundleSerializer(MboxMixin, BaseHyperlinkedModelSerializer): +class WebURLMixin(BaseHyperlinkedModelSerializer): + """Embed a link to the web URL.""" + + web_url = SerializerMethodField() + + def get_web_url(self, instance): + request = self.context.get('request') + return request.build_absolute_uri(instance.get_absolute_url()) + + +class BundleSerializer(MboxMixin, WebURLMixin, BaseHyperlinkedModelSerializer): class Meta: model = models.Bundle - fields = ('id', 'url', 'name', 'mbox') + fields = ('id', 'url', 'web_url', 'name', 'mbox') read_only_fields = fields + versioned_field = { + '1.1': ('web_url', ), + } extra_kwargs = { 'url': {'view_name': 'api-bundle-detail'}, } @@ -75,26 +88,30 @@ class CheckSerializer(BaseHyperlinkedModelSerializer): } -class CoverLetterSerializer(MboxMixin, BaseHyperlinkedModelSerializer): +class CoverLetterSerializer(MboxMixin, WebURLMixin, + BaseHyperlinkedModelSerializer): class Meta: model = models.CoverLetter - fields = ('id', 'url', 'msgid', 'date', 'name', 'mbox') + fields = ('id', 'url', 'web_url', 'msgid', 'date', 'name', 'mbox') read_only_fields = fields versioned_field = { - '1.1': ('mbox', ), + '1.1': ('web_url', 'mbox', ), } extra_kwargs = { 'url': {'view_name': 'api-cover-detail'}, } -class PatchSerializer(MboxMixin, BaseHyperlinkedModelSerializer): +class PatchSerializer(MboxMixin, WebURLMixin, BaseHyperlinkedModelSerializer): class Meta: model = models.Patch - fields = ('id', 'url', 'msgid', 'date', 'name', 'mbox') + fields = ('id', 'url', 'web_url', 'msgid', 'date', 'name', 'mbox') read_only_fields = fields + versioned_field = { + '1.1': ('web_url', ), + } extra_kwargs = { 'url': {'view_name': 'api-patch-detail'}, } @@ -127,12 +144,16 @@ class ProjectSerializer(BaseHyperlinkedModelSerializer): } -class SeriesSerializer(MboxMixin, BaseHyperlinkedModelSerializer): +class SeriesSerializer(MboxMixin, WebURLMixin, + BaseHyperlinkedModelSerializer): class Meta: model = models.Series fields = ('id', 'url', 'date', 'name', 'version', 'mbox') read_only_fields = fields + versioned_field = { + '1.1': ('web_url', ), + } extra_kwargs = { 'url': {'view_name': 'api-series-detail'}, } diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py index c807e98a..9d890eb1 100644 --- a/patchwork/api/patch.py +++ b/patchwork/api/patch.py @@ -77,6 +77,7 @@ class StateField(RelatedField): class PatchListSerializer(BaseHyperlinkedModelSerializer): + web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) state = StateField() submitter = PersonSerializer(read_only=True) @@ -88,6 +89,10 @@ class PatchListSerializer(BaseHyperlinkedModelSerializer): checks = SerializerMethodField() tags = SerializerMethodField() + def get_web_url(self, instance): + request = self.context.get('request') + return request.build_absolute_uri(instance.get_absolute_url()) + def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) @@ -110,15 +115,15 @@ class PatchListSerializer(BaseHyperlinkedModelSerializer): class Meta: model = Patch - fields = ('id', 'url', 'project', 'msgid', 'date', 'name', + fields = ('id', 'url', 'web_url', 'project', 'msgid', 'date', 'name', 'commit_ref', 'pull_url', 'state', 'archived', 'hash', 'submitter', 'delegate', 'mbox', 'series', 'comments', 'check', 'checks', 'tags') - read_only_fields = ('project', 'msgid', 'date', 'name', 'hash', - 'submitter', 'mbox', 'mbox', 'series', 'comments', + read_only_fields = ('web_url', 'project', 'msgid', 'date', 'name', + 'hash', 'submitter', 'mbox', 'series', 'comments', 'check', 'checks', 'tags') versioned_fields = { - '1.1': ('comments', ), + '1.1': ('comments', 'web_url'), } extra_kwargs = { 'url': {'view_name': 'api-patch-detail'}, diff --git a/patchwork/api/series.py b/patchwork/api/series.py index ab1b6adb..14768efb 100644 --- a/patchwork/api/series.py +++ b/patchwork/api/series.py @@ -19,9 +19,9 @@ from rest_framework.generics import ListAPIView from rest_framework.generics import RetrieveAPIView -from rest_framework.serializers import HyperlinkedModelSerializer from rest_framework.serializers import SerializerMethodField +from patchwork.api.base import BaseHyperlinkedModelSerializer from patchwork.api.base import PatchworkPermission from patchwork.api.filters import SeriesFilterSet from patchwork.api.embedded import CoverLetterSerializer @@ -31,25 +31,33 @@ from patchwork.api.embedded import ProjectSerializer from patchwork.models import Series -class SeriesSerializer(HyperlinkedModelSerializer): +class SeriesSerializer(BaseHyperlinkedModelSerializer): + web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) submitter = PersonSerializer(read_only=True) mbox = SerializerMethodField() cover_letter = CoverLetterSerializer(read_only=True) patches = PatchSerializer(read_only=True, many=True) + def get_web_url(self, instance): + request = self.context.get('request') + return request.build_absolute_uri(instance.get_absolute_url()) + def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) class Meta: model = Series - fields = ('id', 'url', 'project', 'name', 'date', 'submitter', - 'version', 'total', 'received_total', 'received_all', - 'mbox', 'cover_letter', 'patches') + fields = ('id', 'url', 'web_url', 'project', 'name', 'date', + 'submitter', 'version', 'total', 'received_total', + 'received_all', 'mbox', 'cover_letter', 'patches') read_only_fields = ('date', 'submitter', 'total', 'received_total', 'received_all', 'mbox', 'cover_letter', 'patches') + versioned_fields = { + '1.1': ('web_url', ), + } extra_kwargs = { 'url': {'view_name': 'api-series-detail'}, } diff --git a/patchwork/models.py b/patchwork/models.py index 38f0f753..947c0d29 100644 --- a/patchwork/models.py +++ b/patchwork/models.py @@ -412,6 +412,9 @@ class SeriesMixin(object): class CoverLetter(SeriesMixin, Submission): + def get_absolute_url(self): + return reverse('cover-detail', kwargs={'cover_id': self.id}) + def get_mbox_url(self): return reverse('cover-mbox', kwargs={'cover_id': self.id}) @@ -603,6 +606,9 @@ class Comment(EmailMixin, models.Model): related_query_name='comment', on_delete=models.CASCADE) + def get_absolute_url(self): + return reverse('comment-redirect', kwargs={'comment_id': self.id}) + def save(self, *args, **kwargs): super(Comment, self).save(*args, **kwargs) if hasattr(self.submission, 'patch'): @@ -728,6 +734,12 @@ class Series(FilenameMixin, models.Model): patch=patch, number=number) + def get_absolute_url(self): + # TODO(stephenfin): We really need a proper series view + return reverse('patch-list', + kwargs={'project_id': self.project.id}) + ( + 'series=%d' % self.id) + def get_mbox_url(self): return reverse('series-mbox', kwargs={'series_id': self.id}) diff --git a/patchwork/tests/api/test_bundle.py b/patchwork/tests/api/test_bundle.py index 0cf5f4b3..bc583fb5 100644 --- a/patchwork/tests/api/test_bundle.py +++ b/patchwork/tests/api/test_bundle.py @@ -41,16 +41,22 @@ class TestBundleAPI(APITestCase): fixtures = ['default_tags'] @staticmethod - def api_url(item=None): + def api_url(item=None, version=None): + kwargs = {} + if version: + kwargs['version'] = version + if item is None: - return reverse('api-bundle-list') - return reverse('api-bundle-detail', args=[item]) + return reverse('api-bundle-list', kwargs=kwargs) + kwargs['pk'] = item + return reverse('api-bundle-detail', kwargs=kwargs) def assertSerialized(self, bundle_obj, bundle_json): self.assertEqual(bundle_obj.id, bundle_json['id']) self.assertEqual(bundle_obj.name, bundle_json['name']) self.assertEqual(bundle_obj.public, bundle_json['public']) self.assertIn(bundle_obj.get_mbox_url(), bundle_json['mbox']) + self.assertIn(bundle_obj.get_absolute_url(), bundle_json['web_url']) # nested fields @@ -109,6 +115,16 @@ class TestBundleAPI(APITestCase): resp = self.client.get(self.api_url(), {'owner': 'otheruser'}) self.assertEqual(0, len(resp.data)) + def test_list_old_version(self): + """Validate that newer fields are dropped for older API versions.""" + create_bundle(public=True) + + resp = self.client.get(self.api_url(version='1.0')) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(1, len(resp.data)) + self.assertIn('url', resp.data[0]) + self.assertNotIn('web_url', resp.data[0]) + def test_detail(self): """Validate we can get a specific bundle.""" bundle = create_bundle(public=True) @@ -117,6 +133,13 @@ class TestBundleAPI(APITestCase): self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertSerialized(bundle, resp.data) + def test_detail_version_1_0(self): + bundle = create_bundle(public=True) + + resp = self.client.get(self.api_url(bundle.id, version='1.0')) + self.assertIn('url', resp.data) + self.assertNotIn('web_url', resp.data) + def test_create_update_delete(self): """Ensure creates, updates and deletes aren't allowed""" user = create_maintainer() diff --git a/patchwork/tests/api/test_cover.py b/patchwork/tests/api/test_cover.py index 15e02372..e4d814e4 100644 --- a/patchwork/tests/api/test_cover.py +++ b/patchwork/tests/api/test_cover.py @@ -57,6 +57,7 @@ class TestCoverLetterAPI(APITestCase): self.assertEqual(cover_obj.id, cover_json['id']) self.assertEqual(cover_obj.name, cover_json['name']) self.assertIn(cover_obj.get_mbox_url(), cover_json['mbox']) + self.assertIn(cover_obj.get_absolute_url(), cover_json['web_url']) self.assertIn('comments', cover_json) # nested fields @@ -104,11 +105,15 @@ class TestCoverLetterAPI(APITestCase): 'submitter': 'test@example.org'}) self.assertEqual(0, len(resp.data)) - # test old version of API + def test_list_version_1_0(self): + create_cover() + resp = self.client.get(self.api_url(version='1.0')) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) + self.assertIn('url', resp.data[0]) self.assertNotIn('mbox', resp.data[0]) + self.assertNotIn('web_url', resp.data[0]) def test_detail(self): """Validate we can get a specific cover letter.""" @@ -127,8 +132,12 @@ class TestCoverLetterAPI(APITestCase): for key, value in parsed_headers.items(): self.assertIn(value, resp.data['headers'][key]) - # test old version of API - resp = self.client.get(self.api_url(cover_obj.id, version='1.0')) + def test_detail_version_1_0(self): + cover = create_cover() + + resp = self.client.get(self.api_url(cover.id, version='1.0')) + self.assertIn('url', resp.data) + self.assertNotIn('web_url', resp.data) self.assertNotIn('comments', resp.data) def test_create_update_delete(self): diff --git a/patchwork/tests/api/test_patch.py b/patchwork/tests/api/test_patch.py index 016dab66..27b99248 100644 --- a/patchwork/tests/api/test_patch.py +++ b/patchwork/tests/api/test_patch.py @@ -62,6 +62,7 @@ class TestPatchAPI(APITestCase): self.assertEqual(patch_obj.msgid, patch_json['msgid']) self.assertEqual(patch_obj.state.slug, patch_json['state']) self.assertIn(patch_obj.get_mbox_url(), patch_json['mbox']) + self.assertIn(patch_obj.get_absolute_url(), patch_json['web_url']) self.assertIn('comments', patch_json) # nested fields @@ -137,6 +138,15 @@ class TestPatchAPI(APITestCase): ('state', 'new')]) self.assertEqual(2, len(resp.data)) + def test_list_version_1_0(self): + create_patch() + + resp = self.client.get(self.api_url(version='1.0')) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(1, len(resp.data)) + self.assertIn('url', resp.data[0]) + self.assertNotIn('web_url', resp.data[0]) + def test_detail(self): """Validate we can get a specific patch.""" patch = create_patch( @@ -158,8 +168,12 @@ class TestPatchAPI(APITestCase): self.assertEqual(patch.diff, resp.data['diff']) self.assertEqual(0, len(resp.data['tags'])) - # test old version of API + def test_detail_version_1_0(self): + patch = create_patch() + resp = self.client.get(self.api_url(item=patch.id, version='1.0')) + self.assertIn('url', resp.data) + self.assertNotIn('web_url', resp.data) self.assertNotIn('comments', resp.data) def test_create(self): diff --git a/patchwork/tests/api/test_series.py b/patchwork/tests/api/test_series.py index e6f7cdba..11324bc3 100644 --- a/patchwork/tests/api/test_series.py +++ b/patchwork/tests/api/test_series.py @@ -62,6 +62,7 @@ class TestSeriesAPI(APITestCase): self.assertEqual(series_obj.received_total, series_json['received_total']) self.assertIn(series_obj.get_mbox_url(), series_json['mbox']) + self.assertIn(series_obj.get_absolute_url(), series_json['web_url']) # nested fields @@ -119,6 +120,16 @@ class TestSeriesAPI(APITestCase): 'submitter': 'test@example.org'}) self.assertEqual(0, len(resp.data)) + def test_list_old_version(self): + """Validate that newer fields are dropped for older API versions.""" + create_series() + + resp = self.client.get(self.api_url(version='1.0')) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(1, len(resp.data)) + self.assertIn('url', resp.data[0]) + self.assertNotIn('web_url', resp.data[0]) + def test_detail(self): """Validate we can get a specific series.""" cover = create_cover() @@ -129,6 +140,13 @@ class TestSeriesAPI(APITestCase): self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertSerialized(series, resp.data) + def test_detail_version_1_0(self): + series = create_series() + + resp = self.client.get(self.api_url(series.id, version='1.0')) + self.assertIn('url', resp.data) + self.assertNotIn('web_url', resp.data) + def test_create_update_delete(self): """Ensure creates, updates and deletes aren't allowed""" user = create_maintainer()