def element_in_queue(self, **kwargs) -> Optional[FlaggedBy]: """ Check if the element described by kwargs is in any queue. Return a FlaggedBy object or none :param kwargs: "magic" -> atm keywords like argument_uid, statement_uid and premisegroup_uid. Please update this! :return: """ queues = [get_queue_by_key(key) for key in review_queues] status = [ queue().element_in_queue( self.db_user, argument_uid=kwargs.get('argument_uid'), statement_uid=kwargs.get('statement_uid'), premisegroup_uid=kwargs.get('premisegroup_uid')) for queue in queues ] if FlaggedBy.user in status: return FlaggedBy.user if FlaggedBy.other in status: return FlaggedBy.other return None
def test_get_text_of_element(self): for key in review_queues: queue = get_queue_by_key(key) adapter = QueueAdapter(queue=queue(), db_user=self.user, application_url='main', translator=self.tn) review_table = get_review_model_by_key(key) db_review = DBDiscussionSession.query(review_table).filter_by( is_executed=False, is_revoked=False).first() text = adapter.get_text_of_element(db_review) self.assertTrue(isinstance(text, str))
def test_get_review_count(self): for key in review_queues: queue = get_queue_by_key(key) adapter = QueueAdapter(queue=queue(), db_user=self.user, application_url='main', translator=self.tn) review_table = get_review_model_by_key(key) db_review = DBDiscussionSession.query(review_table).filter_by( is_executed=False, is_revoked=False).first() okay, not_okay = adapter.get_review_count(db_review.uid) self.assertTrue(isinstance(okay, int)) self.assertTrue(isinstance(not_okay, int))
def test_get_all_votes_for(self): for key in review_queues: queue = get_queue_by_key(key) adapter = QueueAdapter(queue=queue(), db_user=self.user, application_url='main', translator=self.tn) review_table = get_review_model_by_key(key) db_review = DBDiscussionSession.query(review_table).filter_by( is_executed=False, is_revoked=False).first() pro, con = adapter.get_all_votes_for(db_review) self.assertTrue(isinstance(pro, list)) self.assertTrue(isinstance(con, list))
def __get_executed_review_element_of(table_key, main_page, db_review, translator, is_executed) -> Optional[dict]: """ :param table_key: Shortcut for the table :param main_page: Main page of D-BAS :param db_review: Element :param translator: current ui_locales :param is_executed :return: Element """ queue = get_queue_by_key(table_key) adapter = QueueAdapter(queue=queue(), application_url=main_page, translator=translator) full_text = adapter.get_text_of_element(db_review) if not full_text: return None # pretty print intro = translator.get(_.otherUsersSaidThat) + ' ' if full_text.startswith(intro): short_text = full_text[len(intro):len(intro) + 1].upper() short_text += full_text[len(intro) + 1:len(intro) + txt_len_history_page] else: short_text = full_text[0:txt_len_history_page] short_text += '...' if len(full_text) > txt_len_history_page else '.' short_text = f'<span class="text-primary">{short_text}</span>' pro_list, con_list = adapter.get_all_votes_for(db_review) # and build up some dict pdict = __handle_table_of_review_element(table_key, db_review, short_text, full_text, is_executed) if not pdict: return None pdict['entry_id'] = db_review.uid pdict['timestamp'] = sql_timestamp_pretty_print(db_review.timestamp, translator.get_lang()) pdict['votes_pro'] = pro_list pdict['votes_con'] = con_list pdict['reporter'] = __get_user_dict_for_review(db_review.detector_uid, main_page) return pdict
def test_get_subpage_of_queue(self): self.db_user.participates_in.append(self.issue) for key in review_queues: queue = get_queue_by_key(key) adapter = QueueAdapter(queue=queue(), db_user=self.db_user, application_url='main', translator=self.tn) subpage = adapter.get_subpage_of_queue({}, key) self.assertIn('elements', subpage) self.assertIn('no_arguments_to_review', subpage) self.assertIn('button_set', subpage) self.assertIn('session', subpage) self.assertTrue(key, subpage['elements']['page_name']) self.assertIn('reviewed_element', subpage['elements'])
def cancel_review(request): """ Trys to cancel an ongoing review :param request: current request of the server :return: json-dict() """ LOG.debug("Cancel an ongoing review. %s", request.json_body) db_user = request.validated['user'] queue = request.validated['queue'] db_review = request.validated['review'] _tn = Translator(get_language_from_cookie(request)) queue = get_queue_by_key(queue) adapter = QueueAdapter(queue(), db_user, request.application_url, _tn) return adapter.cancel_ballot(db_review)
def undo_review(request): """ Trys to undo a done review process :param request: current request of the server :return: json-dict() """ LOG.debug("Undo a review process. %s", request.json_body) db_user = request.validated['user'] queue = request.validated['queue'] db_review = request.validated['review'] _tn = Translator(get_language_from_cookie(request)) queue = get_queue_by_key(queue) adapter = QueueAdapter(queue(), db_user, request.application_url, _tn) return adapter.revoke_ballot(db_review)
def __handle_table_of_review_element(table_key, review, short_text, full_text, is_executed): """ :param table_key: :param review: :param short_text: :param full_text: :param is_executed: :return: """ pdict = dict() pdict['row_id'] = table_key + str(review.uid) pdict['argument_shorttext'] = short_text pdict['argument_fulltext'] = full_text pdict['is_innocent'] = True queue = get_queue_by_key(table_key) adapter = QueueAdapter(queue()) return adapter.get_history_table_row(review, pdict, is_executed=is_executed, short_text=short_text, full_text=full_text)
def test_get_history_table_row(self): for key in review_queues: queue = get_queue_by_key(key) adapter = QueueAdapter(queue=queue(), db_user=self.user, application_url='main', translator=self.tn) review_table = get_review_model_by_key(key) db_review = DBDiscussionSession.query(review_table).filter_by( is_executed=False, is_revoked=False).first() row = adapter.get_history_table_row(db_review, {}, is_executed=False, short_text='short', full_text='.' * 40) if not row: continue if key == key_delete: self.assertIn('reason', row.keys()) continue self.assertTrue(any(['_shorttext' in key for key in row.keys()])) self.assertTrue(any(['_fulltext' in key for key in row.keys()]))
def __test_cancel_ballot(self, key): queue = get_queue_by_key(key) adapter = QueueAdapter(queue=queue(), db_user=self.user, application_url='main', translator=self.tn) review_table = get_review_model_by_key(key) last_reviewer_table = get_last_reviewer_by_key(key) # count of elements before we add new things to cancel review_count_1 = DBDiscussionSession.query(review_table).count() # add things which we can cancel if key in [key_merge, key_split]: db_new_review = review_table(detector=4, premisegroup=5) elif key is key_duplicate: db_new_review = review_table(detector=4, duplicate_statement=5, original_statement=4) else: db_new_review = review_table(detector=4) DBDiscussionSession.add(db_new_review) DBDiscussionSession.flush() if key == key_split: DBDiscussionSession.add( last_reviewer_table(reviewer=3, review=db_new_review.uid, should_split=True)) elif key == key_merge: DBDiscussionSession.add( last_reviewer_table(reviewer=3, review=db_new_review.uid, should_merge=True)) else: DBDiscussionSession.add( last_reviewer_table(reviewer=3, review=db_new_review.uid, is_okay=True)) DBDiscussionSession.flush() # count of elements after we add new things to cancel review_count_2 = DBDiscussionSession.query(review_table).count() review_canceled_1 = DBDiscussionSession.query(ReviewCanceled).count() # cancel things adapter.cancel_ballot(db_new_review) # count of elements after we canceled review_count_3 = DBDiscussionSession.query(review_table).count() review_canceled_2 = DBDiscussionSession.query(ReviewCanceled).count() self.assertLess(review_count_1, review_count_2) self.assertEqual(review_count_3, review_count_2) self.assertTrue(db_new_review.is_revoked) self.assertLess(review_canceled_1, review_canceled_2) self.__delete_review_in_test_cancel_ballot(key, db_new_review, last_reviewer_table, review_table)