Ejemplo n.º 1
0
    def def_get_statement_count_of(self):
        engelbert = DBDiscussionSession.query(User).filter_by(
            nickname='Engelbert').first()
        self.assertEqual(user.get_statement_count_of(None, True), 0)
        self.assertEqual(user.get_statement_count_of(None, False), 0)
        self.assertEqual(user.get_statement_count_of(engelbert, True), 0)
        self.assertEqual(user.get_statement_count_of(engelbert, False), 0)

        # add statement
        db_st = Statement(True)
        DBDiscussionSession.add(db_st)
        DBDiscussionSession.flush()
        transaction.commit()
        db_tv = TextVersion('hello, here i am', engelbert.uid, db_st.uid)
        DBDiscussionSession.add(db_tv)
        DBDiscussionSession.flush()
        transaction.commit()
        self.assertEqual(1, user.get_statement_count_of(engelbert, True))
        self.assertEqual(1, user.get_statement_count_of(engelbert, False))

        # set older timestamp
        db_tv.timestamp = DBDiscussionSession.query(TextVersion).get(
            1).timestamp
        DBDiscussionSession.add(db_tv)
        DBDiscussionSession.flush()
        transaction.commit()
        self.assertEqual(0, user.get_statement_count_of(engelbert, True))
        self.assertEqual(1, user.get_statement_count_of(engelbert, False))
Ejemplo n.º 2
0
def _position_edge(position: Statement):
    """
    Generates the d3 representation of an edge between a position and the issue.
    :param position:
    :return:
    """
    return {
        'id': f'edge_{position.uid}_issue',
        'label': position.get_text(),
        'type': 'position',
        'timestamp': position.get_first_timestamp().timestamp,
        'edge_type': 'arrow',
        'color': 'grey',
        'target': 'issue',
        'source': f'statement_{position.uid}'
    }
Ejemplo n.º 3
0
def __append_extras_dict_with_flag(request: Request, db_user: User,
                                   db_issue: Issue, db_statement: Statement,
                                   pdict: dict):
    """

    :param request:
    :param db_user:
    :param db_issue:
    :param db_statement:
    :param pdict:
    :return:
    """
    item_len = len(pdict['items']['elements'])
    _dh = DictionaryHelper(get_language_from_cookie(request), db_issue.lang)
    extras_dict = _dh.prepare_extras_dict(db_issue.slug,
                                          True,
                                          True,
                                          True,
                                          request.registry,
                                          request.application_url,
                                          request.path,
                                          db_user=db_user)

    if item_len == 0:  # is the discussion at the end?
        _dh.add_discussion_end_text(pdict['discussion'],
                                    extras_dict,
                                    db_user.nickname,
                                    at_dont_know=True,
                                    current_premise=db_statement.get_text())
    pdict['extras'] = extras_dict
Ejemplo n.º 4
0
def __append_extras_dict_without_flag(request: Request, db_user: User,
                                      db_issue: Issue, db_statement: Statement,
                                      pdict: dict, attitude: Attitudes):
    """

    :param request:
    :param db_user:
    :param db_issue:
    :param db_statement:
    :param pdict:
    :param attitude:
    :return:
    """
    item_len = len(pdict['items']['elements'])
    _dh = DictionaryHelper(get_language_from_cookie(request), db_issue.lang)
    supportive = attitude in [Attitudes.AGREE, Attitudes.DONT_KNOW]
    logged_in = db_user is not None and db_user.nickname != nick_of_anonymous_user
    extras_dict = _dh.prepare_extras_dict(db_issue.slug, False, True, True,
                                          request.registry,
                                          request.application_url,
                                          request.path, db_user)

    if item_len == 0 or item_len == 1 and logged_in:  # is the discussion at the end?
        _dh.add_discussion_end_text(pdict['discussion'],
                                    extras_dict,
                                    db_user.nickname,
                                    at_justify=True,
                                    current_premise=db_statement.get_text(),
                                    supportive=supportive)
    pdict['extras'] = extras_dict
Ejemplo n.º 5
0
def __revoke_statement(db_statement: Statement, db_user: User):
    """
    Revokes the user as author of the statement

    :param db_user: User
    :param db_statement: Statement
    :return: Statement, Boolean, String
    """
    LOG.debug("Statement %s will be revoked (old author: %s)",
              db_statement.uid, db_user.uid)
    __remove_user_from_arguments_with_statement(db_statement, db_user)

    db_anonymous = DBDiscussionSession.query(User).filter_by(
        nickname=nick_of_anonymous_user).first()
    LOG.debug("Statement %s will get a new author %s (old author: %s)",
              db_statement.uid, db_anonymous.uid, db_user.uid)

    db_statement.author_uid = db_anonymous.uid
    __transfer_textversion_to_new_author(db_statement.uid, db_user.uid,
                                         db_anonymous.uid)

    DBDiscussionSession.add(db_statement)
    DBDiscussionSession.flush()

    return db_statement
Ejemplo n.º 6
0
def __is_conclusion_in_premisegroups(premisegroups: list,
                                     db_conclusion: Statement) -> bool:
    for premisegroup in premisegroups:
        if any([
                db_conclusion.get_textversion().content.lower() in pg.lower()
                for pg in premisegroup
        ]):
            return True
    return False
Ejemplo n.º 7
0
def __append_extras_dict_during_justification_statement(
        request: Request, db_user: User, db_issue: Issue,
        db_statement: Statement, pdict: dict, attitude: Attitudes):
    system_lang = get_language_from_cookie(request)
    supportive = attitude in [Attitudes.AGREE, Attitudes.DONT_KNOW]
    item_len = len(pdict['items']['elements'])
    _dh = DictionaryHelper(system_lang, db_issue.lang)
    logged_in = (db_user
                 and db_user.nickname != nick_of_anonymous_user) is not None

    if attitude in (Attitudes.AGREE, Attitudes.DISAGREE):
        extras_dict = _dh.prepare_extras_dict(db_issue.slug, False, True, True,
                                              request.registry,
                                              request.application_url,
                                              request.path, db_user)
        if item_len == 0 or item_len == 1 and logged_in:
            _dh.add_discussion_end_text(
                pdict['discussion'],
                extras_dict,
                db_user.nickname,
                at_justify=True,
                current_premise=db_statement.get_text(),
                supportive=supportive)

    else:
        extras_dict = _dh.prepare_extras_dict(db_issue.slug,
                                              True,
                                              True,
                                              True,
                                              request.registry,
                                              request.application_url,
                                              request.path,
                                              db_user=db_user)
        # is the discussion at the end?
        if item_len == 0:
            _dh.add_discussion_end_text(
                pdict['discussion'],
                extras_dict,
                db_user.nickname,
                at_dont_know=True,
                current_premise=db_statement.get_text())

    pdict['extras'] = extras_dict
Ejemplo n.º 8
0
def __add_statement(is_position: bool) -> Statement:
    """
    Adds a new statement to the database

    :param is_position: True if the statement should be a position
    :return: New statement object
    """
    db_statement = Statement(is_position=is_position)
    DBDiscussionSession.add(db_statement)
    DBDiscussionSession.flush()
    return db_statement
Ejemplo n.º 9
0
def get_all_arguments_with_text_and_url_by_statement(db_statement: Statement,
                                                     urlmanager,
                                                     color_statement=False,
                                                     is_jump=False):
    """
    Given a statement_uid, it returns all arguments, which use this statement and adds
    the corresponding text to it, which normally appears in the bubbles. The resulting
    text depends on the provided language.

    :param db_statement: Statement
    :param urlmanager:
    :param color_statement: True, if the statement (specified by the ID) should be colored
    :return: list of dictionaries containing some properties of these arguments
    :rtype: list
    """
    LOG.debug("main %s", db_statement.uid)
    arguments = get_all_arguments_by_statement(db_statement.uid)
    uids = [arg.uid for arg in arguments] if arguments else None
    results = list()
    sb = '<{} data-argumentation-type="position">'.format(
        tag_type) if color_statement else ''
    se = '</{}>'.format(tag_type) if color_statement else ''

    if not uids:
        return []

    uids.sort()
    for uid in uids:
        statement_text = db_statement.get_text()
        attack_type = 'jump' if is_jump else ''
        argument_text = get_text_for_argument_uid(uid,
                                                  anonymous_style=True,
                                                  attack_type=attack_type)
        pos = argument_text.lower().find(statement_text.lower())

        argument_text = argument_text[:pos] + sb + argument_text[pos:]
        pos += len(statement_text) + len(sb)
        argument_text = argument_text[:pos] + se + argument_text[pos:]

        results.append({
            'uid': uid,
            'text': argument_text,
            'url': urlmanager.get_url_for_jump(uid)
        })
    return results
Ejemplo n.º 10
0
def set_statement(text: str, db_user: User, is_start: bool, db_issue: Issue) -> Tuple[Statement, bool]:
    """
    Saves statement for user

    :param text: given statement
    :param db_user: User of given user
    :param is_start: if it is a start statement
    :param db_issue: Issue
    :return: Statement, is_duplicate or -1, False on error
    """

    logger('StatementsHelper', 'user: '******', user_id: ' + str(db_user.uid) +
           ', text: ' + str(text) + ', issue: ' + str(db_issue.uid))

    # escaping and cleaning
    text = text.strip()
    text = ' '.join(text.split())
    text = escape_string(text)
    _tn = Translator(db_issue.lang)
    if text.startswith(_tn.get(_.because).lower() + ' '):
        text = text[len(_tn.get(_.because) + ' '):]
    while text.endswith(('.', '?', '!', ',')):
        text = text[:-1]

    # check, if the text already exists
    db_duplicate = DBDiscussionSession.query(TextVersion).filter(
        func.lower(TextVersion.content) == text.lower()).first()
    if db_duplicate:
        db_statement = DBDiscussionSession.query(Statement).filter(Statement.uid == db_duplicate.statement_uid,
                                                                   Statement.issue_uid == db_issue.uid).one()
        return db_statement, True

    # add text
    statement = Statement(is_position=is_start, issue=db_issue.uid)
    DBDiscussionSession.add(statement)
    DBDiscussionSession.flush()

    # add textversion
    textversion = TextVersion(content=text, author=db_user.uid, statement_uid=statement.uid)
    DBDiscussionSession.add(textversion)
    DBDiscussionSession.flush()

    transaction.commit()
    return statement, False
Ejemplo n.º 11
0
    def get_dict_for_attitude(self, db_position: Statement) -> dict:
        """
        Prepares the discussion dict with all bubbles for the second step in discussion,
        where the user chooses her attitude.

        :param uid: Argument.uid
        :param db_position: Statement
        :return: dict()
        """
        logger('DictionaryHelper', 'at_attitude')
        _tn = Translator(self.lang)
        add_premise_text = ''
        save_statement_url = 'set_new_start_statement'
        statement_text = db_position.get_html()

        if self.lang != 'de':
            pos = len('<' + tag_type + ' data-argumentation-type="position">')
            statement_text = statement_text[0:pos +
                                            1].lower() + statement_text[pos +
                                                                        1:]

        text = _tn.get(_.whatDoYouThinkAbout)
        text += ' ' + statement_text + '?'
        bubble = create_speechbubble_dict(BubbleTypes.SYSTEM,
                                          content=text,
                                          omit_bubble_url=True,
                                          lang=self.lang)

        bubbles_array = [bubble]

        return {
            'bubbles': bubbles_array,
            'add_premise_text': add_premise_text,
            'save_statement_url': save_statement_url,
            'mode': '',
            'broke_limit': self.broke_limit
        }
Ejemplo n.º 12
0
    def get_dict_for_justify_statement(self, db_statement: Statement, slug,
                                       is_supportive, count_of_items,
                                       db_user: User):
        """
        Prepares the discussion dict with all bubbles for the third step in discussion,
        where the user justifies his position.

        :param db_statement: Statement
        :param db_user: User
        :param slug: Issue.slug
        :param is_supportive: Boolean
        :param count_of_items: Integer
        :return: dict()
        """
        LOG.debug("Entering get_dict_for_justify_statement")
        _tn = Translator(self.lang)

        bubbles_array = history_handler.create_bubbles(self.history,
                                                       self.nickname,
                                                       self.lang, self.slug)

        save_statement_url = 'set_new_start_statement'
        text = db_statement.get_text()
        if not text:
            return None

        tag_start = '<{} data-argumentation-type="position">'.format(tag_type)
        tag_end = '</{}>'.format(tag_type)

        # system bubble
        system_question = get_system_bubble_text_for_justify_statement(
            is_supportive, _tn, tag_start, text, tag_end)

        # user bubble
        nickname = db_user.nickname if db_user and db_user.nickname != nick_of_anonymous_user else None
        user_text, add_premise_text = get_user_bubble_text_for_justify_statement(
            db_statement.uid, db_user, is_supportive, _tn)

        question_bubble = create_speechbubble_dict(BubbleTypes.SYSTEM,
                                                   content=system_question,
                                                   omit_bubble_url=True,
                                                   lang=self.lang)
        url = UrlManager(slug).get_url_for_statement_attitude(db_statement.uid)
        select_bubble = create_speechbubble_dict(
            BubbleTypes.USER,
            bubble_url=url,
            content=user_text,
            omit_bubble_url=False,
            statement_uid=db_statement.uid,
            is_supportive=is_supportive,
            db_user=db_user,
            lang=self.lang)

        if not bubbles_already_last_in_list(bubbles_array, select_bubble):
            bubbles_array.append(select_bubble)

        self.__append_now_bubble(bubbles_array)

        if not bubbles_already_last_in_list(bubbles_array, question_bubble):
            bubbles_array.append(question_bubble)

        if not self.nickname and count_of_items == 1:
            _t = Translator(self.lang)
            db_user = DBDiscussionSession.query(User).filter_by(
                nickname=nickname).first()
            msg_dict = {
                'm': _.voteCountTextFirstM,
                'f': _.voteCountTextFirstF,
                'n': _.voteCountTextFirst,
            }

            if db_user:
                msg = msg_dict[db_user.gender]
            else:
                msg = _.voteCountTextFirst

            msg = _t.get(msg) + '.'

            bubbles_array.append(
                create_speechbubble_dict(BubbleTypes.INFO,
                                         uid='now_first',
                                         content=msg +
                                         _tn.get(_.onlyOneItemWithLink),
                                         omit_bubble_url=True,
                                         lang=self.lang))
        return {
            'bubbles': bubbles_array,
            'add_premise_text': add_premise_text,
            'save_statement_url': save_statement_url,
            'mode': '',
            'is_supportive': is_supportive,
            'broke_limit': self.broke_limit
        }