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 """ logger('StatementsHelper', statement_text) user.update_last_action(db_user) new_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) add_rep, broke_limit = add_reputation_for(db_user, rep_reason_first_position) if not add_rep: add_rep, broke_limit = add_reputation_for(db_user, rep_reason_new_statement) # send message if the user is now able to review if broke_limit: url += '#access-review' return { 'status': 'success', 'url': 'url', 'statement_uids': [new_statement.uid], 'error': '' }
def add_review_opinion_for_edit(db_user, main_page, db_review, is_edit_okay, _t): """ :param db_user: :param main_page: :param db_review: :param is_edit_okay: :param _t: :return: """ logger('review.opinions', 'main') db_user_created_flag = DBDiscussionSession.query(User).get( db_review.detector_uid) broke_limit = False # add new vote __add_vote_for(db_user, db_review, is_edit_okay, LastReviewerEdit) # get all keep and delete votes count_of_edit, count_of_dont_edit = __get_review_count( LastReviewerEdit, 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 __accept_edit_review(db_review) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_success_edit) else: # just close the review add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_bad_edit) db_review.set_executed(True) db_review.update_timestamp() elif count_of_edit - count_of_dont_edit >= min_difference: # accept the edit __accept_edit_review(db_review) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_success_edit) db_review.set_executed(True) db_review.update_timestamp() elif count_of_dont_edit - count_of_edit >= min_difference: # decline edit add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_bad_edit) db_review.set_executed(True) db_review.update_timestamp() DBDiscussionSession.add(db_review) DBDiscussionSession.flush() transaction.commit() if broke_limit: send_request_for_info_popup_to_socketio( db_user_created_flag.nickname, _t.get(_.youAreAbleToReviewNow), main_page + '/review') return ''
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 handle_justification_argument(db_issue: Issue, db_user: User, db_argument: Argument, attitude: str, relation: str, history, path) -> Tuple[dict, dict]: """ :param db_issue: :param db_user: :param db_argument: :param attitude: :param relation: :param history: :param path: :return: """ logger('ViewHelper', 'justify argument') ui_locales = db_issue.lang nickname = db_user.nickname supportive = attitude in [Attitudes.AGREE, Attitudes.DONT_KNOW] item_dict, discussion_dict = preparation_for_justify_argument( db_issue, db_user, db_argument, relation, supportive, history, path) add_rep, broke_limit = add_reputation_for(nickname, rep_reason_first_confrontation) if broke_limit: _t = Translator(ui_locales) send_request_for_info_popup_to_socketio( nickname, _t.get(_.youAreAbleToReviewNow), '/review') return item_dict, discussion_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 reaction(db_issue: Issue, db_user: User, db_arg_user: Argument, db_arg_sys: Argument, relation: Relations, history, path) -> dict: """ Initialize the reaction step for a position in a discussion. Creates helper and returns a dictionary containing different feedback options for the confrontation with an argument in a discussion. :param db_issue: :param db_user: :param db_arg_user: :param db_arg_sys: :param relation: :param history: :param path: :return: """ logger('Core', 'Entering discussion.reaction') # set votes and reputation add_rep, broke_limit = add_reputation_for(db_user, reason=rep_reason_first_argument_click) add_click_for_argument(db_arg_user, db_user) _ddh = DiscussionDictHelper(db_issue.lang, db_user.nickname, history, slug=db_issue.slug, broke_limit=broke_limit) _idh = ItemDictHelper(db_issue.lang, db_issue, path=path, history=history) discussion_dict = _ddh.get_dict_for_argumentation(db_arg_user, db_arg_sys.uid, relation, history, db_user) item_dict = _idh.get_array_for_reaction(db_arg_sys.uid, db_arg_user.uid, db_arg_user.is_supportive, relation, discussion_dict['gender']) return { 'issues': issue_helper.prepare_json_of_issue(db_issue, db_user), 'discussion': discussion_dict, 'items': item_dict, 'title': db_issue.title }
def __keep_the_element_of_optimization_review(db_review, application_url, _t): """ Adds row for LastReviewerOptimization :param db_review: ReviewOptimization :param application_url: URL :param _t: Translator :return: None """ # add new vote db_user_who_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_rep, broke_limit = add_reputation_for(db_user_who_created_flag, rep_reason_bad_flag) if broke_limit: send_request_for_info_popup_to_socketio( db_user_who_created_flag.nickname, _t.get(_.youAreAbleToReviewNow), application_url + '/review') 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: """ add_rep, broke_limit = add_reputation_for(db_user, rep_reason_first_justification) if not add_rep: add_rep, broke_limit = add_reputation_for(db_user, rep_reason_new_statement) # send message if the user is now able to review 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 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 data: dict if requests came via the API :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) user.update_last_action(db_user) prepared_dict = {'error': error, 'statement_uids': statement_uids} if url == -1: return prepared_dict # add reputation add_rep, broke_limit = add_reputation_for(db_user, rep_reason_first_new_argument) if not add_rep: add_rep, broke_limit = add_reputation_for(db_user, rep_reason_new_statement) # send message if the user is now able to review if broke_limit: url += '#access-review' prepared_dict['url'] = url prepared_dict['url'] = url logger('ArgumentsHelper', 'returning {}'.format(prepared_dict)) return prepared_dict
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 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_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))
def add_review_opinion_for_merge(db_user, main_page, db_review, should_merge, _t): """ Adds row to the merge review :param request: Pyramids request object :param review_uid: ReviewMerge :param should_merge: True, if it should be merged :param _t: Translator :return: String """ logger('review.opinions', 'main {}'.format(db_review.uid)) db_user_created_flag = DBDiscussionSession.query(User).get( db_review.detector_uid) # add new vote __add_vote_for(db_user, db_review, should_merge, LastReviewerMerge) broke_limit = False # get all keep and delete votes count_of_keep, count_of_reset = __get_review_count(LastReviewerMerge, db_review.uid) logger('review.opinions', 'result ' + str(count_of_keep) + ':' + str(count_of_reset)) # do we reached any limit? reached_max = max(count_of_keep, count_of_reset) >= max_votes if reached_max: if count_of_keep > count_of_reset: # split pgroup logger('review.opinions', 'max reached for review {}'.format(db_review.uid)) __merge_premisegroup(db_review) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_success_flag) else: # just close the review logger( 'review.opinions', 'max reached / forget about review {}'.format(db_review.uid)) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_bad_flag) db_review.set_executed(True) db_review.update_timestamp() elif count_of_reset - count_of_keep >= min_difference: # just close the review logger('review.opinions', 'vote says forget about review {}'.format(db_review.uid)) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_bad_flag) db_review.set_executed(True) db_review.update_timestamp() elif count_of_keep - count_of_reset >= min_difference: # split pgroup logger('review.opinions', 'vote says merge for review {}'.format(db_review.uid)) __merge_premisegroup(db_review) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_success_flag) db_review.set_executed(True) db_review.update_timestamp() DBDiscussionSession.add(db_review) DBDiscussionSession.flush() transaction.commit() if broke_limit: send_request_for_info_popup_to_socketio( db_user_created_flag.nickname, _t.get(_.youAreAbleToReviewNow), main_page + '/review') return ''
def add_review_opinion_for_delete(db_user, main_page, db_review, should_delete, _t): """ :param db_user: :param main_page: :param review_uid: :param should_delete: :param _t: :return: """ logger('review.opinions', 'main') db_user_created_flag = DBDiscussionSession.query(User).get( db_review.detector_uid) # add new vote __add_vote_for(db_user, db_review, not should_delete, LastReviewerDelete) broke_limit = False # get all keep and delete votes count_of_keep, count_of_delete = __get_review_count( LastReviewerDelete, db_review.uid) logger('review.opinions', 'result ' + str(count_of_keep) + ':' + str(count_of_delete)) # do we reached any limit? reached_max = max(count_of_keep, count_of_delete) >= max_votes if reached_max: if count_of_delete > count_of_keep: # disable the flagged part logger('review.opinions', 'max reached / delete for review {}'.format(db_review.uid)) set_able_object_of_review(db_review, True) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_success_flag) else: # just close the review logger('review.opinions', 'max reached / keep for review {}'.format(db_review.uid)) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_bad_flag) db_review.set_executed(True) db_review.update_timestamp() elif count_of_keep - count_of_delete >= min_difference: # just close the review logger('review.opinions', 'vote says keep for review {}'.format(db_review.uid)) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_bad_flag) db_review.set_executed(True) db_review.update_timestamp() elif count_of_delete - count_of_keep >= min_difference: # disable the flagged part logger('review.opinions', 'vote says delete for review {}'.format(db_review.uid)) set_able_object_of_review(db_review, True) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_success_flag) db_review.set_executed(True) db_review.update_timestamp() DBDiscussionSession.add(db_review) DBDiscussionSession.flush() transaction.commit() if broke_limit: send_request_for_info_popup_to_socketio( db_user_created_flag.nickname, _t.get(_.youAreAbleToReviewNow), main_page + '/review') return True
def add_review_opinion_for_duplicate(db_user, main_page, db_review, is_duplicate, _t): """ Adds row to the duplicate review :param request: Pyramids request object :param is_duplicate: Boolean :param review_uid: ReviewDuplicate.uid :param _t: Translator :return: String """ logger('review.opinions', 'main {}, duplicate {}'.format(db_review.uid, is_duplicate)) db_user_created_flag = DBDiscussionSession.query(User).get( db_review.detector_uid) # add new vote __add_vote_for(db_user, db_review, not is_duplicate, LastReviewerDuplicate) broke_limit = False # get all keep and delete votes count_of_keep, count_of_reset = __get_review_count(LastReviewerDuplicate, db_review.uid) logger('review.opinions', 'result ' + str(count_of_keep) + ':' + str(count_of_reset)) # 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 logger('review.opinions', 'max reached / bend for review {}'.format(db_review.uid)) __bend_objects_of_duplicate_review(db_review) add_rep, broke_limit = add_reputation_for( db_user_created_flag, rep_reason_success_duplicate) else: # just close the review logger( 'review.opinions', 'max reached / forget about review {}'.format(db_review.uid)) add_rep, broke_limit = add_reputation_for( db_user_created_flag, rep_reason_bad_duplicate) db_review.set_executed(True) db_review.update_timestamp() elif count_of_keep - count_of_reset >= min_difference: # just close the review logger('review.opinions', 'vote says forget about review {}'.format(db_review.uid)) add_rep, broke_limit = add_reputation_for(db_user_created_flag, rep_reason_bad_duplicate) db_review.set_executed(True) db_review.update_timestamp() elif count_of_reset - count_of_keep >= min_difference: # disable the flagged part logger('review.opinions', 'vote says bend for review {}'.format(db_review.uid)) __bend_objects_of_duplicate_review(db_review) add_rep, broke_limit = add_reputation_for( db_user_created_flag, rep_reason_success_duplicate) db_review.set_executed(True) db_review.update_timestamp() DBDiscussionSession.add(db_review) DBDiscussionSession.flush() transaction.commit() if broke_limit: send_request_for_info_popup_to_socketio( db_user_created_flag.nickname, _t.get(_.youAreAbleToReviewNow), main_page + '/review') return True