From: Stephen Finucane Date: Sat, 29 Oct 2016 13:13:38 +0000 (+0100) Subject: filters: Handle invalid ids X-Git-Tag: v2.0.0-rc1~176 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5136285b1f0dac3caa251ce8b96173b5a9048ecc;p=thirdparty%2Fpatchwork.git filters: Handle invalid ids Two filters - SubmitterFilter and DelegateFilter - don't attempt to handle invalid id values and will bubble an exception up as a 5xx error. Correct this. Signed-off-by: Stephen Finucane Reviewed-by: Andrew Donnellan --- diff --git a/patchwork/filters.py b/patchwork/filters.py index fca90087..ea832b7a 100644 --- a/patchwork/filters.py +++ b/patchwork/filters.py @@ -94,26 +94,25 @@ class SubmitterFilter(Filter): def _set_key(self, key): self.person = None self.person_match = None - submitter_id = None key = key.strip() if not key: return try: - submitter_id = int(key) - except ValueError: + self.person = Person.objects.get(id=int(key)) + except (ValueError, Person.DoesNotExist): pass - - if submitter_id: - self.person = Person.objects.get(id=submitter_id) + else: self.applied = True return people = Person.objects.filter(name__icontains=key) - if people: - self.person_match = key - self.applied = True + if not people: + return + + self.person_match = key + self.applied = True def kwargs(self): if self.person: @@ -122,9 +121,9 @@ class SubmitterFilter(Filter): return {'submitter__in': Person.objects.filter(user=user).values('pk').query} return {'submitter': self.person} - - if self.person_match: + elif self.person_match: return {'submitter__name__icontains': self.person_match} + return {} def condition(self): @@ -339,19 +338,16 @@ class DelegateFilter(Filter): def _set_key(self, key): self.delegate = None self.delegate_match = None - delegate_id = None key = key.strip() if not key: return try: - delegate_id = int(key) - except ValueError: - pass - - if delegate_id: self.delegate = User.objects.get(id=int(key)) + except (ValueError, User.DoesNotExist): + pass + else: self.applied = True return