Ejemplo n.º 1
0
def create_or_update_time_spent(task_id, person_id, date, duration, add=False):
    """
    Create a new time spent if it doesn't exist. If it exists, it update it
    with the new duratin and returns it from the database.
    """
    try:
        time_spent = TimeSpent.get_by(task_id=task_id,
                                      person_id=person_id,
                                      date=date)
    except DataError:
        raise WrongDateFormatException

    if time_spent is not None:
        if duration == 0:
            time_spent.delete()
        elif add:
            time_spent.update({"duration": time_spent.duration + duration})
        else:
            time_spent.update({"duration": duration})
    else:
        time_spent = TimeSpent.create(task_id=task_id,
                                      person_id=person_id,
                                      date=date,
                                      duration=duration)

    task = Task.get(task_id)
    task.duration = 0
    time_spents = TimeSpent.get_all_by(task_id=task_id)
    for time_spent in time_spents:
        task.duration += time_spent.duration
    task.save()
    events.emit("task:update", {"task_id": task_id})

    return time_spent.serialize()
Ejemplo n.º 2
0
def create_or_update_time_spent(task_id, person_id, date, duration, add=False):
    """
    Create a new time spent if it doesn't exist. If it exists, it update it
    with the new duratin and returns it from the database.
    """
    try:
        time_spent = TimeSpent.get_by(
            task_id=task_id,
            person_id=person_id,
            date=date
        )
    except DataError:
        raise WrongDateFormatException

    if time_spent is not None:
        if add:
            time_spent.update({"duration": time_spent.duration + duration})
        else:
            time_spent.update({"duration": duration})
    else:
        time_spent = TimeSpent.create(
            task_id=task_id,
            person_id=person_id,
            date=date,
            duration=duration
        )
    return time_spent.serialize()
Ejemplo n.º 3
0
def create_or_update_time_spent(task_id, person_id, date, duration, add=False):
    """
    Create a new time spent if it doesn't exist. If it exists, it update it
    with the new duratin and returns it from the database.
    """
    try:
        time_spent = TimeSpent.get_by(task_id=task_id,
                                      person_id=person_id,
                                      date=date)
    except DataError:
        raise WrongDateFormatException

    task = Task.get(task_id)
    project_id = str(task.project_id)
    if time_spent is not None:
        if duration == 0:
            time_spent.delete()
        elif add:
            time_spent.update({"duration": time_spent.duration + duration})
        else:
            time_spent.update({"duration": duration})
        events.emit(
            "time-spent:update",
            {"time_spent_id": str(time_spent.id)},
            project_id=project_id,
        )
    else:
        time_spent = TimeSpent.create(task_id=task_id,
                                      person_id=person_id,
                                      date=date,
                                      duration=duration)
        persons_service.update_person_last_presence(person_id)
        events.emit(
            "time-spent:new",
            {"time_spent_id": str(time_spent.id)},
            project_id=project_id,
        )

    task.duration = 0
    time_spents = TimeSpent.get_all_by(task_id=task_id)
    for task_time_spent in time_spents:
        task.duration += task_time_spent.duration
    task.save()
    clear_task_cache(task_id)
    events.emit("task:update", {"task_id": task_id}, project_id=project_id)

    return time_spent.serialize()
Ejemplo n.º 4
0
def create_or_update_time_spent(task_id, person_id, date, duration, add=False):
    try:
        time_spent = TimeSpent.get_by(task_id=task_id,
                                      person_id=person_id,
                                      date=date)
    except DataError:
        raise WrongDateFormatException

    if time_spent is not None:
        if add:
            time_spent.update({"duration": time_spent.duration + duration})
        else:
            time_spent.update({"duration": duration})
    else:
        time_spent = TimeSpent.create(task_id=task_id,
                                      person_id=person_id,
                                      date=date,
                                      duration=duration)
    return time_spent.serialize()
Ejemplo n.º 5
0
def create_or_update_time_spent(
        task_id,
        person_id,
        date,
        duration,
        add=False,
        description=None,
        validation_status_id=None):
    """
    Create a new time spent if it doesn't exist. If it exists, it updates it
    with the new duratin and returns it from the database.
    """
    try:
        time_spent = TimeSpent.get_by(
            task_id=task_id, person_id=person_id, date=date
        )
    except DataError:
        raise WrongDateFormatException

    task = Task.get(task_id)
    project_id = str(task.project_id)
    if time_spent is not None:
        if duration == 0:
            time_spent.delete()
        elif add:
            time_spent.update({"duration": time_spent.duration + duration})
        else:
            time_spent.update({"duration": duration})

        if description:
            time_spent.update({"description": description, })

        if validation_status_id:
            time_spent.update({"validation_status_id": validation_status_id})

        events.emit(
            "time-spent:update",
            {"time_spent_id": str(time_spent.id)},
            project_id=project_id
        )
    else:
        if not validation_status_id:
            # Default value in migration does not work. See migration file
            # 18e010daad1d_add_timespent_description_and_validated.py
            validation_default_value = app.config["DEFAULT_TIMESPENT_VALIDATION_STATUS"]
            validation_status_id = TimeSpentStatus.query.filter(
                TimeSpentStatus.name.like(validation_default_value)).first().id

        time_spent = TimeSpent.create(
            task_id=task_id,
            person_id=person_id,
            date=date,
            duration=duration,
            description=description,
            validation_status_id=validation_status_id,
        )
        events.emit(
            "time-spent:new",
            {"time_spent_id": str(time_spent.id)},
            project_id=project_id
        )

    task.duration = 0
    time_spents = TimeSpent.get_all_by(task_id=task_id)
    for task_time_spent in time_spents:
        task.duration += task_time_spent.duration
    task.save()
    clear_task_cache(task_id)
    events.emit(
        "task:update",
        {"task_id": task_id},
        project_id=project_id
    )

    return time_spent.serialize()