示例#1
0
def get_issues_overiew(db_user: User, app_url: str) -> dict:
    """
    Returns dictionary with keywords 'user' and 'others', which got lists with dicts with infos
    IMPORTANT: URL's are generated for the frontend!

    :param db_user: User
    :param app_url: current applications url
    :return: dict
    """

    if not db_user or db_user.nickname == nick_of_anonymous_user:
        return {'user': [], 'other': []}

    user.update_last_action(db_user)
    if db_user.is_admin():
        db_issues_other_users = DBDiscussionSession.query(Issue).filter(
            Issue.author_uid != db_user.uid).all()
    else:
        db_issues_other_users = get_visible_issues_for_user_as_query(
            db_user.uid).filter(Issue.author_uid != db_user.uid).all()

    db_issues_of_user = DBDiscussionSession.query(Issue).filter_by(
        author_uid=db_user.uid).order_by(Issue.uid.asc()).all()
    return {
        'user':
        [__create_issue_dict(issue, app_url) for issue in db_issues_of_user],
        'other': [
            __create_issue_dict(issue, app_url)
            for issue in db_issues_other_users
        ]
    }
示例#2
0
 def test_update_last_action(self):
     db_user = DBDiscussionSession.query(User).filter_by(
         nickname=str('Tobias')).first()
     last_action_old = db_user.last_action
     user.update_last_action(db_user)
     last_action_new = db_user.last_action
     self.assertNotEqual(last_action_old, last_action_new)
def delete_notifications(uids_list, db_user, ui_locales,
                         application_url) -> dict:
    """
    Simply deletes a specific notification

    :param uids_list: List of message ids which should be deleted
    :param db_user: User
    :param ui_locales: Language of current users session
    :param application_url: Url of the App
    :return: Dictionary with info and/or error
    """
    user_handler.update_last_action(db_user)
    _tn = Translator(ui_locales)

    for uid in uids_list:
        # inbox
        DBDiscussionSession.query(Message).filter(
            Message.uid == uid, Message.to_author_uid == db_user.uid,
            Message.is_inbox == True).delete()
        # send
        DBDiscussionSession.query(Message).filter(
            Message.uid == uid, Message.from_author_uid == db_user.uid,
            Message.is_inbox == False).delete()
    transaction.commit()
    prepared_dict = dict()
    prepared_dict['unread_messages'] = count_of_new_notifications(db_user)
    prepared_dict['total_in_messages'] = str(
        len(get_box_for(db_user, ui_locales, application_url, True)))
    prepared_dict['total_out_messages'] = str(
        len(get_box_for(db_user, ui_locales, application_url, False)))
    prepared_dict['success'] = _tn.get(_.messageDeleted)

    return prepared_dict
示例#4
0
def set_issue(db_user: User, info: str, long_info: str, title: str,
              db_lang: Language, is_public: bool, is_read_only: bool) -> dict:
    """
    Sets new issue, which will be a new discussion

    :param db_user: User
    :param info: Short information about the new issue
    :param long_info: Long information about the new issue
    :param title: Title of the new issue
    :param db_lang: Language
    :param is_public: Boolean
    :param is_read_only: Boolean
    :rtype: dict
    :return: Collection with information about the new issue
    """
    user.update_last_action(db_user)

    DBDiscussionSession.add(
        Issue(title=title,
              info=info,
              long_info=long_info,
              author_uid=db_user.uid,
              is_read_only=is_read_only,
              is_private=not is_public,
              lang_uid=db_lang.uid))
    DBDiscussionSession.flush()
    transaction.commit()
    db_issue = DBDiscussionSession.query(Issue).filter(
        Issue.title == title, Issue.info == info).first()

    return {'issue': get_issue_dict_for(db_issue, 0, db_lang.ui_locales)}
示例#5
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': ''
    }
示例#6
0
def set_positions_premise(db_issue: Issue, db_user: User,
                          db_conclusion: Statement,
                          premisegroups: List[List[str]], supportive: bool,
                          history: str, mailer) -> dict:
    """
    Set new premise for a given position and returns dictionary with url for the next step of the discussion

    :param mailer:
    :param history:
    :param supportive:
    :param premisegroups:
    :param db_conclusion:
    :param db_user:
    :param db_issue:
    :rtype: dict
    :return: Prepared collection with statement_uids of the new premises and an url or an error
    """
    user.update_last_action(db_user)

    prepared_dict = __process_input_of_start_premises(premisegroups,
                                                      db_conclusion,
                                                      supportive, db_issue,
                                                      db_user)
    if prepared_dict['error']:
        return prepared_dict

    __set_url_of_start_premises(prepared_dict, db_conclusion, supportive,
                                db_issue, db_user, history, mailer)
    __add_reputation(db_user, db_issue, prepared_dict['url'], prepared_dict)

    return prepared_dict
示例#7
0
def set_arguments_premises(db_issue: Issue, db_user: User, db_argument: Argument, premisegroups: List[List[str]],
                           attack_type: Relations, history, mailer) -> dict:
    """
    Set new premise for a given conclusion and returns dictionary with url for the next step of the discussion

    :param db_issue:
    :param db_user:
    :param db_argument:
    :param premisegroups:
    :param attack_type:
    :param history:
    :param mailer:
    :rtype: dict
    :return: Prepared collection with statement_uids of the new premises and next url or an error
    """
    # escaping will be done in QueryHelper().set_statement(...)
    langs = {'default_locale_name': db_issue.lang, 'discussion_lang': db_issue.lang}
    arg_infos = {
        'arg_id': db_argument.uid,
        'attack_type': attack_type,
        'premisegroups': premisegroups,
        'history': history
    }
    url, statement_uids, error = __process_input_premises_for_arguments_and_receive_url(langs, arg_infos, db_issue,
                                                                                        db_user, mailer)
    user.update_last_action(db_user)

    prepared_dict = {
        'error': error,
        'statement_uids': statement_uids
    }

    if url is None:
        return prepared_dict

    # add reputation
    had_access = has_access_to_review_system(db_user)
    rep_added = add_reputation_for(db_user, get_reason_by_action(ReputationReasons.first_new_argument))
    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'
        prepared_dict['url'] = url

    prepared_dict['url'] = url

    LOG.debug("Returning %s", prepared_dict)
    return prepared_dict
示例#8
0
    def test_update_last_action(self):
        user.update_last_action(self.user_tobi)
        self.assertFalse(user.update_last_action(self.user_tobi))

        old_ts = arrow.get(2016, 5, 5)
        self.user_tobi.last_action = old_ts
        self.user_tobi.last_login = old_ts
        DBDiscussionSession.add(self.user_tobi)
        settings = self.user_tobi.settings
        settings.should_hold_the_login(False)
        DBDiscussionSession.add(settings)
        transaction.commit()

        self.assertTrue(user.update_last_action(self.user_tobi))
        self.assertFalse(user.update_last_action(self.user_tobi))
示例#9
0
def main_admin(request):
    """
    View configuration for the content view. Only logged in user can reach this page.

    :param request: current webservers request
    :return: dictionary with title and project name as well as a value, weather the user is logged in
    """
    LOG.debug("def")
    db_user = request.validated['user']

    should_log_out = user.update_last_action(request.validated['user'])
    if should_log_out:
        return user_logout(request, True)

    ui_locales = get_language_from_cookie(request)
    extras_dict = DictionaryHelper(
        ui_locales).prepare_extras_dict_for_normal_page(
            request.registry, request.application_url, request.path, db_user)
    dashboard_elements = {
        'entities': lib.get_overview(request.path),
        'api_tokens': lib.get_application_tokens()
    }

    return {
        'language': str(ui_locales),
        'title': 'Admin' if db_user.is_admin() else '(B)admin',
        'project': project_name,
        'extras': extras_dict,
        'dashboard': dashboard_elements,
        'discussion': {
            'broke_limit': False
        }
    }
示例#10
0
def main_table(request):
    """
    View configuration for the content view. Only logged in user can reach this page.

    :param request: current webservers request
    :return: dictionary with title and project name as well as a value, weather the user is logged in
    """
    LOG.debug("def")
    should_log_out = user.update_last_action(request.validated['user'])
    if should_log_out:
        return user_logout(request, True)

    ui_locales = get_language_from_cookie(request)
    extras_dict = DictionaryHelper(
        ui_locales).prepare_extras_dict_for_normal_page(
            request.registry, request.application_url, request.path,
            request.validated['user'])
    table_name = request.matchdict['table']
    if not table_name.lower() in lib.table_mapper:
        return exception_response(400)
    table_dict = lib.get_table_dict(table_name, request.application_url)

    return {
        'language': str(ui_locales),
        'title': 'Admin - ' + table_name,
        'project': project_name,
        'extras': extras_dict,
        'table': table_dict,
        'discussion': {
            'broke_limit': False
        }
    }
示例#11
0
def set_arguments_premises(db_issue: Issue, db_user: User,
                           db_argument: Argument,
                           premisegroups: List[List[str]],
                           attack_type: Relations, history, mailer) -> dict:
    """
    Set new premise for a given conclusion and returns dictionary with url for the next step of the discussion

    :param data: dict if requests came via the API
    :rtype: dict
    :return: Prepared collection with statement_uids of the new premises and next url or an error
    """
    # escaping will be done in QueryHelper().set_statement(...)
    langs = {
        'default_locale_name': db_issue.lang,
        'discussion_lang': db_issue.lang
    }
    arg_infos = {
        'arg_id': db_argument.uid,
        'attack_type': attack_type,
        'premisegroups': premisegroups,
        'history': history
    }
    url, statement_uids, error = __process_input_premises_for_arguments_and_receive_url(
        langs, arg_infos, db_issue, db_user, mailer)
    user.update_last_action(db_user)

    prepared_dict = {'error': error, 'statement_uids': statement_uids}

    if url == -1:
        return prepared_dict

    # add reputation
    add_rep, broke_limit = add_reputation_for(db_user,
                                              rep_reason_first_new_argument)
    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'
        prepared_dict['url'] = url

    prepared_dict['url'] = url

    logger('ArgumentsHelper', 'returning {}'.format(prepared_dict))
    return prepared_dict
示例#12
0
def preparation_for_view(request):
    """
    Does some elementary things like: getting nickname, session id and history.
    Additionally boolean, if the session is expired

    :param request: Current request
    :return: nickname, session_id, session_expired, history
    """
    session_expired = user.update_last_action(request.validated['user'])
    return request.authenticated_userid, session_expired
示例#13
0
def read_notifications(uids_list, db_user) -> dict:
    """
    Simply marks a notification as read

    :param uids_list: List of message ids notification which should be marked as read
    :param db_user: User
    :return: Dictionary with info and/or error
    """
    prepared_dict = dict()
    user_handler.update_last_action(db_user)

    for uid in uids_list:
        DBDiscussionSession.query(Message).filter(
            Message.uid == uid, Message.to_author_uid == db_user.uid,
            Message.is_inbox == True).first().set_read(True)
    transaction.commit()
    prepared_dict['unread_messages'] = count_of_new_notifications(db_user)
    prepared_dict['error'] = ''

    return prepared_dict
示例#14
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': ''
    }
示例#15
0
def __call_from_discussion_step(request, f: Callable[[Any, Any, Any], Any]):
    """
    Checks for an expired session, the authentication and calls f the users nickname.
    On error an HTTPNotFound-Error is raised, otherwise the discussion dict is returned.

    :param request: A pyramid request
    :param f: A function with three arguments
    :return: prepared collection for the discussion
    """
    logger('Views', 'def')
    session_expired = user.update_last_action(request.validated['user'])
    if session_expired:
        request.session.invalidate()
        headers = forget(request)
        location = request.application_url + 'discuss?session_expired=true',
        return HTTPFound(location=location, headers=headers)

    request_dict = prepare_request_dict(request)
    prepared_discussion = f(request_dict)
    if prepared_discussion:
        __modify_discussion_url(prepared_discussion)
        __modify_discussion_bubbles(prepared_discussion)

    return prepared_discussion, request_dict
 def test_update_last_action(self):
     last_action_old = self.user_tobi.last_action
     user.update_last_action(self.user_tobi)
     last_action_new = self.user_tobi.last_action
     self.assertNotEqual(last_action_old, last_action_new)