From: Stephen Finucane <[email protected]> This includes adding a serializer plus the endpoint itself.
Signed-off-by: Stephen Finucane <[email protected]> --- 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 7245e13..8565b18 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, Status from patchwork.views import patch_to_mbox @@ -227,6 +227,33 @@ def state_to_dict(obj): } +def status_to_dict(obj): + """Return a trimmed down dictionary representation of a Status + 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_status_to_dict(obj): + """Return a combined patch status.""" + state_names = dict(Status.STATE_CHOICES) + + return { + 'state': state_names[obj.combined_status_state], + 'total': len(obj.statuses), + 'statuses': [status_to_dict(status) for status in obj.statuses] + } + + ####################################################################### # Public XML-RPC methods ####################################################################### @@ -234,7 +261,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() @@ -459,3 +486,76 @@ def state_get(state_id): return state_to_dict(state) except State.DoesNotExist: return {} + + +@xmlrpc_method() +def status_list(filt=None): + """Get a list of statuses 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] + + statuses = Status.objects.filter(**dfilter) + + if max_count > 0: + return map(status_to_dict, statuses[:max_count]) + else: + return map(status_to_dict, statuses) + except Status.DoesNotExist: + return [] + + +@xmlrpc_method() +def status_get(status_id): + """Return structure for the given status ID.""" + try: + status = Status.objects.filter(id=status_id)[0] + return status_to_dict(status) + except Status.DoesNotExist: + return {} + + +@xmlrpc_method() +def patch_status_get(patch_id): + """Return structure for the given patches combined status.""" + try: + patch = Patch.objects.filter(id=patch_id)[0] + return patch_status_to_dict(patch) + except Patch.DoesNotExist: + return {} -- 2.0.0 _______________________________________________ Patchwork mailing list [email protected] https://lists.ozlabs.org/listinfo/patchwork
