[PATCH v2 09/10] views/xmlrpc: Add 'checks' endpoint

Stephen Finucane stephen.finucane at intel.com
Fri Oct 2 00:52:33 AEST 2015


From: Stephen Finucane <stephenfinucane at hotmail.com>

This includes adding a serializer plus the endpoint itself.

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/views/xmlrpc.py | 104 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 102 insertions(+), 2 deletions(-)

diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py
index 8715dea..9e1b880 100644
--- a/patchwork/views/xmlrpc.py
+++ b/patchwork/views/xmlrpc.py
@@ -31,7 +31,7 @@ from django.http import (
     HttpResponse, HttpResponseRedirect, HttpResponseServerError)
 from django.views.decorators.csrf import csrf_exempt
 
-from patchwork.models import Patch, Project, Person, State
+from patchwork.models import Patch, Project, Person, State, Check
 from patchwork.views import patch_to_mbox
 
 
@@ -226,6 +226,33 @@ def state_to_dict(obj):
     }
 
 
+def check_to_dict(obj):
+    """Return a trimmed down dictionary representation of a Check
+    object which is OK to send to the client."""
+    return {
+        'id': obj.id,
+        'date': unicode(obj.date).encode('utf-8'),
+        'patch': unicode(obj.patch).encode('utf-8'),
+        'patch_id': obj.patch_id,
+        'user': unicode(obj.user).encode('utf-8'),
+        'user_id': obj.user_id,
+        'state': obj.get_state_display(),
+        'target_url': obj.target_url,
+        'description': obj.description,
+        'context': obj.context,
+    }
+
+def patch_check_to_dict(obj):
+    """Return a combined patch check."""
+    state_names = dict(Check.STATE_CHOICES)
+
+    return {
+        'state': state_names[obj.combined_check_state],
+        'total': len(obj.checks),
+        'checks': [check_to_dict(check) for check in obj.checks]
+    }
+
+
 #######################################################################
 # Public XML-RPC methods
 #######################################################################
@@ -233,7 +260,7 @@ def state_to_dict(obj):
 @xmlrpc_method()
 def pw_rpc_version():
     """Return Patchwork XML-RPC interface version."""
-    return 1
+    return 1.1
 
 
 @xmlrpc_method()
@@ -458,3 +485,76 @@ def state_get(state_id):
         return state_to_dict(state)
     except State.DoesNotExist:
         return {}
+
+
+ at xmlrpc_method()
+def check_list(filt=None):
+    """Get a list of checks matching the given filters."""
+    if filt is None:
+        filt = {}
+
+    try:
+        # We allow access to many of the fields. But, some fields are
+        # filtered by raw object so we must lookup by ID instead over
+        # XML-RPC.
+        ok_fields = [
+            'id',
+            'user',
+            'project_id',
+            'patch_id',
+            'max_count',
+        ]
+
+        dfilter = {}
+        max_count = 0
+
+        for key in filt:
+            parts = key.split('__')
+            if parts[0] not in ok_fields:
+                # Invalid field given
+                return []
+            if len(parts) > 1:
+                if LOOKUP_TYPES.count(parts[1]) == 0:
+                    # Invalid lookup type given
+                    return []
+
+            if parts[0] == 'user_id':
+                dfilter['user'] = Person.objects.filter(id=filt[key])[0]
+            if parts[0] == 'project_id':
+                dfilter['patch__project'] = Project.objects.filter(
+                    id=filt[key])[0]
+            elif parts[0] == 'patch_id':
+                dfilter['patch'] = Patch.objects.filter(id=filt[key])[0]
+            elif parts[0] == 'max_count':
+                max_count = filt[key]
+            else:
+                dfilter[key] = filt[key]
+
+        checks = Check.objects.filter(**dfilter)
+
+        if max_count > 0:
+            return map(check_to_dict, checks[:max_count])
+        else:
+            return map(check_to_dict, checks)
+    except Check.DoesNotExist:
+        return []
+
+
+ at xmlrpc_method()
+def check_get(check_id):
+    """Return structure for the given check ID."""
+    try:
+        check = Check.objects.filter(id=check_id)[0]
+        return check_to_dict(check)
+    except Check.DoesNotExist:
+        return {}
+
+
+ at xmlrpc_method()
+def patch_check_get(patch_id):
+    """Return structure for the given patch's combined checks."""
+    try:
+        patch = Patch.objects.filter(id=patch_id)[0]
+        return patch_check_to_dict(patch)
+    except Patch.DoesNotExist:
+        return {}
-- 
2.0.0



More information about the Patchwork mailing list