<commit_before>import logging

from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_text
from django.db.models import Q

from collections import defaultdict
from awx.main.utils import getattrd
import _old_access as old_access

logger = logging.getLogger(__name__)

def log_migration(wrapped):
    '''setup the logging mechanism for each migration method
    as it runs, Django resets this, so we use a decorator
    to re-add the handler for each method.
    '''
    handler = logging.FileHandler("tower_rbac_migrations.log", mode="a", encoding="UTF-8")
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(formatter)

    def wrapper(*args, **kwargs):
        logger.handlers = []
        logger.addHandler(handler)
        return wrapped(*args, **kwargs)
    return wrapper

@log_migration
def migrate_users(apps, schema_editor):
    User = apps.get_model('auth', "User")
    Role = apps.get_model('main', "Role")
    RolePermission = apps.get_model('main', "RolePermission")

    for user in User.objects.iterator():
        try:
            Role.objects.get(content_type=ContentType.objects.get_for_model(User), object_id=user.id)
            logger.info(smart_text(u"found existing role for user: {}".format(user.username)))
        except Role.DoesNotExist:
            role = Role.objects.create(
                singleton_name = smart_text(u'{}-admin_role'.format(user.username)),
                content_object = user,
            )
            role.members.add(user)<commit_msg>fixes:#1371 - migration log write permissions<commit_after>import logging

from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_text
from django.db.models import Q

from collections import defaultdict
from awx.main.utils import getattrd
import _old_access as old_access

logger = logging.getLogger(__name__)

def log_migration(wrapped):
    '''setup the logging mechanism for each migration method
    as it runs, Django resets this, so we use a decorator
    to re-add the handler for each method.
    '''
    handler = logging.FileHandler("/tmp/tower_rbac_migrations.log", mode="a", encoding="UTF-8")
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(formatter)

    def wrapper(*args, **kwargs):
        logger.handlers = []
        logger.addHandler(handler)
        return wrapped(*args, **kwargs)
    return wrapper

@log_migration
def migrate_users(apps, schema_editor):
    User = apps.get_model('auth', "User")
    Role = apps.get_model('main', "Role")
    RolePermission = apps.get_model('main', "RolePermission")

    for user in User.objects.iterator():
        try:
            Role.objects.get(content_type=ContentType.objects.get_for_model(User), object_id=user.id)
            logger.info(smart_text(u"found existing role for user: {}".format(user.username)))
        except Role.DoesNotExist:
            role = Role.objects.create(
                singleton_name = smart_text(u'{}-admin_role'.format(user.username)),
                content_object = user,
            )
            role.members.add(user)
{'fraction_non_alphanumeric': 0.11239018479248712, 'fraction_numerical': 0.0018176310209027568, 'mean_word_length': 3.4441453566621805, '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': '4093771', 'n_tokens_mistral': 971, 'n_tokens_neox': 949, 'n_words': 227}