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()
def test_get_time_spents(self): person_id = self.person.id user_id = self.user["id"] task_id = self.task.id ts1 = TimeSpent.create( person_id=person_id, task_id=task_id, date=datetime.date(2017, 9, 23), duration=3600 ) ts2 = TimeSpent.create( person_id=user_id, task_id=task_id, date=datetime.date(2017, 9, 23), duration=7200 ) ts3 = TimeSpent.create( person_id=user_id, task_id=task_id, date=datetime.date(2017, 9, 24), duration=7200 ) time_spents = self.get( "/actions/tasks/%s/time-spents/" % task_id ) self.assertEqual( time_spents["total"], sum([ts['duration'] for ts in [ts1, ts2, ts3]])) self.assertEqual(len(time_spents[str(user_id)]), 1) self.assertEqual(len(time_spents[str(person_id)]), 2)
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 test_remove_task_force(self): tasks_service.create_comment(self.task.id, self.task_status.id, self.person.id, "first comment") TimeSpent.create(person_id=self.person.id, task_id=self.task.id, date=datetime.date(2017, 9, 23), duration=3600) deletion_service.remove_task(self.task_id, force=True) self.assertRaises(TaskNotFoundException, tasks_service.get_task, self.task_id)
def remove_person(person_id, force=True): person = Person.get(person_id) if force: for comment in Comment.get_all_by(person_id=person_id): remove_comment(comment.id) comments = Comment.query.filter( Comment.acknowledgements.contains(person) ) for comment in comments: comment.acknowledgements = [ member for member in comment.acknowledgements if str(member.id) != person_id ] comment.save() ApiEvent.delete_all_by(user_id=person_id) Notification.delete_all_by(person_id=person_id) SearchFilter.delete_all_by(person_id=person_id) DesktopLoginLog.delete_all_by(person_id=person_id) LoginLog.delete_all_by(person_id=person_id) Subscription.delete_all_by(person_id=person_id) TimeSpent.delete_all_by(person_id=person_id) for project in Project.query.filter(Project.team.contains(person)): project.team = [ member for member in project.team if str(member.id) != person_id ] project.save() for task in Task.query.filter(Task.assignees.contains(person)): task.assignees = [ assignee for assignee in task.assignees if str(assignee.id) != person_id ] task.save() for task in Task.get_all_by(assigner_id=person_id): task.update({"assigner_id": None}) for output_file in OutputFile.get_all_by(person_id=person_id): output_file.update({"person_id": None}) for working_file in WorkingFile.get_all_by(person_id=person_id): output_file.update({"person_id": None}) for task in WorkingFile.get_all_by(person_id=person_id): output_file.update({"person_id": None}) try: person.delete() events.emit("person:delete", {"person_id": person.id}) except IntegrityError: raise ModelWithRelationsDeletionException( "Some data are still linked to given person." ) return person.serialize_safe()
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 test_get_time_spents(self): person_id = self.person.id user_id = self.user.id task_id = self.task.id TimeSpent.create(person_id=person_id, task_id=task_id, date=datetime.date(2017, 9, 23), duration=3600) TimeSpent.create(person_id=user_id, task_id=task_id, date=datetime.date(2017, 9, 23), duration=7200) time_spents = self.get("/actions/tasks/%s/time-spents/2017-09-23/" % task_id) self.assertEquals(time_spents["total"], 10800) self.assertEquals(time_spents[str(user_id)]["duration"], 7200) self.assertEquals(time_spents[str(person_id)]["duration"], 3600)
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): 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()
def get_time_spents_for_entity(entity_id): """ Return all time spents related to given entity. """ query = ( TimeSpent.query.join(Task) .filter(Task.entity_id == entity_id) .order_by(TimeSpent.date.desc()) ) return TimeSpent.serialize_list(query.all())
def post_creation(self, instance): time_spents = TimeSpent.delete_all_by( person_id=instance.person_id, date=instance.date ) return instance.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()