-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
323 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django import forms | ||
from django.db.models import Q | ||
from django.core.exceptions import ValidationError | ||
|
||
|
||
class NullModelMultipleChoiceCaseInsensitiveField(forms.ModelMultipleChoiceField): | ||
|
||
def __init__(self, *args, **kwargs): | ||
# use the default empty label | ||
kwargs.pop('empty_label') | ||
super(NullModelMultipleChoiceCaseInsensitiveField, self).__init__(*args, **kwargs) | ||
|
||
def clean(self, value): | ||
# let a custom filter handle the actual filtering for null values later in the qs | ||
if value == 'null': | ||
return value | ||
|
||
try: | ||
return super(NullModelMultipleChoiceCaseInsensitiveField, self).clean(value) | ||
|
||
except ValidationError as validation_error: | ||
# Check to make sure the validation error wasn't because of a case sensitive relationship query | ||
q = Q() | ||
for choice in value: | ||
q |= Q(**{'{}__iexact'.format(self.to_field_name): choice}) | ||
queryset = self.queryset.filter(q) | ||
|
||
if not queryset: | ||
raise validation_error | ||
|
||
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import django_filters | ||
|
||
from osf.models import OSFUser | ||
from api.base.filters import JSONAPIFilterSet, MultiValueCharFilter | ||
|
||
|
||
class UserFilterSet(JSONAPIFilterSet): | ||
|
||
full_name = MultiValueCharFilter(name='fullname', lookup_expr='icontains') | ||
id = django_filters.CharFilter(name='guids___id') | ||
|
||
class Meta(JSONAPIFilterSet.Meta): | ||
model = OSFUser | ||
fields = ['id', 'full_name', 'given_name', 'middle_names', 'family_name'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.