Exemplo n.º 1
0
    def get_today(self):
        """
        Return all facts for today, while respecting ``day_start``.

        Returns:
            list: List of ``Fact`` instances.

        Note:
            * This does only return proper facts and does not include any existing 'ongoing fact'.
        """
        self.store.logger.debug(_("Returning today's facts"))

        today = datetime.date.today()
        return self.get_all(
            datetime.datetime.combine(today, self.store.config['day_start']),
            helpers.end_day_to_datetime(today, self.store.config))
Exemplo n.º 2
0
    def get_today(self):
        """
        Return all facts for today, while respecting ``day_start``.

        Returns:
            list: List of ``Fact`` instances.

        Note:
            * This does only return proper facts and does not include any existing 'ongoing fact'.
        """
        self.store.logger.debug(_("Returning today's facts"))

        today = datetime.date.today()
        return self.get_all(
            datetime.datetime.combine(today, self.store.config['day_start']),
            helpers.end_day_to_datetime(today, self.store.config)
        )
Exemplo n.º 3
0
    def get_all(self, start=None, end=None, filter_term=''):
        """
        Return all facts within a given timeframe (beginning of start_date
        end of end_date) that match given search terms.

        Args:
            start_date (datetime.datetime, optional): Consider only Facts starting at or after
                this date. Alternatively you can also pass a ``datetime.datetime`` object
                in which case its own time will be considered instead of the default ``day_start``
                or a ``datetime.time`` which will be considered as today.
                Defaults to ``None``.
            end_date (datetime.datetime, optional): Consider only Facts ending before or at
                this date. Alternatively you can also pass a ``datetime.datetime`` object
                in which case its own time will be considered instead of the default ``day_start``
                or a ``datetime.time`` which will be considered as today.
                Defaults to ``None``.
            filter_term (str, optional): Only consider ``Facts`` with this string as part of their
                associated ``Activity.name``

        Returns:
            list: List of ``Facts`` matching given specifications.

        Raises:
            TypeError: If ``start`` or ``end`` are not ``datetime.date``, ``datetime.time`` or
                ``datetime.datetime`` objects.
            ValueError: If ``end`` is before ``start``.

        Note:
            * This public function only provides some sanity checks and normalization. The actual
                backend query is handled by ``_get_all``.
            * ``search_term`` should be prefixable with ``not`` in order to invert matching.
            * This does only return proper facts and does not include any existing 'ongoing fact'.
        """
        self.store.logger.debug(
            _("Start: '{start}', end: {end} with filter: {filter} has been received."
              .format(start=start, end=end, filter=filter_term)))

        if start is not None:
            if isinstance(start, datetime.datetime):
                # isinstance(datetime.datetime, datetime.date) returns True,
                # which is why we need to catch this case first.
                pass
            elif isinstance(start, datetime.date):
                start = datetime.datetime.combine(
                    start, self.store.config['day_start'])
            elif isinstance(start, datetime.time):
                start = datetime.datetime.combine(datetime.date.today(), start)
            else:
                message = _(
                    "You need to pass either a datetime.date, datetime.time or datetime.datetime"
                    " object.")
                self.store.logger.debug(message)
                raise TypeError(message)

        if end is not None:
            if isinstance(end, datetime.datetime):
                # isinstance(datetime.datetime, datetime.date) returns True,
                # which is why we need to except this case first.
                pass
            elif isinstance(end, datetime.date):
                end = helpers.end_day_to_datetime(end, self.store.config)
            elif isinstance(end, datetime.time):
                end = datetime.datetime.combine(datetime.date.today(), end)
            else:
                message = _(
                    "You need to pass either a datetime.date, datetime.time or datetime.datetime"
                    " object.")
                raise TypeError(message)

        if start and end and (end <= start):
            message = _("End value can not be earlier than start!")
            self.store.logger.debug(message)
            raise ValueError(message)

        return self._get_all(start, end, filter_term)
Exemplo n.º 4
0
 def test_various_end_days(self, base_config, day_start, expectation):
     """Ensure that resulting ``end datetimes`` match our expectation given ``day_end``"""
     base_config['day_start'] = day_start
     end_day = datetime.datetime(2015, 4, 15)
     assert helpers.end_day_to_datetime(end_day, base_config) == expectation
Exemplo n.º 5
0
 def test_end_day_to_daytime(self, base_config, end_day, day_start, expectation):
     """Make sure that end_day conversion matches our expectation."""
     config = base_config
     config['day_start'] = day_start
     assert helpers.end_day_to_datetime(end_day, config) == expectation
Exemplo n.º 6
0
 def test_various_end_days(self, base_config, day_start, expectation):
     """Ensure that resulting ``end datetimes`` match our expectation given ``day_end``"""
     base_config['day_start'] = day_start
     end_day = datetime.datetime(2015, 4, 15)
     assert helpers.end_day_to_datetime(end_day, base_config) == expectation
Exemplo n.º 7
0
 def test_end_day_to_daytime(self, base_config, end_day, day_start,
                             expectation):
     """Make sure that end_day conversion matches our expectation."""
     config = base_config
     config['day_start'] = day_start
     assert helpers.end_day_to_datetime(end_day, config) == expectation
Exemplo n.º 8
0
    def get_all(self, start=None, end=None, filter_term=''):
        """
        Return all facts within a given timeframe (beginning of start_date
        end of end_date) that match given search terms.

        Args:
            start_date (datetime.datetime, optional): Consider only Facts starting at or after
                this date. Alternatively you can also pass a ``datetime.datetime`` object
                in which case its own time will be considered instead of the default ``day_start``
                or a ``datetime.time`` which will be considered as today.
                Defaults to ``None``.
            end_date (datetime.datetime, optional): Consider only Facts ending before or at
                this date. Alternatively you can also pass a ``datetime.datetime`` object
                in which case its own time will be considered instead of the default ``day_start``
                or a ``datetime.time`` which will be considered as today.
                Defaults to ``None``.
            filter_term (str, optional): Only consider ``Facts`` with this string as part of their
                associated ``Activity.name``

        Returns:
            list: List of ``Facts`` matching given specifications.

        Raises:
            TypeError: If ``start`` or ``end`` are not ``datetime.date``, ``datetime.time`` or
                ``datetime.datetime`` objects.
            ValueError: If ``end`` is before ``start``.

        Note:
            * This public function only provides some sanity checks and normalization. The actual
                backend query is handled by ``_get_all``.
            * ``search_term`` should be prefixable with ``not`` in order to invert matching.
            * This does only return proper facts and does not include any existing 'ongoing fact'.
        """
        self.store.logger.debug(_(
            "Start: '{start}', end: {end} with filter: {filter} has been received.".format(
                start=start, end=end, filter=filter_term)
        ))

        if start is not None:
            if isinstance(start, datetime.datetime):
                # isinstance(datetime.datetime, datetime.date) returns True,
                # which is why we need to catch this case first.
                pass
            elif isinstance(start, datetime.date):
                start = datetime.datetime.combine(start, self.store.config['day_start'])
            elif isinstance(start, datetime.time):
                start = datetime.datetime.combine(datetime.date.today(), start)
            else:
                message = _(
                    "You need to pass either a datetime.date, datetime.time or datetime.datetime"
                    " object."
                )
                self.store.logger.debug(message)
                raise TypeError(message)

        if end is not None:
            if isinstance(end, datetime.datetime):
                # isinstance(datetime.datetime, datetime.date) returns True,
                # which is why we need to except this case first.
                pass
            elif isinstance(end, datetime.date):
                end = helpers.end_day_to_datetime(end, self.store.config)
            elif isinstance(end, datetime.time):
                end = datetime.datetime.combine(datetime.date.today(), end)
            else:
                message = _(
                    "You need to pass either a datetime.date, datetime.time or datetime.datetime"
                    " object."
                )
                raise TypeError(message)

        if start and end and (end <= start):
            message = _("End value can not be earlier than start!")
            self.store.logger.debug(message)
            raise ValueError(message)

        return self._get_all(start, end, filter_term)