def test_date_before(self):
     """Test date's before given."""
     qs = MockQuerySet()
     result = apply_filter_to_queryset(
         qs, DateRangeFilter([None, datetime(2009, 12, 2)]), 'zz')
     self.assert_filters_equals([Q(zz__lte=datetime(2009, 12, 3))],
                                result.filters)
 def test_date_after(self):
     """Test date's after given."""
     qs = MockQuerySet()
     result = apply_filter_to_queryset(
         qs, DateRangeFilter([datetime(2009, 12, 1), None]), 'zz')
     self.assert_filters_equals([Q(zz__gte=datetime(2009, 12, 1))],
                                result.filters)
 def test_date_filters(self):
     """Test that date filters are built properly."""
     spec = '{"date": ["09/01/2015", "09/30/2015"]}'
     result = self.driver.build_filter(spec)
     expected = ContactsFilter(date=DateRangeFilter(
         [datetime(2015, 9, 1), datetime(2015, 9, 30)]))
     self.assertEquals(expected, result)
Exemplo n.º 4
0
 def test_filter_by_date_after(self):
     """Should show only commrec with last_action_time after date."""
     ds = CommRecordsDataSource()
     date_range = DateRangeFilter([datetime(2015, 10, 1), None])
     recs = ds.run_unaggregated(self.company,
                                CommRecordsFilter(date_time=date_range), [])
     subjects = {r['subject'] for r in recs}
     expected = {self.record_3.subject}
     self.assertEqual(expected, subjects)
 def test__plain_filter_date_range(self):
     """Convert date range to plain object."""
     filt = ContactsFilter(date=DateRangeFilter(
         [datetime(2015, 9, 1), datetime(2015, 9, 30)]))
     self.assertEqual(
         {
             'date': [datetime(2015, 9, 1),
                      datetime(2015, 9, 30)],
         }, plain_filter(filt))
Exemplo n.º 6
0
 def test_filter_by_date_after(self):
     """Should show only partner with last_action_time after date."""
     ds = PartnersDataSource()
     recs = ds.run_unaggregated(
         self.company,
         PartnersFilter(
             date=DateRangeFilter([datetime(2015, 10, 1), None])), [])
     names = {r['name'] for r in recs}
     expected = {self.partner_b.name}
     self.assertEqual(expected, names)
 def test_default_filter(self):
     """should produce a populated filter object."""
     ds = ContactsDataSource()
     default_filter = ds.get_default_filter(None, self.company)
     self.assertEquals(datetime.now().year,
                       default_filter.date.dates[1].year)
     # Take out value dated today. Too hard to run through assertEquals.
     default_filter.date.dates[1] = None
     expected = ContactsFilter(
         date=DateRangeFilter([datetime(2014, 1, 1), None]))
     self.assertEquals(expected, default_filter)
    def test_date_range(self):
        """Test date ranges

        The range end should have an extra day added.
        """
        qs = MockQuerySet()
        result = apply_filter_to_queryset(
            qs, DateRangeFilter([datetime(2009, 12, 1),
                                 datetime(2009, 12, 2)]), 'zz')
        self.assert_filters_equals(
            [Q(zz__range=(datetime(2009, 12, 1), datetime(2009, 12, 3)))],
            result.filters)
    def test_filter_by_date_before(self):
        """
        Should show only contact with last_action_time before date.

        """
        ds = ContactsDataSource()
        recs = ds.run_unaggregated(
            self.company,
            ContactsFilter(
                date=DateRangeFilter([None, datetime(2015, 9, 30)])), [])
        names = {r['name'] for r in recs}
        expected = {self.sue.name}
        self.assertEqual(expected, names)
Exemplo n.º 10
0
 def get_default_filter(self, data_type, company):
     filter_spec = PartnersFilter(date=DateRangeFilter(
         [datetime(2014, 1, 1), datetime.now()]))
     adorned = adorn_filter(company, self, filter_spec)
     return adorned
Exemplo n.º 11
0
 def test_daterange_none(self):
     """Test dates with no range given."""
     qs = MockQuerySet()
     result = apply_filter_to_queryset(qs, DateRangeFilter(None), 'zz')
     self.assert_filters_equals([], result.filters)
Exemplo n.º 12
0
    def build_filter(self, filter_json):
        """
        Build a filter for the given filter_json

        filters look like: {
            filter_column1: some filter_spec...,
            filter_column2: another filter_spec...,

        }

        filter_specs can be any of:

        missing/null/[]: Empty lists, nulls, or missing keys are all
            interpreted as do not filter on this column.

        [date, date]: A list of two dates is date range. The filter
            class must have a key type of 'date'.

        1/"a": A single choice.

        [1, 2, 3]: A list of choices is an "or group". Match any of the
            choices.

        [[1, 2, 3], [4, 5, 6]]: A list of lists of choices is an "and group".
            Must match any of the first AND any of each of the subsequent
            groups.

        {'field1': some filter}: Match each field with the filter given:
            A CompositeAndFilter. The filter class must have a key type of
            'composite'.

        {'nolink': True}: A special object which means to find objects not
            linked to the objects in this column. i.e. If this is a tags
            column, find untagged objects.
        """
        kwargs = {}
        filter_spec = json.loads(filter_json)
        filter_type = self.ds.filter_type()
        types = filter_type.filter_key_types()
        for key, data in filter_spec.iteritems():
            key_type = types.get(key, None)
            if key_type is None:
                # empty lists are the same as no filter
                if data == []:
                    continue

                elif (isinstance(data, list) and
                        len(data) > 0 and
                        isinstance(data[0], list)):
                    kwargs[key] = AndGroupFilter([
                        OrGroupFilter([
                            MatchFilter(v)
                            for v in or_f
                            ])
                        for or_f in data])

                elif isinstance(data, list) and len(data) > 0:
                    kwargs[key] = OrGroupFilter([
                        MatchFilter(v)
                        for v in data
                        ])

                elif isinstance(data, dict) and 'nolink' in data:
                    kwargs[key] = UnlinkedFilter()

                else:
                    kwargs[key] = MatchFilter(data)

            elif key_type == 'date_range':
                date_strings = filter_spec.get(key, None)
                dates = [
                    (datetime.strptime(d, "%m/%d/%Y") if d else None)
                    for d in date_strings]

                kwargs[key] = DateRangeFilter(dates)

            elif key_type == 'composite':
                value_map = {
                    col: MatchFilter(value)
                    for col, value in filter_spec.get(key, {}).iteritems()
                }

                kwargs[key] = CompositeAndFilter(value_map)

            else:
                message = 'DataSource %s has unknown filter key type %s' % (
                    self.ds.__class__.__name__,
                    repr(key_type))
                raise KeyError(message)

        return filter_type(**kwargs)