def test_filtering_stop(self): qs = mock.Mock(spec=['filter']) value = mock.Mock(start=None, stop=datetime(2015, 9, 6, 11, 45)) f = DateTimeFromToRangeFilter() f.filter(qs, value) qs.filter.assert_called_once_with( None__lte=datetime(2015, 9, 6, 11, 45))
def test_filtering_start(self): qs = mock.Mock(spec=['filter']) value = mock.Mock(start=datetime(2015, 4, 7, 8, 30), stop=None) f = DateTimeFromToRangeFilter() f.filter(qs, value) qs.filter.assert_called_once_with( None__gte=datetime(2015, 4, 7, 8, 30))
def test_filtering_ignores_lookup_expr(self): qs = mock.Mock() value = mock.Mock( start=datetime(2015, 4, 7, 8, 30), stop=datetime(2015, 9, 6, 11, 45)) f = DateTimeFromToRangeFilter(lookup_expr='gte') f.filter(qs, value) qs.filter.assert_called_once_with( None__range=(datetime(2015, 4, 7, 8, 30), datetime(2015, 9, 6, 11, 45)))
def test_filtering_range(self): qs = mock.Mock(spec=['filter']) value = mock.Mock( start=datetime(2015, 4, 7, 8, 30), stop=datetime(2015, 9, 6, 11, 45)) f = DateTimeFromToRangeFilter() f.filter(qs, value) qs.filter.assert_called_once_with( None__range=(datetime(2015, 4, 7, 8, 30), datetime(2015, 9, 6, 11, 45)))
class TelemetryFilter(django_filters.FilterSet): datetime = DateTimeFromToRangeFilter() sensor__category = ChoiceFilter(choices=SENSOR_CATEGORY) sensor__code_name = ChoiceFilter(choices=Sensor.get_sensors_name()) class Meta: model = Telemetry fields = ['datetime', 'sensor__category', 'sensor__code_name']
class ObservationImageFilter(filters.FilterSet): group = filters.CharFilter( field_name='observation_unit__study__group__name', lookup_expr='exact') is_public = filters.BooleanFilter( field_name='observation_unit__study__is_public') study = filters.CharFilter(field_name='observation_unit__study__name', lookup_expr='iexact') study_contains = filters.CharFilter( field_name='observation_unit__study__name', lookup_expr='icontains') observation_unit = filters.CharFilter(field_name='observation_unit__name', lookup_expr='iexact') observation_unit_contains = filters.CharFilter( field_name='observation_unit__name', lookup_expr='icontains') plant = filters.CharFilter(field_name='observation_unit__plants__name', lookup_expr='iexact') plant_contain = filters.CharFilter( field_name='observation_unit__plants__name', lookup_expr='icontains') accession_number = filters.CharFilter( field_name='observation_unit__accession__germplasm_number', lookup_expr='iexact') accession_number_contains = filters.CharFilter( field_name='observation_unit__accession__germplasm_number', lookup_expr='icontains') accession_institute = filters.CharFilter( field_name='observation_unit__accession__institute__code', lookup_expr='iexact') accession_institute_contains = filters.CharFilter( field_name='observation_unit__accession__institute__code', lookup_expr='icontains') created = DateTimeFromToRangeFilter(field_name='creation_time') class Meta: model = ObservationImage fields = { 'observer': ['exact', 'iexact', 'icontains'], 'observation_image_uid': ['iexact'] }
def test_filtering_skipped_with_none_value(self): qs = mock.Mock(spec=['filter']) f = DateTimeFromToRangeFilter() result = f.filter(qs, None) self.assertEqual(qs, result)
def test_filtering_start(self): qs = mock.Mock(spec=['filter']) value = mock.Mock(start=datetime(2015, 4, 7, 8, 30), stop=None) f = DateTimeFromToRangeFilter() f.filter(qs, value) qs.filter.assert_called_once_with(None__gte=datetime(2015, 4, 7, 8, 30))
def test_default_field(self): f = DateTimeFromToRangeFilter() field = f.field self.assertIsInstance(field, DateTimeRangeField)
class ObservationFilter(TermFilterMixin, filters.FilterSet): term = filters.CharFilter(label='term', method='term_filter') group = filters.CharFilter( field_name='observation_unit__study__group__name', lookup_expr='exact') is_public = filters.BooleanFilter( field_name='observation_unit__study__is_public') study = filters.CharFilter(field_name='observation_unit__study__name', lookup_expr='iexact') study_contains = filters.CharFilter( field_name='observation_unit__study__name', lookup_expr='icontains') studies = filters.CharFilter(label='studies', method='studies_filter') observation_variable = filters.CharFilter( field_name='observation_variable__name', lookup_expr='iexact') observation_variables = filters.CharFilter( field_name='observation_variable__name', lookup_expr='in') observation_variable_contains = filters.CharFilter( field_name='observation_variable__name', lookup_expr='icontains') observation_unit = filters.CharFilter(field_name='observation_unit__name', lookup_expr='iexact') observation_unit_contains = filters.CharFilter( field_name='observation_unit__name', lookup_expr='icontains') plant = filters.CharFilter(field_name='observation_unit__plants__name', lookup_expr='iexact') plant_contain = filters.CharFilter( field_name='observation_unit__plants__name', lookup_expr='icontains') accession_number = filters.CharFilter( field_name='observation_unit__accession__germplasm_number', lookup_expr='iexact') accession_number_contains = filters.CharFilter( field_name='observation_unit__accession__germplasm_number', lookup_expr='icontains') accession_institute = filters.CharFilter( field_name='observation_unit__accession__institute__code', lookup_expr='iexact') accession_institute_contains = filters.CharFilter( field_name='observation_unit__accession__institute__code', lookup_expr='icontains') created = DateTimeFromToRangeFilter(field_name='creation_time') value_range_min = filters.CharFilter(method='value_range_min_filter') value_range_max = filters.CharFilter(method='value_range_max_filter') taxon = filters.CharFilter( field_name='observation_unit__accession__passports__taxa__name', lookup_expr='iexact') class Meta: model = Observation fields = {'observer': ['exact', 'iexact', 'icontains']} def value_range_min_filter(self, queryset, _, value): self._check_observation_variable_in_filter() queryset = queryset.filter(value__int__gte=int(value)) return queryset def value_range_max_filter(self, queryset, _, value): self._check_observation_variable_in_filter() queryset = queryset.filter(value__int__lte=int(value)) return queryset def _check_observation_variable_in_filter(self): if 'observation_variable' not in self.data.keys(): msg = 'Can not use value_range filter if not filtered by observation variable' raise ValidationError(format_error_message(msg)) try: observation_variable = ObservationVariable.objects.get( name=self.data['observation_variable']) except ObservationVariable.DoesNotExist: msg = 'Used observation_variable to filter does not exist' raise ValidationError(format_error_message(msg)) if observation_variable.scale.data_type.name not in ('Numerical', ): msg = "Used observation_variable's data type is not numeric" raise ValidationError(format_error_message(msg)) def studies_filter(self, queryset, _, value): return queryset.filter( observation_unit__study__name__in=value.split(','))