[PATCH 5/8] xmlrpc: catch possible exceptions in patch filtering.

Daniel Axtens dja at axtens.net
Sat Sep 3 17:07:17 AEST 2016


Currently, filtering by project, submitter, delegate or state uses a
filter(id=filt[key])[0]. This will throw an exception when something
isn't found, as filter will return [], and getting the first element of
that will fail.

Convert them to explicit get()s, so it's clearer that they can throw an
exception, then catch the 3 possible types of DoesNotExists exceptions.

Signed-off-by: Daniel Axtens <dja at axtens.net>
---
 patchwork/views/xmlrpc.py | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py
index 489c87b74023..c5e7b9078f15 100644
--- a/patchwork/views/xmlrpc.py
+++ b/patchwork/views/xmlrpc.py
@@ -582,18 +582,22 @@ def patch_list(filt=None):
                 # Invalid lookup type given
                 return []
 
-        if parts[0] == 'project_id':
-            dfilter['project'] = Project.objects.filter(id=filt[key])[0]
-        elif parts[0] == 'submitter_id':
-            dfilter['submitter'] = Person.objects.filter(id=filt[key])[0]
-        elif parts[0] == 'delegate_id':
-            dfilter['delegate'] = Person.objects.filter(id=filt[key])[0]
-        elif parts[0] == 'state_id':
-            dfilter['state'] = State.objects.filter(id=filt[key])[0]
-        elif parts[0] == 'max_count':
-            max_count = filt[key]
-        else:
-            dfilter[key] = filt[key]
+        try:
+            if parts[0] == 'project_id':
+                dfilter['project'] = Project.objects.get(id=filt[key])
+            elif parts[0] == 'submitter_id':
+                dfilter['submitter'] = Person.objects.get(id=filt[key])
+            elif parts[0] == 'delegate_id':
+                dfilter['delegate'] = Person.objects.get(id=filt[key])
+            elif parts[0] == 'state_id':
+                dfilter['state'] = State.objects.get(id=filt[key])
+            elif parts[0] == 'max_count':
+                max_count = filt[key]
+            else:
+                dfilter[key] = filt[key]
+        except (Project.DoesNotExist, Person.DoesNotExist, State.DoesNotExist):
+            # Invalid Project, Person or State given
+            return []
 
     patches = Patch.objects.filter(**dfilter)
 
-- 
2.7.4



More information about the Patchwork mailing list