Skip to content

Commit

Permalink
Fix RPM epoch filter to not crash on non-numeric values
Browse files Browse the repository at this point in the history
This patch also fixes the documentation to properly show the value
should be an int.

JIRA: PDC-997
  • Loading branch information
lubomir committed Sep 4, 2015
1 parent 87d27e8 commit 3b0fa70
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
17 changes: 17 additions & 0 deletions pdc/apps/common/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import django_filters
import django.forms.widgets as widgets
from .models import Label, SigKey
from .hacks import convert_str_to_int

SelectMultiple = widgets.SelectMultiple

Expand Down Expand Up @@ -49,6 +50,22 @@ def filter(self, qs, value):
return qs


class MultiIntFilter(MultiValueFilter):
"""
MultiValueFilter that reports error when input is not a number.
"""
@value_is_not_empty
def filter(self, qs, value):
# This can't actually call to parent method, as double invocation of
# @value_is_not_empty would cause the filter with empty value to be
# ignored.
value = [convert_str_to_int(val, name=self.name) for val in value]
qs = qs.filter(**{self.name + '__in': value})
if self.distinct:
qs = qs.distinct()
return qs


class ComposeFilterSetOptions(FilterSetOptions):
def __init__(self, options=None):
# HACK NOTE:
Expand Down
1 change: 1 addition & 0 deletions pdc/apps/common/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def replace_url(match):
'NullableCharFilter': 'string | null',
'BooleanFilter': 'bool',
'ActiveReleasesFilter': 'bool',
'MultiIntFilter': 'int',
}
LOOKUP_TYPES = {
'icontains': 'case insensitive, substring match',
Expand Down
4 changes: 2 additions & 2 deletions pdc/apps/package/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import django_filters

from pdc.apps.common.filters import MultiValueFilter, NullableCharFilter
from pdc.apps.common.filters import MultiValueFilter, MultiIntFilter, NullableCharFilter
from . import models


class RPMFilter(django_filters.FilterSet):
name = MultiValueFilter()
version = MultiValueFilter()
epoch = MultiValueFilter()
epoch = MultiIntFilter()
release = MultiValueFilter()
arch = MultiValueFilter()
srpm_name = MultiValueFilter()
Expand Down
5 changes: 5 additions & 0 deletions pdc/apps/package/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ def test_query_with_wrong_params(self):
response = self.client.get(url + 'wrong_param/', format='json')
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_query_with_bad_epoch(self):
url = reverse('rpms-list')
response = self.client.get(url, {'epoch': 'foo'}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_query_with_only_key(self):
url = reverse('rpms-list')
response = self.client.get(url + '?name', format='json')
Expand Down

0 comments on commit 3b0fa70

Please sign in to comment.