]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
xmlrpc: catch possible exceptions in patch filtering
authorDaniel Axtens <dja@axtens.net>
Sat, 3 Sep 2016 07:07:17 +0000 (17:07 +1000)
committerStephen Finucane <stephenfinucane@hotmail.com>
Sat, 3 Sep 2016 22:40:59 +0000 (23:40 +0100)
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@axtens.net>
Reviewed-by: Stephen Finucane <stephenfinucane@hotmail.com>
patchwork/views/xmlrpc.py

index 489c87b74023b9ffd6223d532d36516a57934646..c5e7b9078f1563ec314f360c65fc3006375e5a67 100644 (file)
@@ -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)