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 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 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 send_add_argument_notification(url, attacked_argument_uid, user, mailer): """ Sends an notification because an argument was added :param url: String :param attacked_argument_uid: Argument.uid :param user: User :param mailer: Instance of pyramid mailer :return: """ # getting current argument, arguments author, current user and some settings db_argument = DBDiscussionSession.query(Argument).get( attacked_argument_uid) db_author = DBDiscussionSession.query(User).get(db_argument.author_uid) db_current_user = DBDiscussionSession.query(User).filter_by( nickname=user).first() if db_author == db_current_user: return None db_author_settings = db_author.settings user_lang = DBDiscussionSession.query(Language).get( db_author_settings.lang_uid).ui_locales # send notification via websocket to last author _t_user = Translator(user_lang) if db_author_settings.should_send_notifications: send_request_for_info_popup_to_socketio(db_author.nickname, _t_user.get(_.argumentAdded), url) # send mail to last author if db_author_settings.should_send_mails: email_helper.send_mail_due_to_added_text(user_lang, url, db_author, mailer) # find admin db_admin = DBDiscussionSession.query(User).filter_by( nickname=nick_of_admin).first() topic = _t_user.get(_.argumentAdded) content = get_text_for_add_argument_message(db_author.firstname, user_lang, url, True) DBDiscussionSession.add( Message(from_author_uid=db_admin.uid, to_author_uid=db_author.uid, topic=topic, content=content, is_inbox=True)) 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 __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 send_notification(from_user, to_user, topic, content, mainpage): """ Sends message to an user and places a copy in the outbox of current user. Returns the uid and timestamp :param from_user: User :param to_user: User :param topic: String :param content: String :param mainpage: String :return: """ content = escape_string(content) notification_in = Message(from_author_uid=from_user.uid, to_author_uid=to_user.uid, topic=topic, content=content, is_inbox=True) notification_out = Message(from_author_uid=from_user.uid, to_author_uid=to_user.uid, topic=topic, content=content, is_inbox=False, read=True) DBDiscussionSession.add_all([notification_in, notification_out]) DBDiscussionSession.flush() transaction.commit() db_settings = to_user.settings if db_settings.should_send_notifications: user_lang = DBDiscussionSession.query(Language).get( db_settings.lang_uid).ui_locales _t_user = Translator(user_lang) send_request_for_info_popup_to_socketio(to_user.nickname, _t_user.get(_.newNotification), mainpage + '/notifications', increase_counter=True) db_inserted_notification = DBDiscussionSession.query(Message).filter( Message.from_author_uid == from_user.uid, Message.to_author_uid == to_user.uid, Message.topic == topic, Message.content == content, Message.is_inbox == True).order_by(Message.uid.desc()).first() return db_inserted_notification
def add_reputation_and_send_popup(db_user: User, db_rep_reason: Optional[ReputationReason], main_page: str, translator: Translator) -> bool: """ Adds reputation to a specific user and checks (send info popup) to this user. Returns true if the user now has access :param db_user: user, which should get reputation :param db_rep_reason: Any reputation reason :param main_page: URL of the app :param translator: Instance of a translator :return: """ has_access = has_access_to_review_system(db_user) add_reputation_for(db_user, db_rep_reason) # send popup if the user had not access but not she has if not has_access and has_access_to_review_system(db_user): send_request_for_info_popup_to_socketio(db_user.nickname, translator.get(_.youAreAbleToReviewNow), f'{main_page}/review') return True return False
def send_add_text_notification(url, conclusion_id, db_user: User, mailer): """ Send notifications and mails to related users. :param url: current url :param conclusion_id: Statement.uid :param db_user: current users nickname :param mailer: Instance of pyramid mailer :return: None """ # getting all text versions, the overview author, last editor and settings ob both authors as well as their languages db_textversions = DBDiscussionSession.query(TextVersion).filter_by( statement_uid=conclusion_id).all() db_root_author = DBDiscussionSession.query(User).get( db_textversions[0].author_uid) db_last_editor = DBDiscussionSession.query(User).get( db_textversions[-1].author_uid) db_root_author_settings = db_root_author.settings db_last_editor_settings = db_last_editor.settings root_lang = DBDiscussionSession.query(Language).get( db_root_author_settings.lang_uid).ui_locales editor_lang = DBDiscussionSession.query(Language).get( db_last_editor_settings.lang_uid).ui_locales _t_editor = Translator(editor_lang) _t_root = Translator(root_lang) # send mail to overview author if db_root_author_settings.should_send_mails \ and db_user != db_root_author: email_helper.send_mail_due_to_added_text(root_lang, url, db_root_author, mailer) # send mail to last author if db_last_editor_settings.should_send_mails \ and db_last_editor != db_root_author \ and db_last_editor != db_user: email_helper.send_mail_due_to_added_text(editor_lang, url, db_last_editor, mailer) # send notification via websocket to overview author if db_root_author_settings.should_send_notifications and db_root_author != db_user: send_request_for_info_popup_to_socketio(db_root_author.nickname, _t_root.get(_.statementAdded), url, increase_counter=True) # send notification via websocket to last author if db_last_editor_settings.should_send_notifications \ and db_last_editor != db_root_author \ and db_last_editor != db_user: send_request_for_info_popup_to_socketio(db_last_editor.nickname, _t_editor.get( _.statementAdded), url, increase_counter=True) # find admin, because generic mails are being sent by the admin db_admin = user_handler.get_list_of_admins()[0] # get topic and content for messages to both authors topic1 = _t_root.get(_.statementAdded) content1 = get_text_for_message(db_root_author.firstname, root_lang, url, _.statementAddedMessageContent, True) topic2 = _t_editor.get(_.statementAdded) content2 = get_text_for_message(db_last_editor.firstname, editor_lang, url, _.statementAddedMessageContent, True) if db_root_author != db_user: DBDiscussionSession.add( Message(from_author_uid=db_admin.uid, to_author_uid=db_root_author.uid, topic=topic1, content=content1, is_inbox=True)) if db_root_author != db_last_editor and db_user != db_last_editor: DBDiscussionSession.add( Message(from_author_uid=db_admin.uid, to_author_uid=db_last_editor.uid, topic=topic2, content=content2, is_inbox=True)) DBDiscussionSession.flush() transaction.commit()
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
def send_edit_text_notification(db_user, textversion, path, mailer): """ Sends an notification to the root-author and last author, when their text was edited. :param db_user: Current User :param textversion: new Textversion :param path: curren path :param mailer: Instance of pyramid mailer :return: None """ all_textversions = DBDiscussionSession.query(TextVersion).filter_by( statement_uid=textversion.statement_uid).order_by( TextVersion.uid.desc()).all() # TODO #432 oem = all_textversions[-1] root_author = oem.author_uid new_author = textversion.author_uid last_author = all_textversions[-2].author_uid if len( all_textversions) > 1 else root_author settings_root_author = root_author.settings settings_last_author = last_author.settings # create content db_editor = DBDiscussionSession.query(User).get(new_author) db_settings = db_editor.settings editor_ui_locales = db_settings.lang # add some information for highlights if path is not None: path += '?edited_statement=' + str(textversion.statement_uid) if settings_root_author.should_send_mails is True \ and root_author != db_user.uid \ and path is not None: email_helper.send_mail_due_to_edit_text(textversion.statement_uid, root_author, db_editor, path, mailer) if new_author != last_author \ and settings_last_author.should_send_mails is True \ and new_author != db_user.uid \ and path is not None: email_helper.send_mail_due_to_edit_text(textversion.statement_uid, last_author, db_editor, path, mailer) # check for different authors if root_author == new_author: return None # send notifications user_lang1 = DBDiscussionSession.query(Language).get( settings_root_author.lang_uid).ui_locales user_lang2 = DBDiscussionSession.query(Language).get( settings_last_author.lang_uid).ui_locales if settings_root_author.should_send_notifications \ and root_author != db_user.uid: _t_user = Translator(user_lang1) db_root_author = DBDiscussionSession.query(User).get(root_author) send_request_for_info_popup_to_socketio(db_root_author.nickname, _t_user.get(_.textChange), path, increase_counter=True) if last_author != root_author \ and last_author != new_author \ and last_author != db_user.uid \ and settings_last_author.should_send_notifications: _t_user = Translator(user_lang2) db_last_author = DBDiscussionSession.query(User).get(last_author) send_request_for_info_popup_to_socketio(db_last_author.nickname, _t_user.get(_.textChange), path, increase_counter=True) _t1 = Translator(user_lang1) topic1 = _t1.get(_.textversionChangedTopic) content1 = get_text_for_edit_text_message(editor_ui_locales, db_editor.public_nickname, textversion.content, oem.content, path) _t2 = Translator(user_lang2) topic2 = _t2.get(_.textversionChangedTopic) content2 = get_text_for_edit_text_message(editor_ui_locales, db_editor.public_nickname, textversion.content, oem.content, path) notifications = [] if new_author != root_author: notifications.append( Message(from_author_uid=new_author, to_author_uid=root_author, topic=topic1, content=content1, is_inbox=True)) if new_author != last_author: notifications.append( Message(from_author_uid=new_author, to_author_uid=last_author, topic=topic2, content=content2, is_inbox=True)) if len(notifications) > 0: DBDiscussionSession.add_all(notifications) DBDiscussionSession.flush()