def test_save_new(self, fact_manager, fact): """Make sure instance is saved and returned.""" dbus_fact = helpers.hamster_to_dbus_fact(fact) result = fact_manager.Save(dbus_fact) result = helpers.dbus_to_hamster_fact(result) assert fact.as_tuple(include_pk=False) == result.as_tuple( include_pk=False)
def get_all(self, start=None, end=None, filter_term=''): """ Return all facts within a given timeframe. The 'timeframe' begins at the begining of ``start`` and concludes at the end of ``end``. Args: start (datetime.datetime, datetime.date, datetime.time or None, 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 (datetime.datetime, datetime.date, datetime.time or None, 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 ``Fact``s 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'. """ if not (isinstance(start, datetime.datetime) or isinstance(start, datetime.date) or (isinstance(start, datetime.time) or (start is None))): raise TypeError if not (isinstance(end, datetime.datetime) or isinstance(end, datetime.date) or (isinstance(end, datetime.time) or (end is None))): raise TypeError if start and end and (end <= start): message = _("End value can not be earlier than start!") raise ValueError(message) start = helpers.datetime_to_text(start) end = helpers.datetime_to_text(end) filter_term = text_type(filter_term) result = self._interface.GetAll(start, end, filter_term) return [helpers.dbus_to_hamster_fact(fact) for fact in result]
def get_tmp_fact(self): """ Provide a way to retrieve any existing 'ongoing fact'. Returns: hamster_lib.Fact: An instance representing our current 'ongoing fact'.capitalize Raises: KeyError: If no ongoing fact is present. """ result = self._interface.GetTmpFact() return helpers.dbus_to_hamster_fact(result)
def stop_tmp_fact(self): """ Stop current 'ongoing fact'. Returns: hamster_lib.Fact: The stored fact. Raises: ValueError: If there is no currently 'ongoing fact' present. """ result = self._interface.StopTmpFact() return helpers.dbus_to_hamster_fact(result)
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'. """ result = self._interface.GetToday() return [helpers.dbus_to_hamster_fact(fact) for fact in result]
def get(self, pk): """ Return a ``Fact`` by its primary key. Args: pk (int): Primary key of the ``Fact to be retrieved``. Returns: hamster_lib.Fact: The ``Fact`` corresponding to the primary key. """ result = self._interface.Get(int(pk)) return helpers.dbus_to_hamster_fact(result)
def GetTodays(self): # NOQA """ Get facts of today, respecting hamster day_start, day_end settings. Returns: list: A list of ``helpers.DBushamster_lib.Fact``-tuples. For details on those, please see ``helpers.DBushamster_lib.Fact``. Note: This only returns proper facts and will not include any ongoing fact! """ facts = self.controller.store.facts.get_today() return [helpers.dbus_to_hamster_fact(fact) for fact in facts]
def save(self, fact): """ Save a Fact. For details see the corresponding method in ``hamster_lib.storage``. """ if not isinstance(fact, lib_objects.Fact): message = _( "You need to pass a ``hamster_lib.objects.Fact`` instance") raise TypeError(message) dbus_fact = helpers.hamster_to_dbus_fact(fact) result = self._interface.Save(dbus_fact) return helpers.dbus_to_hamster_fact(result)
def Save(self, fact_tuple): """ Take a fact save it to our backend. Args: fact_tuple (DBushamster_lib.Fact): ``hamster_lib.Fact`` to be saved. Returns: tuple (DBusFact): Serialized version of the saved ``hamster_lib.Fact`` instance. """ fact = helpers.dbus_to_hamster_fact(fact_tuple) result = self._controller.store.facts.save(fact) self._main_object.FactChanged() self._main_object.CategoryChanged() self._main_object.TagChanged() self._main_object.ActivityChanged() # This is because during creation/updating a new related instances # may be created as well. return helpers.hamster_to_dbus_fact(result)
def test_dbus_to_hamster_fact(fact_tuple, expectation): """Make sure that de-serialization works as intended.""" result = helpers.dbus_to_hamster_fact(fact_tuple) assert result == expectation assert isinstance(result, Fact)
def factory(**kwargs): fact = fact_factory.build(**kwargs) result = fact_manager.Save(helpers.hamster_to_dbus_fact(fact)) return helpers.dbus_to_hamster_fact(result)
def test_get(self, fact_manager, stored_fact): """Make sure instance is returned.""" result = fact_manager.Get(stored_fact.pk) result = helpers.dbus_to_hamster_fact(result) assert result == stored_fact