Skip to content

Commit

Permalink
REST: Allow filtering by both project ID and linkname
Browse files Browse the repository at this point in the history
In hindsight, it's a bit odd that we would filter project by linkname by
everything else by ID. Simply support both.

Signed-off-by: Stephen Finucane <[email protected]>
Fixes: e27b68a ("REST: Filter on Project.linkname - not Project.pk")
  • Loading branch information
stephenfin committed May 18, 2017
1 parent 94234ed commit 08b2115
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions patchwork/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,34 @@ class TimestampMixin(FilterSet):
since = IsoDateTimeFilter(name='date', **{LOOKUP_FIELD: 'gte'})


class ProjectChoiceField(ModelChoiceField):

def to_python(self, value):
if value in self.empty_values:
return None

try:
filters = {'pk': int(value)}
except ValueError:
filters = {'name__iexact': ' '.join(value.split('-'))}

try:
value = self.queryset.get(**filters)
except (ValueError, TypeError, self.queryset.model.DoesNotExist):
raise ValidationError(self.error_messages['invalid_choice'],
code='invalid_choice')
return value


class ProjectFilter(ModelChoiceFilter):

field_class = ProjectChoiceField


class ProjectMixin(FilterSet):

project = ModelChoiceFilter(to_field_name='linkname',
queryset=Project.objects.all())
project = ProjectFilter(to_field_name='linkname',
queryset=Project.objects.all())


class SeriesFilter(ProjectMixin, TimestampMixin, FilterSet):
Expand Down

0 comments on commit 08b2115

Please sign in to comment.