Пример #1
0
    def test_ranges(self):

        start = datetime(2012, 1, 1, 22, 0)  # sunday
        end = datetime(2012, 1, 2, 03, 0)
        dateranges = dates.DateRanges()
        overlaps = lambda method: dateranges.overlaps(method, start, end)

        dateranges.now = datetime(2011, 1, 1, 0, 0)
        self.assertFalse(overlaps('today'))
        self.assertFalse(overlaps('this_year'))
        self.assertTrue(overlaps('next_year'))
        self.assertTrue(overlaps('this_and_next_year'))

        dateranges.now = datetime(2012, 1, 1, 12, 0)
        self.assertTrue(overlaps('today'))
        self.assertTrue(overlaps('this_year'))
        self.assertTrue(overlaps('this_and_next_year'))

        dateranges.now = datetime(2011, 12, 31, 12, 0)  # saturday
        self.assertFalse(overlaps('today'))
        self.assertTrue(overlaps('tomorrow'))
        self.assertTrue(overlaps('this_week'))

        dateranges.now = datetime(2011, 12, 24, 12, 0)  # sat. one week before
        self.assertTrue(overlaps('this_week'))
        self.assertTrue(overlaps('next_week'))  # spillover

        dateranges.now = datetime(2011, 12, 23, 12, 0)  # fri. one week before
        self.assertFalse(overlaps('this_week'))
        self.assertTrue(overlaps('next_week'))

        dateranges.now = datetime(2012, 1, 2, 12, 0)
        self.assertTrue(overlaps('this_week'))

        dateranges.now = datetime(2011, 12, 29, 0, 0)  # thursday
        self.assertTrue(overlaps('this_week'))
        self.assertTrue(overlaps('this_weekend'))
        self.assertTrue(overlaps('next_week'))  # spillover
        self.assertFalse(overlaps('next_weekend'))

        dateranges.now = datetime(2011, 12, 22, 0, 0)  # one week earlier
        self.assertFalse(overlaps('this_week'))
        self.assertFalse(overlaps('this_weekend'))
        self.assertTrue(overlaps('next_week'))
        self.assertTrue(overlaps('next_weekend'))

        dateranges.now = datetime(2011, 12, 12, 0, 0)
        self.assertFalse(overlaps('this_month'))
        self.assertTrue(overlaps('next_month'))

        dateranges.now = datetime(2012, 1, 1, 0, 0)
        self.assertTrue(overlaps('this_month'))
        self.assertFalse(overlaps('next_month'))
Пример #2
0
    def export(self, search=None, term=None, max=None, imported=False, **kw):
        # Unfortunantely, the search/filter/items/query functions depend all
        # on some view-state variables - we have to find the subset ourselves.
        if search:
            search = search.replace('*', '')
            search = '"{}"*'.format(search)
            kw['SearchableText'] = search

        elif term:
            kw['categories'] = {'query': term.values(), 'operator': 'and'}

        subset = self.catalog(
            path={
                'query': self.path,
                'depth': 1
            },
            object_provides=IEventsDirectoryItem.__identifier__,
            review_state='published',
            **kw)

        if not imported:
            # Unfortunantely, we cannot query for 'not having an interface' nor
            # not having not the attribute 'source' - we have to build the
            # difference of the query above and all external events!
            external = self.catalog(
                path={
                    'query': self.path,
                    'depth': 1
                },
                object_provides=IExternalEvent.__identifier__,
                review_state='published',
                **kw)
            if len(external):
                subset_ids = list(
                    frozenset([brain.id for brain in subset]) -
                    frozenset([brain.id for brain in external]))
                subset = [brain for brain in subset if brain.id in subset_ids]

        if term:
            filter_key = partial(is_exact_match, term=term)
            subset = filter(filter_key, subset)

        subset = sorted(subset, key=self.sortkey())

        # Get lazy list from indexer using the subset
        start, end = getattr(dates.DateRanges(), 'this_and_next_year')
        ll = self.ix_published.lazy_list(start, end, subset)

        # Check if upper limit is valid
        if not isinstance(max, (int, long)) or (max <= 0):
            max = len(ll)

        return islice(ll, max)
Пример #3
0
    def test_morning(self):

        dateranges = dates.DateRanges()
        dateranges.now = datetime.today()
        overlaps = lambda: dateranges.overlaps('today', start, end)

        start = datetime.today().replace(hour=1, minute=0)
        end = datetime.today().replace(hour=4, minute=0)
        self.assertTrue(overlaps())

        start = datetime.today().replace(hour=1, minute=0)
        end = datetime.today().replace(hour=3, minute=0)
        self.assertTrue(overlaps())

        start = datetime.today().replace(hour=1, minute=0)
        end = datetime.today().replace(hour=2, minute=59)
        self.assertFalse(overlaps())
Пример #4
0
    def __init__(self, *args, **kwargs):
        self._daterange = dates.default_daterange
        start, end = getattr(dates.DateRanges(), 'custom')
        self._custom_start, self._custom_end = dates.as_range(start, end)
        self._state = 'published'
        self._import_source = ''

        self.subset = None

        super(EventsDirectoryCatalog, self).__init__(*args, **kwargs)

        self.ix_submitted = self.index_for_state('submitted')
        self.ix_published = self.index_for_state('published')
        self.ix_hidden = self.index_for_state('hidden')
        self.indices = dict(submitted=self.ix_submitted,
                            published=self.ix_published,
                            hidden=self.ix_hidden)
Пример #5
0
 def daterange_dates(self):
     if self._daterange == 'custom':
         return self._custom_start, self._custom_end
     else:
         return getattr(dates.DateRanges(), self._daterange)
Пример #6
0
 def get_last_custom_end_date(self):
     """ Returns the last selected custom end date. """
     unused, default = getattr(dates.DateRanges(), 'custom')
     return session.get_session(self.context,
                                'custom_date_end') or default
Пример #7
0
 def get_last_custom_start_date(self):
     """ Returns the last selected custom start date. """
     default, unused = getattr(dates.DateRanges(), 'custom')
     return session.get_session(self.context,
                                'custom_date_start') or default
Пример #8
0
 def occurrence_groups(self):
     dr = dates.DateRanges()
     start, end = dr.this_year[0], dr.next_year[1]
     result = grouped_occurrences(occurrences(self.context, start, end),
                                  self.request)
     return result