]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
xmlrpc: Treat negative max_count as meaning "return last N results"
authorAdam Jackson <ajax@redhat.com>
Mon, 30 Nov 2015 19:59:45 +0000 (14:59 -0500)
committerStephen Finucane <stephen.finucane@intel.com>
Mon, 8 Feb 2016 18:46:30 +0000 (18:46 +0000)
This is most useful when listing patches, as it lets you get the most
recent results instead of the oldest.

v2: Resolve "Negative indexing is not supported." issue and include
    unit tests to ensure this doesn't regress. Bump API version to
    1.2

Signed-off-by: Adam Jackson <ajax@redhat.com>
Signed-off-by: Stephen Finucane <stephen.finucane@intel.com>
patchwork/bin/pwclient
patchwork/tests/test_xmlrpc.py
patchwork/views/xmlrpc.py

index 58f233875d918d081157a7e0dda2ae6611f99864..a271132c50c3eac75f22a8b30afacb32dc02cc30 100755 (executable)
@@ -408,7 +408,12 @@ def main():
     filter_parser.add_argument(
         '-n', metavar='MAX#',
         type=int,
-        help='''Restrict number of results'''
+        help='''Return first n results'''
+    )
+    filter_parser.add_argument(
+        '-N', metavar='MAX#',
+        type=int,
+        help='''Return last N results'''
     )
     filter_parser.add_argument(
         '-m', metavar='MESSAGEID',
@@ -556,6 +561,12 @@ def main():
         except:
             action_parser.error("Invalid maximum count '%s'" % args.get('n'))
 
+    if args.get('N') is not None:
+        try:
+            filt.add("max_count", 0 - args.get('N'))
+        except:
+            action_parser.error("Invalid maximum count '%s'" % args.get('N'))
+
     do_signoff = args.get('signoff')
 
     # grab settings from config files
index c69062efbd1d956d805deca7e78edd0a34bd2d78..bbb7dfbbd744f895c31fde3fbb59d96230330dcb 100644 (file)
@@ -76,3 +76,9 @@ class XMLRPCTest(LiveServerTestCase):
         patches = self.rpc.patch_list({'max_count': 2})
         self.assertEqual(len(patches), 2)
         self.assertEqual(patches[0]['id'], patch_objs[0].id)
+
+    def testListNegativeMaxCount(self):
+        patch_objs = self._createPatches(5)
+        patches = self.rpc.patch_list({'max_count': -1})
+        self.assertEqual(len(patches), 1)
+        self.assertEqual(patches[0]['id'], patch_objs[-1].id)
index 86abd6dbcf1946ca3fba987e29c9a8db76411306..2881afba74f21a1f749359d37fa44f78637f6036 100644 (file)
@@ -379,7 +379,7 @@ def pw_rpc_version():
     Returns:
         Version of the API.
     """
-    return (1, 1, 0)
+    return (1, 2, 0)
 
 
 @xmlrpc_method()
@@ -407,6 +407,9 @@ def project_list(search_str=None, max_count=0):
 
         if max_count > 0:
             return list(map(project_to_dict, projects[:max_count]))
+        elif max_count < 0:
+            return list(map(project_to_dict,
+                            projects[len(projects) + max_count:]))
         else:
             return list(map(project_to_dict, projects))
     except Project.DoesNotExist:
@@ -459,6 +462,8 @@ def person_list(search_str=None, max_count=0):
 
         if max_count > 0:
             return list(map(person_to_dict, people[:max_count]))
+        elif max_count < 0:
+            return list(map(person_to_dict, people[len(people) + max_count:]))
         else:
             return list(map(person_to_dict, people))
     except Person.DoesNotExist:
@@ -602,6 +607,9 @@ def patch_list(filt=None):
 
         if max_count > 0:
             return list(map(patch_to_dict, patches[:max_count]))
+        elif max_count < 0:
+            return list(map(patch_to_dict,
+                            patches[len(patches) + max_count:]))
         else:
             return list(map(patch_to_dict, patches))
     except Patch.DoesNotExist:
@@ -795,6 +803,8 @@ def state_list(search_str=None, max_count=0):
 
         if max_count > 0:
             return list(map(state_to_dict, states[:max_count]))
+        elif max_count < 0:
+            return list(map(state_to_dict, states[len(states) + max_count:]))
         else:
             return list(map(state_to_dict, states))
     except State.DoesNotExist: