<commit_before>import getpass
import django.contrib.auth.models as auth_models
import django.contrib.contenttypes as contenttypes

from django.core.management.base import BaseCommand, CommandError


def _get_password(use):
    print "*" * 80
    password = ""
    check_password = None

    while check_password != password:
        password = getpass.getpass("Please enter password for {0}: "
                                   .format(use))
        check_password = getpass.getpass("Please re-enter for confirmation: ")
        if check_password != password:
            print "Passwords don't match; please try again..."

    return password


def _create_or_update_user(name, email, permissions):
    if not auth_models.User.objects.filter(username=name).exists():
        password = _get_password(name)
        user = auth_models.User.objects.create_user(name, email, password)
    else:
        print "User '{0}' exists: not changing password".format(name)
        user = auth_models.User.objects.get(username=name)

    # Remove all permissions:
    user.user_permissions.clear()

    # Set to requested:
    for permission in permissions:
        user.user_permissions.add(permission)


def _configure_users():
    # Create dummy ContentType:
    ct = contenttypes.models.ContentType.objects.get_or_create(
        model='',
        app_label='toolkit'
    )[0]

    # Create 'write' permission:
    write_permission = auth_models.Permission.objects.get_or_create(
        name='Write access to all toolkit content',
        content_type=ct,
        codename='write'
    )[0]

    # Create 'read' permission:
    read_permission = auth_models.Permission.objects.get_or_create(
        name='Read access to all toolkit content',
        content_type=ct,
        codename='read'
    )[0]

    # retrieve permission for editing diary.models.RotaEntry rows:
    diary_content_type = contenttypes.models.ContentType.objects.get(
        app_label='diary',
        model='rotaentry',
    )

    edit_rota_permission = auth_models.Permission.objects.get(
        codename='change_rotaentry',
        content_type=diary_content_type
    )

    # Configure "admin" user with the read and write permissions:
    _create_or_update_user(
        "admin", '<EMAIL>',
        [write_permission, read_permission, edit_rota_permission])

    # Read only (and write to the rota) user:
    _create_or_update_user(
        "volunteer", 'toolkit_admin_readonly@localhost',
        [edit_rota_permission])


class Command(BaseCommand):
    args = ''
    help = 'Configure standard cube toolkit users'
<commit_msg>Give admin user wagtail admin permission<commit_after>import getpass
import django.contrib.auth.models as auth_models
import django.contrib.contenttypes as contenttypes

from django.core.management.base import BaseCommand, CommandError


def _get_password(use):
    print "*" * 80
    password = ""
    check_password = None

    while check_password != password:
        password = getpass.getpass("Please enter password for {0}: "
                                   .format(use))
        check_password = getpass.getpass("Please re-enter for confirmation: ")
        if check_password != password:
            print "Passwords don't match; please try again..."

    return password


def _create_or_update_user(name, email, permissions):
    if not auth_models.User.objects.filter(username=name).exists():
        password = _get_password(name)
        user = auth_models.User.objects.create_user(name, email, password)
    else:
        print "User '{0}' exists: not changing password".format(name)
        user = auth_models.User.objects.get(username=name)

    # Remove all permissions:
    user.user_permissions.clear()

    # Set to requested:
    for permission in permissions:
        user.user_permissions.add(permission)


def _configure_users():
    # Create dummy ContentType:
    ct = contenttypes.models.ContentType.objects.get_or_create(
        model='',
        app_label='toolkit'
    )[0]

    # Create 'write' permission:
    write_permission = auth_models.Permission.objects.get_or_create(
        name='Write access to all toolkit content',
        content_type=ct,
        codename='write'
    )[0]

    # Create 'read' permission:
    read_permission = auth_models.Permission.objects.get_or_create(
        name='Read access to all toolkit content',
        content_type=ct,
        codename='read'
    )[0]

    # retrieve permission for editing diary.models.RotaEntry rows:
    diary_content_type = contenttypes.models.ContentType.objects.get(
        app_label='diary',
        model='rotaentry',
    )

    edit_rota_permission = auth_models.Permission.objects.get(
        codename='change_rotaentry',
        content_type=diary_content_type
    )

    # wagtail admin permission:
    wagtail_content_type = contenttypes.models.ContentType.objects.get(
        model='admin',
        app_label='wagtailadmin'
    )
    wagtail_admin_permission = auth_models.Permission.objects.get(
        codename='access_admin',
        content_type=wagtail_content_type
    )

    # Configure "admin" user with the read and write permissions:
    _create_or_update_user(
        "admin", 'toolkit_admin@localhost',
        [write_permission, read_permission, edit_rota_permission,
         wagtail_admin_permission])

    # Read only (and write to the rota) user:
    _create_or_update_user(
        "volunteer", 'toolkit_admin_readonly@localhost',
        [edit_rota_permission])


class Command(BaseCommand):
    args = ''
    help = 'Configure standard cube toolkit users'

{'fraction_non_alphanumeric': 0.09350974432326122, 'fraction_numerical': 0.0025031289111389237, 'mean_word_length': 3.1071953010279003, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6721049', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1580, 'n_words': 361}