Ejemplo n.º 1
0
    def test_get_author_or_first_supporter_of_element(self):
        arg, user1, user2 = 1, 2, 3

        self.assertIsNone(
            tg.get_author_or_first_supporter_of_element(arg, user1, True))

        DBDiscussionSession.add(MarkedArgument(argument=arg, user=user1))
        transaction.commit()
        self.assertIsNone(
            tg.get_author_or_first_supporter_of_element(arg, user1, True))
        self.assertIsNotNone(
            tg.get_author_or_first_supporter_of_element(arg, user2, True))

        DBDiscussionSession.add(MarkedArgument(argument=arg, user=user2))
        transaction.commit()
        self.assertIsNotNone(
            tg.get_author_or_first_supporter_of_element(arg, user1, True))
        self.assertIsNotNone(
            tg.get_author_or_first_supporter_of_element(arg, user2, True))

        DBDiscussionSession.query(MarkedArgument).filter_by(
            argument_uid=arg, author_uid=user1).delete()
        DBDiscussionSession.query(MarkedArgument).filter_by(
            argument_uid=arg, author_uid=user2).delete()
        transaction.commit()

        self.assertIsNone(
            tg.get_author_or_first_supporter_of_element(arg, user1, True))
        self.assertIsNone(
            tg.get_author_or_first_supporter_of_element(arg, user2, True))
Ejemplo n.º 2
0
def __mark_or_unmark_it(stmt_or_arg: Union[Statement, Argument], should_mark,
                        db_user, _t):
    """
    Marks or unmark an argument/statement, which represents the users opinion

    :param stmt_or_arg: Statement.uid / Argument.uid
    :param should_mark: Boolean
    :param db_user: User
    :param _t: Translator
    :return: String, String
    """
    logger('QueryHelper', '{} {}'.format(stmt_or_arg.uid, db_user.nickname))

    is_argument = isinstance(stmt_or_arg, Argument)
    table = MarkedArgument if is_argument else MarkedStatement
    column = MarkedArgument.argument_uid if is_argument else MarkedStatement.statement_uid

    if should_mark:
        db_el = DBDiscussionSession.query(table).filter(
            column == stmt_or_arg.uid).first()
        logger('QueryHelper',
               'Element is present{}'.format(' now' if db_el else ''))
        if not db_el:
            new_el = MarkedArgument(
                argument=stmt_or_arg.uid,
                user=db_user.uid) if is_argument else MarkedStatement(
                    statement=stmt_or_arg.uid, user=db_user.uid)
            DBDiscussionSession.add(new_el)
    else:
        logger('QueryHelper', 'Element is deleted')
        DBDiscussionSession.query(table).filter(
            column == stmt_or_arg.uid).delete()

    DBDiscussionSession.flush()
    transaction.commit()

    return {'success': _t.get(_.opinionSaved), 'error': ''}
Ejemplo n.º 3
0
def __mark_or_unmark_it(stmt_or_arg: Union[Statement, Argument], should_mark,
                        db_user, _t):
    """
    Marks or unmark an argument/statement, which represents the users opinion

    :param stmt_or_arg: Statement.uid / Argument.uid
    :param should_mark: Boolean
    :param db_user: User
    :param _t: Translator
    :return: String, String
    """
    LOG.debug("statement oder argument id: %s, user: %s", stmt_or_arg.uid,
              db_user.nickname)

    is_argument = isinstance(stmt_or_arg, Argument)
    table = MarkedArgument if is_argument else MarkedStatement
    column = MarkedArgument.argument_uid if is_argument else MarkedStatement.statement_uid

    if should_mark:
        db_el = DBDiscussionSession.query(table).filter(
            column == stmt_or_arg.uid).first()
        if not db_el:
            LOG.debug("Element is not present")
            new_el = MarkedArgument(
                argument=stmt_or_arg.uid,
                user=db_user.uid) if is_argument else MarkedStatement(
                    statement=stmt_or_arg.uid, user=db_user.uid)
            DBDiscussionSession.add(new_el)
    else:
        LOG.debug("ELement is deleted")
        DBDiscussionSession.query(table).filter(
            column == stmt_or_arg.uid).delete()

    DBDiscussionSession.flush()

    return {'success': _t.get(_.opinionSaved), 'error': ''}
Ejemplo n.º 4
0
def __process_input_premises_for_arguments_and_receive_url(
        langs: dict, arg_infos: dict, db_issue: Issue, db_user: User,
        mailer) -> Tuple[Optional[str], Optional[List[int]], str]:
    """
    Inserts given text in premisegroups as new arguments in dependence of the parameters and returns a URL

    :param langs: dict with default_locale_name and discussion_lang
    :param arg_infos: dict with arg_id, attack_type, premisegroups and the history
    :param db_issue: Issue
    :param db_user: User
    :return: URL, [Statement.uids], String
    """
    discussion_lang = langs['discussion_lang']
    arg_id: int = arg_infos['arg_id']
    attack_type: str = arg_infos['attack_type']
    premisegroups = arg_infos['premisegroups']
    history = arg_infos['history']

    LOG.debug("Count of new pgroups: %s", len(premisegroups))
    _tn = Translator(discussion_lang)

    slug = db_issue.slug
    error = ''

    # insert all premise groups into our database
    # all new arguments are collected in a list
    new_argument_uids = []
    for premisegroup in premisegroups:  # premise groups is a list of lists
        new_argument = insert_new_premises_for_argument(
            premisegroup, attack_type, arg_id, db_issue, db_user)
        if not isinstance(new_argument, Argument):  # break on error
            a = _tn.get(_.notInsertedErrorBecauseEmpty)
            b = _tn.get(_.minLength)
            c = environ.get('MIN_LENGTH_OF_STATEMENT', 10)
            d = _tn.get(_.eachStatement)
            if isinstance(new_argument, str):
                error = new_argument
            else:
                error = '{} ({}: {} {})'.format(a, b, c, d)
            return None, None, error

        new_argument_uids.append(new_argument.uid)

    statement_uids = []
    # @OPTIMIZE
    # Query all recently stored premises (internally: statements) and collect their ids
    # This is a bad workaround, let's just think about it in future.
    for uid in new_argument_uids:
        current_pgroup = DBDiscussionSession.query(Argument).get(
            uid).premisegroup_uid
        current_premises = DBDiscussionSession.query(Premise).filter_by(
            premisegroup_uid=current_pgroup).all()
        for premise in current_premises:
            statement_uids.append(premise.statement_uid)

    # #arguments=0: empty input
    # #arguments=1: deliver new url
    # #arguments>1: deliver url where the nickname has to choose between her inputs
    _um = url = UrlManager(slug, history)
    if len(new_argument_uids) == 0:
        a = _tn.get(_.notInsertedErrorBecauseEmpty)
        b = _tn.get(_.minLength)
        c = environ.get('MIN_LENGTH_OF_STATEMENT', 10)
        error = '{} ({}: {})'.format(a, b, c)

    elif len(new_argument_uids) == 1:
        url = _um.get_url_for_new_argument(new_argument_uids)

    else:
        url = __receive_url_for_processing_input_of_multiple_premises_for_arguments(
            new_argument_uids, _um)

    # send notifications and mails
    if len(new_argument_uids) > 0:
        # add marked arguments
        DBDiscussionSession.add_all([
            MarkedArgument(argument=uid, user=db_user.uid)
            for uid in new_argument_uids
        ])
        DBDiscussionSession.flush()
        transaction.commit()

        new_uid = random.choice(new_argument_uids)  # TODO eliminate random
        attack = get_relation_between_arguments(arg_id, new_uid)

        tmp_url = _um.get_url_for_reaction_on_argument(arg_id, attack, new_uid)

        nh.send_add_argument_notification(tmp_url, arg_id, db_user.nickname,
                                          mailer)

    return url, statement_uids, error