[PATCH v3 14/16] REST: Add maintainers to '/projects'

Stephen Finucane stephen at that.guru
Sat Nov 26 05:18:33 AEDT 2016


This is in the UI. Expose it in the API.

Signed-off-by: Stephen Finucane <stephen at that.guru>
Reviewed-by: Daniel Axtens <dja at axtens.net>
---
v3:
- Add missing unit tests
---
 patchwork/api/project.py         |  8 ++++++--
 patchwork/tests/test_rest_api.py | 11 +++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/patchwork/api/project.py b/patchwork/api/project.py
index deaeb09..3cc2c32 100644
--- a/patchwork/api/project.py
+++ b/patchwork/api/project.py
@@ -22,6 +22,7 @@ from rest_framework.generics import ListAPIView
 from rest_framework.generics import RetrieveUpdateAPIView
 from rest_framework.serializers import CharField
 from rest_framework.serializers import HyperlinkedModelSerializer
+from rest_framework.serializers import HyperlinkedRelatedField
 
 from patchwork.api.base import PatchworkPermission
 from patchwork.models import Project
@@ -31,12 +32,15 @@ class ProjectSerializer(HyperlinkedModelSerializer):
     link_name = CharField(max_length=255, source='linkname')
     list_id = CharField(max_length=255, source='listid')
     list_email = CharField(max_length=200, source='listemail')
+    maintainers = HyperlinkedRelatedField(
+        many=True, read_only=True, view_name='api-user-detail',
+        source='maintainer_project')
 
     class Meta:
         model = Project
         fields = ('id', 'url', 'name', 'link_name', 'list_id', 'list_email',
-                  'web_url', 'scm_url', 'webscm_url')
-        read_only_fields = ('name',)
+                  'web_url', 'scm_url', 'webscm_url', 'maintainers')
+        read_only_fields = ('name', 'maintainers')
         extra_kwargs = {
             'url': {'view_name': 'api-project-detail'},
         }
diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
index f44a77f..ddc787f 100644
--- a/patchwork/tests/test_rest_api.py
+++ b/patchwork/tests/test_rest_api.py
@@ -57,11 +57,22 @@ class TestProjectAPI(APITestCase):
         self.assertEqual(project_obj.name, project_json['name'])
         self.assertEqual(project_obj.linkname, project_json['link_name'])
         self.assertEqual(project_obj.listid, project_json['list_id'])
+        self.assertEqual(len(project_json['maintainers']),
+                         project_obj.maintainer_project.all().count())
 
     def test_list(self):
         """Validate we can list the default test project."""
         project = create_project()
 
+        # anonymous user
+        resp = self.client.get(self.api_url())
+        self.assertEqual(status.HTTP_200_OK, resp.status_code)
+        self.assertEqual(1, len(resp.data))
+        self.assertSerialized(project, resp.data[0])
+
+        # maintainer
+        user = create_maintainer(project)
+        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))
-- 
2.7.4



More information about the Patchwork mailing list