def create_task(task_id):
    """
    Celery queued task that creates a next task following the workflow.
    For example, if the input is a task_id from a tt1 task,
    this method will create one tt2 task

    :arg task_id: Integer pybossa task id
    """
    task = task_factory.get_task(task_id)
    task.add_next_task()
def close_task(task_id):
    """
    Celery queued task that set's pybossa task state to completed

    :arg task_id: Integer pybossa task id

    """
    
    current_task = task_factory.get_task(task_id)
    current_task.close_task()
def check_task(task_id):
    """
    Celery queued task that check pybossa's tasks status

    :arg task_id: Integer pybossa task id
    :returns: If the given pybossa task is finished
    :rtype: bool

    """
    task = task_factory.get_task(task_id)
    return task.check_answer()
def close_t1(book_id):
    """
    Celery queued task that set's pybossa task type 1 (selection) 
    state to completed and close it. This function is is useful into case 
    that the book has only pages with tables.

    :arg task_id: Integer pybossa task id

    """
    
    tt_select = Apptt_select(short_name=book_id + "_tt1")
    tasks = tt_select.get_tasks()
    
    for task in tasks:
        tt_task = task_factory.get_task(task.id)
        tt_task.special_close_task()
        tt_task.add_next_task()
    def test_get_task_01(self):
        self.app_tt_meta.add_task(task_info={"answer": "Yes", "page": 1})
        self.app_tt_meta.add_task(task_info={"answer": "Yes", "page": 2})
        self.app_tt_meta.add_task(task_info={"answer": "No", "page": 3})

        tasks = pbclient.get_tasks(self.app_tt_meta.app_id)

        try:
            for task in tasks:
                t2 = task_factory.get_task(task.id)
                if task.id == t2.task.id:
                    self.assertEquals(t2.task.info["page"], task.info["page"])
                    self.assertEquals(t2.task.info["answer"], task.info["answer"])
                    self.assertEquals(t2.app_short_name, self.app_tt_meta.short_name)

        except Exception as e:
            print e
            assert False
def available_tasks(task_id):
    """
    Celery queued task that verify if there next available
    tasks at the workflow for a given pybossa task

    :arg task_id: Integer pybossa task id
    :returns: If there are available tasks
    :rtype: bool

    """
    current_task = task_factory.get_task(task_id)

    if(current_task):
        next_app = current_task.get_next_app()
        available_tasks = next_app.get_tasks()

        for task in available_tasks:
            if task.state != "completed":
                return True

    return False
def save_fact(factInfo):
    taskFacade = task_factory.get_task(factInfo['task_id'])

    user_id = factInfo['user_id']
    book_id = taskFacade.get_book_id()
    page_id = taskFacade.task.info['page']
    top_pos = factInfo['top_pos']
    left_pos = factInfo['left_pos']
    bottom_pos = factInfo['bottom_pos']
    right_pos = factInfo['right_pos']

    isUpdate = 'id' in factInfo
    post_id = factInfo['post_id'] if 'post_id' in factInfo else ''
    fact_text = factInfo['fact_text'] if 'fact_text' in factInfo else ''
    fact_id = factInfo['id'] if isUpdate else -1

    con = None

    try:
        con = __create_db_connection(app.config['DB_NAME'])
        cursor = con.cursor()
        query = ""

        if (isUpdate):
            query = "UPDATE facts SET (user_id, book_id, page_id, top_pos, left_pos, bottom_pos, right_pos, post_id, fact_text) = ('" + user_id + "', '" + book_id + "', " + str(page_id) + ", " + str(top_pos) + ", "+ str(left_pos) + ", " + str(bottom_pos) + ", " + str(right_pos) + ", '" + post_id + "', '" + fact_text + "') WHERE id = " + str(fact_id)
        else:
            query = "BEGIN; INSERT INTO facts(user_id, book_id, page_id, top_pos, left_pos, bottom_pos, right_pos, post_id, fact_text) values ('" + user_id + "', '" + book_id + "', " + str(page_id) + ", " + str(top_pos) + ", "+ str(left_pos) + ", " + str(bottom_pos) + ", " + str(right_pos) + ", '" + post_id + "', '" + fact_text + "') RETURNING id;"

        print(query)
        cursor.execute(query)

        if not isUpdate:
            rows = cursor.fetchone()
            fact_id = rows[0]

        con.commit()
    except psycopg2.DatabaseError, e:
        print 'Error %s' % e
 def test_get_task_02(self):
     try:
         task_factory.get_task(-1)
     except Meb_task_factory_exception as e:
         self.assertEquals(e.code, 1)