def test_create_from_raw_fact_valid(self, raw_fact_parametrized): """Make sure the constructed ``Fact``s anatomy reflets our expectations.""" raw_fact, expectation = raw_fact_parametrized fact = Fact.create_from_raw_fact(raw_fact) assert fact.start == expectation['start'] assert fact.end == expectation['end'] assert fact.activity.name == expectation['activity'] if fact.activity.category: assert fact.activity.category.name == expectation['category'] else: assert expectation['category'] is None assert fact.description == expectation['description']
def create(self, start, end, activity, category, description): """ Create a fact for the given date """ command = activity if category: command = command + '@' + category if description: command = command + ',' + description fact = Fact.create_from_raw_fact(command) fact.start = self._cleanStart(start.toPyDateTime()) fact.end = self._cleanEnd(end.toPyDateTime()) try: fact = self._control.facts.save(fact) except ValueError as err: self.errorMessage.emit("Fact error: {0}".format(err)) else: self.factAdded.emit(FactPyQt(fact))
def _on_start_tracking_button(self, button): """ Start a new *ongoing fact*. Note: Whilst we accept the full ``raw_fact`` syntax, we ignore any ``Fact.end`` information encoded in the string. Unlike legacy hamster we *only* deal with *ongoing facts* in this widget. """ # [FIXME] # This should be done in one place only. And the hamster-lib. If at all # via hamster-lib.helpers. def complete_tmp_fact(fact): """Apply fallback logic in case no start time has been encoded.""" if not fact.start: fact.start = datetime.datetime.now() # Make sure we dismiss any extracted end information. fact.end = None return fact raw_fact = _u(self.raw_fact_entry.props.text) try: fact = Fact.create_from_raw_fact(raw_fact) except Exception as error: helpers.show_error(helpers.get_parent_window(self), error) else: fact = complete_tmp_fact(fact) try: fact = self._controler.store.facts.save(fact) except Exception as error: helpers.show_error(self.get_top_level(), error) else: self.emit('tracking-started') self._controler.signal_handler.emit('facts-changed') self.reset()
def updated_fact(self): """Fact instance using values at the time of accessing it.""" def get_raw_fact_value(): """Get text from raw fact entry field.""" return _u(self._raw_fact_widget.get_text()) def get_description_value(): """Get unicode value from widget.""" text_view = self._description_widget.get_child() text_buffer = text_view.get_buffer() start, end = text_buffer.get_bounds() return _u(text_buffer.get_text(start, end, True)) # Create a new fact instance from the provided raw string. fact = Fact.create_from_raw_fact(get_raw_fact_value()) # Instead of transferring all attributes of the parsed fact to the # existing ``self._fact`` we just go the other way round and attach the # old facts PK to the newly created instance. fact.pk = self._fact.pk # Explicit description trumps anything that may have been included in # the `raw_fact``. fact.description = get_description_value() return fact
def start(self, command): """ Start a fact """ if not command: self.errorMessage.emit('Empty fact information, can\'t start fact.') return # Some basic handling # If the command has a comma but no @, add the @ since comma is for comment if ( ',' in command ) and ( not '@' in command ): command = command.replace( ',', '@,' ) fact = Fact.create_from_raw_fact(command) if not fact.start: # No start time for the fact, set to now fact.start = datetime.datetime.now() # Clean up the fact start as per the hamster-QML interface. fact.start = self._cleanStart(fact.start) # Save the fact. If the fact does not have an end time it will be set as the # current fact. # At this point first check if there is not alreay a fact ongoing, # if there it, it must be stopped with the stop time set to before # the stop time of the ongoing fact. self.stop(fact.start, True) try: fact = self._control.facts.save(fact) except ValueError as err: self.errorMessage.emit("Fact start error: {0}".format(err)) else: self.startSuccessful.emit() # Check if the started fact has a end time. If it does have one, a # start and end time was specified and the fact was added to the # database. If it does not have a end it is an ongoing fact. if fact.end: self.factAdded.emit(FactPyQt(fact)) self.current()
def _on_start_tracking_button(self, button): """ Start a new *ongoing fact*. Note: Whilst we accept the full ``raw_fact`` syntax, we ignore any ``Fact.end`` information encoded in the string. Unlike legacy hamster we *only* deal with *ongoing facts* in this widget. """ # [FIXME] # This should be done in one place only. And the hamster-lib. If at all # via hamster-lib.helpers. def complete_tmp_fact(fact): """Apply fallback logic in case no start time has been encoded.""" if not fact.start: fact.start = datetime.datetime.now() # Make sure we dismiss any extracted end information. fact.end = None return fact raw_fact = _u(self.raw_fact_entry.props.text) try: fact = Fact.create_from_raw_fact(raw_fact) except Exception as error: helpers.show_error(self.get_toplevel(), error) else: fact = complete_tmp_fact(fact) try: fact = self._controler.store.facts.save(fact) except Exception as error: helpers.show_error(self.get_top_level(), error) else: self.emit('tracking-started') self._controler.signal_handler.emit('facts-changed') self.reset()
def test_create_from_raw_fact_with_delta(self, raw_fact, expectations): fact = Fact.create_from_raw_fact(raw_fact) assert fact.start == expectations['start']
def test_create_from_raw_fact_invalid(self, invalid_raw_fact_parametrized): """Make sure invalid string raises an exception.""" with pytest.raises(ValueError): Fact.create_from_raw_fact(invalid_raw_fact_parametrized)
def test_create_from_raw_fact_valid(self, valid_raw_fact_parametrized): """Make sure that a valid raw fact creates a proper Fact.""" assert Fact.create_from_raw_fact(valid_raw_fact_parametrized)