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()
def reset_task_data(task_id): clear_task_cache(task_id) task = Task.get(task_id) retake_count = 0 real_start_date = None last_comment_date = None end_date = None task_status_id = TaskStatus.get_by(short_name="todo").id comments = ( Comment.query.join(TaskStatus) .filter(Comment.object_id == task_id) .order_by(Comment.created_at) .add_columns( TaskStatus.is_retake, TaskStatus.is_done, TaskStatus.short_name ) .all() ) previous_is_retake = False for ( comment, task_status_is_retake, task_status_is_done, task_status_short_name, ) in comments: if task_status_is_retake and not previous_is_retake: retake_count += 1 previous_is_retake = task_status_is_retake if task_status_short_name.lower() == "wip" and real_start_date is None: real_start_date = comment.created_at if task_status_is_done: end_date = comment.created_at else: end_date = None task_status_id = comment.task_status_id last_comment_date = comment.created_at duration = 0 time_spents = TimeSpent.get_all_by(task_id=task.id) for time_spent in time_spents: duration += time_spent.duration task.update( { "duration": duration, "retake_count": retake_count, "real_start_date": real_start_date, "last_comment_date": last_comment_date, "end_date": end_date, "task_status_id": task_status_id, } ) project_id = str(task.project_id) events.emit("task:update", {"task_id": task.id}, project_id) return task.serialize()
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()
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()