def __create_app(self): """ Create a new app with name, shortname and description passed in constructor and with category_id = 1 and return app.id. Or return the app.id from the app registered in pybossa database. :returns: app.id :rtype: int """ apps = pbclient.find_app(short_name=self.short_name) if not len(apps) == 0: app = apps[0] msg = '{app_name} app is already registered in the DB'.format(app_name=app.name.encode('utf-8', 'replace')) logger.info(unicode(msg, "utf-8")) return app.id else: logger.info("The application is not registered in PyBOSSA. Creating it...") ans = pbclient.create_app(name=self.name, short_name=self.short_name, description=self.description) try: if ans: app = pbclient.find_app(short_name=self.short_name)[0] app.info = dict(newtask="%s/app/%s/newtask" % (flask_app.config['PYBOSSA_URL'], self.short_name)) app.category_id = 1 pbclient.update_app(app) return app.id except Exception as ex: logger.error(Meb_apps_exception(4, -1, self.short_name)) raise ex
def fix_dates_t2(app_short_name): apps = pbclient.find_app(short_name=app_short_name) if len(apps) > 0: app = pbclient.find_app(short_name=app_short_name)[0] trs = pbclient.find_taskruns(app.id, limit=sys.maxint) for tr in trs: infos = tr.info[1:len(tr.info)-1] infos = __fix_task_run_info_dict(infos) if infos != "": infos = ast.literal_eval(infos) if type(infos) is tuple: info_list = [] for info in infos: info = __change_fields(info) info = __undo_fix_task_run_info_dict(info) info_list.append(info) #print "info_list: " + str(info_list) #print "info_tuple: " + str(tuple(info_list)) tr.info = json.dumps(tuple(info_list)) elif type(infos) is dict: info_dict = __change_fields(infos) info_dict = __undo_fix_task_run_info_dict(info_dict) tr.info = "[" + json.dumps(info_dict) + "]" __update_taskrun(tr) else: print "(empty info): tr.id = " + str(tr.id)
def test_04_init_and_close(self): # Creating new tt applications create_and_close_t1(self.app, "rpparaiba1918") time.sleep(40) pb_app = pbclient.find_app(short_name="rpparaiba1918_tt1") tasks_t1 = pbclient.get_tasks(pb_app[0].id, sys.maxint) for task in tasks_t1: self.assertTrue(task.state == "completed") pb_app = pbclient.find_app(short_name="rpparaiba1918_tt2") tasks_t2 = pbclient.get_tasks(pb_app[0].id, sys.maxint) self.assertTrue(len(tasks_t1) == len(tasks_t2))
def create_t2_task(test_case, book_id, task_t1): # creating an answer for the T1 app task_run = dict(app_id=task_t1.app_id, task_id=task_t1.id, info="Yes") # Anonymous submission submit_answer(test_case.base_url, task_run) # FB authenticated user submission task_run['facebook_user_id'] = '12345' submit_answer(test_case.base_url, task_run) # Signalling the T1 task completion done_task(test_case.app, task_t1.id) time.sleep(5) # check if T1 task is closed task_t1 = pbclient.get_tasks(task_t1.app_id, sys.maxint)[0] test_case.assertTrue(task_t1.state == "completed") # one task from T2 app should exist app_t2 = pbclient.find_app(short_name=book_id + "_tt2") test_case.assertTrue(len(app_t2) > 0, "Error tt_app was not created") t2_tasks = pbclient.get_tasks(app_t2[0].id, sys.maxint) test_case.assertTrue(len(t2_tasks) == 1) return t2_tasks[0]
def create_t3_task(test_case, book_id, task_t2): # creating an answer for the T2 app answer_t2 = '[{\"id\":\"new\",\"top\":244,\"left\":24,\"width\":506,\"height\":177,\"text\":{\"titulo\":\"test-titulo\",\"subtitulo\":\"test-subtitulo\",\"assunto\":\"0\",\"fontes\":\"test-fontes\",\"outros\":\"\",\"dataInicial\":\"01/01/1901\",\"dataFinal\":\"01/01/1902\",\"girar\":false,\"nao_girar\":true},\"editable\":true}, {\"id\":\"new\",\"top\":100,\"left\":300,\"width\":250,\"height\":230,\"text\":{\"titulo\":\"test-titulo\",\"subtitulo\":\"test-subtitulo\",\"assunto\":\"3\",\"fontes\":\"test-fontes\",\"outros\":\"test-outros\",\"dataInicial\":\"01/01/1901\",\"dataFinal\":\"01/01/1902\",\"girar\":false,\"nao_girar\":true},\"editable\":true}]' task_run = dict(app_id=task_t2.app_id, task_id=task_t2.id, info=answer_t2) # Anonymous submission submit_answer(test_case.base_url, task_run) # FB authenticated user submission task_run['facebook_user_id'] = '12345' submit_answer(test_case.base_url, task_run) # Signalling the T2 task completion done_task(test_case.app, task_t2.id) time.sleep(15) # check if T2 task is closed task_t2 = pbclient.get_tasks(task_t2.app_id, sys.maxint)[0] test_case.assertTrue(task_t2.state == "completed") # one task from T3 app should exist app_t3 = pbclient.find_app(short_name=book_id + "_tt3") test_case.assertTrue(len(app_t3) > 0, "Error tt_app was not created") t3_tasks = pbclient.get_tasks(app_t3[0].id, sys.maxint) test_case.assertTrue(len(t3_tasks) == 2) return t3_tasks
def create_t4_task(test_case, book_id, task_t3, number_of_expected_t4_tasks): # creating an answer for the T3 app answer_t3 = '{\"img_url\":\"https://localhost/mb-static/books/rpparaiba1918/metadados/tabelasBaixa/image0_0.png\",\"linhas\":[[0,0,507,0],[0,54,507,54],[0,103,507,103],[0,178,507,178]],\"colunas\":[[0,0,0,178],[239,0,239,178],[507,0,507,178]],\"maxX\":507,\"maxY\":178}' task_run = dict(app_id=task_t3.app_id, task_id=task_t3.id, info=answer_t3) # Anonymous submission submit_answer(test_case.base_url, task_run) # FB authenticated user submission task_run['facebook_user_id'] = '12345' submit_answer(test_case.base_url, task_run) # Signalling the T3 task completion done_task(test_case.app, task_t3.id) time.sleep(10) # check if T3 task is closed task_t3 = pbclient.get_tasks(task_t3.app_id, sys.maxint)[0] test_case.assertTrue(task_t3.state == "completed") # one task from T4 app should exist app_t4 = pbclient.find_app(short_name=book_id + "_tt4") test_case.assertTrue(len(app_t4) > 0, "Error tt_app was not created") t4_tasks = pbclient.get_tasks(app_t4[0].id, sys.maxint) test_case.assertTrue(len(t4_tasks) == number_of_expected_t4_tasks) return t4_tasks
def test_05_mbdb_loading(self): # Creating new tt applications create_tt_apps(self.app, self.book_id) app_t1 = pbclient.find_app(short_name=self.book_id + "_tt1") self.assertTrue(len(app_t1) > 0, "Error tt_app was not created") # check if the book was created self.assertTrue(get_book(self.book_id)) # check if the workflow transaction (T1) was created task_t1 = pbclient.get_tasks(app_t1[0].id, sys.maxint)[0] self.assertTrue(len(get_workflow_transaction()) == 74) workflow_transaction_info = dict(task_id_1=task_t1.id, task_id_2=None, task_id_3=None, task_id_4=None) self.assertTrue(get_workflow_transaction(workflow_transaction_info)) task_t2 = create_t2_task(self, self.book_id, task_t1) # check if the workflow transaction (T2) was created workflow_transaction_info = dict(task_id_1=task_t1.id, task_id_2=task_t2.id, task_id_3=None, task_id_4=None) self.assertTrue(get_workflow_transaction(workflow_transaction_info)) # check if the page was created page = get_page(self.book_id, task_t2.info['page']) self.assertTrue(page) tasks_t3 = create_t3_task(self, self.book_id, task_t2) # check if the workflow transactions (T3) were created workflow_transaction_info = dict(task_id_1=task_t1.id, task_id_2=task_t2.id, task_id_3=tasks_t3[0].id, task_id_4=None) self.assertTrue(get_workflow_transaction(workflow_transaction_info)) workflow_transaction_info = dict(task_id_1=task_t1.id, task_id_2=task_t2.id, task_id_3=tasks_t3[1].id, task_id_4=None) self.assertTrue(get_workflow_transaction(workflow_transaction_info)) # check if the page tables were created page_tables = get_page_table(self.book_id, page.id) self.assertTrue(len(page_tables) == 2) # check if the metadata was created self.assertTrue(get_metadata(page_tables[0].id)) self.assertTrue(get_metadata(page_tables[1].id)) tasks_t4 = create_t4_task(self, self.book_id, tasks_t3[0], 1) close_t4_task(self, self.book_id, tasks_t4[0]) # check if the workflow transactions (T4) were created workflow_transaction_info = dict(task_id_1=task_t1.id, task_id_2=task_t2.id, task_id_3=tasks_t3[0].id, task_id_4=tasks_t4[0].id) self.assertTrue(get_workflow_transaction(workflow_transaction_info)) tasks_t4 = create_t4_task(self, self.book_id, tasks_t3[1], 2) close_t4_task(self, self.book_id, tasks_t4[1]) # check if the workflow transactions (T4) were created workflow_transaction_info = dict(task_id_1=task_t1.id, task_id_2=task_t2.id, task_id_3=tasks_t3[1].id, task_id_4=tasks_t4[1].id) self.assertTrue(get_workflow_transaction(workflow_transaction_info)) # check if the cells were created self.assertTrue(len(get_cell(page_tables[0].id)) == 6) self.assertTrue(len(get_cell(page_tables[1].id)) == 6)
def test_add_next_task_02(self): """ Invalid book """ try: book_title = "BLBLB_title" app = Apptt_meta(short_name="BLBLB_tt2", title=book_title) app.add_task(task_info=dict(link="http://archive.org/download/livro1/n1", page=1)) app.add_task(task_info=dict(link="http://archive.org/download/livro1/n2", page=2)) tasks = pbclient.get_tasks(app_id=app.app_id) task1 = TTTask2(tasks[0].id, app_short_name=app.short_name) task2 = TTTask2(tasks[1].id, app_short_name=app.short_name) book_id = "BLBLB" data_mngr.record_book(dict(bookid=book_id, title=book_title, contributor="cont1", publisher="pub1", volume="1", img="image1")) task_run1 = dict(app_id=app.app_id, task_id=task1.task.id, info="[{\"id\":\"new\",\"top\":50,\"left\":9.5,\"width\":525,\"height\":626,\"text\":{\"titulo\":\"\",\"subtitulo\":\"\",\"assunto\":\"4\",\"fontes\":\"\",\"outros\":\"\",\"dataInicial\":\"\",\"dataFinal\":\"\",\"girar\":false,\"nao_girar\":true},\"editable\":true}]") task_run2 = dict(app_id=app.app_id, task_id=task1.task.id, info="[{\"id\":\"new\",\"top\":50,\"left\":15.5,\"width\":625,\"height\":626,\"text\":{\"titulo\":\"\",\"subtitulo\":\"\",\"assunto\":\"4\",\"fontes\":\"\",\"outros\":\"\",\"dataInicial\":\"\",\"dataFinal\":\"\",\"girar\":false,\"nao_girar\":true},\"editable\":true}]") # Anonymous submission submit_answer(self.base_url, task_run1) # FB authenticated user submission task_run2['facebook_user_id'] = '12345' submit_answer(self.base_url, task_run2) time.sleep(2) self.assertFalse(task1.check_answer()) task1.add_next_task() assert False except Meb_exception_tt2 as e: self.assertEquals(e.code, 3) finally: next_app = None next_app_list = pbclient.find_app(short_name=app.short_name[:-1] + "3") if len(next_app_list) > 0: next_app = next_app_list[0] next_task = None tasks = pbclient.get_tasks(next_app.id) for t in tasks: if t.info["page"] == task1.task.info["page"]: pbclient.delete_task(task_id=t.id) pbclient.delete_app(app_id=next_app.id) pbclient.delete_task(task1.task.id) pbclient.delete_task(task2.task.id) pbclient.delete_app(app.app_id) data_mngr.delete_book(book_id="BLBLB")
def test_01_t2_creation(self): # Creating new tt applications create_tt_apps(self.app, self.book_id) app_t1 = pbclient.find_app(short_name=self.book_id + "_tt1") self.assertTrue(len(app_t1) > 0, "Error tt_app was not created") task_t1 = pbclient.get_tasks(app_t1[0].id, sys.maxint)[0] create_t2_task(self, self.book_id, task_t1)
def delete_workflow_transactions(book_id): app_t1 = pbclient.find_app(short_name=book_id + "_tt1") if len(app_t1) > 0: t1_tasks = pbclient.get_tasks(app_t1[0].id, sys.maxint) t1_ids = [] for task in t1_tasks: t1_ids.append(task.id) return data_mngr.delete_workflow_transactions(t1_ids)
def delete_app(short_name): for i in range(1, 5): apps = pbclient.find_app(short_name="%s_tt%d" % (short_name, i)) if len(apps) == 0: return tt_app = apps[0] pbclient.delete_app(tt_app.id)
def test_03_init(self): # Creating tt_app where book_id does not exist inexistent_id = "XX_does_not_exist_XX" init_req = create_tt_apps(self.app, inexistent_id) self.assertEqual(init_req.data, "False", "Error application can not be created") search_list = pbclient.find_app(short_name=inexistent_id + "_tt1") self.assertTrue(len(search_list) == 0, "Error application with inexistent id was found")
def test_02_init(self): # Creating new tt applications create_tt_apps(self.app, "rpparaiba1918") pb_app = pbclient.find_app(short_name="rpparaiba1918_tt1") # application is already created n_app_tasks = len(pbclient.get_tasks(pb_app[0].id, sys.maxint)) rv = self.app.get("/api/rpparaiba1918/init") self.assertTrue(rv.data, "Error tt_app was not created") # if application is already created init cant duplicate the tasks self.assertEqual(n_app_tasks, len(pbclient.get_tasks(pb_app[0].id, sys.maxint)), "Error duplicated tasks")
def get_tasks_progress(bookid): tt_suffix = ['tt1', 'tt2', 'tt3', 'tt4'] result = dict(tt1=dict(total=0, completed=0), tt2=dict(total=0, completed=0), tt3=dict(total=0, completed=0), tt4=dict(total=0, completed=0)) for suffix in tt_suffix: app = pbclient.find_app(short_name=str(bookid) + "_" + suffix)[0] tasks = pbclient.get_tasks(app.id, limit=max_limit) result[suffix]['total'] = len(tasks) for task in tasks: if task.state == 'completed': result[suffix]['completed'] += 1 return result
def create_home_app(): try: meb_short_name = "meb_home" apps = pbclient.find_app(short_name=meb_short_name) pyb_app = None if len(apps) != 0: pyb_app = apps[0] else: ans = pbclient.create_app(name="Memória Estatística do Brasil", short_name=meb_short_name, description="Página inicial do MEB.") if ans: pyb_app = pbclient.find_app(short_name=meb_short_name)[0] pbclient.create_task(pyb_app.id, {}) if pyb_app == None: return False new_long_desc_template = meb_util.set_url(urllib2.urlopen( urllib2.Request(app.config['URL_TEMPLATES'] + "/templates/long_description-home.html")), meb_short_name) new_template = meb_util.set_url(urllib2.urlopen( urllib2.Request(app.config['URL_TEMPLATES'] + "/templates/template-home.html")), meb_short_name) pyb_app.info['thumbnail'] = app.config['URL_TEMPLATES'] + "/images/meb_icon.png" pyb_app.category_id = 1 pyb_app.long_description = new_long_desc_template pyb_app.info['task_presenter'] = new_template pbclient.update_app(pyb_app) return True except Exception as e: return False
def submit_report(a_shortname, t_id, msg, u_ident): a_id = pbclient.find_app(short_name=a_shortname)[0].id infos = {} infos['msg'] = json.dumps(msg, ensure_ascii=False) infos['app_id'] = a_id infos['task_id'] = t_id infos['user_id'] = str(u_ident) infos['created'] = datetime.datetime.now() try: data_mngr.record_report(infos) return True except Exception as e: print e return False # TODO: send exception to log
def tearDown(self): next_app = None next_app_list = pbclient.find_app(short_name=self.app.short_name[:-1] + "2") if len(next_app_list) > 0: next_app = next_app_list[0] next_task = None tasks = pbclient.get_tasks(next_app.id) for t in tasks: if t.info["page"] == self.task1.task.info["page"]: pbclient.delete_task(task_id=t.id) pbclient.delete_app(app_id=next_app.id) pbclient.delete_task(self.task1.task.id) pbclient.delete_task(self.task2.task.id) pbclient.delete_app(self.app.app_id) data_mngr.delete_book(self.task1.get_book_id())
def test_01_init(self): # Creating new tt applications create_tt_apps(self.app, "rpparaiba1918") pb_app = pbclient.find_app(short_name="rpparaiba1918_tt1") self.assertTrue(len(pb_app) > 0, "Error tt_app was not created")
if __name__ == "__main__": usage = "usage: %prog [options]" parser = OptionParser(usage) parser.add_option( "-u", "--update-templates", dest="app_short_name", help="Update app templates: long-description and template", metavar="SHORT_NAME") (options, args) = parser.parse_args() if options.app_short_name: app_short_name = options.app_short_name print app_short_name apps = pbclient.find_app(short_name=app_short_name) if len(apps) == 0: print "App %s not found" % (app_short_name) exit(0) app = apps[0] app_type = app_short_name[-4:] template_type = None long_desc_type = None app_name = None if app_type == "_tt1": template_type = "template-select.html" long_desc_type = "long_description-select.html" app_name = unicode("Seleção", "utf-8")
def test_add_next_task_03(self): """ Invalid book """ try: book_title = "BLABDL_title" app = Apptt_struct(short_name="BLABDL_tt3", title=book_title) app.add_task(task_info=dict( hasZoom=False, zoom=[0, 0, 489, 168], coords=[[0, 0, 7, 9], [7, 0, 37, 9], [37, 0, 118, 9], [118, 0, 251, 9], [251, 0, 489, 9], [0, 9, 7, 53], [7, 9, 37, 53], [37, 9, 118, 53], [118, 9, 251, 53], [251, 9, 489, 53], [0, 53, 7, 61], [7, 53, 37, 61], [37, 53, 118, 61], [118, 53, 251, 61], [251, 53, 489, 61], [0, 61, 7, 100], [7, 61, 37, 100], [37, 61, 118, 100], [118, 61, 251, 100], [251, 61, 489, 100], [0, 100, 7, 144], [7, 100, 37, 144], [37, 100, 118, 144], [118, 100, 251, 144], [251, 100, 489, 144], [0, 144, 7, 153], [7, 144, 37, 153], [37, 144, 118, 153], [118, 144, 251, 153], [251, 144, 489, 153], [0, 153, 7, 181], [7, 153, 37, 181], [37, 153, 118, 181], [118, 153, 251, 181], [251, 153, 489, 181], [0, 181, 7, 239], [7, 181, 37, 239], [37, 181, 118, 239], [118, 181, 251, 239], [251, 181, 489, 239], [0, 239, 7, 280], [7, 239, 37, 280], [37, 239, 118, 280], [118, 239, 251, 280], [251, 239, 489, 280], [0, 280, 7, 289], [7, 280, 37, 289], [37, 280, 118, 289], [118, 280, 251, 289], [251, 280, 489, 289], [0, 289, 7, 506], [7, 289, 37, 506], [37, 289, 118, 506], [118, 289, 251, 506], [251, 289, 489, 506]], table_id=0, img_url="http://localhost/mb-static/books/BLABDL/metadados/tabelasBaixa/image2000_0.png", page=1 ) ) app.add_task(task_info=dict( hasZoom=False, zoom=[0, 0, 489, 168], coords=[[0, 0, 7, 9], [7, 0, 37, 9], [37, 0, 118, 9], [118, 0, 251, 9], [251, 0, 489, 9], [0, 9, 7, 53], [7, 9, 37, 53], [37, 9, 118, 53], [118, 9, 251, 53], [251, 9, 489, 53], [0, 53, 7, 61], [7, 53, 37, 61], [37, 53, 118, 61], [118, 53, 251, 61], [251, 53, 489, 61], [0, 61, 7, 100], [7, 61, 37, 100], [37, 61, 118, 100], [118, 61, 251, 100], [251, 61, 489, 100], [0, 100, 7, 144], [7, 100, 37, 144], [37, 100, 118, 144], [118, 100, 251, 144], [251, 100, 489, 144], [0, 144, 7, 153], [7, 144, 37, 153], [37, 144, 118, 153], [118, 144, 251, 153], [251, 144, 489, 153], [0, 153, 7, 181], [7, 153, 37, 181], [37, 153, 118, 181], [118, 153, 251, 181], [251, 153, 489, 181], [0, 181, 7, 239], [7, 181, 37, 239], [37, 181, 118, 239], [118, 181, 251, 239], [251, 181, 489, 239], [0, 239, 7, 280], [7, 239, 37, 280], [37, 239, 118, 280], [118, 239, 251, 280], [251, 239, 489, 280], [0, 280, 7, 289], [7, 280, 37, 289], [37, 280, 118, 289], [118, 280, 251, 289], [251, 280, 489, 289], [0, 289, 7, 506], [7, 289, 37, 506], [37, 289, 118, 506], [118, 289, 251, 506], [251, 289, 489, 506]], table_id=1, img_url="http://localhost/mb-static/books/BLABDL/metadados/tabelasBaixa/image2000_1.png", page=1 ) ) tasks = pbclient.get_tasks(app_id=app.app_id) task1 = TTTask3(tasks[0].id, app_short_name=app.short_name) task2 = TTTask3(tasks[1].id, app_short_name=app.short_name) book_id = "BLABDL" task_run1 = dict(app_id=app.app_id, task_id=task1.task.id, info='{\"img_url\":\"https://localhost/mb-static/books/BLABDL/metadados/tabelasBaixa/image2000_0.png\",\"linhas\":[[0,0,507,0],[0,54,507,54],[0,103,507,103],[0,178,507,178]],\"colunas\":[[0,0,0,178],[239,0,239,178],[507,0,507,178]],\"maxX\":507,\"maxY\":178}') task_run2 = dict(app_id=app.app_id, task_id=task1.task.id, info='{\"img_url\":\"https://localhost/mb-static/books/BLABDL/metadados/tabelasBaixa/image2000_1.png\",\"linhas\":[[0,0,507,0],[0,54,507,54],[0,103,507,103],[0,178,507,178]],\"colunas\":[[0,0,0,178],[239,0,239,178],[507,0,507,178]],\"maxX\":507,\"maxY\":178}') # Anonymous submission submit_answer(self.base_url, task_run1) # FB authenticated user submission task_run2['facebook_user_id'] = '12345' submit_answer(self.base_url, task_run2) time.sleep(2) self.assertTrue(task1.check_answer()) task1.add_next_task() assert False except Meb_exception_tt3 as e: self.assertEquals(e.code, 5) finally: next_app = None next_app_list = pbclient.find_app(short_name=app.short_name[:-1] + "4") if len(next_app_list) > 0: next_app = next_app_list[0] next_task = None tasks = pbclient.get_tasks(next_app.id) for t in tasks: if t.info["img_url"] == task1.task.info["img_url"]: pbclient.delete_task(task_id=t.id) pbclient.delete_app(app_id=next_app.id) pbclient.delete_task(task1.task.id) pbclient.delete_task(task2.task.id) pbclient.delete_app(app.app_id) data_mngr.delete_book(book_id="BLABDL")