<commit_before>#!/usr/bin/env python

"""Import permissions, roles, and their relations from a JSON file.

:Copyright: 2006-2019 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

import json

import click

from byceps.services.authorization import service as authz_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context


@click.command()
@click.argument('json_file', type=click.File())
def execute(json_file):
    data = json.load(json_file)

    permissions = data['permissions']
    roles = data['roles']

    create_permissions(permissions)
    create_roles(roles)


def create_permissions(permissions):
    for permission in permissions:
        authz_service.create_permission(permission['id'], permission['title'])


def create_roles(roles):
    for role in roles:
        role_id = role['id']

        authz_service.create_role(role_id, role['title'])

        for permission_id in role['assigned_permissions']:
            authz_service.assign_permission_to_role(permission_id, role_id)<commit_msg>Display total of permissions and roles imported<commit_after>#!/usr/bin/env python

"""Import permissions, roles, and their relations from a JSON file.

:Copyright: 2006-2019 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

import json

import click

from byceps.services.authorization import service as authz_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context


@click.command()
@click.argument('json_file', type=click.File())
def execute(json_file):
    data = json.load(json_file)

    permissions = data['permissions']
    roles = data['roles']

    click.echo(f'Importing {len(permissions)} permissions ... ', nl=False)
    create_permissions(permissions)
    click.secho('done.', fg='green')

    click.echo(f'Importing {len(roles)} roles ... ', nl=False)
    create_roles(roles)
    click.secho('done.', fg='green')


def create_permissions(permissions):
    for permission in permissions:
        authz_service.create_permission(permission['id'], permission['title'])


def create_roles(roles):
    for role in roles:
        role_id = role['id']

        authz_service.create_role(role_id, role['title'])

        for permission_id in role['assigned_permissions']:
            authz_service.assign_permission_to_role(permission_id, role_id)
{'fraction_non_alphanumeric': 0.11059907834101383, 'fraction_numerical': 0.006702974444909929, 'mean_word_length': 4.632075471698113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5886460', 'n_tokens_mistral': 752, 'n_tokens_neox': 723, 'n_words': 171}