Date range widget
=================

Implementation of date range criteria for faceted navigation usage.

Imports
-------

    >>> from DateTime import DateTime
    >>> from zope.component import getMultiAdapter
    >>> from Products.CMFCore.utils import getToolByName
    >>> from eea.facetednavigation.interfaces import ICriteria

Set up
------

    >>> portal = layer['portal']
    >>> request = layer['request']
    >>> sandbox = portal['sandbox']
    >>> sandbox.setTitle('Faceted navigation folder test')

    >>> subtyper = getMultiAdapter((sandbox, request), name=u'faceted_subtyper')
    >>> subtyper.enable()

    >>> putils = getToolByName(sandbox, 'plone_utils')
    >>> handler = getMultiAdapter((sandbox, request), name=u'faceted_configure')
    >>> query = getMultiAdapter((sandbox, request), name=u'faceted_query')

    >>> in_the_past = DateTime('1970/01/01')
    >>> today = DateTime()
    >>> in_the_future = DateTime('2030/12/31')

Cleanup default widgets
-----------------------

    >>> cids = ICriteria(sandbox).keys()

Default widgets

    >>> cids
    [...'c1',...]

    >>> for cid in cids:
    ...     ICriteria(sandbox).delete(cid)
    >>> ICriteria(sandbox).keys()
    []

Add hidden text widget
----------------------

    >>> _ = handler(addWidget_button=True, wtype='text')
    >>> _ = handler(updateCriterion_button='Save', cid='c0',
    ...     default='Relative date', index='Title', hidden=True)

Add test folders
----------------
Past

    >>> past = portal.invokeFactory('Document', 'past', title='Relative date widget 1')
    >>> past = portal._getOb(past)
    >>> past.setLanguage('en')
    >>> putils.editMetadata(past, effective_date=today - 365)

3 days old

    >>> past_n = portal.invokeFactory('Document', 'past_n', title='Relative date widget 2')
    >>> past_n = portal._getOb(past_n)
    >>> past_n.setLanguage('en')
    >>> putils.editMetadata(past_n, effective_date=today - 3)

Present

    >>> now = portal.invokeFactory('Document', 'now', title='Relative date widget 3')
    >>> now = portal._getOb(now)
    >>> now.setLanguage('en')
    >>> putils.editMetadata(now, effective_date=today)

3 days in the future

    >>> future_n = portal.invokeFactory('Document', 'future_n', title='Relative date widget 4')
    >>> future_n = portal._getOb(future_n)
    >>> future_n.setLanguage('en')
    >>> putils.editMetadata(future_n, effective_date=today + 3)

Future

    >>> future = portal.invokeFactory('Document', 'future', title='Relative date widget 3')
    >>> future = portal._getOb(future)
    >>> future.setLanguage('en')
    >>> putils.editMetadata(future, effective_date=today + 365)

Empty query
-----------

    >>> brains = query.query(batch=False)
    >>> len(brains)
    5

Add widget
----------

    >>> _ = handler(wtype='daterange', addWidget_button=True)


Get some results (default set)
------------------------------

All before now

    >>> start = in_the_past
    >>> end = today
    >>> date_query = '%s=>%s' % (start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d'))

    >>> _ = handler(updateCriterion_button='Save', cid='c1', hidden=True,
    ...     index='effective', default=date_query)
    >>> brains = query.query()
    >>> [brain.getId for brain in brains]
    ['now', 'past_n', 'past']

All before last week

    >>> start = in_the_past
    >>> end = today - 7
    >>> date_query = '%s=>%s' % (start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d'))

    >>> _ = handler(updateCriterion_button='Save', cid='c1', hidden=True,
    ...     index='effective', default=date_query)
    >>> brains = query.query()
    >>> [brain.getId for brain in brains]
    ['past']


All before yesterday

    >>> start = in_the_past
    >>> end = today - 2
    >>> date_query = '%s=>%s' % (start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d'))

    >>> _ = handler(updateCriterion_button='Save', cid='c1', hidden=True,
    ...     index='effective', default=date_query)
    >>> brains = query.query()
    >>> [brain.getId for brain in brains]
    ['past_n', 'past']

Last week

    >>> start = today - 7
    >>> end = today
    >>> date_query = '%s=>%s' % (start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d'))

    >>> _ = handler(updateCriterion_button='Save', cid='c1', hidden=True,
    ...     index='effective', default=date_query)
    >>> brains = query.query()
    >>> [brain.getId for brain in brains]
    ['now', 'past_n']

Today

    >>> start = end = today
    >>> date_query = '%s=>%s' % (start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d'))

    >>> _ = handler(updateCriterion_button='Save', cid='c1', hidden=True,
    ...     index='effective', default=date_query)
    >>> brains = query.query()
    >>> [brain.getId for brain in brains]
    ['now']

Next week

    >>> start = today
    >>> end = today + 7
    >>> date_query = '%s=>%s' % (start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d'))

    >>> _ = handler(updateCriterion_button='Save', cid='c1', hidden=True,
    ...     index='effective', default=date_query)
    >>> brains = query.query()
    >>> [brain.getId for brain in brains]
    ['future_n', 'now']

All after next week

    >>> start = today + 7
    >>> end = in_the_future
    >>> date_query = '%s=>%s' % (start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d'))

    >>> _ = handler(updateCriterion_button='Save', cid='c1', hidden=True,
    ...     index='effective', default=date_query)
    >>> brains = query.query()
    >>> [brain.getId for brain in brains]
    ['future']

All after now

    >>> start = today
    >>> end = in_the_future
    >>> date_query = '%s=>%s' % (start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d'))

    >>> _ = handler(updateCriterion_button='Save', cid='c1', hidden=True,
    ...     index='effective', default=date_query)
    >>> brains = query.query()
    >>> [brain.getId for brain in brains]
    ['future', 'future_n', 'now']

Get some results (no default)
------------------------------

    >>> _ = handler(updateCriterion_button='Save', cid='c1', hidden=False,
    ...     index='effective')

All before now

    >>> start = in_the_past
    >>> end = today

    >>> brains = query.query(c1=[start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d')])
    >>> [brain.getId for brain in brains]
    ['now', 'past_n', 'past']

All before last week

    >>> start = in_the_past
    >>> end = today - 7

    >>> brains = query.query(c1=[start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d')])
    >>> [brain.getId for brain in brains]
    ['past']


All before yesterday

    >>> start = in_the_past
    >>> end = today - 1

    >>> brains = query.query(c1=[start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d')])
    >>> [brain.getId for brain in brains]
    ['past_n', 'past']

Last week

    >>> start = today - 7
    >>> end = today

    >>> brains = query.query(c1=[start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d')])
    >>> [brain.getId for brain in brains]
    ['now', 'past_n']

Today

    >>> start = end = today

    >>> brains = query.query(c1=[start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d')])
    >>> [brain.getId for brain in brains]
    ['now']

Next week

    >>> start = today
    >>> end = today + 7

    >>> brains = query.query(c1=[start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d')])
    >>> [brain.getId for brain in brains]
    ['future_n', 'now']

All after next week

    >>> start = today + 7
    >>> end = in_the_future

    >>> brains = query.query(c1=[start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d')])
    >>> [brain.getId for brain in brains]
    ['future']

All after now

    >>> start = today
    >>> end = in_the_future

    >>> brains = query.query(c1=[start.strftime('%Y/%m/%d'), end.strftime('%Y/%m/%d')])
    >>> [brain.getId for brain in brains]
    ['future', 'future_n', 'now']
