def add_vote(self, db_user: User, db_review: ReviewDuplicate, is_okay: bool, application_url: str, translator: Translator, **kwargs): """ Adds an vote for this queue. If any (positive or negative) limit is reached, the flagged element will disabled and the origin will be set as root for any relative :param db_user: current user who votes :param db_review: the review, which is voted vor :param is_okay: True, if the element is rightly flagged :param application_url: the app url :param translator: a instance of a translator :param kwargs: optional, keyworded arguments :return: """ LOG.debug("Adding vote for review with id %s. Duplicate? %s", db_review.uid, is_okay) db_user_created_flag = DBDiscussionSession.query(User).get( db_review.detector_uid) rep_reason = None # add new vote add_vote_for(db_user, db_review, is_okay, LastReviewerDuplicate) # get all keep and delete votes count_of_reset, count_of_keep = self.get_review_count(db_review.uid) # do we reached any limit? reached_max = max(count_of_keep, count_of_reset) >= max_votes if reached_max: if count_of_reset > count_of_keep: # disable the flagged part self.__bend_objects_of_review(db_review) rep_reason = get_reason_by_action( ReputationReasons.success_duplicate) else: # just close the review rep_reason = get_reason_by_action( ReputationReasons.bad_duplicate) db_review.set_executed(True) db_review.update_timestamp() elif count_of_keep - count_of_reset >= min_difference: # just close the review rep_reason = get_reason_by_action(ReputationReasons.bad_duplicate) db_review.set_executed(True) db_review.update_timestamp() elif count_of_reset - count_of_keep >= min_difference: # disable the flagged part self.__bend_objects_of_review(db_review) rep_reason = get_reason_by_action( ReputationReasons.success_duplicate) db_review.set_executed(True) db_review.update_timestamp() if rep_reason: add_reputation_and_send_popup(db_user_created_flag, rep_reason, application_url, translator) DBDiscussionSession.add(db_review) DBDiscussionSession.flush() transaction.commit() return True
def add_vote(self, db_user: User, db_review: ReviewEdit, is_okay: bool, application_url: str, translator: Translator, **kwargs): """ Adds an vote for this queue. If any (positive or negative) limit is reached, the flagged element get a new textversion :param db_user: current user who votes :param db_review: the review, which is voted vor :param is_okay: True, if the element is rightly flagged :param application_url: the app url :param translator: a instance of a translator :param kwargs: optional, keyworded arguments :return: """ LOG.debug("Add a vote for edit queue") db_user_created_flag = DBDiscussionSession.query(User).get( db_review.detector_uid) rep_reason = None # add new vote add_vote_for(db_user, db_review, is_okay, LastReviewerEdit) # get all keep and delete votes count_of_edit, count_of_dont_edit = self.get_review_count( db_review.uid) # do we reached any limit? reached_max = max(count_of_edit, count_of_dont_edit) >= max_votes if reached_max: if count_of_dont_edit < count_of_edit: # accept the edit self.__accept_edit_review(db_review) rep_reason = get_reason_by_action( ReputationReasons.success_edit) else: # just close the review rep_reason = get_reason_by_action(ReputationReasons.bad_edit) db_review.set_executed(True) db_review.update_timestamp() elif count_of_edit - count_of_dont_edit >= min_difference: # accept the edit self.__accept_edit_review(db_review) rep_reason = get_reason_by_action(ReputationReasons.success_edit) db_review.set_executed(True) db_review.update_timestamp() elif count_of_dont_edit - count_of_edit >= min_difference: # decline edit rep_reason = get_reason_by_action(ReputationReasons.bad_edit) db_review.set_executed(True) db_review.update_timestamp() if rep_reason: add_reputation_and_send_popup(db_user_created_flag, rep_reason, application_url, translator) DBDiscussionSession.add(db_review) DBDiscussionSession.flush() transaction.commit() return True
def set_position(db_user: User, db_issue: Issue, statement_text: str, feature_data: dict = {}) -> dict: """ Set new position for current discussion and returns collection with the next url for the discussion. :param statement_text: The text of the new position statement. :param db_issue: The issue which gets the new position :param db_user: The user who sets the new position. :param feature_data: More data which is used by additional features :rtype: dict :return: Prepared collection with statement_uids of the new positions and next url or an error """ LOG.debug("%s", statement_text) new_statement: Statement = insert_as_statement(statement_text, db_user, db_issue, is_start=True) if db_issue.decision_process: dp = db_issue.decision_process if 'decidotron_cost' not in feature_data: transaction.abort() LOG.error('Cost missing for an issue with a decision_process') return { 'status': 'fail', # best error management 'errors': 'Cost missing for an issue with a decision_process' } else: cost = int(float(feature_data['decidotron_cost'])) if dp.min_position_cost <= cost <= (dp.max_position_cost or dp.budget) and not dp.position_ended(): add_associated_cost(db_issue, new_statement, cost) else: transaction.abort() LOG.error( f'Cost has to be {dp.min_position_cost} <= cost <= {dp.max_position_cost or dp.budget}. cost is: {cost}') return { 'status': 'fail', 'errors': f'Cost has to be {dp.min_position_cost} <= cost <= {dp.max_position_cost or dp.budget}.' } _um = UrlManager(db_issue.slug) url = _um.get_url_for_statement_attitude(new_statement.uid) rep_added = add_reputation_for(db_user, get_reason_by_action(ReputationReasons.first_position)) had_access = has_access_to_review_system(db_user) if not rep_added: add_reputation_for(db_user, get_reason_by_action(ReputationReasons.new_statement)) broke_limit = has_access_to_review_system(db_user) and not had_access if broke_limit: url += '#access-review' return { 'status': 'success', 'url': url, 'statement_uids': [new_statement.uid], 'errors': '' }
def set_arguments_premises(db_issue: Issue, db_user: User, db_argument: Argument, premisegroups: List[List[str]], attack_type: Relations, history, mailer) -> dict: """ Set new premise for a given conclusion and returns dictionary with url for the next step of the discussion :param db_issue: :param db_user: :param db_argument: :param premisegroups: :param attack_type: :param history: :param mailer: :rtype: dict :return: Prepared collection with statement_uids of the new premises and next url or an error """ # escaping will be done in QueryHelper().set_statement(...) langs = { 'default_locale_name': db_issue.lang, 'discussion_lang': db_issue.lang } arg_infos = { 'arg_id': db_argument.uid, 'attack_type': attack_type, 'premisegroups': premisegroups, 'history': history } url, statement_uids, error = __process_input_premises_for_arguments_and_receive_url( langs, arg_infos, db_issue, db_user, mailer) prepared_dict = {'error': error, 'statement_uids': statement_uids} if url is None: return prepared_dict # add reputation had_access = has_access_to_review_system(db_user) rep_added = add_reputation_for( db_user, get_reason_by_action(ReputationReasons.first_new_argument)) if not rep_added: add_reputation_for( db_user, get_reason_by_action(ReputationReasons.new_statement)) broke_limit = has_access_to_review_system(db_user) and not had_access if broke_limit: url += '#access-review' prepared_dict['url'] = url prepared_dict['url'] = url LOG.debug("Returning %s", prepared_dict) return prepared_dict
def test_add_reputation_for(self): DBDiscussionSession.query(ReputationHistory).filter_by( reputator_uid=9).delete() for reason in ReputationReasons: db_reason = get_reason_by_action(reason) self.assertTrue(add_reputation_for(self.user_torben, db_reason)) db_reason = get_reason_by_action( ReputationReasons.first_argument_click) self.assertFalse(add_reputation_for(self.user_torben, db_reason)) DBDiscussionSession.query(ReputationHistory).filter_by( reputator_uid=9).delete() DBDiscussionSession.flush() transaction.commit()
def __keep_the_element_of_optimization_review( db_review: ReviewOptimization, main_page: str, translator: Translator): """ Adds row for LastReviewerOptimization :param db_review: ReviewOptimization :param main_page: URL :param translator: Translator :return: None """ # add new vote db_user_created_flag = DBDiscussionSession.query(User).get( db_review.detector_uid) # get all keep and delete votes db_keep_version = DBDiscussionSession.query( LastReviewerOptimization).filter( LastReviewerOptimization.review_uid == db_review.uid, LastReviewerOptimization.is_okay == True).all() if len(db_keep_version) > max_votes: add_reputation_and_send_popup( db_user_created_flag, get_reason_by_action(ReputationReasons.bad_flag), main_page, translator) db_review.set_executed(True) db_review.update_timestamp() DBDiscussionSession.add(db_review) DBDiscussionSession.flush() transaction.commit()
def __add_reputation(db_user: User, db_issue: Issue, url: str, prepared_dict: dict): """ :param db_user: :param db_issue: :param url: :param prepared_dict: :return: """ had_access = has_access_to_review_system(db_user) rep_added = add_reputation_for(db_user, get_reason_by_action(ReputationReasons.first_justification)) if not rep_added: add_reputation_for(db_user, get_reason_by_action(ReputationReasons.new_statement)) broke_limit = has_access_to_review_system(db_user) and not had_access if broke_limit: _t = Translator(db_issue.lang) send_request_for_info_popup_to_socketio(db_user.nickname, _t.get(_.youAreAbleToReviewNow), '/review') prepared_dict['url'] = '{}{}'.format(url, '#access-review')
def test_add_reputation_and_send_popup(self): db_reason = get_reason_by_action(ReputationReasons.success_flag) self.assertFalse( add_reputation_and_send_popup(self.user_torben, db_reason, 'asd', Translator('en'))) DBDiscussionSession.query(ReputationHistory).filter_by( reputator_uid=9).delete() DBDiscussionSession.flush() transaction.commit()
def set_position(db_user: User, db_issue: Issue, statement_text: str) -> dict: """ Set new position for current discussion and returns collection with the next url for the discussion. :param statement_text: The text of the new position statement. :param db_issue: The issue which gets the new position :param db_user: The user who sets the new position. :rtype: dict :return: Prepared collection with statement_uids of the new positions and next url or an error """ LOG.debug("%s", statement_text) user.update_last_action(db_user) new_statement: Statement = insert_as_statement(statement_text, db_user, db_issue, is_start=True) _um = UrlManager(db_issue.slug) url = _um.get_url_for_statement_attitude(new_statement.uid) rep_added = add_reputation_for( db_user, get_reason_by_action(ReputationReasons.first_position)) had_access = has_access_to_review_system(db_user) if not rep_added: add_reputation_for( db_user, get_reason_by_action(ReputationReasons.new_statement)) broke_limit = has_access_to_review_system(db_user) and not had_access if broke_limit: url += '#access-review' return { 'status': 'success', 'url': url, 'statement_uids': [new_statement.uid], 'error': '' }
def test_get_reputation_reason_by_action(self): for action in ReputationReasons.list(): db_reason = get_reason_by_action(action) self.assertIsNotNone(db_reason) self.assertTrue(db_reason.points != 0)
def test_get_reason_by_action(self): for reason in ReputationReasons: self.assertIsNotNone(get_reason_by_action(reason))
def test_add_reputation_for_anonymous(self): db_reason = get_reason_by_action( ReputationReasons.first_argument_click) self.assertFalse(add_reputation_for(self.user_anonymous, db_reason))