From: Daniel Axtens Date: Sat, 3 Sep 2016 07:07:17 +0000 (+1000) Subject: xmlrpc: catch possible exceptions in patch filtering X-Git-Tag: v2.0.0-rc1~257 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fdf35e3a79a57abecdd504bb65943d147e798f6f;p=thirdparty%2Fpatchwork.git xmlrpc: catch possible exceptions in patch filtering 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 Reviewed-by: Stephen Finucane --- diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py index 489c87b7..c5e7b907 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)