<commit_before>"""User management for administrators.

Admins can list users, delete user, grant or revoke admin permissions.
TODO(apugachev) Consider having admin role id in settigns as a constant.
TODO(apugachev) Look for a simpler way of deciding if user is admin.
TODO(apugachev) Add warnings for destroying operations.
TODO(apugachev) Add navigation inside this blueprint's pages.
"""

import urlparse

from flask import request, redirect, url_for, flash, current_app
from flask import blueprints
from flaskext import principal

from C4GD_web.clients import clients
from C4GD_web.views.forms import DeleteUserForm, AddUserToProject, \
    RemoveUserFromProject 
from C4GD_web.views import pagination
from C4GD_web.views.pagination import Pagination
from C4GD_web.utils import user_tenants_list, user_tenants_with_roles_list


bp = blueprints.Blueprint(
    'global_user_management', __name__, url_prefix='/global/users')


@bp.before_request
def authorize():
    principal.Permission(('role', 'admin')).test()


def get_admin_role_id():
    """Return ID of Admin role.

    :raises: RuntimeError if Admin roles does not exist
    """
    for role in clients.keystone.roles.list():
        if role.name == current_app.config['ADMIN_ROLE_NAME']:
            return role.id
    else:
        raise RuntimeError, 'Admin role does not exist'

    
def get_member_role_id():
    """Return ID of Member role.

    :raises: RuntimeError if Member roles does not exist
    """
    for role in clients.keystone.roles.list():
        if role.name == 'Member':
            return role.id
    else:
        raise RuntimeError, 'Member role does not exist'


@bp.route('/', methods=['GET'])
def index():
    """List users.

    TODO(apugachev): find way to count users without fetching all users.
    This would allow to use marker and limit to fetch one page only.
    """
    page = int(request.args.get('page', 1))
    users = clients.keystone.users.list(limit=1000000)
    p = pagination.Pagination(users)<commit_msg>sort users by name<commit_after>"""User management for administrators.

Admins can list users, delete user, grant or revoke admin permissions.
TODO(apugachev) Consider having admin role id in settigns as a constant.
TODO(apugachev) Look for a simpler way of deciding if user is admin.
TODO(apugachev) Add warnings for destroying operations.
TODO(apugachev) Add navigation inside this blueprint's pages.
"""

import urlparse

from flask import request, redirect, url_for, flash, current_app
from flask import blueprints
from flaskext import principal

from C4GD_web.clients import clients
from C4GD_web.views.forms import DeleteUserForm, AddUserToProject, \
    RemoveUserFromProject 
from C4GD_web.views import pagination
from C4GD_web.views.pagination import Pagination
from C4GD_web.utils import user_tenants_list, user_tenants_with_roles_list


bp = blueprints.Blueprint(
    'global_user_management', __name__, url_prefix='/global/users')


@bp.before_request
def authorize():
    principal.Permission(('role', 'admin')).test()


def get_admin_role_id():
    """Return ID of Admin role.

    :raises: RuntimeError if Admin roles does not exist
    """
    for role in clients.keystone.roles.list():
        if role.name == current_app.config['ADMIN_ROLE_NAME']:
            return role.id
    else:
        raise RuntimeError, 'Admin role does not exist'

    
def get_member_role_id():
    """Return ID of Member role.

    :raises: RuntimeError if Member roles does not exist
    """
    for role in clients.keystone.roles.list():
        if role.name == 'Member':
            return role.id
    else:
        raise RuntimeError, 'Member role does not exist'


@bp.route('/', methods=['GET'])
def index():
    """List users.

    TODO(apugachev): find way to count users without fetching all users.
    This would allow to use marker and limit to fetch one page only.
    """
    page = int(request.args.get('page', 1))
    users = sorted(clients.keystone.users.list(limit=1000000), key=lambda x: x.name)
    p = pagination.Pagination(users)
{'fraction_non_alphanumeric': 0.09115545700911555, 'fraction_numerical': 0.006405518600640552, 'mean_word_length': 3.8564593301435406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2250278', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1208, 'n_words': 409}