Example #1
0
def update(id, name=None, description=None):
    """
    Update an existing task with the supplied name or desciption.

    The provided name must adhear to the same validation conditions as
    for the create() method.  E.g. it must not be empty, and must not
    already exist. If the empty string is passed as the descripition,
    any existing description will be removed.

    :param id: The ID of an existing task.
    :param name: A new name for the task. (Default value = None)
    :param description:  A new description for the task. (Default value = None)
    :raises: ValidationError If the name already exists on a different task,
             or if the name is invalid.
    """
    log.debug("updating task %s with name=%s", id, name)

    try:
        with transaction() as session:
            task = session.query(Task).get(id)
            if name is not None:
                task.name = name
            if description is not None:
                if description == "":
                    task.description = None
                else:
                    task.description = description
    except IntegrityError:
        raise ValidationError("A task with name %s already exists" % name)
Example #2
0
def update(id, task=None, start=None, stop=None):
    """
    Update one or more fields of a given timer.

    :param id: The ID of the existing timer.
    :param task:  The new task name. (Default value = None)
    :param start:  The new start time. (Default value = None)
    :param stop:  The new stop time. (Default value = None)
    :raises: ValidationError if the timer fails validation checks.
    """
    with transaction() as session:
        try:
            timer = session.query(Timer).get(id)

            if task is not None:
                try:
                    task = session.query(Task).filter(Task.name == task).one()
                except NoResultFound:
                    raise ValidationError("No such task %s" % task)
                timer.task = task

            if start is not None:
                timer.start = start

            if stop is not None:
                if stop == "":
                    timer.stop = None
                else:
                    timer.stop = stop

            _validate(timer)

        except AssertionError as err:
            raise ValidationError("Invalid timer %s: %s" % (timer, err))
Example #3
0
def remove(id):
    """
    Remove an existing timer.

    :param id: The id of the timer to delete.
    """
    with transaction() as session:
        session.query(Timer).filter(Timer.id == id).delete()
Example #4
0
def get(name):
    """
    Get a task record by name.

    :param name: The name of the task.
    :return: A Task record.
    """
    with transaction() as session:
        return session.query(Task).filter(Task.name == name).one()
Example #5
0
def last():
    """Fetch the last timer, active or not.

    :return dict: The dictionary represenation of the timer or None
    """
    with transaction() as session:
        result = session.query(Timer).order_by(Timer.start.desc()).first()

        return result.as_dict() if result else None
Example #6
0
def create(name, description=None):
    """
    Create a new task.

    :param name: The task name.
    :param description:  An optional description which will be shown when
                         listing tasks.
    :raises: ValidationError If there is a database conflict because a task
             with the given name already exists, or if the name is invalid.
    """
    log.debug("creating task with name %s, description=%s", name, description)

    if name == "":
        raise ValidationError("Cannot use empty name")

    try:
        with transaction() as session:
            task = Task(name=name, description=description)
            session.add(task)
    except IntegrityError:
        raise ValidationError("A task with name %s already exists" % name)
Example #7
0
def remove(name):
    """
    Remove a task by name.

    A task may be removed only if there are no timers, active or stopped,
    using the existing task.

    :param name: The name of an existing task.
    :raises: ValidationError if no task exists with the given name, or if
             there is a problem removing the task.
    """
    log.debug("remove task with name %s", name)

    try:
        with transaction() as session:
            try:
                task = session.query(Task).filter(Task.name == name).one()
            except NoResultFound:
                raise ValidationError("no such task with name %s", name)

            session.delete(task)
    except IntegrityError:
        raise ValidationError("Can not remove a task with existing records")
Example #8
0
def create(task, start):
    """Create a new timer for the given task.

    :param str: task: The name of an existing task.
    :param datetime.datetime start: The UTC starting time.
    :raises: ValidationError If the timer fails to validate.
    """

    with transaction() as session:
        try:
            task = session.query(Task).filter(Task.name == task).one()
        except NoResultFound:
            raise ValidationError("Invalid task %s" % task)

        for active in session.query(Timer).filter(Timer.stop.is_(None)).all():
            active.stop = start

        try:
            timer = Timer(task=task, start=start)
            _validate(timer)
            session.add(timer)
        except AssertionError as err:
            raise ValidationError(err)
Example #9
0
def session(log):
    connect(db_url="sqlite:///", echo=False)

    with transaction() as session:
        yield session
Example #10
0
def tasks():
    """Generator for iterating through all tasks."""
    with transaction() as session:
        for task in session.query(Task).all():
            yield task
Example #11
0
def slice(start, end):
    with transaction() as session:
        for timer in (session.query(Timer).filter(start <= Timer.start,
                                                  Timer.start < end).all()):
            yield timer.as_dict()
Example #12
0
def timers():
    """Generator for iterating over all timers."""
    with transaction() as session:
        for timer in session.query(Timer).all():
            yield timer
Example #13
0
def active():
    """Fetch the active timer if there is one, or None if not."""
    with transaction() as session:
        return session.query(Timer).filter(Timer.stop.is_(None)).one_or_none()