[Patchew-devel] [PATCH 9/9] rest: add ability to look up projects by name

Paolo Bonzini pbonzini at redhat.com
Sat Aug 18 14:10:33 UTC 2018


Compared to Shubham's patch, this uses a redirect instead of a GET
argument on the collection.  This approach could be extended in the
future to request methods other than GET.

Signed-off-by: Paolo Bonzini <pbonzini at redhat.com>
---
 api/rest.py        | 12 +++++++++++-
 api/urls.py        |  1 +
 tests/test_rest.py |  8 ++++++++
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/api/rest.py b/api/rest.py
index 4132e38..bf45125 100644
--- a/api/rest.py
+++ b/api/rest.py
@@ -10,7 +10,7 @@
 
 from collections import OrderedDict
 from django.contrib.auth.models import User
-from django.http import Http404
+from django.http import Http404, HttpResponseRedirect
 from django.template import loader
 
 from mod import dispatch_module_hook
@@ -206,6 +206,16 @@ class ProjectsViewSet(viewsets.ModelViewSet):
         project.project_head = request.data['new_head']
         return Response({"new_head": project.project_head, "count": ret})
 
+class ProjectsByNameViewSet(viewsets.GenericViewSet):
+    queryset = Project.objects.all().order_by('id')
+    permission_classes = (PatchewPermission,)
+    lookup_field = 'name'
+
+    def retrieve(self, request, *args, **kwargs):
+        instance = self.get_object()
+        url = reverse_detail(instance, request)
+        return HttpResponseRedirect(url, status=status.HTTP_307_TEMPORARY_REDIRECT)
+
 # Common classes for series and messages
 
 class HyperlinkedMessageField(HyperlinkedIdentityField):
diff --git a/api/urls.py b/api/urls.py
index 2fd4e0f..2d95328 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -30,6 +30,7 @@ router = DefaultRouter(trailing_slash=True)
 router.include_format_suffixes = False
 router.register('users', rest.UsersViewSet)
 router.register('projects', rest.ProjectsViewSet)
+router.register('projects/by-name', rest.ProjectsByNameViewSet)
 router.register('series', rest.SeriesViewSet, base_name='series')
 router.register('messages', rest.MessagesViewSet)
 
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 7b035bc..8c86d9d 100755
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -86,6 +86,14 @@ class RestTest(PatchewTestCase):
         self.assertEquals(resp.data['mailing_list'], "qemu-block at nongnu.org")
         self.assertEquals(resp.data['parent_project'], self.PROJECT_BASE)
 
+    def test_project_by_name(self):
+        resp = self.api_client.get(self.REST_BASE + 'projects/by-name/QEMU/')
+        self.assertEquals(resp.status_code, 307)
+        resp = self.api_client.get(resp['Location'])
+        self.assertEquals(resp.data['resource_uri'], self.PROJECT_BASE)
+        self.assertEquals(resp.data['name'], "QEMU")
+        self.assertEquals(resp.data['mailing_list'], "qemu-devel at nongnu.org")
+
     def test_update_project_head(self):
         resp = self.apply_and_retrieve('0001-simple-patch.mbox.gz',
                                        self.p.id, '20160628014747.20971-1-famz at redhat.com')
-- 
2.17.1




More information about the Patchew-devel mailing list