class ViewpointFilterSet(FilterSet): id = filters.CharFilter(field_name="id", lookup_expr="exact") city = filters.CharFilter(field_name="city__label", lookup_expr="exact") themes = filters.CharFilter(field_name="themes__label", lookup_expr="exact") city_id = filters.CharFilter(field_name="city", lookup_expr="exact") themes_id = filters.CharFilter(field_name="themes", lookup_expr="exact") date_from = filters.DateFilter(field_name="pictures__date", lookup_expr="gte") date_to = filters.DateFilter(field_name="pictures__date", lookup_expr="lte") last_picture = filters.DateFilter( field_name="last_accepted_picture_date", lookup_expr="lte" ) class Meta: model = Viewpoint fields = [ "id", "city", "themes", "city_id", "themes_id", "date_from", "date_to", "active", "last_picture", ]
class DocumentFilter(FilterSet): """Фильтр по стартовой и конечной дате документа.""" min_date = filters.DateFilter(field_name="create_date", lookup_expr='gte') max_date = filters.DateFilter(field_name="create_date", lookup_expr='lte') class Meta: model = Document fields = ['min_date', 'max_date']
class CampaignFilterSet(FilterSet): date_from = filters.DateFilter(field_name="start_date", lookup_expr="gte") date_to = filters.DateFilter(field_name="start_date", lookup_expr="lte") class Meta: model = Campaign fields = [ "date_from", "date_to", ]
class PhotoFilter(FilterSet): date_taken_from = filters.DateFilter(field_name='date_taken', lookup_expr='gte') date_taken_to = filters.DateFilter(field_name='date_taken', lookup_expr='lte') class Meta: model = models.Photo fields = [ 'date_taken_from', 'date_taken_to', 'motive', 'security_level', 'category', 'media', 'album', 'place', 'tags', 'scanned', 'on_home_page', 'splash', 'lapel', ]
class SignalFilterSet(FilterSet): id = filters.NumberFilter() address_text = filters.CharFilter(field_name='location__address_text', lookup_expr='icontains') area_code = filters.ChoiceFilter(field_name='location__area_code', choices=area_choices) area_type_code = filters.ChoiceFilter(field_name='location__area_type_code', choices=area_type_choices) buurt_code = filters.MultipleChoiceFilter(field_name='location__buurt_code', choices=buurt_choices) category_id = filters.MultipleChoiceFilter(field_name='category_assignment__category_id', choices=category_choices) category_slug = filters.ModelMultipleChoiceFilter( queryset=_get_child_category_queryset(), to_field_name='slug', field_name='category_assignment__category__slug', ) contact_details = filters.MultipleChoiceFilter(method='contact_details_filter', choices=contact_details_choices) created_before = filters.IsoDateTimeFilter(field_name='created_at', lookup_expr='lte') directing_department = filters.MultipleChoiceFilter( method='directing_department_filter', choices=department_choices ) created_after = filters.IsoDateTimeFilter(field_name='created_at', lookup_expr='gte') feedback = filters.ChoiceFilter(method='feedback_filter', choices=feedback_choices) has_changed_children = filters.MultipleChoiceFilter(method='has_changed_children_filter', choices=boolean_choices) kind = filters.MultipleChoiceFilter(method='kind_filter', choices=kind_choices) # SIG-2636 incident_date = filters.DateFilter(field_name='incident_date_start', lookup_expr='date') incident_date_before = filters.DateFilter(field_name='incident_date_start', lookup_expr='date__gte') incident_date_after = filters.DateFilter(field_name='incident_date_start', lookup_expr='date__lte') maincategory_slug = filters.ModelMultipleChoiceFilter( queryset=_get_parent_category_queryset(), to_field_name='slug', field_name='category_assignment__category__parent__slug', ) note_keyword = filters.CharFilter(method='note_keyword_filter') priority = filters.MultipleChoiceFilter(field_name='priority__priority', choices=Priority.PRIORITY_CHOICES) source = filters.MultipleChoiceFilter(choices=source_choices) stadsdeel = filters.MultipleChoiceFilter(field_name='location__stadsdeel', choices=stadsdelen_choices) status = filters.MultipleChoiceFilter(field_name='status__state', choices=status_choices) type = filters.MultipleChoiceFilter(field_name='type_assignment__name', choices=Type.CHOICES) updated_before = filters.IsoDateTimeFilter(field_name='updated_at', lookup_expr='lte') updated_after = filters.IsoDateTimeFilter(field_name='updated_at', lookup_expr='gte') def _cleanup_form_data(self): """ Cleanup the form data """ self.form.cleaned_data.pop('category_slug', None) self.form.cleaned_data.pop('maincategory_slug', None) if not self.form.cleaned_data.get('area_code', None) or not self.form.cleaned_data.get('area_type_code', None): self.form.cleaned_data.pop('area_code', None) self.form.cleaned_data.pop('area_type_code', None) def filter_queryset(self, queryset): """ Add custom category filtering to the filter_queryset """ if not self.form.cleaned_data['category_id']: main_categories = self.form.cleaned_data['maincategory_slug'] sub_categories = self.form.cleaned_data['category_slug'] if main_categories or sub_categories: queryset = queryset.filter( Q(category_assignment__category__parent_id__in=[c.pk for c in main_categories]) | Q(category_assignment__category_id__in=[c.pk for c in sub_categories]) ) self._cleanup_form_data() queryset = super(SignalFilterSet, self).filter_queryset(queryset=queryset) return queryset.distinct() # Custom filter functions def contact_details_filter(self, queryset, name, value): """ Filter `signals.Signal` instances according to presence of contact details. """ choices = value # we have a MultipleChoiceFilter ... # Deal with all choices selected, or none selected: if len(choices) == len(contact_details_choices()): # No filters are present, or ... all filters are present. In that # case we want all Signal instances with an email address, or a # phone number, or none of those (according to) the UX design. # This is the same as not filtering, hence in both cases just # return the queryset. return queryset # Set-up our Q objects for the individual options. has_no_email = (Q(reporter__email__isnull=True) | Q(reporter__email='')) has_no_phone = (Q(reporter__phone__isnull=True) | Q(reporter__phone='')) is_anonymous = has_no_email & has_no_phone q_objects = { 'email': ~has_no_email, 'phone': ~has_no_phone, 'none': is_anonymous, } # The individual choices have to be combined using logical OR: q_total = q_objects[choices.pop()] while choices: q_total |= q_objects[choices.pop()] return queryset.filter(q_total) def directing_department_filter(self, queryset, name, value): """ Filter Signals on directing department. * A directing department can only be set on a Parent Signal * When providing the option "null" select all parent Signals without one or more directing departments * When providing on or more department codes as options select all parent Signals which match directing departments Example 1: "?directing_department=ASC" will select all parent Signals where ASC is the directing department Example 2: "?directing_department=ASC&directing_department=null" will select all parent Signals without a directing department OR ASC as the directing department Example 3: "?directing_department=null" will select all parent Signals without a directing department """ choices = value # we have a MultipleChoiceFilter ... if len(choices) == len(department_choices()): # No filters are present, or ... all filters are present. In that case we want all Signal instances return queryset # Directing departments are only set on parent Signals parent_q_filter = (Q(parent__isnull=True) & Q(children__isnull=False)) if 'null' in choices and len(choices) == 1: # "?directing_department=null" will select all parent Signals without a directing department return queryset.filter( parent_q_filter & ( Q(directing_departments_assignment__isnull=True) | Q(directing_departments_assignment__departments__isnull=True) ) ) elif 'null' in choices and len(choices) > 1: # "?directing_department=ASC&directing_department=null" will select all parent Signals without a directing # department OR ASC as the directing department choices.pop(choices.index('null')) return queryset.filter( parent_q_filter & ( Q(directing_departments_assignment__isnull=True) | Q(directing_departments_assignment__departments__isnull=True) | Q(directing_departments_assignment__departments__code__in=choices) ) ) elif len(choices): # "?directing_department=ASC" will select all parent Signals where ASC is the directing department return queryset.filter( parent_q_filter & Q(directing_departments_assignment__departments__code__in=choices) ) return queryset def feedback_filter(self, queryset, name, value): # Only signals that have feedback queryset = queryset.annotate(feedback_count=Count('feedback')).filter(feedback_count__gte=1) if value in ['satisfied', 'not_satisfied']: is_satisfied = True if value == 'satisfied' else False queryset = queryset.annotate( feedback_max_created_at=Max('feedback__created_at'), feedback_max_submitted_at=Max('feedback__submitted_at') ).filter( feedback__is_satisfied=is_satisfied, feedback__submitted_at__isnull=False, feedback__created_at=F('feedback_max_created_at'), feedback__submitted_at=F('feedback_max_submitted_at') ) elif value == 'not_received': queryset = queryset.annotate( feedback_max_created_at=Max('feedback__created_at') ).filter( feedback__submitted_at__isnull=True, feedback__created_at=F('feedback_max_created_at') ) return queryset def kind_filter(self, queryset, name, value): choices = value # we have a MultipleChoiceFilter ... if (len(choices) == len(kind_choices()) or {'signal', 'parent_signal', 'child_signal'} == set(choices) or {'parent_signal', 'exclude_parent_signal'} == set(choices)): return queryset q_objects = { 'signal': (Q(parent__isnull=True) & Q(children__isnull=True)), 'parent_signal': (Q(parent__isnull=True) & Q(children__isnull=False)), 'exclude_parent_signal': ~(Q(parent__isnull=True) & Q(children__isnull=False)), 'child_signal': (Q(parent__isnull=False)), } q_filter = q_objects[choices.pop()] while choices: q_filter |= q_objects[choices.pop()] return queryset.filter(q_filter) if q_filter else queryset def note_keyword_filter(self, queryset, name, value): return queryset.filter(notes__text__icontains=value) def has_changed_children_filter(self, queryset, name, value): # we have a MultipleChoiceFilter ... choices = list(set(map(lambda x: True if x in [True, 'True', 'true', 1] else False, value))) q_filter = Q(children__isnull=False) if len(choices) == 2: return queryset.filter(q_filter) if True in choices: q_filter &= Q(updated_at__lt=F('children__updated_at')) # noqa Selects all parent signals with changes in the child signals if False in choices: q_filter &= Q(updated_at__gt=F('children__updated_at')) # noqa Selects all parent signals with NO changes in the child signals return queryset.filter(q_filter)
class SignalFilter(FilterSet): """ !!! This is the filter used in the V0 version of the API. V0 will be deprecated soon !!! - V1 filters can be found in signals/apps/api/v1/filters.py """ id = IntegerFilter() in_bbox = filters.CharFilter(method='in_bbox_filter', label='bbox') geo = filters.CharFilter(method="locatie_filter", label='x,y,r') location__stadsdeel = filters.MultipleChoiceFilter(choices=STADSDELEN) location__buurt_code = filters.MultipleChoiceFilter(choices=buurt_choices) location__address_text = filters.CharFilter(lookup_expr='icontains') created_at = filters.DateFilter(field_name='created_at', lookup_expr='date') created_at__gte = filters.DateFilter(field_name='created_at', lookup_expr='date__gte') created_at__lte = filters.DateFilter(field_name='created_at', lookup_expr='date__lte') updated_at = filters.DateFilter(field_name='updated_at', lookup_expr='date') updated_at__gte = filters.DateFilter(field_name='updated_at', lookup_expr='date__gte') updated_at__lte = filters.DateFilter(field_name='updated_at', lookup_expr='date__lte') incident_date_start = filters.DateFilter(field_name='incident_date_start', lookup_expr='date') incident_date_start__gte = filters.DateFilter( field_name='incident_date_start', lookup_expr='date__gte') incident_date_start__lte = filters.DateFilter( field_name='incident_date_start', lookup_expr='date__lte') incident_date_end = filters.DateFilter(field_name='incident_date_end', lookup_expr='date') operational_date = filters.DateFilter(field_name='operational_date', lookup_expr='date') expire_date = filters.DateFilter(field_name='expire_date', lookup_expr='date') expire_date__gte = filters.DateFilter(field_name='expire_date', lookup_expr='date__gte') expire_date__lte = filters.DateFilter(field_name='expire_date', lookup_expr='date__lte') status__state = filters.MultipleChoiceFilter(choices=status_choices) # TODO: these filter (category__main, category__sub) should be removed category__main = filters.ModelMultipleChoiceFilter( queryset=Category.objects.filter(parent__isnull=True), to_field_name='name', field_name='category_assignment__category__parent__name') category__sub = filters.ModelMultipleChoiceFilter( queryset=Category.objects.all(), to_field_name='name', field_name='category_assignment__category__name') # category__main and category__sub filters will be replaced with main_slug and sub_slug main_slug = filters.ModelMultipleChoiceFilter( queryset=Category.objects.filter(parent__isnull=True).select_related(), to_field_name='slug', field_name='category_assignment__category__parent__slug', ) sub_slug = filters.ModelMultipleChoiceFilter( queryset=Category.objects.all().select_related(), to_field_name='slug', field_name='category_assignment__category__slug', ) priority__priority = filters.MultipleChoiceFilter( choices=Priority.PRIORITY_CHOICES) class Meta(object): model = Signal fields = ( 'id', 'signal_id', 'status__state', 'category__main', 'category__sub', 'main_slug', 'sub_slug', 'location__buurt_code', 'location__stadsdeel', 'location__address_text', 'reporter__email', 'in_bbox', 'geo', ) def in_bbox_filter(self, qs, name, value): bbox_values, err = bbox.valid_bbox(value) lon1, lat1, lon2, lat2 = bbox_values poly_bbox = Polygon.from_bbox((lon1, lat1, lon2, lat2)) if err: raise ValidationError(f"bbox invalid {err}:{bbox_values}") return qs.filter(location__geometrie__bboverlaps=poly_bbox) def locatie_filter(self, qs, name, value): lon, lat, radius = parse_xyr(value) point = Point(lon, lat) return qs.filter( location__geometrie__dwithin=(point, bbox.dist_to_deg(radius, lat)))
class CreatedAtFilterSet(django_filters.FilterSet): date_from = filters.DateFilter(field_name="created_at", lookup_expr="gte") date_to = filters.DateFilter(field_name="created_at", lookup_expr="lte")
class SignalFilter(FilterSet): id = filters.NumberFilter() created_before = filters.IsoDateTimeFilter(field_name='created_at', lookup_expr='lte') created_after = filters.IsoDateTimeFilter(field_name='created_at', lookup_expr='gte') updated_before = filters.IsoDateTimeFilter(field_name='updated_at', lookup_expr='lte') updated_after = filters.IsoDateTimeFilter(field_name='updated_at', lookup_expr='gte') status = filters.MultipleChoiceFilter(field_name='status__state', choices=status_choices) maincategory_slug = filters.ModelMultipleChoiceFilter( queryset=_get_parent_category_queryset(), to_field_name='slug', field_name='category_assignment__category__parent__slug', ) # category_slug, because we will soon be using one type of category, instead of main vs sub # categories. This way the naming in the API will remain consistent category_slug = filters.ModelMultipleChoiceFilter( queryset=_get_child_category_queryset(), to_field_name='slug', field_name='category_assignment__category__slug', ) priority = filters.ChoiceFilter(field_name='priority__priority', choices=Priority.PRIORITY_CHOICES) stadsdeel = filters.MultipleChoiceFilter(field_name='location__stadsdeel', choices=STADSDELEN) buurt_code = filters.MultipleChoiceFilter(field_name='location__buurt_code', choices=buurt_choices) address_text = filters.CharFilter(field_name='location__address_text', lookup_expr='icontains') incident_date = filters.DateFilter(field_name='incident_date_start', lookup_expr='date') incident_date_before = filters.DateFilter(field_name='incident_date_start', lookup_expr='date__gte') incident_date_after = filters.DateFilter(field_name='incident_date_start', lookup_expr='date__lte') source = filters.MultipleChoiceFilter(choices=source_choices) feedback = filters.ChoiceFilter(method='feedback_filter', choices=feedback_choices) def feedback_filter(self, queryset, name, value): # Only signals that have feedback queryset = queryset.annotate(feedback_count=Count('feedback')).filter(feedback_count__gte=1) if value in ['satisfied', 'not_satisfied']: is_satisfied = True if value == 'satisfied' else False queryset = queryset.annotate( feedback_max_created_at=Max('feedback__created_at'), feedback_max_submitted_at=Max('feedback__submitted_at') ).filter( feedback__is_satisfied=is_satisfied, feedback__submitted_at__isnull=False, feedback__created_at=F('feedback_max_created_at'), feedback__submitted_at=F('feedback_max_submitted_at') ) elif value == 'not_received': queryset = queryset.annotate( feedback_max_created_at=Max('feedback__created_at') ).filter( feedback__submitted_at__isnull=True, feedback__created_at=F('feedback_max_created_at') ) return queryset def _categories_filter(self, queryset, main_categories, sub_categories): if not main_categories and not sub_categories: return queryset queryset = queryset.filter( Q(category_assignment__category__parent_id__in=[c.pk for c in main_categories]) | Q(category_assignment__category_id__in=[c.pk for c in sub_categories]) ) return queryset def filter_queryset(self, queryset): main_categories = [] sub_categories = [] for name, value in self.form.cleaned_data.items(): if name.lower() == 'maincategory_slug': main_categories = value elif name.lower() == 'category_slug': sub_categories = value else: queryset = self.filters[name].filter(queryset, value) return self._categories_filter(queryset=queryset, main_categories=main_categories, sub_categories=sub_categories)
class SignalFilter(FilterSet): id = filters.NumberFilter() country = filters.MultipleChoiceFilter(field_name='country') city = filters.MultipleChoiceFilter(field_name='city') created_before = filters.IsoDateTimeFilter(field_name='created_at', lookup_expr='lte') created_after = filters.IsoDateTimeFilter(field_name='created_at', lookup_expr='gte') updated_before = filters.IsoDateTimeFilter(field_name='updated_at', lookup_expr='lte') updated_after = filters.IsoDateTimeFilter(field_name='updated_at', lookup_expr='gte') status = filters.MultipleChoiceFilter(field_name='status__state', choices=status_choices) maincategory_slug = filters.ModelMultipleChoiceFilter( queryset=_get_parent_category_queryset(), to_field_name='slug', field_name='category_assignment__category__parent__slug', ) # category_slug, because we will soon be using one type of category, instead of main vs sub # categories. This way the naming in the API will remain consistent category_slug = filters.ModelMultipleChoiceFilter( queryset=_get_child_category_queryset(), to_field_name='slug', field_name='category_assignment__category__slug', ) category_filter_label = filters.ModelMultipleChoiceFilter( queryset=_get_parent_category_queryset(), to_field_name='filter_label', field_name='category_assignment__category__filter_label', ) priority = filters.MultipleChoiceFilter(field_name='priority__priority', choices=Priority.PRIORITY_CHOICES) stadsdeel = filters.MultipleChoiceFilter(field_name='location__stadsdeel', choices=stadsdelen) buurt_code = filters.MultipleChoiceFilter( field_name='location__buurt_code', choices=buurt_choices) address_text = filters.CharFilter(field_name='location__address_text', lookup_expr='icontains') incident_date = filters.DateFilter(field_name='incident_date_start', lookup_expr='date') incident_date_before = filters.DateFilter(field_name='incident_date_start', lookup_expr='date__gte') incident_date_after = filters.DateFilter(field_name='incident_date_start', lookup_expr='date__lte') source = filters.MultipleChoiceFilter(choices=source_choices) feedback = filters.ChoiceFilter(method='feedback_filter', choices=feedback_choices) contact_details = filters.MultipleChoiceFilter( method='contact_details_filter', choices=contact_details_choices, ) # SIG-2148 Filter on Signal Type type = filters.MultipleChoiceFilter(method='type_filter', choices=Type.CHOICES) note_keyword = filters.CharFilter(method='note_keyword_filter', ) directing_department = filters.MultipleChoiceFilter( field_name='directing_departments_assignment__departments__code', choices=department_choices) def feedback_filter(self, queryset, name, value): # Only signals that have feedback queryset = queryset.annotate(feedback_count=Count('feedback')).filter( feedback_count__gte=1) if value in ['satisfied', 'not_satisfied']: is_satisfied = True if value == 'satisfied' else False queryset = queryset.annotate( feedback_max_created_at=Max('feedback__created_at'), feedback_max_submitted_at=Max( 'feedback__submitted_at')).filter( feedback__is_satisfied=is_satisfied, feedback__submitted_at__isnull=False, feedback__created_at=F('feedback_max_created_at'), feedback__submitted_at=F('feedback_max_submitted_at')) elif value == 'not_received': queryset = queryset.annotate( feedback_max_created_at=Max('feedback__created_at')).filter( feedback__submitted_at__isnull=True, feedback__created_at=F('feedback_max_created_at')) return queryset def _categories_filter(self, queryset, main_categories, sub_categories): if not main_categories and not sub_categories: return queryset queryset = queryset.filter( Q(category_assignment__category__parent_id__in=[ c.pk for c in main_categories ]) | Q(category_assignment__category_id__in=[ c.pk for c in sub_categories ])) return queryset def filter_queryset(self, queryset): main_categories = [] sub_categories = [] for name, value in self.form.cleaned_data.items(): if name.lower() == 'maincategory_slug': main_categories = value elif name.lower() == 'category_slug': sub_categories = value else: queryset = self.filters[name].filter(queryset, value) return self._categories_filter(queryset=queryset, main_categories=main_categories, sub_categories=sub_categories) def contact_details_filter(self, queryset, name, value): """ Filter `signals.Signal` instances according to presence of contact details. """ # Set-up our Q objects for the individual options. has_no_email = (Q(reporter__email__isnull=True) | Q(reporter__email='')) has_no_phone = (Q(reporter__phone__isnull=True) | Q(reporter__phone='')) is_anonymous = has_no_email & has_no_phone q_objects = { 'email': ~has_no_email, 'phone': ~has_no_phone, 'none': is_anonymous, } choices = value # we have a MultipleChoiceFilter ... # Deal with all choices selected, or none selected: if len(choices) == len(contact_details_choices): # No filters are present, or ... all filters are present. In that # case we want all Signal instances with an email address, or a # phone number, or none of those (according to) the UX design. # This is the same as not filtering, hence in both cases just # return the queryset. return queryset # The individual choices have to be combined using logical OR: q_total = q_objects[choices.pop()] while choices: q_total |= q_objects[choices.pop()] return queryset.filter(q_total) def type_filter(self, queryset, name, value): return queryset.annotate(type_assignment_id=Max('types__id')).filter( types__id=F('type_assignment_id'), types__name__in=value) def note_keyword_filter(self, queryset, name, value): return queryset.filter(notes__text__icontains=value).distinct()
class OffersFilter(SimpleOffersFilter): date_from = filters.DateFilter(name="valid_through", lookup_expr="gte") date_to = filters.DateFilter(name="date_posted", lookup_expr="lte") class Meta(SimpleOffersFilter.Meta): fields = SimpleOffersFilter.Meta.fields + ['date_from', 'date_to']