Skip to content

Commit 08b2115

Browse files
committed
REST: Allow filtering by both project ID and linkname
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")
1 parent 94234ed commit 08b2115

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

patchwork/api/filters.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,34 @@ class TimestampMixin(FilterSet):
4141
since = IsoDateTimeFilter(name='date', **{LOOKUP_FIELD: 'gte'})
4242

4343

44+
class ProjectChoiceField(ModelChoiceField):
45+
46+
def to_python(self, value):
47+
if value in self.empty_values:
48+
return None
49+
50+
try:
51+
filters = {'pk': int(value)}
52+
except ValueError:
53+
filters = {'name__iexact': ' '.join(value.split('-'))}
54+
55+
try:
56+
value = self.queryset.get(**filters)
57+
except (ValueError, TypeError, self.queryset.model.DoesNotExist):
58+
raise ValidationError(self.error_messages['invalid_choice'],
59+
code='invalid_choice')
60+
return value
61+
62+
63+
class ProjectFilter(ModelChoiceFilter):
64+
65+
field_class = ProjectChoiceField
66+
67+
4468
class ProjectMixin(FilterSet):
4569

46-
project = ModelChoiceFilter(to_field_name='linkname',
47-
queryset=Project.objects.all())
70+
project = ProjectFilter(to_field_name='linkname',
71+
queryset=Project.objects.all())
4872

4973

5074
class SeriesFilter(ProjectMixin, TimestampMixin, FilterSet):

0 commit comments

Comments
 (0)