Esempio n. 1
0
def get_issue_dict_for(db_issue: Issue, uid: int, lang: str) -> dict:
    """
    Creates an dictionary for the issue

    :param db_issue: Issue
    :param uid: current selected Issue.uid
    :param lang: ui_locales
    :return: dict()
    """
    _um = UrlManager(db_issue.slug)
    issue_dict = {
        'uid': str(db_issue.uid),
        'slug': db_issue.slug,
        'title': db_issue.title,
        'url': '/' + db_issue.slug,
        'review_url':
        _um.get_review_url() if str(uid) != str(db_issue.uid) else '',
        'info': db_issue.info,
        'stat_count': get_number_of_statements(db_issue.uid),
        'date': sql_timestamp_pretty_print(db_issue.date, lang),
        'author': db_issue.author.public_nickname,
        'error': '',
        'author_url': '/user/{}'.format(db_issue.author.uid),
        'enabled': 'disabled' if str(uid) == str(db_issue.uid) else 'enabled'
    }
    return issue_dict
Esempio n. 2
0
def insert_as_statement(text: str, db_user: User, db_issue: Issue, is_start=False) -> Statement:
    """
    Inserts the given text as statement and returns the uid

    :param text: String
    :param db_user: User
    :param db_issue: Issue
    :param is_start: Boolean
    :return: Statement
    """
    new_statement, is_duplicate = set_statement(text, db_user, is_start, db_issue)

    # add marked statement
    DBDiscussionSession.add(MarkedStatement(statement=new_statement.uid, user=db_user.uid))
    DBDiscussionSession.add(SeenStatement(statement_uid=new_statement.uid, user_uid=db_user.uid))
    DBDiscussionSession.flush()

    _tn = Translator(db_issue.lang)
    _um = UrlManager(db_issue.slug)
    append_action_to_issue_rss(db_issue=db_issue, db_author=db_user,
                               title=_tn.get(_.positionAdded if is_start else _.statementAdded),
                               description='...' + get_text_for_statement_uid(new_statement.uid) + '...',
                               url=_um.get_url_for_statement_attitude(new_statement.uid))

    return new_statement
Esempio n. 3
0
def get_statements_with_value(issue_uid: int, search_value: str = '') -> list:
    """
    This method returns statements fitting the given parametes.
    It returns the result as a list of single dicts containing the information of each result
    with the data: text, statement_uid, content, score, url.

    :param issue_uid: uid of the issue to search in
    :param search_value: the position of the statement
    :return: statements fitting a certain text
    """
    query = get_statements_with_value_path(issue_uid, search_value)
    slug = DBDiscussionSession.query(Issue).get(issue_uid).slug
    _um = UrlManager(slug=slug)

    results = []
    current_results = response_as_dict(query)['result']
    if current_results is not None:
        results = list(
            map(
                lambda res: {
                    'text': res['text'],
                    'statement_uid': res['statement_uid'],
                    'html': res['html'],
                    'score': res['score'],
                    'url': _um.get_url_for_statement_attitude(res[
                        'statement_uid'])
                }, current_results))

    return results
Esempio n. 4
0
def get_all_statements_with_value(search_value: str, issue_uid: int) -> list:
    """
    Returns all statements matching the given search_value

    :param issue_uid: uid of the issue to be searched in
    :param search_value: text to be searched for
    :return: statements matching the given search value in the given issue, uses levensthein.
    """
    issues_statements_uids = [
        el.statement_uid
        for el in DBDiscussionSession.query(StatementToIssue).filter_by(
            issue_uid=issue_uid).all()
    ]
    db_statements = get_enabled_statement_as_query().filter(
        Statement.uid.in_(issues_statements_uids)).all()
    return_array = []
    slug = DBDiscussionSession.query(Issue).get(issue_uid).slug
    _um = UrlManager(slug=slug)
    for stat in db_statements:
        db_tv = DBDiscussionSession.query(TextVersion).filter_by(
            statement_uid=stat.uid).order_by(TextVersion.uid.asc()).first()
        if search_value.lower() in db_tv.content.lower():
            rd = __get_fuzzy_string_dict(current_text=search_value,
                                         return_text=db_tv.content,
                                         uid=db_tv.statement_uid)
            rd['url'] = _um.get_url_for_statement_attitude(db_tv.statement_uid)
            return_array.append(rd)

    return_array = __sort_array(return_array)

    return return_array[:RESULT_LENGTH]
Esempio n. 5
0
def url_to_statement(issue_uid, statement_uid, agree=True):
    """
    Generate URL to given statement_uid in specific issue (by slug).
    Used to directly jump into the discussion.

    :param issue_uid: uid of current issue
    :type issue_uid: id
    :param statement_uid: Statement id to generate the link to
    :type statement_uid: int
    :param agree: Indicate (dis-)agreement with a statement
    :type agree: boolean
    :return: direct URL to jump to the provided statement
    :rtype: str
    """
    if isinstance(agree, str):
        if agree == "true":
            mode = "agree"
        else:
            mode = "disagree"
    else:
        mode = "agree" if agree is True else "disagree"
    slug = resolve_issue_uid_to_slug(issue_uid)
    url_manager = UrlManager(slug=slug)
    return "api/" + url_manager.get_url_for_justifying_statement(
        statement_uid, mode)
Esempio n. 6
0
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': ''
    }
Esempio n. 7
0
    def prepare_item_dict_for_attitude(self, statement_uid):
        """
        Prepares the dict with all items for the second step in discussion, where the user chooses her attitude.

        :param statement_uid: Statement.uid
        :return:
        """
        logger('ItemDictHelper', 'def')
        _tn = Translator(self.lang)

        slug = DBDiscussionSession.query(Issue).get(self.db_issue.uid).slug
        statements_array = []

        _um = UrlManager(slug, history=self.path)

        db_arguments = DBDiscussionSession.query(Argument).filter(
            Argument.conclusion_uid == statement_uid,
            Argument.is_supportive == True).all()
        uid = random.choice(db_arguments).uid if len(db_arguments) > 0 else 0

        title_t = _tn.get(_.iAgreeWithInColor) + '.'
        title_f = _tn.get(_.iDisagreeWithInColor) + '.'
        title_d = _tn.get(_.iHaveNoOpinionYetInColor) + '.'
        url_t = _um.get_url_for_justifying_statement(statement_uid,
                                                     Attitudes.AGREE.value)
        url_f = _um.get_url_for_justifying_statement(statement_uid,
                                                     Attitudes.DISAGREE.value)
        url_d = _um.get_url_for_justifying_statement(uid,
                                                     Attitudes.DONT_KNOW.value)
        d_t = self.__create_answer_dict(Attitudes.AGREE.value,
                                        [{
                                            'title': title_t,
                                            'id': Attitudes.AGREE.value
                                        }], Attitudes.AGREE.value, url_t)
        d_f = self.__create_answer_dict(Attitudes.DISAGREE.value,
                                        [{
                                            'title': title_f,
                                            'id': Attitudes.DISAGREE.value
                                        }], Attitudes.DISAGREE.value, url_f)
        d_d = self.__create_answer_dict(Attitudes.DONT_KNOW.value,
                                        [{
                                            'title': title_d,
                                            'id': Attitudes.DONT_KNOW.value
                                        }], Attitudes.DONT_KNOW.value, url_d)
        statements_array.append(d_t)
        statements_array.append(d_f)
        statements_array.append(d_d)

        return {
            'elements': statements_array,
            'extras': {
                'cropped_list': False
            }
        }
Esempio n. 8
0
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': ''
    }
Esempio n. 9
0
    def get_array_for_reaction(self, argument_uid_sys, argument_uid_user, is_supportive, attack, gender):
        """
        Prepares the dict with all items for the argumentation window.

        :param argument_uid_sys: Argument.uid
        :param argument_uid_user: Argument.uid
        :param is_supportive: Boolean
        :param attack: String
        :param gender: Gender of the author of the attack
        :return:
        """
        LOG.debug("Entering get_array_for_reaction")
        slug = self.db_issue.slug

        db_sys_argument = DBDiscussionSession.query(Argument).get(argument_uid_sys)
        db_user_argument = DBDiscussionSession.query(Argument).get(argument_uid_user)
        statements_array = []
        if not db_sys_argument or not db_user_argument:
            return {'elements': statements_array, 'extras': {'cropped_list': False}}

        rel_dict = get_relation_text_dict_with_substitution(self.lang, True, attack_type=attack, gender=gender)
        mode = Attitudes.AGREE if is_supportive else Attitudes.DISAGREE
        _um = UrlManager(slug, history=self.path)

        relations = [relation for relation in Relations]
        for relation in relations:
            url = self.__get_url_based_on_relation(relation, attack, _um, mode, db_user_argument, db_sys_argument)
            d = {'title': rel_dict[relation.value + '_text'], 'id': relation.value}
            tmp = self.__create_answer_dict(relation.value, [d], relation.value, url)
            statements_array.append(tmp)

        # last item is the change attack button or step back, if we have bno other attack
        attacking_arg_uids = get_all_attacking_arg_uids_from_history(self.path)
        attacking_arg_uids.append(argument_uid_sys)
        arg_id_sys, new_attack = attacks.get_attack_for_argument(argument_uid_user,
                                                                 restrictive_arg_uids=attacking_arg_uids,
                                                                 history=self.path)

        if not new_attack:
            url = _um.get_last_valid_url_before_reaction()
            relation = 'step_back'
        else:
            relation = 'no_opinion'
            url = _um.get_url_for_reaction_on_argument(argument_uid_user, new_attack, arg_id_sys)
        statements_array.append(
            self.__create_answer_dict(relation, [{'title': rel_dict[relation + '_text'], 'id': relation}], relation,
                                      url))

        return {'elements': statements_array, 'extras': {'cropped_list': False}}
Esempio n. 10
0
    def get_array_for_choosing(self, argument_or_statement_id, pgroup_ids, is_argument, is_supportive, nickname):
        """
        Prepares the dict with all items for the choosing an premise, when the user inserted more than one new premise.

        :param argument_or_statement_id: Argument.uid or Statement.uid
        :param pgroup_ids: PremiseGroups.uid
        :param is_argument: Boolean
        :param is_supportive: Boolean
        :param nickname:
        :return: dict()
        """
        LOG.debug("Entering get_array_for_choosing")
        statements_array = []
        slug = self.db_issue.slug
        _um = UrlManager(slug, history=self.path)
        conclusion_uid = argument_or_statement_id if not is_argument else None
        argument_uid = argument_or_statement_id if is_argument else None
        db_user = DBDiscussionSession.query(User).filter_by(nickname=nickname).first()

        for group_id in pgroup_ids:
            sarray = self.__get_choose_array_for_pgroup(is_argument, is_supportive, conclusion_uid, argument_uid,
                                                        db_user, group_id, _um)
            if not sarray:
                return {'elements': statements_array, 'extras': {'cropped_list': False}}

            statements_array.append(sarray)

        return {'elements': statements_array, 'extras': {'cropped_list': False}}
Esempio n. 11
0
    def __get_choose_array_for_pgroup(self, is_argument: bool, is_supportive: bool, conclusion_uid: int,
                                      argument_uid: int, db_user: User, group_id: int, _um: UrlManager):
        db_premises = DBDiscussionSession.query(Premise).filter_by(premisegroup_uid=group_id).all()
        premise_array = []
        for premise in db_premises:
            text = premise.get_text()
            premise_array.append({'title': text, 'id': premise.statement_uid})
            if db_user and db_user.nickname != nick_of_anonymous_user:  # add seen by if the statement is visible
                add_seen_statement(premise.statement_uid, db_user)

        # get attack for each premise, so the urls will be unique
        db_argument = DBDiscussionSession.query(Argument).filter(Argument.premisegroup_uid == group_id,
                                                                 Argument.is_supportive == is_supportive)
        if conclusion_uid and not is_argument:
            db_argument = db_argument.filter_by(conclusion_uid=conclusion_uid).first()
        else:
            db_argument = db_argument.filter_by(argument_uid=argument_uid).first()

        if not db_argument:
            return None

        attacking_arg_uids = get_all_attacking_arg_uids_from_history(self.path)
        arg_id_sys, attack = attacks.get_attack_for_argument(db_argument.uid,
                                                             restrictive_arg_uids=attacking_arg_uids)
        url = _um.get_url_for_reaction_on_argument(db_argument.uid, attack, arg_id_sys)

        if is_argument:
            is_author = is_author_of_argument(db_user, argument_uid)
        else:
            is_author = is_author_of_statement(db_user, conclusion_uid)
        return self.__create_answer_dict(str(db_argument.uid), premise_array, 'choose', url,
                                         is_markable=True, is_editable=True, is_author=is_author)
Esempio n. 12
0
def __set_url_of_start_premises(prepared_dict: dict, db_conclusion: Statement,
                                supportive: bool, db_issue: Issue,
                                db_user: User, history, mailer):
    LOG.debug("Entering __set_url_of_start_premises")

    # arguments=0: empty input
    # arguments=1: deliver new url
    # arguments>1: deliver url where the user has to choose between her inputs
    _um = UrlManager(db_issue.slug, history)
    _main_um = UrlManager(db_issue.slug, history=history)
    new_argument_uids = prepared_dict['argument_uids']

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

    else:
        pgroups = [
            DBDiscussionSession.query(Argument).get(arg_uid).premisegroup_uid
            for arg_uid in new_argument_uids
        ]
        url = _um.get_url_for_choosing_premisegroup(False, supportive,
                                                    db_conclusion.uid, pgroups)

    # send notifications and mails
    email_url = _main_um.get_url_for_justifying_statement(
        db_conclusion.uid,
        Attitudes.AGREE if supportive else Attitudes.DISAGREE)
    nh.send_add_text_notification(email_url, db_conclusion.uid, db_user,
                                  mailer)
    prepared_dict['url'] = url
Esempio n. 13
0
def __create_argument_by_raw_input(db_user: User, premisegroup: [str], db_conclusion: Statement, is_supportive,
                                   db_issue: Issue) \
        -> Tuple[Union[Argument, None], List[int]]:
    """
    Consumes the input to create a new argument

    :param db_user: User
    :param premisegroup: String
    :param db_conclusion: Statement
    :param is_supportive: Boolean
    :param db_issue: Issue
    :return:
    """
    logger('StatementsHelper',
           'main with premisegroup {} as premisegroup, conclusion {} in issue {}'.format(premisegroup,
                                                                                         db_conclusion.uid,
                                                                                         db_issue.uid))

    new_statements = []

    for text in premisegroup:
        statement = insert_as_statement(text, db_user, db_issue)
        new_statements.append(statement)

    # second, set the new statements as premisegroup
    new_premisegroup = set_statements_as_new_premisegroup(new_statements, db_user, db_issue)
    logger('StatementsHelper', 'new pgroup ' + str(new_premisegroup.uid))

    # third, insert the argument
    new_argument = __create_argument_by_uids(db_user, new_premisegroup.uid, db_conclusion.uid, None, is_supportive,
                                             db_issue)
    transaction.commit()

    if new_argument:
        _tn = Translator(db_issue.lang)
        _um = UrlManager(db_issue.slug)
        append_action_to_issue_rss(db_issue=db_issue, db_author=db_user, title=_tn.get(_.argumentAdded),
                                   description='...' + get_text_for_argument_uid(new_argument.uid,
                                                                                 anonymous_style=True) + '...',
                                   url=_um.get_url_for_justifying_statement(new_argument.uid, Attitudes.DONT_KNOW))

    return new_argument, [s.uid for s in new_statements]
Esempio n. 14
0
def get_arguments_by_statement_uid(db_statement: Statement, db_issue: Issue) -> dict:
    """
    Collects every argument which uses the given statement

    :param db_statement: Statement
    :param db_issue: issue
    :rtype: dict
    :return: prepared collection with several arguments
    """
    slug = resolve_issue_uid_to_slug(db_issue.uid)
    _um = UrlManager(slug)
    return {'arguments': get_all_arguments_with_text_and_url_by_statement_id(db_statement, _um, True, is_jump=True)}
Esempio n. 15
0
    def get_array_for_dont_know_reaction(self, argument_uid, db_user, gender):
        """
        Prepares the dict with all items for the third step, where a supportive argument will be presented.

        :param argument_uid: Argument.uid
        :param db_user: User
        :param gender: m, f or n
        :return:
        """
        LOG.debug("Entering get_array_for_dont_know_reaction")
        slug = self.db_issue.slug
        statements_array = []

        db_arguments = get_enabled_arguments_as_query()
        db_argument = db_arguments.filter_by(uid=argument_uid).first()
        if not db_argument:
            return {'elements': statements_array, 'extras': {'cropped_list': False}}

        # set real argument in history
        tmp_path = self.path.replace('/justify/{}/d'.format(db_argument.conclusion_uid),
                                     '/justify/{}/d'.format(argument_uid))
        _um = UrlManager(slug, history=tmp_path)

        if db_user and db_user.nickname != nick_of_anonymous_user:  # add seen by if the statement is visible
            add_seen_argument(argument_uid, db_user)

        rel_dict = get_relation_text_dict_with_substitution(self.lang, False, is_dont_know=True, gender=gender)

        relation = Relations.UNDERMINE.value
        url = self.__get_dont_know_item_for_undermine(db_argument, Attitudes.DISAGREE, _um)
        d = self.__create_answer_dict(relation, [{'title': rel_dict[relation + '_text'], 'id': relation}], relation,
                                      url)
        statements_array.append(d)

        relation = Relations.SUPPORT.value
        url = self.__get_dont_know_item_for_support(argument_uid, _um)
        d = self.__create_answer_dict(relation, [{'title': rel_dict[relation + '_text'], 'id': relation}], relation,
                                      url)
        statements_array.append(d)

        relation = Relations.UNDERCUT.value
        url = self.__get_dont_know_item_for_undercut(argument_uid, Attitudes.AGREE, _um)
        d = self.__create_answer_dict(relation, [{'title': rel_dict[relation + '_text'], 'id': relation}], relation,
                                      url)
        statements_array.append(d)

        relation = Relations.REBUT.value
        url = self.__get_dont_know_item_for_rebut(db_argument, Attitudes.DISAGREE, _um)
        d = self.__create_answer_dict(relation, [{'title': rel_dict[relation + '_text'], 'id': relation}], relation,
                                      url)
        statements_array.append(d)

        return {'elements': statements_array, 'extras': {'cropped_list': False}}
Esempio n. 16
0
    def test_mail_for_justifying_statement(self):
        url = UrlManager(slug='cat-or-dog')
        url = url.get_url_for_justifying_statement(statement_uid=123,
                                                   attitude=Attitudes.AGREE)

        for language, for_html in list(
                itertools.product(['de', 'en'], [True, False])):
            subject = "Test Mail"
            body = get_text_for_message(
                nickname=DummyUser().firstname,
                lang=language,
                path=url,
                message_content=_.statementAddedMessageContent,
                for_html=for_html)
            was_send, was_send_msg, msg = send_mail(
                mailer=DummyMailer,
                subject=subject,
                body=body,
                recipient=DummyUser().email,
                lang=language)
            self.assertTrue(was_send)
            self.assertEqual(msg.body, body)
Esempio n. 17
0
 def test_mail_for_reaction_on_argument(self):
     url = UrlManager(slug='cat-or-dog')
     url = url.get_url_for_reaction_on_argument(argument_uid=123,
                                                relation=Relations.REBUT,
                                                confrontation_argument=35)
     for language, for_html in list(
             itertools.product(['de', 'en'], [True, False])):
         subject = "Test Mail"
         body = get_text_for_message(
             nickname=DummyUser().firstname,
             lang=language,
             path=url,
             message_content=_.argumentAddedMessageContent,
             for_html=for_html)
         was_send, was_send_msg, msg = send_mail(
             mailer=DummyMailer,
             subject=subject,
             body=body,
             recipient=DummyUser().email,
             lang=language)
         self.assertTrue(was_send)
         self.assertEqual(msg.body, body)
Esempio n. 18
0
    def test_get_all_arguments_with_text_and_url_by_statement_id(self):
        um = UrlManager(slug='slug')

        results = {
            47: 'we should close public swimming pools because our swimming pools are very old and it would take a major investment to repair them',
            48: 'Someone argued that we should close public swimming pools because our swimming pools are very old and it would take a major investment to repair them. Other participants said that schools need the swimming pools for their sports lessons.',
            49: 'we should close public swimming pools does not hold, because the rate of non-swimmers is too high'
        }
        db_statement = DBDiscussionSession.query(Statement).get(38)
        res = lib.get_all_arguments_with_text_and_url_by_statement(db_statement, um)
        self.assertEqual(len(res), 3)
        for r in res:
            self.assertIn(r['uid'], results)
Esempio n. 19
0
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': ''
    }
Esempio n. 20
0
    def test_get_all_arguments_with_text_and_url_by_statement_id_with_color(self):
        um = UrlManager(slug='slug')

        results = {
            47: '<span data-argumentation-type="position">we should close public swimming pools</span> because our swimming pools are very old and it would take a major investment to repair them',
            48: 'Someone argued that <span data-argumentation-type="position">we should close public swimming pools</span> because our swimming pools are very old and it would take a major investment to repair them. Other participants said that schools need the swimming pools for their sports lessons.',
            49: '<span data-argumentation-type="position">we should close public swimming pools</span> does not hold, because the rate of non-swimmers is too high'
        }

        db_statement = DBDiscussionSession.query(Statement).get(38)
        res = lib.get_all_arguments_with_text_and_url_by_statement_id(db_statement, um, color_statement=True)
        self.assertEqual(3, len(res))
        for r in res:
            self.assertIn(r['uid'], results)
            self.assertEqual(results[r['uid']], r['text'])
Esempio n. 21
0
    def test_get_all_arguments_with_text_and_url_by_statement_id_with_color_and_jump(self):
        um = UrlManager(slug='slug')

        results = {
            47: '<span data-argumentation-type="position">we should close public swimming pools</span> because our swimming pools are very old and it would take a major investment to repair them.',
            48: 'our swimming pools are very old and it would take a major investment to repair them is not a good reason for <span data-argumentation-type="position">we should close public swimming pools</span>. Because schools need the swimming pools for their sports lessons.',
            49: '<span data-argumentation-type="position">we should close public swimming pools</span> does not hold because the rate of non-swimmers is too high.'
        }

        db_statement = DBDiscussionSession.query(Statement).get(38)
        res = lib.get_all_arguments_with_text_and_url_by_statement(db_statement, um, color_statement=True,
                                                                   is_jump=True)
        self.assertEqual(len(res), 3)
        for r in res:
            self.assertIn(r['uid'], results)
            self.assertIn('jump', r['url'])
            self.assertEqual(results[r['uid']], r['text'])
Esempio n. 22
0
def get_arguments_by_statement(statement: Statement, issue: Issue) -> dict:
    """
    Collects every argument which uses the given statement.

    :param statement: Statement which is used for query
    :param issue: Extract information for url manager from issue
    :rtype: dict
    :return: prepared collection with several arguments
    """
    _um = UrlManager(issue.slug)
    return {
        'arguments':
        get_all_arguments_with_text_and_url_by_statement(statement,
                                                         _um,
                                                         True,
                                                         is_jump=True)
    }
Esempio n. 23
0
    def get_array_for_justify_argument(self, argument_uid, attack_type, db_user, history):
        """
        Prepares the dict with all items for a step in discussion, where the user justifies his attack she has done.

        :param argument_uid: Argument.uid
        :param attack_type: String
        :param db_user:
        :param history:
        :return:
        """
        LOG.debug("Entering get_array_for_justify_argument with arg %s anf attack %s", argument_uid, attack_type)
        statements_array = []
        _tn = Translator(self.lang)
        slug = self.db_issue.slug
        # description in docs: dbas/logic
        db_arguments = self.__get_arguments_based_on_attack(attack_type, argument_uid)
        uids = [argument.uid for argument in db_arguments if db_arguments]

        _um = UrlManager(slug, history=self.path)

        for argument in db_arguments:
            sarray = self.__get_statement_array_for_justify_argument(argument_uid, attack_type, db_user, history,
                                                                     argument, uids, _um)
            statements_array.append(sarray)

        shuffle_list_by_user(db_user, statements_array)

        if not self.issue_read_only:
            if db_user and db_user.nickname != nick_of_anonymous_user:
                text = _tn.get(_.newPremiseRadioButtonText)
                if len(statements_array) == 0:
                    text = _tn.get(_.newPremiseRadioButtonTextAsFirstOne)
                a_dict = self.__create_answer_dict('justify_premise', [{'id': '0', 'title': text}], 'justify', 'add')
                statements_array.append(a_dict)

            else:
                # elif len(statements_array) == 1:
                a_dict = self.__create_answer_dict('login', [{'id': '0', 'title': _tn.get(_.onlyOneItem)}], 'justify',
                                                   'login')
                statements_array.append(a_dict)

        return {'elements': statements_array, 'extras': {'cropped_list': len(uids) < len(db_arguments)}}
Esempio n. 24
0
    def get_array_for_justify_statement(self, db_statement: Statement, db_user: User, is_supportive: bool, history):
        """
        Prepares the dict with all items for the third step in discussion, where the user justifies his position.

        :param db_statement: Statement
        :param db_user: User
        :param is_supportive: Boolean
        :param history: history
        :return:
        """
        LOG.debug("Entering get_array_for_justify_statement")
        statements_array = []
        _tn = Translator(self.lang)
        slug = self.db_issue.slug
        db_arguments: List[Argument] = attacks.get_arguments_by_conclusion(db_statement.uid, is_supportive)
        uids: List[int] = [argument.uid for argument in db_arguments if db_arguments]

        _um = UrlManager(slug, history=self.path)

        for argument in db_arguments:
            sarray = self.__get_statement_array_for_justify_statement(db_user, history, argument, uids, _tn, _um)

            statements_array.append(sarray)

        shuffle_list_by_user(db_user, statements_array)

        if not self.issue_read_only:
            if db_user and db_user.nickname != nick_of_anonymous_user:
                statements_array.append(self.__create_answer_dict('start_premise',
                                                                  [{
                                                                      'title': _tn.get(_.newPremiseRadioButtonText),
                                                                      'id': 0
                                                                  }],
                                                                  'justify',
                                                                  'add'))
            else:
                statements_array.append(self.__create_answer_dict('login',
                                                                  [{'id': '0', 'title': _tn.get(_.onlyOneItem)}],
                                                                  'justify',
                                                                  'login'))

        return {'elements': statements_array, 'extras': {'cropped_list': len(uids) < len(db_arguments)}}
Esempio n. 25
0
    def get_array_for_justify_argument(self, argument_uid, attack_type,
                                       db_user, history):
        """
        Prepares the dict with all items for a step in discussion, where the user justifies his attack she has done.

        :param argument_uid: Argument.uid
        :param attack_type: String
        :param db_user:
        :param history:
        :return:
        """
        logger('ItemDictHelper',
               'def: arg {}, attack {}'.format(argument_uid, attack_type))
        statements_array = []
        _tn = Translator(self.lang)
        slug = self.db_issue.slug
        # description in docs: dbas/logic
        db_arguments = self.__get_arguments_based_on_attack(
            attack_type, argument_uid)
        uids = [argument.uid for argument in db_arguments if db_arguments]

        _um = UrlManager(slug, history=self.path)

        for argument in db_arguments:
            if db_user and db_user.nickname != nick_of_anonymous_user:  # add seen by if the statement is visible
                add_seen_argument(argument_uid, db_user)
            # get all premises in this group
            db_premises = DBDiscussionSession.query(Premise).filter_by(
                premisegroup_uid=argument.premisegroup_uid).all()
            premises_array = []
            for premise in db_premises:
                text = premise.get_text()
                premises_array.append({
                    'id': premise.statement_uid,
                    'title': text
                })

            # for each justifying premise, we need a new confrontation: (restriction is based on fix #38)
            is_undermine = Relations.UNDERMINE if attack_type == Relations.UNDERMINE else None
            attacking_arg_uids = get_all_attacking_arg_uids_from_history(
                self.path)

            arg_id_sys, attack = attacks.get_attack_for_argument(
                argument.uid,
                last_attack=is_undermine,
                restrictive_arg_uids=attacking_arg_uids,
                history=self.path)
            the_other_one = True
            url = ''

            # with a chance of 50% or at the end we will seed the new "support step"
            if not attack:
                new_arg = get_another_argument_with_same_conclusion(
                    argument.uid, history)
                the_other_one = new_arg is None
                if new_arg:
                    the_other_one = False
                    url = _um.get_url_for_support_each_other(
                        argument.uid, new_arg.uid)

            if the_other_one:
                url = _um.get_url_for_reaction_on_argument(
                    argument.uid, attack, arg_id_sys)

            statements_array.append(
                self.__create_answer_dict(
                    argument.uid,
                    premises_array,
                    'justify',
                    url,
                    is_markable=True,
                    is_editable=not is_arguments_premise_in_edit_queue(
                        argument),
                    is_author=is_author_of_argument(db_user, argument.uid),
                    is_visible=argument.uid in uids,
                    attack_url=_um.get_url_for_jump(argument.uid)))

        shuffle_list_by_user(db_user, statements_array)

        if not self.issue_read_only:
            if db_user and db_user.nickname != nick_of_anonymous_user:
                text = _tn.get(_.newPremiseRadioButtonText)
                if len(statements_array) == 0:
                    text = _tn.get(_.newPremiseRadioButtonTextAsFirstOne)
                a_dict = self.__create_answer_dict('justify_premise',
                                                   [{
                                                       'id': '0',
                                                       'title': text
                                                   }], 'justify', 'add')
                statements_array.append(a_dict)

            else:
                # elif len(statements_array) == 1:
                a_dict = self.__create_answer_dict(
                    'login', [{
                        'id': '0',
                        'title': _tn.get(_.onlyOneItem)
                    }], 'justify', 'login')
                statements_array.append(a_dict)

        return {
            'elements': statements_array,
            'extras': {
                'cropped_list': len(uids) < len(db_arguments)
            }
        }
Esempio n. 26
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
        }
Esempio n. 27
0
    def get_array_for_justify_statement(self, db_statement: Statement,
                                        db_user: User, is_supportive: bool,
                                        history):
        """
        Prepares the dict with all items for the third step in discussion, where the user justifies his position.

        :param db_statement: Statement
        :param db_user: User
        :param is_supportive: Boolean
        :param history: history
        :return:
        """
        logger('ItemDictHelper', 'def')
        statements_array = []
        _tn = Translator(self.lang)
        slug = self.db_issue.slug
        db_arguments: List[Argument] = attacks.get_arguments_by_conclusion(
            db_statement.uid, is_supportive)
        uids: List[int] = [
            argument.uid for argument in db_arguments if db_arguments
        ]

        _um = UrlManager(slug, history=self.path)

        for argument in db_arguments:
            if db_user and argument.uid in uids:  # add seen by if the statement is visible
                add_seen_argument(argument.uid, db_user)

            # get all premises in the premisegroup of this argument
            db_premises = DBDiscussionSession.query(Premise).filter_by(
                premisegroup_uid=argument.premisegroup_uid).all()
            premise_array = []
            for premise in db_premises:
                text = premise.get_text()
                premise_array.append({
                    'title': text,
                    'id': premise.statement_uid
                })

            # filter forbidden attacks
            forbidden_attacks = attacks.get_forbidden_attacks_based_on_history(
                self.path)

            # get attack for each premise, so the urls will be unique
            arg_id_sys, attack = attacks.get_attack_for_argument(
                argument.uid,
                history=self.path,
                restrictive_arg_uids=forbidden_attacks)
            already_used = 'reaction/' + str(argument.uid) + '/' in self.path
            additional_text = '(' + _tn.get(_.youUsedThisEarlier) + ')'

            new_arg = None
            url = None
            if not attack:
                new_arg = get_another_argument_with_same_conclusion(
                    argument.uid, history)
                if new_arg:
                    url = _um.get_url_for_support_each_other(
                        argument.uid, new_arg.uid)

            if attack or new_arg is None or url is None:
                url = _um.get_url_for_reaction_on_argument(
                    argument.uid, attack.value, arg_id_sys)

            statements_array.append(
                self.__create_answer_dict(
                    str(argument.uid),
                    premise_array,
                    'justify',
                    url,
                    already_used=already_used,
                    already_used_text=additional_text,
                    is_editable=not is_arguments_premise_in_edit_queue(
                        argument),
                    is_markable=True,
                    is_author=is_author_of_argument(db_user, argument.uid),
                    is_visible=argument.uid in uids,
                    attack_url=_um.get_url_for_jump(argument.uid)))

        shuffle_list_by_user(db_user, statements_array)

        if not self.issue_read_only:
            if db_user and db_user.nickname != nick_of_anonymous_user:
                statements_array.append(
                    self.__create_answer_dict(
                        'start_premise',
                        [{
                            'title': _tn.get(_.newPremiseRadioButtonText),
                            'id': 0
                        }], 'justify', 'add'))
            else:
                statements_array.append(
                    self.__create_answer_dict(
                        'login', [{
                            'id': '0',
                            'title': _tn.get(_.onlyOneItem)
                        }], 'justify', 'login'))

        return {
            'elements': statements_array,
            'extras': {
                'cropped_list': len(uids) < len(db_arguments)
            }
        }
Esempio n. 28
0
    def get_array_for_start(self, db_user: User) -> dict():
        """
        Prepares the dict with all items for the first step in discussion, where the user chooses a position.

        :param db_user: User
        :return:
        """
        logger('ItemDictHelper', 'def user: {}'.format(db_user.nickname))
        db_statements = DBDiscussionSession.query(Statement) \
            .filter(Statement.is_disabled == False,
                    Statement.is_position == True,
                    Statement.issue_uid == self.db_issue.uid).all()

        uids = [element.uid for element in db_statements if db_statements]
        slug = self.db_issue.slug

        statements_array = []
        _um = UrlManager(slug, history=self.path)

        for statement in db_statements:
            if statement.uid in uids:  # add seen by if the statement is visible
                add_seen_statement(statement.uid, db_user)
            statements_array.append(
                self.__create_answer_dict(
                    statement.uid, [{
                        'title': statement.get_text(),
                        'id': statement.uid
                    }],
                    'start',
                    _um.get_url_for_statement_attitude(statement.uid),
                    is_editable=not is_statement_in_edit_queue(statement.uid),
                    is_markable=True,
                    is_author=is_author_of_statement(db_user, statement.uid),
                    is_visible=statement.uid in uids))

        _tn = Translator(self.lang)

        shuffle_list_by_user(db_user, statements_array)

        if not self.issue_read_only:
            if db_user.nickname == nick_of_anonymous_user:
                statements_array.append(
                    self.__create_answer_dict(
                        'login', [{
                            'id': '0',
                            'title': _tn.get(_.wantToStateNewPosition)
                        }], 'justify', 'login'))
            else:
                title = _tn.get(_.newConclusionRadioButtonText
                                ) if len(db_statements) > 0 else _tn.get(
                                    _.newConclusionRadioButtonTextNewIdea)
                statements_array.append(
                    self.__create_answer_dict('start_statement', [{
                        'title': title,
                        'id': 0
                    }], 'start', 'add'))

        return {
            'elements': statements_array,
            'extras': {
                'cropped_list': len(uids) < len(db_statements)
            }
        }
Esempio n. 29
0
    def get_array_for_choosing(self, argument_or_statement_id, pgroup_ids,
                               is_argument, is_supportive, nickname):
        """
        Prepares the dict with all items for the choosing an premise, when the user inserted more than one new premise.

        :param argument_or_statement_id: Argument.uid or Statement.uid
        :param pgroup_ids: PremiseGroups.uid
        :param is_argument: Boolean
        :param is_supportive: Boolean
        :param nickname:
        :return: dict()
        """
        logger('ItemDictHelper', 'def')
        statements_array = []
        slug = self.db_issue.slug
        _um = UrlManager(slug, history=self.path)
        conclusion_uid = argument_or_statement_id if not is_argument else None
        argument_uid = argument_or_statement_id if is_argument else None
        db_user = DBDiscussionSession.query(User).filter_by(
            nickname=nickname).first()

        for group_id in pgroup_ids:
            db_premises = DBDiscussionSession.query(Premise).filter_by(
                premisegroup_uid=group_id).all()
            premise_array = []
            for premise in db_premises:
                text = premise.get_text()
                premise_array.append({
                    'title': text,
                    'id': premise.statement_uid
                })
                if db_user and db_user.nickname != nick_of_anonymous_user:  # add seen by if the statement is visible
                    add_seen_statement(premise.statement_uid, db_user)

            # get attack for each premise, so the urls will be unique
            db_argument = DBDiscussionSession.query(Argument).filter(
                Argument.premisegroup_uid == group_id,
                Argument.is_supportive == is_supportive)
            if conclusion_uid and not is_argument:
                db_argument = db_argument.filter_by(
                    conclusion_uid=conclusion_uid).first()
            else:
                db_argument = db_argument.filter_by(
                    argument_uid=argument_uid).first()

            if not db_argument:
                print(group_id)
                return {
                    'elements': statements_array,
                    'extras': {
                        'cropped_list': False
                    }
                }

            attacking_arg_uids = get_all_attacking_arg_uids_from_history(
                self.path)
            arg_id_sys, attack = attacks.get_attack_for_argument(
                db_argument.uid, restrictive_arg_uids=attacking_arg_uids)
            url = _um.get_url_for_reaction_on_argument(db_argument.uid, attack,
                                                       arg_id_sys)

            if is_argument:
                is_author = is_author_of_argument(db_user, argument_uid)
            else:
                is_author = is_author_of_statement(db_user, conclusion_uid)
            statements_array.append(
                self.__create_answer_dict(str(db_argument.uid),
                                          premise_array,
                                          'choose',
                                          url,
                                          is_markable=True,
                                          is_editable=True,
                                          is_author=is_author))

        return {
            'elements': statements_array,
            'extras': {
                'cropped_list': False
            }
        }
Esempio n. 30
0
    def __get_url_for_jump_array(self, slug, arg_uid):
        """
        Returns urls for the answers to jump to an argument

        :param slug: String
        :param arg_uid: Argument.uid
        :return: dict()
        """

        db_argument = DBDiscussionSession.query(Argument).get(arg_uid)
        _um = UrlManager(slug, history=self.path)
        db_premises = DBDiscussionSession.query(Premise).filter_by(
            premisegroup_uid=db_argument.premisegroup_uid).all()
        forbidden_attacks = attacks.get_forbidden_attacks_based_on_history(
            self.path)

        db_undercutted_arg = None
        len_undercut = 0
        if db_argument.argument_uid is not None:
            db_undercutted_arg = DBDiscussionSession.query(Argument).get(
                db_argument.argument_uid)
            len_undercut = 1 if db_undercutted_arg.argument_uid is None else 2

        arg_id_sys, sys_attack = attacks.get_attack_for_argument(
            db_argument.uid,
            redirected_from_jump=True,
            restrictive_arg_uids=forbidden_attacks)
        url0 = _um.get_url_for_reaction_on_argument(db_argument.uid,
                                                    sys_attack, arg_id_sys)

        if len_undercut == 0:
            url1 = _um.get_url_for_justifying_statement(
                db_argument.conclusion_uid, Attitudes.AGREE)
            url2 = _um.get_url_for_justifying_argument(db_argument.uid,
                                                       Attitudes.AGREE,
                                                       Relations.UNDERCUT)
            url3 = _um.get_url_for_justifying_statement(
                db_argument.conclusion_uid, Attitudes.DISAGREE)
            if len(db_premises) == 1:
                url4 = _um.get_url_for_justifying_statement(
                    db_premises[0].statement_uid, Attitudes.DISAGREE)
            else:
                url4 = _um.get_url_for_justifying_argument(
                    db_argument.uid, Attitudes.DISAGREE, Relations.UNDERMINE)

        elif len_undercut == 1:
            url1 = None
            url2 = _um.get_url_for_justifying_argument(db_argument.uid,
                                                       Attitudes.AGREE,
                                                       Relations.UNDERCUT)
            url3 = _um.get_url_for_jump(db_undercutted_arg.uid)
            if len(db_premises) == 1:
                url4 = _um.get_url_for_justifying_statement(
                    db_premises[0].statement_uid, Attitudes.DISAGREE)
            else:
                url4 = _um.get_url_for_justifying_argument(
                    db_argument.uid, Attitudes.DISAGREE, Relations.UNDERMINE)

        else:
            url1 = None
            url2 = None
            url3 = _um.get_url_for_jump(db_undercutted_arg.uid)
            if len(db_premises) == 1:
                url4 = _um.get_url_for_justifying_statement(
                    db_premises[0].statement_uid, Attitudes.DISAGREE)
            else:
                url4 = _um.get_url_for_justifying_argument(
                    db_argument.uid, Attitudes.DISAGREE, Relations.UNDERMINE)

        return [url0, url1, url2, url3, url4]