Exemplo n.º 1
0
def update_badge():
    """
    Returns the new count for the badge of every table

    :return: dict(), string
    """
    logger('AdminLib', '')
    ret_array = []
    for t in table_mapper:
        ret_array.append({
            'name': table_mapper[t]['name'],
            'count': DBDiscussionSession.query(table_mapper[t]['table']).count()
        })

    return ret_array
Exemplo n.º 2
0
    def test_get_author_data(self):
        db_user, author_string, some_boolean = lib.get_author_data(0)
        self.assertFalse(some_boolean)
        self.assertIsNone(db_user)

        user = DBDiscussionSession.query(User).get(1)
        _, author_string, some_boolean = lib.get_author_data(1, gravatar_on_right_side=False)
        self.assertTrue(some_boolean)
        self.assertIn('{}'.format(user.nickname), author_string)
        self.assertIn('right', author_string)

        _, author_string, some_boolean = lib.get_author_data(1, gravatar_on_right_side=True)
        self.assertTrue(some_boolean)
        self.assertIn('{}'.format(user.nickname), author_string)
        self.assertIn('left', author_string)
Exemplo n.º 3
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(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'])
Exemplo n.º 4
0
def __return_d3_data(graph_arg_lists, db_issue: Issue):
    """

    :param graph_arg_lists:
    :param db_issue:
    :return:
    """
    graph_arg_list = []
    for key in graph_arg_lists:
        graph_arg_list += graph_arg_lists[key]
    graph_arg_list = [
        DBDiscussionSession.query(Argument).get(uid)
        for uid in list(set(graph_arg_list))
    ]

    graph_stat_list = get_all_statements_for_args(graph_arg_list)
    graph_stat_list = [
        DBDiscussionSession.query(Statement).get(uid)
        for uid in graph_stat_list
    ]

    LOG.debug("Stat_list: %s", [stat.uid for stat in graph_stat_list])
    LOG.debug("Arg_list: %s", [arg.uid for arg in graph_arg_list])
    return get_d3_data(db_issue, graph_stat_list, graph_arg_list)
Exemplo n.º 5
0
def get_references(request: Request):
    """
    Query database to get stored references from site. Generate a list with text versions of references.

    :param request: request
    :return: References assigned to the queried URL
    """
    host = request.validated["host"]
    path = request.validated["path"]
    LOG.debug("Querying references for host: {}, path: {}".format(host, path))
    refs_db: List[StatementReference] = DBDiscussionSession.query(StatementReference).filter_by(host=host,
                                                                                                path=path).all()
    return {
        "references": [DataReference(ref) for ref in refs_db]
    }
Exemplo n.º 6
0
Arquivo: lib.py Projeto: kroschu/dbas
def __get_clicks_and_marks(argument_uid, statement_uid, db_user):
    db_clicks = None
    db_marks = None
    if argument_uid:
        db_clicks = DBDiscussionSession.query(ClickedArgument). \
            filter(ClickedArgument.argument_uid == argument_uid,
                   ClickedArgument.is_up_vote == True,
                   ClickedArgument.is_valid,
                   ClickedArgument.author_uid != db_user.uid).all()
        db_marks = DBDiscussionSession.query(MarkedArgument). \
            filter(MarkedArgument.argument_uid == argument_uid,
                   MarkedArgument.author_uid != db_user.uid).all()

    elif statement_uid:
        db_clicks = DBDiscussionSession.query(ClickedStatement). \
            filter(ClickedStatement.statement_uid == statement_uid,
                   ClickedStatement.is_up_vote == True,
                   ClickedStatement.is_valid,
                   ClickedStatement.author_uid != db_user.uid).all()
        db_marks = DBDiscussionSession.query(MarkedStatement). \
            filter(MarkedStatement.statement_uid == statement_uid,
                   MarkedStatement.author_uid != db_user.uid).all()

    return db_clicks, db_marks
Exemplo n.º 7
0
    def cancel_ballot(self, db_user: User, db_review: ReviewDelete):
        """
        Cancels any ongoing vote

        :param db_user: current user
        :param db_review: any element from a review queue
        :return:
        """
        DBDiscussionSession.query(ReviewDelete).get(db_review.uid).set_revoked(True)
        DBDiscussionSession.query(LastReviewerDelete).filter_by(review_uid=db_review.uid).delete()
        db_review_canceled = ReviewCanceled(author=db_user.uid, review_data={key_delete: db_review.uid},
                                            was_ongoing=True)

        DBDiscussionSession.add(db_review_canceled)
        DBDiscussionSession.flush()
        transaction.commit()
        return True
Exemplo n.º 8
0
    def test_get_user_with_same_opinion_for_statements(self):
        lang = 'en'
        main_page = 'url'
        db_user = DBDiscussionSession.query(User).filter_by(
            nickname='Tobias').first()

        for uid in [1, 2, 3]:
            response = get_user_with_same_opinion_for_statements(
                statement_uids=[uid],
                is_supportive=True,
                db_user=db_user,
                lang=lang,
                main_page=main_page)
            verify_structure_of_statement_premisgroup_argument_dictionary(
                self, response)
Exemplo n.º 9
0
def main_user(request):
    """
    View configuration for the public user page.

    :param request: current request of the server
    :return: dictionary with title and project name as well as a value, weather the user is logged in
    """
    match_dict = request.matchdict
    logger('main_user', 'request.matchdict: {}'.format(match_dict))

    uid = match_dict.get('uid', 0)
    logger('main_user', 'uid: {}'.format(uid))

    if not is_integer(uid):
        raise HTTPNotFound

    current_user = DBDiscussionSession.query(User).get(uid)
    if current_user is None or current_user.nickname == nick_of_anonymous_user:
        logger('main_user', 'no user: {}'.format(uid), error=True)
        raise HTTPNotFound()

    ui_locales = get_language_from_cookie(request)
    user_dict = user.get_information_of(current_user, ui_locales)

    db_user_of_request = DBDiscussionSession.query(User).filter_by(
        nickname=request.authenticated_userid).first()
    can_send_notification = False
    if db_user_of_request:
        can_send_notification = current_user.uid != db_user_of_request.uid

    prep_dict = __main_dict(request, user_dict['public_nick'])
    prep_dict.update({
        'user': user_dict,
        'can_send_notification': can_send_notification
    })
    return prep_dict
Exemplo n.º 10
0
def add_row(table_name, data):
    """
    Updates data of a row in the table

    :param table_name: Name of the table
    :param data: Dictionary with data for the update
    :return: Empty string or error message
    """
    LOG.debug("%s", data)

    table = table_mapper[table_name.lower()]['table']
    try:
        if 'uid' in data:
            del data['uid']
        new_one = table(**data)
        DBDiscussionSession.add(new_one)
    except IntegrityError as e:
        LOG.error("%s", e)
        return exception_response(400,
                                  error='SQLAlchemy IntegrityError: ' + str(e))

    DBDiscussionSession.flush()
    transaction.commit()
    return True
Exemplo n.º 11
0
def valid_user_as_author_of_statement(request):
    """

    :param request:
    :return:
    """
    if not has_keywords(('statement_id', int))(request):
        return False

    statement_id = request.validated['statement_id']

    if valid_user(request):
        db_user = request.validated['user']
        db_textversion = DBDiscussionSession.query(TextVersion) \
            .filter_by(statement_uid=statement_id) \
            .order_by(TextVersion.uid.asc()).first()
        if db_textversion and db_textversion.author_uid == db_user.uid:
            request.validated['statement'] = DBDiscussionSession.query(
                Statement).get(statement_id)
            return True
        else:
            _tn = Translator(get_language_from_cookie(request))
            add_error(request, _tn.get(_.userIsNotAuthorOfStatement))
    return False
Exemplo n.º 12
0
def __get_dash_dict(name, href):
    """
    Returns dictionary with all attributes

    :param name: name of current table
    :param href: link for current table
    :return: {'count': count, 'name': name, 'href': href}
    """
    count = DBDiscussionSession.query(
        table_mapper[name.lower()]['table']).count()
    return {
        'name': name,
        'href': href,
        'count': count,
    }
Exemplo n.º 13
0
def __check_duplicate(db_issue: Issue, text: str) -> Optional[Statement]:
    """
    Check if there is already a textversion with the given text. If true the statement2issue relation will be
    checked and set and the duplicate (Statement) returned

    :param db_issue: related Issue
    :param text: the text
    :return:
    """
    db_tv = DBDiscussionSession.query(TextVersion).filter(
        func.lower(TextVersion.content) == text.lower()).first()
    if not db_tv:
        return None

    db_statement2issue = DBDiscussionSession.query(StatementToIssue).filter(
        StatementToIssue.issue_uid == db_issue.uid,
        StatementToIssue.statement_uid == db_tv.statement_uid).all()

    if not db_statement2issue:
        __add_statement2issue(db_tv.statement_uid, db_issue.uid)

    db_statement = DBDiscussionSession.query(Statement).get(
        db_tv.statement_uid)
    return db_statement
Exemplo n.º 14
0
def get_all_references_by_reference_text(ref_text=None):
    """
    Query database for all occurrences of a given reference text. Prepare list with information about
    used issue, author and a url to the statement.

    :param ref_text: Reference text
    :return: list of used references
    """
    if ref_text:
        refs = list()
        matched = DBDiscussionSession.query(StatementReferences).filter_by(
            reference=ref_text).all()
        for reference in matched:
            user = DBDiscussionSession.query(User).get(reference.author_uid)
            issue = DBDiscussionSession.query(Issue).get(reference.issue_uid)
            textversion = DBDiscussionSession.query(TextVersion).get(
                reference.statement_uid)
            statement_url = url_to_statement(issue.uid,
                                             reference.statement_uid)
            refs.append({
                "reference":
                extract_reference_information(reference),
                "author":
                extract_author_information(user),
                "issue":
                extract_issue_information(issue),
                "arguments":
                get_all_arguments_with_text_by_statement_id(
                    reference.statement_uid),
                "statement": {
                    "uid": reference.statement_uid,
                    "url": statement_url,
                    "text": textversion.content
                }
            })
        return refs
Exemplo n.º 15
0
def __get_references_for_statement(uid, main_page):
    """
    Returns all references for the current given statement

    :param uid: uid of the statement
    :param main_page: current overview page
    :return: dict
    """
    LOG.debug("%s", uid)
    db_references = DBDiscussionSession.query(StatementReference).filter_by(
        statement_uid=uid).all()
    references_array = [
        __get_values_of_reference(ref, main_page) for ref in db_references
    ]
    return {uid: references_array}
Exemplo n.º 16
0
    def is_review_locked(self, review_uid):
        """
        Is the OptimizationReviewLocks set?

        :param review_uid: OptimizationReviewLocks.uid
        :return: Boolean
        """
        self.tidy_up_optimization_locks()
        LOG.debug("Check whether review %s is locked.", review_uid)
        db_lock = DBDiscussionSession.query(OptimizationReviewLocks).filter_by(
            review_optimization_uid=review_uid).first()
        if not db_lock:
            return False
        return (get_now() -
                db_lock.locked_since).seconds < max_lock_time_in_sec
Exemplo n.º 17
0
    def test_set_references(self):
        self.config.testing_securitypolicy(userid='Tobias', permissive=True)
        issue_uid = DBDiscussionSession.query(StatementToIssue).filter_by(statement_uid=17).first().issue_uid
        request = testing.DummyRequest(json_body={
            'statement_id': 17,
            'issue': issue_uid,
            'text': 'This is a source',
            'ref_source': 'http://www.google.de/some_source',
        })
        self.assertTrue(set_references(request))

        request = testing.DummyRequest(json_body={
            'uids': [17],
            'is_argument': False
        })
        response = get_reference(request)
        self.assertIsNotNone(response)
        for uid in response['data']:
            self.assertTrue(17, uid)
            self.assertTrue(len(response['data'][uid]) != 0)
            self.assertTrue(len(response['text'][uid]) != 0)

        DBDiscussionSession.query(StatementReference).filter_by(statement_uid=17).delete()
        transaction.commit()
Exemplo n.º 18
0
def get_mark_count_of(user: User, only_today: bool = False) -> Tuple[int, int]:
    """
    Returns the count of marked arguments and statements of the specific user.

    :param user: The user whose arguments are counted.
    :param only_today: True if only the marked arguments of the current day shall be counted.
    :return: A tuple containing the count of marked arguments and marked statements.
    """
    if not user:
        return 0, 0

    marked_arguments = DBDiscussionSession.query(MarkedArgument).filter(
        MarkedArgument.author_uid == user.uid)
    marked_statements = DBDiscussionSession.query(MarkedStatement).filter(
        MarkedStatement.author_uid == user.uid)

    if only_today:
        today = arrow.utcnow().format('YYYY-MM-DD')
        marked_arguments = marked_arguments.filter(
            MarkedArgument.timestamp >= today)
        marked_statements = marked_statements.filter(
            MarkedStatement.timestamp >= today)

    return marked_arguments.count(), marked_statements.count()
Exemplo n.º 19
0
Arquivo: lib.py Projeto: kroschu/dbas
def is_author_of_argument(db_user: User, argument_uid: int) -> bool:
    """
    Is the user with given nickname author of the argument?

    :param db_user: User
    :param argument_uid: Argument.uid
    :return: Boolean
    """
    db_user = db_user if db_user and db_user.nickname != nick_of_anonymous_user else None
    if not db_user:
        return False
    db_argument = DBDiscussionSession.query(Argument).filter(
        Argument.uid == argument_uid,
        Argument.author_uid == db_user.uid).first()
    return True if db_argument else False
Exemplo n.º 20
0
def get_from_database(db_user: User, lang: str) -> List[dict]:
    """
    Returns history from database

    :param db_user: User
    :param lang: ui_locales
    :return: [String]
    """
    db_history = DBDiscussionSession.query(History).filter_by(author_uid=db_user.uid).all()
    return_array = []
    for history in db_history:
        return_array.append({'path': history.path,
                             'timestamp': sql_timestamp_pretty_print(history.timestamp, lang, False, True) + ' GMT'})

    return return_array
Exemplo n.º 21
0
def __receive_url_for_processing_input_of_multiple_premises_for_arguments(new_argument_uids, attack_type, arg_id, _um,
                                                                          supportive) -> str:
    """
    Return the 'choose' url, when the user entered more than one premise for an argument

    :param new_argument_uids: [Argument.uid]
    :param attack_type: String
    :param arg_id: Argument.uid
    :param _um: UrlManager
    :param supportive: Boolean
    :return: String
    """
    pgroups = []
    url = ''
    for uid in new_argument_uids:
        pgroups.append(DBDiscussionSession.query(Argument).get(uid).premisegroup_uid)

    current_argument = DBDiscussionSession.query(Argument).get(arg_id)
    # relation to the arguments premise group
    if attack_type == Relations.UNDERMINE or attack_type == Relations.SUPPORT:
        db_premise = DBDiscussionSession.query(Premise).filter_by(
            premisegroup_uid=current_argument.premisegroup_uid).first()  # TODO what happens with |pgroups| > 1?
        db_statement = DBDiscussionSession.query(Statement).get(db_premise.statement_uid)
        url = _um.get_url_for_choosing_premisegroup(False, supportive, db_statement.uid, pgroups)

    # relation to the arguments relation
    elif attack_type == Relations.UNDERCUT:
        url = _um.get_url_for_choosing_premisegroup(True, supportive, arg_id, pgroups)

    # relation to the arguments conclusion
    elif attack_type == Relations.REBUT:
        is_argument = current_argument.conclusion_uid is not None
        uid = current_argument.argument_uid if is_argument else current_argument.conclusion_uid
        url = _um.get_url_for_choosing_premisegroup(False, is_argument, supportive, uid, pgroups)

    return url
Exemplo n.º 22
0
 def test_user_login(self):
     db_user = DBDiscussionSession.query(User).filter_by(
         nickname='Tobias').first()
     db_user.password = get_hashed_password('tobias')
     transaction.commit()
     from dbas.views import user_login as ajax
     request = testing.DummyRequest(json_body={
         'user': '******',
         'password': '******',
         'keep_login': False,
         'url': ''
     },
                                    mailer=DummyMailer)
     response = ajax(request)
     self.assertTrue(type(response) is HTTPFound)
 def test_set_new_premises_for_argument(self):
     self.config.testing_securitypolicy(userid='Tobias', permissive=True)
     db_arg1 = DBDiscussionSession.query(Argument).filter_by(
         conclusion_uid=2).count()
     db_pgroups1 = DBDiscussionSession.query(PremiseGroup).count()
     request = testing.DummyRequest(json_body={
         'premisegroups': [['some new reason for an argument']],
         'argument_id':
         2,
         'attack_type':
         Relations.SUPPORT.value,
         'issue':
         2
     },
                                    mailer=DummyMailer)
     response = set_new_premises_for_argument(request)
     db_arg2 = DBDiscussionSession.query(Argument).filter_by(
         conclusion_uid=2).count()
     db_pgroups2 = DBDiscussionSession.query(PremiseGroup).count()
     self.assertIsNotNone(response)
     self.assertNotIsInstance(response, HTTPError, response)
     self.assertEqual(db_arg1 + 1, db_arg2)
     self.assertEqual(db_pgroups1 + 1, db_pgroups2)
     self.delete_last_argument_by_conclusion_uid(2)
Exemplo n.º 24
0
    def add_edit_reviews(self, db_user: User, uid: int, text: str):
        """
        Setup a new ReviewEdit row

        :param db_user: User
        :param uid: Statement.uid
        :param text: New content for statement
        :return: -1 if the statement of the element does not exists, -2 if this edit already exists, 1 on success,
        0 otherwise
        """
        db_statement = DBDiscussionSession.query(Statement).get(uid)
        if not db_statement:
            LOG.warning("Statement %s not found (return %s)", uid,
                        Code.DOESNT_EXISTS)
            return Code.DOESNT_EXISTS

        # already set an correction for this?
        if self.is_statement_in_edit_queue(
                uid):  # if we already have an edit, skip this
            LOG.warning("Statement %s already got an edit (return %s)", uid,
                        Code.DUPLICATE)
            return Code.DUPLICATE

        # is text different?
        db_tv = DBDiscussionSession.query(TextVersion).get(
            db_statement.textversion_uid)
        if len(text) > 0 and db_tv.content.lower().strip() != text.lower(
        ).strip():
            LOG.debug("Added review element for %s. (return %s)", uid,
                      Code.SUCCESS)
            DBDiscussionSession.add(
                ReviewEdit(detector=db_user.uid, statement=uid))
            return Code.SUCCESS

        LOG.debug("No case for %s (return %s)", uid, Code.ERROR)
        return Code.ERROR
Exemplo n.º 25
0
def get_reference_usages(request: Request):
    """
    Return a JSON object containing all information about the stored reference and its usages.

    :param request:
    :return: JSON with all information about the stored reference
    :rtype: list
    """
    ref_uid = request.validated["ref_uid"]
    LOG.debug("Retrieving reference usages for ref_uid {}".format(ref_uid))
    db_ref: StatementReference = DBDiscussionSession.query(
        StatementReference).get(ref_uid)
    if db_ref:
        return get_all_references_by_reference_text(db_ref.text)
    return error("Reference could not be found")
Exemplo n.º 26
0
 def setUp(self):
     super().setUp()
     self.first_issue = DBDiscussionSession.query(Issue).filter_by(is_disabled=False).first()
     self.last_issue = DBDiscussionSession.query(Issue).filter_by(is_disabled=False).order_by(
         Issue.uid.desc()).first()
     self.history = '/attitude/2-/justify/2/t-/reaction/12/undercut/13'
     settings = self.user_tobi.settings
     settings.set_last_topic_uid(self.first_issue.uid)
     DBDiscussionSession.add(settings)
     DBDiscussionSession.flush()
Exemplo n.º 27
0
    def get_queue_information(self, db_user: User, session: Session, application_url: str, translator: Translator):
        """
        Setup the subpage for the delete queue

        :param db_user: User
        :param session: session of current webserver request
        :param application_url: current url of the app
        :param translator: Translator
        :return: dict()
        """
        LOG.debug("Entering setup for subpage of deletion queue")
        all_rev_dict = get_all_allowed_reviews_for_user(session, f'already_seen_{self.key}', db_user, ReviewDelete,
                                                        LastReviewerDelete)

        rev_dict = get_base_subpage_dict(ReviewDelete, all_rev_dict['reviews'], all_rev_dict['already_seen_reviews'],
                                         all_rev_dict['first_time'], db_user, all_rev_dict['already_voted_reviews'])
        if not rev_dict['rnd_review']:
            return {
                'stats': None,
                'text': None,
                'reason': None,
                'issue_titles': None,
                'extra_info': None,
                'session': session
            }

        db_reason = DBDiscussionSession.query(ReviewDeleteReason).get(rev_dict['rnd_review'].reason_uid)
        stats = get_reporter_stats_for_review(rev_dict['rnd_review'], translator.get_lang(), application_url)

        reason = ''
        if db_reason.reason == 'offtopic':
            reason = translator.get(_.argumentFlaggedBecauseOfftopic)
        if db_reason.reason == 'spam':
            reason = translator.get(_.argumentFlaggedBecauseSpam)
        if db_reason.reason == 'harmful':
            reason = translator.get(_.argumentFlaggedBecauseHarmful)

        rev_dict['already_seen_reviews'].append(rev_dict['rnd_review'].uid)
        session[f'already_seen_{self.key}'] = rev_dict['already_seen_reviews']

        return {
            'stats': stats,
            'text': rev_dict['text'],
            'reason': reason,
            'issue_titles': rev_dict['issue_titles'],
            'extra_info': rev_dict['extra_info'],
            'session': session
        }
Exemplo n.º 28
0
def get_issue_based_on_header(request):
    """

    :param request:
    :return:
    """
    # logger('IssueHelper', 'get_issue_based_on_header', 'no saved issue found')
    ui_locales = get_language_from_header(request)
    db_issues = get_enabled_issues_as_query()
    db_lang = DBDiscussionSession.query(Language).filter_by(
        ui_locales=ui_locales).first()
    db_issue = db_issues.filter_by(lang_uid=db_lang.uid).first()
    if not db_issue:
        db_issue = db_issues.first()

    return db_issue.uid
Exemplo n.º 29
0
def valid_statement_or_argument(request):
    is_argument = request.json_body.get('is_argument')
    t = Argument if is_argument else Statement
    uid = request.json_body.get('uid')
    if uid:
        db_arg_or_stmt = DBDiscussionSession.query(t).get(uid)
    else:
        add_error(request, 'Missing uid for ' + t.__name__)
        return False

    if db_arg_or_stmt:
        request.validated['arg_or_stmt'] = db_arg_or_stmt
        return True
    else:
        add_error(request, t.__name__ + ' is invalid')
        return False
Exemplo n.º 30
0
def valid_any_issue_by_id(request):
    """
    Query issue from database and put it into the request, even if it is disabled

    :param request:
    :return:
    """

    issue_id = issue_handler.get_issue_id(request)
    if issue_id:
        db_issue: Issue = DBDiscussionSession.query(Issue).get(issue_id)
        request.validated['issue'] = db_issue
        return True

    add_error(request, 'Invalid issue {}'.format(issue_id))
    return False