class PaperFilter(filters.FilterSet): hubs_id__in = filters.Filter(field_name='hubs', lookup_expr='in') authors_id__in = filters.Filter(field_name='authors', lookup_expr='in') author_uploaded_by = filters.Filter( field_name='uploaded_by__author_profile', method='uploaded_by_author' ) class Meta: model = Paper # TODO: Handle filtering on raw_authors in another way exclude = [ 'alternate_ids', 'file', 'csl_item', 'oa_pdf_location', 'raw_authors', 'external_metadata', 'pdf_file_extract', 'edited_file_extract' ] def uploaded_by_author(self, queryset, name, value): filters = { name: value } qs = queryset.filter(**filters) return qs
class UserFilter(filters.FilterSet): invited_by = filters.NumberFilter() referral_code = filters.Filter() class Meta: model = User fields = []
class BlogPostFilter(filters.FilterSet): title = filters.CharFilter(lookup_expr="contains") having_tags = filters.Filter(name="tags", lookup_expr="in") class Meta: model = BlogPost fields = ("title", "char_num", "category", "tags")
class BlogPostFilter(filters.FilterSet): title = filters.CharFilter(lookup_expr='contains') having_tags = filters.Filter(name="tags", lookup_expr='in') class Meta: model = BlogPost fields = ('title', 'char_num', 'category', 'tags')
class ReviewFilter(filters.FilterSet): creator = filters.Filter(field_name='user_id') id = filters.NumberFilter(field_name='id') created_at = filters.DateFromToRangeFilter(field_name='created_at') class Meta: model = ProductReview fields = ['user_id', 'created_at', 'id']
class MatchFilter(filters.FilterSet): player = MatchHasPlayer() anyplayers = MatchHasAnyPlayers() max = filters.Filter(field_name='id', method=limit_filter) class Meta: model = api_models.Match fields = '__all__' exclude = ['details']
class UserFilter(filters_rest_framework.FilterSet): username = filters_rest_framework.CharFilter(name="username", lookup_expr="exact") posts_number = filters_rest_framework.Filter(name="posts_number", method=posts_number) class Meta: model = User fields = [ 'username', 'posts_number' ]
class HubFilter(filters.FilterSet): name__iexact = filters.Filter(field_name="name", lookup_expr='iexact') ordering = ScoreOrderingFilter(fields=['name', 'score']) name__fuzzy = filters.Filter(field_name='name', method='name_trigram_similarity_search') class Meta: model = Hub fields = [field.name for field in model._meta.fields] filter_overrides = { FileField: { 'filter_class': filters.CharFilter, } } def name_trigram_similarity_search(self, qs, name, value): qs = qs.annotate(similarity=TrigramSimilarity(name, value)).filter( similarity__gt=0.15) qs = qs.order_by('-similarity') return qs
class MatchFilter(filters.FilterSet): player = MatchHasPlayer() players = MatchHasAnyPlayers() start_date = filters.DateFilter() max = filters.Filter(field_name='id', method=limit_filter) match_after = AfterDateFilter(field_name="match_date") class Meta: model = api_models.Match fields = '__all__' exclude = ['details']
class BoardgameFilter(filters.FilterSet): class M2MFilter(filters.Filter): def filter(self, qs, value): if not value: return qs values = value.split(',') for v in values: qs = qs.filter(**{self.field_name: v}) return qs mechanics = M2MFilter(field_name='mechanic__name', lookup_expr='iexact') categories = M2MFilter(field_name='category__name', lookup_expr='iexact') boardgame__name = filters.Filter(field_name='name', lookup_expr='icontains') designer__name = filters.Filter(field_name='designer__name', lookup_expr='icontains', distinct=True) publisher__name = filters.Filter(field_name='publisher__name', lookup_expr='icontains', distinct=True) artist__name = filters.Filter(field_name='artist__name', lookup_expr='icontains', distinct=True) class Meta: model = Boardgame fields = { 'id': ['exact'], 'name': ['exact'], 'mechanic__name': ['exact'], 'category__name': ['exact'], 'year_published': ['exact', 'lte', 'gte'], 'average_rating': ['lte', 'gte'], 'num_ratings': ['lte', 'gte'], 'average_weight': ['lte', 'gte'], 'playing_time': ['lte', 'gte'], 'owned': ['lte', 'gte'], 'wishing': ['lte', 'gte'], }
class PennyChatFilter(filters.FilterSet): upcoming_or_popular = filters.Filter(method='filter_upcoming_or_popular') def filter_upcoming_or_popular(self, qs, name, value): if value == 'true': return qs.filter( Q(follow_ups__isnull=False) | Q(date__gt=timezone.now())) else: raise RuntimeError( f'this only works with value="true", found "{value}"') class Meta: model = PennyChat fields = ['participants__user_id', 'upcoming_or_popular']
class ServiceFilter(filters.FilterSet): consent_given = filters.Filter( method='filter_consent_given', widget=BooleanWidget(), help_text=_('Include only services that have or don\'t have a consent given by the current user. ' 'Accepts boolean values "true" and "false".')) class Meta: model = Service fields = ('consent_given',) def filter_consent_given(self, queryset, name, value): if 'consent_given' in queryset.query.annotations.keys(): queryset = queryset.filter(consent_given=value) return queryset
class UserFilterSet(filters.FilterSet): class Meta: model = User fields = [ "is_active", "is_approved", "accepted_terms", "is_verified", "registration_stage" ] is_active = BetterBooleanFilter() is_approved = BetterBooleanFilter() accepted_terms = BetterBooleanFilter() registration_stage = filters.ChoiceFilter( choices=UserRegistrationStageType.choices ) is_verified = filters.Filter(method="filter_is_verified") roles__any = filters.Filter(method="filter_roles_or") roles__all = filters.Filter(method="filter_roles_and") permissions__any = filters.Filter(method="filter_permissions_or") permissions__all = filters.Filter(method="filter_permissions_and") def filter_is_verified(self, queryset, name, value): try: field = BetterBooleanFilterField() cleaned_value = field.clean(value) if cleaned_value is not None: # Django cannot efficiently filter querysets by property # so this basically recreates the logic of the @is_verified User property queryset = queryset.filter( emailaddress__primary=True, emailaddress__verified=cleaned_value ) except ValidationError as e: raise APIException({name: e.messages}) return queryset def filter_roles_or(self, queryset, name, value): role_names = value.split(",") return queryset.filter( roles__name__in=role_names ).distinct() # yapf: disable def filter_roles_and(self, queryset, name, value): role_names = value.split(",") return ( queryset.filter( roles__name__in=role_names ).annotate( num_roles=Count("roles") ).filter( num_roles=len(role_names) ) ) # yapf: disable def filter_permissions_or(self, queryset, name, value): permission_names = value.split(",") return queryset.filter( roles__permissions__name__in=permission_names ).distinct() # yapf: disable def filter_permissions_and(self, queryset, name, value): permission_names = value.split(",") return ( queryset.filter( roles__permissions__name__in=permission_names ).annotate( num_permissions=Count("roles__permissions") ).filter( num_permissions=len(permission_names) ) ) # yapf: disable
class BookDetailFilter(filters.FilterSet): author = filters.Filter('author', lookup_expr='exact') class Meta: model = Book fields = '__all__'