Esempio n. 1
0
def get_or_create_task_type(
    department,
    name,
    color="#888888",
    priority=1,
    for_shots=False,
    for_entity="Asset",
    short_name="",
    shotgun_id=None
):
    """
    Create a new task type if it doesn't exist. If it exists, it returns the
    type from database.
    """
    task_type = TaskType.get_by(name=name)
    if task_type is None:
        task_type = TaskType(
            name=name,
            short_name=short_name,
            department_id=department["id"],
            color=color,
            priority=priority,
            for_shots=for_shots,
            shotgun_id=shotgun_id
        )
        task_type.save()
    return task_type.serialize()
Esempio n. 2
0
    def save_task_type(self, department, data):
        task_type = TaskType.get_by(shotgun_id=data["shotgun_id"])
        data["department_id"] = department.id
        if task_type is None:
            task_type = TaskType.get_by(name=data["name"],
                                        for_entity=data["for_entity"])

        if task_type is None:
            task_type = TaskType(**data)
            task_type.save()
            current_app.logger.info("Task Type created: %s" % task_type)
        else:
            existing_task_type = TaskType.get_by(
                name=data["name"],
                for_entity=data["for_entity"],
                department_id=data["department_id"],
            )
            if existing_task_type is not None:
                data.pop("name", None)
                data.pop("for_entity", None)
                data.pop("department_id", None)
            task_type.update(data)
            tasks_service.clear_task_type_cache(str(task_type.id))
            current_app.logger.info("Task Type updated: %s" % task_type)
        return task_type
Esempio n. 3
0
def get_or_create_task_type(departmemt, name, color="#888888"):
    task_type = TaskType.get_by(name=name)
    if task_type is None:
        task_type = TaskType(
            name=name,
            department_id=departmemt.id,
            color=color
        )
        task_type.save()
    return task_type
Esempio n. 4
0
def get_or_create_task_type(department,
                            name,
                            color="#888888",
                            priority=1,
                            for_shots=False):
    task_type = TaskType.get_by(name=name)
    if task_type is None:
        task_type = TaskType(name=name,
                             department_id=department["id"],
                             color=color,
                             priority=priority,
                             for_shots=for_shots)
        task_type.save()
    return task_type.serialize()
Esempio n. 5
0
    def save_task_type(self, department, data):
        task_type = TaskType.get_by(shotgun_id=data["shotgun_id"])
        data["department_id"] = department.id

        if task_type is None:
            task_type = TaskType.get_by(name=data["name"])

        if task_type is None:
            task_type = TaskType(**data)
            task_type.save()
            current_app.logger.info("Task Type created: %s" % task_type)
        else:
            task_type.update(data)
            current_app.logger.info("Task Type updated: %s" % task_type)

        return task_type
Esempio n. 6
0
class TaskServiceTestCase(ApiDBTestCase):
    def setUp(self):
        super(TaskServiceTestCase, self).setUp()

        self.generate_fixture_project_status()
        self.generate_fixture_project()
        self.generate_fixture_asset_type()
        self.generate_fixture_asset()
        self.generate_fixture_sequence()
        self.generate_fixture_shot()
        self.generate_fixture_department()
        self.generate_fixture_task_type()
        self.generate_fixture_task_status()
        self.generate_fixture_task_status_wip()
        self.generate_fixture_task_status_to_review()
        self.generate_fixture_person()
        self.generate_fixture_assigner()
        self.generate_fixture_task()
        self.generate_fixture_shot_task()
        self.generate_fixture_file_status()
        self.generate_fixture_software()
        self.generate_fixture_working_file()
        self.generate_fixture_output_type()
        self.generate_fixture_output_file()

        self.task_id = self.task.id
        self.open_status_id = self.task_status.id
        self.wip_status_id = self.task_status_wip.id
        self.to_review_status_id = self.task_status_to_review.id

        self.is_event_fired = False
        events.unregister_all()

    def handle_event(self, data):
        self.is_event_fired = True
        self.assertEqual(data["previous_task_status_id"],
                         str(self.open_status_id))

    def assert_event_is_fired(self):
        self.assertTrue(self.is_event_fired)

    def test_get_status(self):
        task_status = tasks_service.get_or_create_status("WIP", "wip")
        self.assertEqual(task_status["name"], "WIP")

    def test_get_wip_status(self):
        task_status = tasks_service.get_wip_status()
        self.assertEqual(task_status["name"], "WIP")

    def test_get_done_status(self):
        task_status = tasks_service.get_done_status()
        self.assertEqual(task_status["name"], "Done")

    def test_get_todo_status(self):
        task_status = tasks_service.get_todo_status()
        self.assertEqual(task_status["name"], "Todo")

    def test_get_to_review_status(self):
        task_status = tasks_service.get_to_review_status()
        self.assertEqual(task_status["name"], "To review")

    def test_create_task(self):
        shot = self.shot.serialize()
        task_type = self.task_type.serialize()
        status = tasks_service.get_todo_status()
        task = tasks_service.create_task(task_type, shot)
        task = tasks_service.get_task(task["id"])
        self.assertEquals(task["entity_id"], shot["id"])
        self.assertEquals(task["task_type_id"], task_type["id"])
        self.assertEquals(task["project_id"], shot["project_id"])
        self.assertEquals(task["task_status_id"], status["id"])

    def test_status_to_wip(self):
        events.register("task:start", "mark_event_as_fired", self)

        now = datetime.datetime.now()
        self.task.update({"real_start_date": None})
        tasks_service.start_task(self.task.id)

        task = Task.get(self.task.id)
        self.assertEqual(task.task_status_id, self.wip_status_id)
        self.assertGreater(task.real_start_date.isoformat(), now.isoformat())
        self.assert_event_is_fired()

    def test_status_to_wip_twice(self):
        tasks_service.start_task(self.task.id)
        task = Task.get(self.task.id)
        real_start_date = task.real_start_date
        task.update({"task_status_id": self.task_status.id})

        tasks_service.start_task(self.task.id)
        task = Task.get(self.task.id)
        self.assertEqual(task.real_start_date, real_start_date)

    def test_publish_task(self):
        handler = ToReviewHandler(self.open_status_id,
                                  self.to_review_status_id)
        events.register("task:to-review", "mark_event_as_fired", handler)
        tasks_service.task_to_review(self.task.id, self.person.serialize(),
                                     "my comment")
        self.is_event_fired = handler.is_event_fired
        data = handler.data

        task = Task.get(self.task.id)
        self.assertEqual(task.task_status_id, self.to_review_status_id)
        self.assert_event_is_fired()

        self.assertEquals(data["previous_task_status_id"],
                          str(self.open_status_id))

        self.assertEquals(data["comment"], "my comment")

    def test_assign_task(self):
        tasks_service.assign_task(self.task.id, self.assigner.id)
        self.assertEqual(self.task.assignees[1].id, self.assigner.id)

    def test_get_department_from_task(self):
        department = tasks_service.get_department_from_task(self.task.id)
        self.assertEqual(department["name"], "Modeling")

    def test_get_task(self):
        self.assertRaises(TaskNotFoundException, tasks_service.get_task,
                          "wrong-id")
        task = tasks_service.get_task(self.task_id)
        self.assertEqual(str(self.task_id), task["id"])
        self.output_file.delete()
        self.working_file.delete()
        tasks_service.remove_task(task["id"])

        self.assertRaises(TaskNotFoundException, tasks_service.get_task,
                          self.task_id)

    def test_get_tasks_for_sequence(self):
        self.generate_fixture_sequence_task()
        tasks = tasks_service.get_tasks_for_sequence(self.sequence.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.sequence_task.id))

    def test_get_tasks_for_shot(self):
        tasks = tasks_service.get_tasks_for_shot(self.shot.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.shot_task.id))

    def test_get_tasks_for_scene(self):
        self.generate_fixture_scene()
        self.generate_fixture_scene_task()
        tasks = tasks_service.get_tasks_for_scene(self.scene.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.scene_task.id))

    def test_get_dict_tasks_for_asset(self):
        tasks = tasks_service.get_task_dicts_for_entity(self.asset.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.task.id))
        self.assertEqual(tasks[0]["task_type_name"], str("Shaders"))
        self.assertEqual(tasks[0]["entity_name"], str("Tree"))

    def test_get_task_types_for_shot(self):
        task_types = tasks_service.get_task_types_for_shot(self.shot.id)
        self.assertEqual(len(task_types), 1)
        self.assertEqual(task_types[0]["id"], str(self.task_type_animation.id))

    def test_get_task_types_for_scene(self):
        self.generate_fixture_scene()
        self.generate_fixture_scene_task()
        task_types = tasks_service.get_task_types_for_scene(self.scene.id)
        self.assertEqual(len(task_types), 1)
        self.assertEqual(task_types[0]["id"], str(self.task_type_animation.id))

    def test_get_task_types_for_sequence(self):
        self.generate_fixture_sequence_task()
        task_types = tasks_service.get_task_types_for_sequence(
            self.sequence.id)
        self.assertEqual(len(task_types), 1)
        self.assertEqual(task_types[0]["id"], str(self.task_type_animation.id))

    def test_get_task_types_for_entity(self):
        task_types = tasks_service.get_task_types_for_entity(self.asset.id)
        self.assertEqual(len(task_types), 1)
        self.assertEqual(task_types[0]["id"], str(self.task_type.id))

    def test_get_task_dicts_for_entity_utf8(self):
        start_date = fields.get_date_object("2017-02-20")
        due_date = fields.get_date_object("2017-02-28")
        real_start_date = fields.get_date_object("2017-02-22")
        self.working_file.delete()
        self.output_file.delete()
        self.task.delete()
        self.task_type = TaskType(name="Modélisation",
                                  color="#FFFFFF",
                                  department_id=self.department.id)
        self.task_type.save()
        self.task = Task(name="Première Tâche",
                         project_id=self.project.id,
                         task_type_id=self.task_type.id,
                         task_status_id=self.task_status.id,
                         entity_id=self.asset.id,
                         assignees=[self.person],
                         assigner_id=self.assigner.id,
                         duration=50,
                         estimation=40,
                         start_date=start_date,
                         due_date=due_date,
                         real_start_date=real_start_date)
        self.task.save()

        tasks = tasks_service.get_task_dicts_for_entity(self.asset.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["name"], u"Première Tâche")
        self.assertEqual(tasks[0]["task_type_name"], u"Modélisation")

    def test_get_or_create_time_spents(self):
        person_id = self.person.id
        task_id = self.task.id

        duration = 3600
        time_spent = tasks_service.create_or_update_time_spent(
            person_id=person_id,
            task_id=task_id,
            date="2017-09-23",
            duration=duration)
        self.assertEquals(time_spent["duration"], duration)

        duration = 7200
        time_spent = tasks_service.create_or_update_time_spent(
            person_id=person_id,
            task_id=task_id,
            date="2017-09-23",
            duration=duration)
        self.assertEquals(time_spent["duration"], duration)

        duration = 7200
        time_spent = tasks_service.create_or_update_time_spent(
            person_id=person_id,
            task_id=task_id,
            date="2017-09-23",
            duration=duration,
            add=True)
        self.assertEquals(time_spent["duration"], 2 * duration)

    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 test_clear_assignation(self):
        task_id = self.task.id
        tasks_service.assign_task(self.task.id, self.person.id)
        tasks_service.clear_assignation(task_id)
        task = tasks_service.get_task(task_id)
        self.assertEquals(len(task["assignees"]), 0)

    def test_get_tasks_for_person(self):
        projects = [self.project.serialize()]
        tasks = tasks_service.get_person_tasks(self.user.id, projects)
        self.assertEqual(len(tasks), 0)

        tasks_service.assign_task(self.task.id, self.user.id)
        tasks = tasks_service.get_person_tasks(self.user.id, projects)
        self.assertEqual(len(tasks), 1)

        tasks_service.create_comment(self.task.id, self.task_status.id,
                                     self.person.id, "first comment")
        tasks_service.create_comment(self.task.id, self.task_status.id,
                                     self.person.id, "last comment")
        tasks = tasks_service.get_person_tasks(self.person.id, projects)
        self.assertEqual(len(tasks), 2)
        self.assertEqual(tasks[1]["last_comment"]["text"], "last comment")
        self.assertEqual(tasks[1]["last_comment"]["person_id"],
                         str(self.person.id))

    def test_get_done_tasks_for_person(self):
        projects = [self.project.serialize()]
        tasks = tasks_service.get_person_done_tasks(self.user.id, projects)
        self.assertEqual(len(tasks), 0)

        tasks_service.assign_task(self.task.id, self.user.id)
        tasks = tasks_service.get_person_done_tasks(self.user.id, projects)
        self.assertEqual(len(tasks), 0)

        done_status = tasks_service.get_done_status()
        tasks_service.update_task(self.task.id,
                                  {"task_status_id": done_status["id"]})
        tasks = tasks_service.get_person_done_tasks(self.user.id, projects)
        self.assertEqual(len(tasks), 1)

    def test_update_task(self):
        done_status = tasks_service.get_done_status()
        tasks_service.update_task(self.task.id,
                                  {"task_status_id": done_status["id"]})
        self.assertEquals(str(self.task.task_status_id), done_status["id"])
        self.assertIsNotNone(self.task.end_date)
        self.assertLess(self.task.end_date, datetime.datetime.now())

    def test_remove_task(self):
        self.working_file.delete()
        self.output_file.delete()
        tasks_service.remove_task(self.task_id)
        self.assertRaises(TaskNotFoundException, tasks_service.get_task,
                          self.task_id)

    def test_remove_task_force(self):
        tasks_service.create_comment(self.task.id, self.task_status.id,
                                     self.person.id, "first comment")
        tasks_service.remove_task(self.task_id, force=True)
        self.assertRaises(TaskNotFoundException, tasks_service.get_task,
                          self.task_id)
Esempio n. 7
0
class TaskServiceTestCase(ApiDBTestCase):
    def setUp(self):
        super(TaskServiceTestCase, self).setUp()

        self.generate_fixture_project_status()
        self.generate_fixture_project()
        self.generate_fixture_asset_type()
        self.generate_fixture_asset()
        self.generate_fixture_sequence()
        self.generate_fixture_shot()
        self.generate_fixture_department()
        self.generate_fixture_task_type()
        self.generate_fixture_task_status()
        self.generate_fixture_task_status_wip()
        self.generate_fixture_task_status_to_review()
        self.generate_fixture_person()
        self.generate_fixture_assigner()
        self.generate_fixture_task()
        self.generate_fixture_shot_task()
        self.generate_fixture_file_status()
        self.generate_fixture_software()
        self.generate_fixture_working_file()
        self.generate_fixture_output_type()
        self.generate_fixture_output_file()

        self.project_id = str(self.project.id)
        self.task_id = str(self.task.id)
        self.open_status_id = self.task_status.id
        self.wip_status_id = self.task_status_wip.id
        self.to_review_status_id = self.task_status_to_review.id

        self.is_event_fired = False
        events.unregister_all()

    def handle_event(self, data):
        self.is_event_fired = True
        self.assertEqual(
            data["previous_task_status_id"], str(self.open_status_id)
        )

    def assert_event_is_fired(self):
        self.assertTrue(self.is_event_fired)

    def test_get_status(self):
        task_status = tasks_service.get_or_create_status("WIP", "wip")
        self.assertEqual(task_status["name"], "WIP")

    def test_get_wip_status(self):
        task_status = tasks_service.get_wip_status()
        self.assertEqual(task_status["name"], "WIP")

    def test_get_done_status(self):
        task_status = tasks_service.get_done_status()
        self.assertEqual(task_status["name"], "Done")

    def test_get_todo_status(self):
        task_status = tasks_service.get_todo_status()
        self.assertEqual(task_status["name"], "Todo")

    def test_get_to_review_status(self):
        task_status = tasks_service.get_to_review_status()
        self.assertEqual(task_status["name"], "To review")

    def test_create_task(self):
        shot = self.shot.serialize()
        task_type = self.task_type.serialize()
        status = tasks_service.get_todo_status()
        task = tasks_service.create_task(task_type, shot)
        task = tasks_service.get_task(task["id"])
        self.assertEqual(task["entity_id"], shot["id"])
        self.assertEqual(task["task_type_id"], task_type["id"])
        self.assertEqual(task["project_id"], shot["project_id"])
        self.assertEqual(task["task_status_id"], status["id"])

    def test_create_tasks(self):
        shot = self.shot.serialize()
        shot_2 = self.generate_fixture_shot("S02").serialize()
        task_type = self.task_type.serialize()
        status = tasks_service.get_todo_status()
        tasks = tasks_service.create_tasks(task_type, [shot, shot_2])
        self.assertEqual(len(tasks), 2)
        task = tasks[0]
        task = tasks_service.get_task(task["id"])
        self.assertEqual(task["entity_id"], shot["id"])
        self.assertEqual(task["task_type_id"], task_type["id"])
        self.assertEqual(task["project_id"], shot["project_id"])
        self.assertEqual(task["task_status_id"], status["id"])

    def test_status_to_wip(self):
        events.register("task:start", "mark_event_as_fired", self)

        now = datetime.datetime.now()
        self.task.update({"real_start_date": None})
        tasks_service.start_task(self.task.id)

        task = Task.get(self.task.id)
        self.assertEqual(task.task_status_id, self.wip_status_id)
        self.assertGreater(task.real_start_date.isoformat(), now.isoformat())
        self.assert_event_is_fired()

    def test_status_to_wip_twice(self):
        tasks_service.start_task(self.task.id)
        task = Task.get(self.task.id)
        real_start_date = task.real_start_date
        task.update({"task_status_id": self.task_status.id})

        tasks_service.start_task(self.task.id)
        task = Task.get(self.task.id)
        self.assertEqual(task.real_start_date, real_start_date)

    def test_publish_task(self):
        handler = ToReviewHandler(
            self.open_status_id, self.to_review_status_id
        )
        events.register("task:to-review", "mark_event_as_fired", handler)
        tasks_service.task_to_review(
            self.task.id, self.person.serialize(), "my comment"
        )
        self.is_event_fired = handler.is_event_fired
        data = handler.data

        task = Task.get(self.task.id)
        self.assertEqual(task.task_status_id, self.to_review_status_id)
        self.assert_event_is_fired()

        self.assertEqual(
            data["previous_task_status_id"], str(self.open_status_id)
        )

        self.assertEqual(data["comment"], "my comment")

    def test_assign_task(self):
        tasks_service.assign_task(self.task.id, self.assigner.id)
        self.assertEqual(self.task.assignees[1].id, self.assigner.id)

    def test_get_department_from_task(self):
        department = tasks_service.get_department_from_task(self.task.id)
        self.assertEqual(department["name"], "Modeling")

    def test_get_task(self):
        self.assertRaises(
            TaskNotFoundException, tasks_service.get_task, "wrong-id"
        )
        task = tasks_service.get_task(self.task_id)
        self.assertEqual(str(self.task_id), task["id"])
        self.output_file.delete()
        self.working_file.delete()
        deletion_service.remove_task(task["id"])

        self.assertRaises(
            TaskNotFoundException, tasks_service.get_task, str(self.task_id)
        )

    def test_get_tasks_for_sequence(self):
        self.generate_fixture_sequence_task()
        tasks = tasks_service.get_tasks_for_sequence(self.sequence.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.sequence_task.id))

    def test_get_tasks_for_shot(self):
        tasks = tasks_service.get_tasks_for_shot(self.shot.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.shot_task.id))

    def test_get_tasks_for_scene(self):
        self.generate_fixture_scene()
        self.generate_fixture_scene_task()
        tasks = tasks_service.get_tasks_for_scene(self.scene.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.scene_task.id))

    def test_get_dict_tasks_for_asset(self):
        tasks = tasks_service.get_task_dicts_for_entity(self.asset.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.task.id))
        self.assertEqual(tasks[0]["task_type_name"], str("Shaders"))
        self.assertEqual(tasks[0]["entity_name"], str("Tree"))

    def test_get_task_types_for_shot(self):
        task_types = tasks_service.get_task_types_for_shot(self.shot.id)
        self.assertEqual(len(task_types), 1)
        self.assertEqual(task_types[0]["id"], str(self.task_type_animation.id))

    def test_get_task_types_for_scene(self):
        self.generate_fixture_scene()
        self.generate_fixture_scene_task()
        task_types = tasks_service.get_task_types_for_scene(self.scene.id)
        self.assertEqual(len(task_types), 1)
        self.assertEqual(task_types[0]["id"], str(self.task_type_animation.id))

    def test_get_task_types_for_sequence(self):
        self.generate_fixture_sequence_task()
        task_types = tasks_service.get_task_types_for_sequence(
            self.sequence.id
        )
        self.assertEqual(len(task_types), 1)
        self.assertEqual(task_types[0]["id"], str(self.task_type_animation.id))

    def test_get_task_types_for_entity(self):
        task_types = tasks_service.get_task_types_for_entity(self.asset.id)
        self.assertEqual(len(task_types), 1)
        self.assertEqual(task_types[0]["id"], str(self.task_type.id))

    def test_get_task_dicts_for_entity_utf8(self):
        start_date = fields.get_date_object("2017-02-20")
        due_date = fields.get_date_object("2017-02-28")
        real_start_date = fields.get_date_object("2017-02-22")
        self.working_file.delete()
        self.output_file.delete()
        self.task.delete()
        self.task_type = TaskType(
            name="Modélisation",
            color="#FFFFFF",
            department_id=self.department.id,
        )
        self.task_type.save()
        self.task = Task(
            name="Première Tâche",
            project_id=self.project.id,
            task_type_id=self.task_type.id,
            task_status_id=self.task_status.id,
            entity_id=self.asset.id,
            assignees=[self.person],
            assigner_id=self.assigner.id,
            duration=50,
            estimation=40,
            start_date=start_date,
            due_date=due_date,
            real_start_date=real_start_date,
        )
        self.task.save()

        tasks = tasks_service.get_task_dicts_for_entity(self.asset.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["name"], u"Première Tâche")
        self.assertEqual(tasks[0]["task_type_name"], u"Modélisation")

    def test_get_or_create_time_spents(self):
        person_id = self.person.id
        task_id = self.task.id

        duration = 3600
        time_spent = tasks_service.create_or_update_time_spent(
            person_id=person_id,
            task_id=task_id,
            date="2017-09-23",
            duration=duration,
        )
        self.assertEqual(time_spent["duration"], duration)

        duration = 7200
        time_spent = tasks_service.create_or_update_time_spent(
            person_id=person_id,
            task_id=task_id,
            date="2017-09-23",
            duration=duration,
        )
        self.assertEqual(time_spent["duration"], duration)

        duration = 7200
        time_spent = tasks_service.create_or_update_time_spent(
            person_id=person_id,
            task_id=task_id,
            date="2017-09-23",
            duration=duration,
            add=True,
        )
        self.assertEqual(time_spent["duration"], 2 * duration)

    def test_get_time_spents(self):
        pass
        """
        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(
            "/data/time-spents?task_id=%s" % 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 test_clear_assignation(self):
        task_id = self.task.id
        tasks_service.assign_task(self.task.id, self.person.id)
        tasks_service.clear_assignation(task_id)
        task = tasks_service.get_task_with_relations(task_id)
        self.assertEqual(len(task["assignees"]), 0)

    def test_get_tasks_for_person(self):
        projects = [self.project.serialize()]
        tasks = tasks_service.get_person_tasks(self.user["id"], projects)
        self.assertEqual(len(tasks), 0)

        tasks_service.assign_task(self.task.id, self.user["id"])
        tasks = tasks_service.get_person_tasks(self.user["id"], projects)
        self.assertEqual(len(tasks), 1)

        comments_service.new_comment(
            self.task.id, self.task_status.id, self.person.id, "first comment"
        )
        comments_service.new_comment(
            self.task.id, self.task_status.id, self.person.id, "last comment"
        )
        tasks = tasks_service.get_person_tasks(self.person.id, projects)
        self.assertEqual(len(tasks), 2)
        self.assertEqual(tasks[1]["last_comment"]["text"], "last comment")
        self.assertEqual(
            tasks[1]["last_comment"]["person_id"], str(self.person.id)
        )

    def test_get_done_tasks_for_person(self):
        projects = [self.project.serialize()]
        tasks = tasks_service.get_person_done_tasks(self.user["id"], projects)
        self.assertEqual(len(tasks), 0)

        tasks_service.assign_task(self.task.id, self.user["id"])
        tasks = tasks_service.get_person_done_tasks(self.user["id"], projects)
        self.assertEqual(len(tasks), 0)

        done_status = tasks_service.get_done_status()
        tasks_service.update_task(
            self.task.id, {"task_status_id": done_status["id"]}
        )
        tasks = tasks_service.get_person_done_tasks(self.user["id"], projects)
        self.assertEqual(len(tasks), 1)

    def test_update_task(self):
        done_status = tasks_service.get_done_status()
        tasks_service.update_task(
            self.task.id, {"task_status_id": done_status["id"]}
        )
        self.assertEqual(str(self.task.task_status_id), done_status["id"])
        self.assertIsNotNone(self.task.end_date)
        self.assertLess(self.task.end_date, datetime.datetime.now())

    def test_remove_task(self):
        self.working_file.delete()
        self.output_file.delete()
        deletion_service.remove_task(self.task_id)
        self.assertRaises(
            TaskNotFoundException, tasks_service.get_task, self.task_id
        )

    def test_remove_tasks(self):
        self.working_file.delete()
        self.output_file.delete()
        task_id = str(self.task_id)
        task2_id = str(self.generate_fixture_task("main 2").id)
        task_ids = [task_id, task2_id]
        deletion_service.remove_tasks(self.project_id, task_ids)
        self.assertRaises(
            TaskNotFoundException, tasks_service.get_task, task_id
        )
        self.assertRaises(
            TaskNotFoundException, tasks_service.get_task, task2_id
        )

    def test_remove_task_force(self):
        comments_service.new_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 test_delete_all_task_types(self):
        self.generate_fixture_project_standard()
        self.generate_fixture_asset_standard()
        task_1_id = str(self.task.id)
        task_2_id = str(self.generate_fixture_task(name="second task").id)
        task_3_id = str(self.shot_task.id)
        task_4_id = str(self.generate_fixture_task_standard().id)
        deletion_service.remove_tasks_for_project_and_task_type(
            self.project.id, self.task_type.id
        )
        self.assertIsNone(Task.get(task_1_id))
        self.assertIsNone(Task.get(task_2_id))
        self.assertIsNotNone(Task.get(task_3_id))
        self.assertIsNotNone(Task.get(task_4_id))

    def test_get_comment_mentions(self):
        mentions = comments_service.get_comment_mentions(
            self.task_id, "Test @Emma Doe"
        )
        self.assertEqual(len(mentions), 0)
        mentions = comments_service.get_comment_mentions(
            self.task_id, "Test @John Doe"
        )
        self.assertEqual(mentions[0], self.person)

    def test_get_comments(self):
        self.generate_fixture_user_client()
        self.generate_fixture_comment()
        self.generate_fixture_comment()
        self.generate_fixture_comment(person=self.user_client)
        self.generate_fixture_comment()
        comments = tasks_service.get_comments(self.task_id, is_manager=True)
        self.assertEqual(len(comments), 4)
        comments = tasks_service.get_comments(self.task_id, is_manager=False)
        self.assertEqual(len(comments), 3)
        comments = tasks_service.get_comments(self.task_id, is_client=True)
        self.assertEqual(len(comments), 1)

    def test_new_comment(self):
        comment = comments_service.new_comment(
            self.task_id, self.task_status.id, self.person.id, "Test @John Doe"
        )
        self.assertEqual(comment["mentions"][0], str(self.person.id))

    def test_get_full_task(self):
        task = tasks_service.get_full_task(self.task.id)
        self.assertEqual(task["project"]["name"], self.project.name)
        self.assertEqual(task["assigner"]["id"], str(self.assigner.id))
        self.assertEqual(task["persons"][0]["id"], str(self.person.id))
        self.assertEqual(task["task_status"]["id"], str(self.task_status.id))
        self.assertEqual(task["task_type"]["id"], str(self.task_type.id))

        task = tasks_service.get_full_task(self.shot_task.id)
        self.assertEqual(task["sequence"]["id"], str(self.sequence.id))

    def test_get_next_position(self):
        self.generate_fixture_preview_file(revision=1)
        self.generate_fixture_preview_file(revision=2)
        self.generate_fixture_preview_file(revision=2, name="second")
        task_id = self.task.id
        position = tasks_service.get_next_position(task_id, 2)
        self.assertEqual(position, 3)

    def test_update_preview_file_position(self):
        self.generate_fixture_preview_file(revision=1)
        self.generate_fixture_preview_file(revision=2)
        preview_file = self.generate_fixture_preview_file(
            revision=2, name="second"
        )
        preview_file_id = str(preview_file.id)
        self.generate_fixture_preview_file(revision=2, name="third")

        preview_files_service.update_preview_file_position(preview_file_id, 1)
        preview_files = PreviewFile.query.filter_by(
            task_id=self.task_id, revision=2
        ).order_by(PreviewFile.position)
        for (i, preview_file) in enumerate(preview_files):
            self.assertEqual(preview_file.position, i + 1)
        self.assertEqual(str(preview_files[0].id), preview_file_id)

        preview_files_service.update_preview_file_position(preview_file_id, 3)
        preview_files = PreviewFile.query.filter_by(
            task_id=self.task_id, revision=2
        ).order_by(PreviewFile.position)
        for (i, preview_file) in enumerate(preview_files):
            self.assertEqual(preview_file.position, i + 1)
        self.assertEqual(str(preview_files[2].id), preview_file_id)
Esempio n. 8
0
class ApiDBTestCase(ApiTestCase):
    def setUp(self):
        """
        Reset database before each test.
        """
        super(ApiDBTestCase, self).setUp()

        from zou.app.utils import dbhelpers
        dbhelpers.drop_all()
        dbhelpers.create_all()
        self.log_in()

    def log_in(self):
        self.generate_fixture_user()
        self.post("auth/login", {
            "email": self.user.email,
            "password": "******"
        }, 200)

    def tearDown(self):
        """
        Delete database after each test.
        """
        from zou.app.utils import dbhelpers
        dbhelpers.drop_all()

    def generate_data(self, cls, number, **kwargs):
        """
        Generate random data for a given data model.
        """
        mixer.init_app(self.flask_app)
        return mixer.cycle(number).blend(cls, id=fields.gen_uuid, **kwargs)

    def generate_fixture_project_status(self):
        self.open_status = ProjectStatus(name="open", color="#FFFFFF")
        self.open_status.save()

    def generate_fixture_project_closed_status(self):
        self.closed_status = ProjectStatus(name="closed", color="#FFFFFF")
        self.closed_status.save()

    def generate_fixture_project(self):
        self.project = Project(name="Cosmos Landromat",
                               project_status_id=self.open_status.id)
        self.project.save()
        self.project.update(
            {"file_tree": file_tree.get_tree_from_file("simple")})

    def generate_fixture_project_closed(self):
        self.project_closed = Project(name="Old Project",
                                      project_status_id=self.closed_status.id)
        self.project_closed.save()

    def generate_fixture_project_standard(self):
        self.project_standard = Project(name="Big Buck Bunny",
                                        project_status_id=self.open_status.id)
        self.project_standard.save()
        self.project_standard.update(
            {"file_tree": file_tree.get_tree_from_file("standard")})

    def generate_fixture_entity(self):
        self.entity = Entity(name="Tree",
                             description="Description Tree",
                             project_id=self.project.id,
                             entity_type_id=self.entity_type.id)
        self.entity.save()

    def generate_fixture_entity_standard(self):
        self.entity_standard = Entity(name="Car",
                                      project_id=self.project_standard.id,
                                      entity_type_id=self.entity_type.id)
        self.entity_standard.save()

    def generate_fixture_sequence(self):
        if hasattr(self, "episode"):
            episode_id = self.episode.id
        else:
            episode_id = None

        self.sequence = Entity(name="S01",
                               project_id=self.project.id,
                               entity_type_id=self.sequence_type.id,
                               parent_id=episode_id)
        self.sequence.save()

    def generate_fixture_sequence_standard(self):
        self.sequence_standard = Entity(name="S01",
                                        project_id=self.project_standard.id,
                                        entity_type_id=self.sequence_type.id)
        self.sequence_standard.save()

    def generate_fixture_episode(self):
        self.episode = Entity(name="E01",
                              project_id=self.project.id,
                              entity_type_id=self.episode_type.id)
        self.episode.save()

    def generate_fixture_shot(self):
        self.shot = Entity(name="P01",
                           description="Description Shot 01",
                           data={
                               "fps": 25,
                               "frame_in": 0,
                               "frame_out": 100
                           },
                           project_id=self.project.id,
                           entity_type_id=self.shot_type.id,
                           parent_id=self.sequence.id)
        self.shot.save()
        self.shot_noseq = Entity(name="P01NOSEQ",
                                 project_id=self.project.id,
                                 entity_type_id=self.shot_type.id)
        self.shot_noseq.save()

    def generate_fixture_shot_standard(self):
        self.shot_standard = Entity(name="P01",
                                    description="Description Shot 01",
                                    data={
                                        "fps": 25,
                                        "frame_in": 0,
                                        "frame_out": 100
                                    },
                                    project_id=self.project_standard.id,
                                    entity_type_id=self.shot_type.id,
                                    parent_id=self.sequence_standard.id)
        self.shot_standard.save()

    def generate_fixture_user(self):
        self.user = Person(first_name="John",
                           last_name="Did",
                           email=u"*****@*****.**",
                           password=auth.encrypt_password("mypassword"))
        self.user.save()

    def generate_fixture_person(self):
        self.person = Person(first_name="John",
                             last_name="Doe",
                             email=u"*****@*****.**",
                             password=auth.encrypt_password("mypassword"))
        self.person.save()

    def generate_fixture_entity_type(self):
        self.entity_type = EntityType(name="Props")
        self.entity_type.save()
        self.shot_type = EntityType(name="Shot")
        self.shot_type.save()
        self.sequence_type = EntityType(name="Sequence")
        self.sequence_type.save()
        self.episode_type = EntityType(name="Episode")
        self.episode_type.save()

    def generate_fixture_department(self):
        self.department = Department(name="Modeling", color="#FFFFFF")
        self.department.save()
        self.department_animation = Department(name="Animation",
                                               color="#FFFFFF")
        self.department_animation.save()

    def generate_fixture_task_type(self):
        self.task_type = TaskType(name="Shaders",
                                  color="#FFFFFF",
                                  department_id=self.department.id)
        self.task_type.save()
        self.task_type_animation = TaskType(
            name="Animation",
            color="#FFFFFF",
            department_id=self.department_animation.id)
        self.task_type_animation.save()

    def generate_fixture_task_status(self):
        self.task_status = TaskStatus(name="Open",
                                      short_name="opn",
                                      color="#FFFFFF")
        self.task_status.save()

    def generate_fixture_task_status_wip(self):
        self.task_status_wip = TaskStatus(name="WIP",
                                          short_name="wip",
                                          color="#FFFFFF")
        self.task_status_wip.save()

    def generate_fixture_task_status_to_review(self):
        self.task_status_to_review = TaskStatus(name="To review",
                                                short_name="pndng",
                                                color="#FFFFFF")
        self.task_status_to_review.save()

    def generate_fixture_assigner(self):
        self.assigner = Person(first_name="Ema", last_name="Peel")
        self.assigner.save()

    def generate_fixture_task(self):
        start_date = fields.get_date_object("2017-02-20")
        due_date = fields.get_date_object("2017-02-28")
        real_start_date = fields.get_date_object("2017-02-22")
        self.task = Task(name="Super modeling",
                         project_id=self.project.id,
                         task_type_id=self.task_type.id,
                         task_status_id=self.task_status.id,
                         entity_id=self.entity.id,
                         assignees=[self.person],
                         assigner_id=self.assigner.id,
                         duration=50,
                         estimation=40,
                         start_date=start_date,
                         due_date=due_date,
                         real_start_date=real_start_date)
        self.task.save()

    def generate_fixture_task_standard(self):
        start_date = fields.get_date_object("2017-02-20")
        due_date = fields.get_date_object("2017-02-28")
        real_start_date = fields.get_date_object("2017-02-22")
        self.task_standard = Task(name="Super modeling",
                                  project_id=self.project_standard.id,
                                  task_type_id=self.task_type.id,
                                  task_status_id=self.task_status.id,
                                  entity_id=self.entity_standard.id,
                                  assignees=[self.person],
                                  assigner_id=self.assigner.id,
                                  duration=50,
                                  estimation=40,
                                  start_date=start_date,
                                  due_date=due_date,
                                  real_start_date=real_start_date)
        self.task_standard.save()

    def generate_fixture_shot_task(self):
        self.shot_task = Task(
            name="Super animation",
            project_id=self.project.id,
            task_type_id=self.task_type_animation.id,
            task_status_id=self.task_status.id,
            entity_id=self.shot.id,
            assignees=[self.person],
            assigner_id=self.assigner.id,
        )
        self.shot_task.save()

    def generate_fixture_shot_task_standard(self):
        self.shot_task_standard = Task(
            name="Super animation",
            project_id=self.project_standard.id,
            task_type_id=self.task_type_animation.id,
            task_status_id=self.task_status.id,
            entity_id=self.shot_standard.id,
            assignees=[self.person],
            assigner_id=self.assigner.id)
        self.shot_task_standard.save()

    def generate_fixture_file_status(self):
        self.file_status = FileStatus(name="To review", color="#FFFFFF")
        self.file_status.save()

    def generate_fixture_working_file(self):
        self.working_file = WorkingFile(name="S01_P01_modeling",
                                        comment="",
                                        revision=1,
                                        task_id=self.task.id,
                                        entity_id=self.entity.id,
                                        person_id=self.person.id)
        self.working_file.save()

    def generate_fixture_shot_working_file(self):
        self.working_file = WorkingFile(name="S01_P01_animation",
                                        comment="",
                                        revision=1,
                                        task_id=self.task.id,
                                        entity_id=self.shot.id,
                                        person_id=self.person.id)
        self.working_file.save()

    def generate_fixture_output_file(self):
        self.output_file = OutputFile(comment="",
                                      revision=1,
                                      task_id=self.task.id,
                                      entity_id=self.entity.id,
                                      person_id=self.person.id,
                                      file_status_id=self.file_status.id)
        self.output_file.save()

    def get_fixture_file_path(self, relative_path):
        current_path = os.getcwd()
        file_path_fixture = os.path.join(current_path, "test", "fixtures",
                                         relative_path)
        return file_path_fixture
Esempio n. 9
0
class TaskInfoTestCase(ApiDBTestCase):
    def setUp(self):
        super(TaskInfoTestCase, self).setUp()

        self.generate_fixture_project_status()
        self.generate_fixture_project()
        self.generate_fixture_entity_type()
        self.generate_fixture_entity()
        self.generate_fixture_sequence()
        self.generate_fixture_shot()
        self.generate_fixture_department()
        self.generate_fixture_task_type()
        self.generate_fixture_task_status()
        self.generate_fixture_task_status_wip()
        self.generate_fixture_task_status_to_review()
        self.generate_fixture_person()
        self.generate_fixture_assigner()
        self.generate_fixture_task()
        self.generate_fixture_shot_task()
        self.generate_fixture_file_status()
        self.generate_fixture_working_file()
        self.generate_fixture_output_file()

        self.task_id = self.task.id
        self.open_status_id = self.task_status.id
        self.wip_status_id = self.task_status_wip.id
        self.to_review_status_id = self.task_status_to_review.id

        self.is_event_fired = False
        events.unregister_all()

    def handle_event(self, data):
        self.is_event_fired = True
        self.assertEqual(data["task_before"]["task_status_id"],
                         str(self.open_status_id))
        self.assertEqual(data["task_after"]["task_status_id"],
                         str(self.wip_status_id))

    def assert_event_is_fired(self):
        self.assertTrue(self.is_event_fired)

    def test_get_status(self):
        task_status = task_info.get_status("WIP_TASK_STATUS", "wip")
        self.assertEqual(task_status.name, "WIP")

    def test_get_wip_status(self):
        task_status = task_info.get_wip_status()
        self.assertEqual(task_status.name, "WIP")

    def test_get_to_review_status(self):
        task_status = task_info.get_to_review_status()
        self.assertEqual(task_status.name, "To review")

    def test_status_to_wip(self):
        events.register("task:start", "mark_event_as_fired", self)

        now = datetime.datetime.now()
        self.task.update({"real_start_date": None})
        task_info.start_task(self.task)

        task = Task.get(self.task.id)
        self.assertEqual(task.task_status_id, self.wip_status_id)
        self.assertGreater(task.real_start_date.isoformat(), now.isoformat())
        self.assert_event_is_fired()

    def test_status_to_wip_twice(self):
        task_info.start_task(self.task)
        task = Task.get(self.task.id)
        real_start_date = task.real_start_date
        task.update({"task_status_id": self.task_status.id})

        task_info.start_task(self.task)
        task = Task.get(self.task.id)
        self.assertEqual(task.real_start_date, real_start_date)

    def test_publish_task(self):
        handler = ToReviewHandler(self.open_status_id,
                                  self.to_review_status_id)
        events.register("task:to-review", "mark_event_as_fired", handler)
        task_info.to_review_task(self.task, self.output_file.serialize())
        self.is_event_fired = handler.is_event_fired
        data = handler.data

        task = Task.get(self.task.id)
        self.assertEqual(task.task_status_id, self.to_review_status_id)
        self.assert_event_is_fired()

        self.assertEquals(data["task_before"]["task_status_id"],
                          str(self.open_status_id))

        self.assertEquals(data["task_after"]["task_status_id"],
                          str(self.to_review_status_id))

        self.assertEquals(data["task_after"]["project"]["id"],
                          str(self.project.id))

        self.assertEquals(data["task_after"]["entity"]["id"],
                          str(self.entity.id))

        self.assertEquals(data["task_after"]["entity_type"]["id"],
                          str(self.entity_type.id))

        self.assertEquals(data["task_after"]["person"]["id"],
                          str(self.person.id))

        self.assertTrue(data["task_after"]["output_file"]["id"],
                        str(self.output_file.id))

    def test_assign_task(self):
        task_info.assign_task(self.task, self.assigner)
        self.assertEqual(self.task.assignees[1].id, self.assigner.id)

    def test_get_department_from_task_type(self):
        department = task_info.get_department_from_task_type(self.task_type)
        self.assertEqual(department.name, "Modeling")

    def test_get_task(self):
        self.assertRaises(TaskNotFoundException, task_info.get_task,
                          "wrong-id")
        task = task_info.get_task(self.task_id)
        self.assertEqual(self.task_id, task.id)
        self.output_file.delete()
        self.working_file.delete()
        task.delete()

        self.assertRaises(TaskNotFoundException, task_info.get_task,
                          self.task_id)

    def test_get_task_dicts_for_shot(self):
        tasks = task_info.get_task_dicts_for_shot(self.shot.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.shot_task.id))

    def test_get_task_dicts_for_asset(self):
        tasks = task_info.get_task_dicts_for_asset(self.entity.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.task.id))

    def test_get_task_dicts_for_entity(self):
        tasks = task_info.get_task_dicts_for_entity(self.entity.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["id"], str(self.task.id))
        self.assertEqual(tasks[0]["task_type_name"], str("Shaders"))
        self.assertEqual(tasks[0]["entity_name"], str("Tree"))

    def test_get_task_dicts_for_entity_utf8(self):
        start_date = fields.get_date_object("2017-02-20")
        due_date = fields.get_date_object("2017-02-28")
        real_start_date = fields.get_date_object("2017-02-22")
        self.working_file.delete()
        self.output_file.delete()
        self.task.delete()
        self.task_type = TaskType(name="Modélisation",
                                  color="#FFFFFF",
                                  department_id=self.department.id)
        self.task_type.save()
        self.task = Task(name="Première Tâche",
                         project_id=self.project.id,
                         task_type_id=self.task_type.id,
                         task_status_id=self.task_status.id,
                         entity_id=self.entity.id,
                         assignees=[self.person],
                         assigner_id=self.assigner.id,
                         duration=50,
                         estimation=40,
                         start_date=start_date,
                         due_date=due_date,
                         real_start_date=real_start_date)
        self.task.save()

        tasks = task_info.get_task_dicts_for_entity(self.entity.id)
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0]["name"], u"Première Tâche")
        self.assertEqual(tasks[0]["task_type_name"], u"Modélisation")
Esempio n. 10
0
class ApiDBTestCase(ApiTestCase):
    def setUp(self):
        """
        Reset database before each test.
        """
        super(ApiDBTestCase, self).setUp()

        from zou.app.utils import dbhelpers
        dbhelpers.drop_all()
        dbhelpers.create_all()
        self.generate_fixture_user()
        self.log_in_admin()

    def tearDown(self):
        """
        Delete database after each test.
        """
        from zou.app.utils import dbhelpers
        dbhelpers.drop_all()

    def generate_data(self, cls, number, **kwargs):
        """
        Generate random data for a given data model.
        """
        mixer.init_app(self.flask_app)
        return mixer.cycle(number).blend(cls, id=fields.gen_uuid, **kwargs)

    def generate_fixture_project_status(self):
        self.open_status = ProjectStatus(name="open", color="#FFFFFF")
        self.open_status.save()

    def generate_fixture_project_closed_status(self):
        self.closed_status = ProjectStatus(name="closed", color="#FFFFFF")
        self.closed_status.save()

    def generate_fixture_project(self):
        self.project = Project(name="Cosmos Landromat",
                               project_status_id=self.open_status.id)
        self.project.save()
        self.project.update(
            {"file_tree": file_tree_service.get_tree_from_file("simple")})

    def generate_fixture_project_closed(self):
        self.project_closed = Project(name="Old Project",
                                      project_status_id=self.closed_status.id)
        self.project_closed.save()

    def generate_fixture_project_standard(self):
        self.project_standard = Project(name="Big Buck Bunny",
                                        project_status_id=self.open_status.id)
        self.project_standard.save()
        self.project_standard.update(
            {"file_tree": file_tree_service.get_tree_from_file("standard")})

    def generate_fixture_project_no_preview_tree(self):
        self.project_no_preview_tree = Project(
            name="Agent 327", project_status_id=self.open_status.id)
        self.project_no_preview_tree.save()
        self.project_no_preview_tree.update(
            {"file_tree": file_tree_service.get_tree_from_file("no_preview")})

    def generate_fixture_entity(self):
        self.entity = Entity(name="Tree",
                             description="Description Tree",
                             project_id=self.project.id,
                             entity_type_id=self.entity_type.id)
        self.entity.save()

    def generate_fixture_entity_character(self):
        self.entity_character = Entity(
            name="Rabbit",
            description="Main character",
            project_id=self.project.id,
            entity_type_id=self.entity_type_character.id)
        self.entity_character.save()

    def generate_fixture_entity_camera(self):
        self.entity_camera = Entity(name="Main camera",
                                    description="Description Camera",
                                    project_id=self.project.id,
                                    entity_type_id=self.entity_type_camera.id)
        self.entity_camera.save()

    def generate_fixture_entity_standard(self):
        self.entity_standard = Entity(name="Car",
                                      project_id=self.project_standard.id,
                                      entity_type_id=self.entity_type.id)
        self.entity_standard.save()

    def generate_fixture_sequence(self, name="S01"):
        if hasattr(self, "episode"):
            episode_id = self.episode.id
        else:
            episode_id = None

        self.sequence = Entity(name=name,
                               project_id=self.project.id,
                               entity_type_id=self.sequence_type.id,
                               parent_id=episode_id)
        self.sequence.save()

    def generate_fixture_sequence_standard(self):
        self.sequence_standard = Entity(name="S01",
                                        project_id=self.project_standard.id,
                                        entity_type_id=self.sequence_type.id)
        self.sequence_standard.save()

    def generate_fixture_episode(self, name="E01"):
        self.episode = Entity(name=name,
                              project_id=self.project.id,
                              entity_type_id=self.episode_type.id)
        self.episode.save()

    def generate_fixture_shot(self, name="P01"):
        self.shot = Entity(name=name,
                           description="Description Shot 01",
                           data={
                               "fps": 25,
                               "frame_in": 0,
                               "frame_out": 100
                           },
                           project_id=self.project.id,
                           entity_type_id=self.shot_type.id,
                           parent_id=self.sequence.id)
        self.shot.save()
        self.shot_noseq = Entity(name="P01NOSEQ",
                                 project_id=self.project.id,
                                 entity_type_id=self.shot_type.id)
        self.shot_noseq.save()

    def generate_fixture_scene(self,
                               name="SC01",
                               project_id=None,
                               sequence_id=None):
        if project_id is None:
            project_id = self.project.id

        if sequence_id is None:
            sequence_id = self.sequence.id

        self.scene = Entity(name=name,
                            description="Description Scene 01",
                            data={},
                            project_id=project_id,
                            entity_type_id=self.scene_type.id,
                            parent_id=self.sequence.id)
        self.scene.save()

    def generate_fixture_shot_standard(self):
        self.shot_standard = Entity(name="P01",
                                    description="Description Shot 01",
                                    data={
                                        "fps": 25,
                                        "frame_in": 0,
                                        "frame_out": 100
                                    },
                                    project_id=self.project_standard.id,
                                    entity_type_id=self.shot_type.id,
                                    parent_id=self.sequence_standard.id)
        self.shot_standard.save()

    def generate_fixture_shot_asset_instance(self,
                                             asset=None,
                                             shot=None,
                                             number=1):
        if asset is None:
            asset = self.entity
        if shot is None:
            shot = self.shot
        self.asset_instance = AssetInstance.create(
            asset_id=asset.id,
            entity_id=shot.id,
            entity_type_id=self.shot_type.id,
            number=number,
            description="Asset instance description")

    def generate_fixture_scene_asset_instance(self,
                                              asset=None,
                                              scene=None,
                                              number=1):
        if asset is None:
            asset = self.entity
        if scene is None:
            scene = self.scene
        self.asset_instance = AssetInstance.create(
            asset_id=asset.id,
            entity_id=scene.id,
            entity_type_id=self.scene_type.id,
            number=number,
            description="Asset instance description")

    def generate_fixture_user(self):
        self.user = Person.create(first_name="John",
                                  last_name="Did",
                                  role="admin",
                                  email=u"*****@*****.**",
                                  password=auth.encrypt_password("mypassword"))
        return self.user

    def generate_fixture_user_manager(self):
        self.user_manager = Person.create(
            first_name="John",
            last_name="Did2",
            role="manager",
            email=u"*****@*****.**",
            password=auth.encrypt_password("mypassword"))
        return self.user_manager

    def generate_fixture_user_cg_artist(self):
        self.user_cg_artist = Person.create(
            first_name="John",
            last_name="Did3",
            email=u"*****@*****.**",
            role="user",
            password=auth.encrypt_password("mypassword"))
        return self.user_cg_artist

    def generate_fixture_person(self):
        self.person = Person(first_name="John",
                             last_name="Doe",
                             desktop_login="******",
                             email=u"*****@*****.**",
                             password=auth.encrypt_password("mypassword"))
        self.person.save()

    def generate_fixture_entity_type(self):
        self.entity_type = EntityType.create(name="Props")
        self.shot_type = EntityType.create(name="Shot")
        self.sequence_type = EntityType.create(name="Sequence")
        self.episode_type = EntityType.create(name="Episode")
        self.scene_type = EntityType.create(name="Scene")

    def generate_fixture_asset_types(self):
        self.entity_type_character = EntityType.create(name="Character")
        self.entity_type_environment = EntityType.create(name="Environment")
        self.entity_type_camera = EntityType.create(name="Camera")

    def generate_fixture_department(self):
        self.department = Department(name="Modeling", color="#FFFFFF")
        self.department.save()
        self.department_animation = Department(name="Animation",
                                               color="#FFFFFF")
        self.department_animation.save()

    def generate_fixture_task_type(self):
        self.task_type = TaskType(name="Shaders",
                                  color="#FFFFFF",
                                  department_id=self.department.id)
        self.task_type.save()
        self.task_type_animation = TaskType(
            name="Animation",
            color="#FFFFFF",
            department_id=self.department_animation.id)
        self.task_type_animation.save()

    def generate_fixture_task_status(self):
        self.task_status = TaskStatus(name="Open",
                                      short_name="opn",
                                      color="#FFFFFF")
        self.task_status.save()

    def generate_fixture_task_status_wip(self):
        self.task_status_wip = TaskStatus(name="WIP",
                                          short_name="wip",
                                          color="#FFFFFF")
        self.task_status_wip.save()

    def generate_fixture_task_status_to_review(self):
        self.task_status_to_review = TaskStatus(name="To review",
                                                short_name="pndng",
                                                color="#FFFFFF")
        self.task_status_to_review.save()

    def generate_fixture_assigner(self):
        self.assigner = Person(first_name="Ema", last_name="Peel")
        self.assigner.save()

    def generate_fixture_task(self, name="Master", entity_id=None):
        if entity_id is None:
            entity_id = self.entity.id

        start_date = fields.get_date_object("2017-02-20")
        due_date = fields.get_date_object("2017-02-28")
        real_start_date = fields.get_date_object("2017-02-22")
        self.task = Task(name=name,
                         project_id=self.project.id,
                         task_type_id=self.task_type.id,
                         task_status_id=self.task_status.id,
                         entity_id=entity_id,
                         assignees=[self.person],
                         assigner_id=self.assigner.id,
                         duration=50,
                         estimation=40,
                         start_date=start_date,
                         due_date=due_date,
                         real_start_date=real_start_date)
        self.task.save()

    def generate_fixture_task_standard(self):
        start_date = fields.get_date_object("2017-02-20")
        due_date = fields.get_date_object("2017-02-28")
        real_start_date = fields.get_date_object("2017-02-22")
        self.task_standard = Task(name="Super modeling",
                                  project_id=self.project_standard.id,
                                  task_type_id=self.task_type.id,
                                  task_status_id=self.task_status.id,
                                  entity_id=self.entity_standard.id,
                                  assignees=[self.person],
                                  assigner_id=self.assigner.id,
                                  duration=50,
                                  estimation=40,
                                  start_date=start_date,
                                  due_date=due_date,
                                  real_start_date=real_start_date)
        self.task_standard.save()

    def generate_fixture_shot_task(self, name="Master"):
        self.shot_task = Task(
            name=name,
            project_id=self.project.id,
            task_type_id=self.task_type_animation.id,
            task_status_id=self.task_status.id,
            entity_id=self.shot.id,
            assignees=[self.person],
            assigner_id=self.assigner.id,
        )
        return self.shot_task.save()

    def generate_fixture_scene_task(self, name="Master"):
        self.scene_task = Task.create(
            name=name,
            project_id=self.project.id,
            task_type_id=self.task_type_animation.id,
            task_status_id=self.task_status.id,
            entity_id=self.scene.id,
            assignees=[self.person],
            assigner_id=self.assigner.id,
        )
        return self.scene_task

    def generate_fixture_sequence_task(self, name="Master"):
        self.sequence_task = Task(
            name=name,
            project_id=self.project.id,
            task_type_id=self.task_type_animation.id,
            task_status_id=self.task_status.id,
            entity_id=self.sequence.id,
            assignees=[self.person],
            assigner_id=self.assigner.id,
        )
        self.sequence_task.save()

    def generate_fixture_shot_task_standard(self):
        self.shot_task_standard = Task(
            name="Super animation",
            project_id=self.project_standard.id,
            task_type_id=self.task_type_animation.id,
            task_status_id=self.task_status.id,
            entity_id=self.shot_standard.id,
            assignees=[self.person],
            assigner_id=self.assigner.id)
        self.shot_task_standard.save()

    def generate_fixture_file_status(self):
        self.file_status = FileStatus(name="To review", color="#FFFFFF")
        self.file_status.save()

    def generate_fixture_working_file(self, name="main", revision=1):
        self.working_file = WorkingFile(name=name,
                                        comment="",
                                        revision=revision,
                                        task_id=self.task.id,
                                        entity_id=self.entity.id,
                                        person_id=self.person.id,
                                        software_id=self.software.id)
        self.working_file.save()
        return self.working_file

    def generate_fixture_shot_working_file(self):
        self.working_file = WorkingFile(name="main",
                                        comment="",
                                        revision=1,
                                        task_id=self.shot_task.id,
                                        entity_id=self.shot.id,
                                        person_id=self.person.id,
                                        software_id=self.software.id)
        self.working_file.save()

    def generate_fixture_output_file(self,
                                     output_type=None,
                                     revision=1,
                                     name="main",
                                     task=None):
        if output_type is None:
            output_type = self.output_type

        if task is None:
            task_type_id = self.task_type.id
            entity_id = self.entity.id
        else:
            task_type_id = task.task_type_id
            entity_id = task.entity_id

        self.output_file = OutputFile.create(
            comment="",
            revision=revision,
            task_type_id=task_type_id,
            entity_id=entity_id,
            person_id=self.person.id,
            file_status_id=self.file_status.id,
            output_type_id=output_type.id,
            name=name)
        return self.output_file

    def generate_fixture_output_type(self, name="Geometry", short_name="Geo"):
        self.output_type = OutputType.create(name=name, short_name=short_name)
        return self.output_type

    def generate_fixture_software(self):
        self.software = Software.create(name="Blender",
                                        short_name="bdr",
                                        file_extension=".blender")
        self.software_max = Software.create(name="3dsMax",
                                            short_name="max",
                                            file_extension=".max")

    def generate_fixture_preview_file(self, revision=1):
        self.preview_file = PreviewFile.create(name="main",
                                               revision=revision,
                                               description="test description",
                                               source="pytest",
                                               task_id=self.task.id,
                                               person_id=self.person.id)
        return self.preview_file

    def get_fixture_file_path(self, relative_path):
        current_path = os.getcwd()
        file_path_fixture = os.path.join(current_path, "tests", "fixtures",
                                         relative_path)
        return file_path_fixture

    def generate_assigned_task(self):
        self.generate_fixture_entity()
        self.generate_fixture_department()
        self.generate_fixture_task_type()
        self.generate_fixture_task_status()
        self.generate_fixture_person()
        self.generate_fixture_assigner()
        self.generate_fixture_task()

    def generate_shot_suite(self):
        self.generate_fixture_entity_type()
        self.generate_fixture_project_status()
        self.generate_fixture_project()
        self.generate_fixture_episode()
        self.generate_fixture_sequence()
        self.generate_fixture_shot()
        self.generate_fixture_scene()