Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
    def GetAll(self, start, end, filter_term):
        """
        Get all facts matching criteria.

        Args:
            start (int): Unix-timestamp for start of timeframe. ``-1`` for ``None``.
            end (int): Unix-timestamp for end of timeframe. ``-1`` for ``None``.
            filter_term (str): Only consider ``hamster_lib.Facts`` with this string as part of
                their associated ``hamster_lib.Activity.name``

        Returns:
            list: A list of ``helpers.DBushamster_lib.Fact``-tuples.
                For details on those, please see ``helpers.hamster_to_dbus_fact``.

        Note:
            Our ``hamsterlib`` manager method is actually more flexible and would allow
            for ``datetime.datetime`` or ``datetime.time`` instances instead of just
            ``datetime.date`` instances for ``start`` and ``end``. But for compatibility
            reasons we stick with the simpler legacy version for now.
        """
        def get_start(start):
            return None

        def get_end(end):
            return None

        def get_filter_term(end):
            return filter_term

        facts = self._controller.store.facts.get_all(
            get_start(start), get_end(end), get_filter_term(filter_term))
        return [helpers.hamster_to_dbus_fact(fact) for fact in facts]
Exemplo n.º 3
0
    def SaveRaw(self, raw_fact):
        """
        Take a raw_fact save it to our backend.

        Args:
            raw_fact (str): ``raw fact`` string.

        Returns:
            tuple (DBushamster_lib.Fact): Serialized version of the saved ``hamster_lib.Fact``
            instance.

        Note: This method is identical to ``Savehamster_lib.Fact`` with the only difference being
            that it takes a ``raw fact`` instead of a serialized ``hamster_lib.Fact`` instance.
        """
        fact = hamster_lib.Fact.create_from_raw_fact(raw_fact)
        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)
Exemplo n.º 4
0
    def Get(self, fact_pk):
        """Get fact by PK.

        Args:
            fact_pk (int): PK of the fact to be retrieved.

        Returns:
            DBushamster_lib.Fact: Serialized ``hamster_lib.Fact`` instance.
        """
        fact = self._controller.facts.get(fact_pk)
        return helpers.hamster_to_dbus_fact(fact)
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
def test_hamster_to_dbus_fact(fact, expectation):
    """Make sure that serialization works as intended."""
    result = helpers.hamster_to_dbus_fact(fact)
    assert result == expectation
    assert isinstance(result, DBusFact)
Exemplo n.º 8
0
 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)