<commit_before>#
# ========================================================
# Copyright (c) 2012 Whamcloud, Inc.  All rights reserved.
# ========================================================


from django.contrib.auth.models import User
from django.db.models.signals import post_syncdb
from django.contrib.contenttypes.models import ContentType
import django.contrib.auth as auth
import settings

from chroma_core.models import ManagedHost, ManagedTarget, ManagedFilesystem, StorageResourceRecord
from chroma_core.models import Job, Command, Volume, VolumeNode

# Ensure that the auto post_syncdb hook is installed
# before our hook, so that Permission objects will be there
# by the time we are called.
import django.contrib.auth.management


def setup_groups(sender, **kwargs):
    if auth.models.Group.objects.count() == 0:
        print "Creating groups..."
        auth.models.Group.objects.create(name = "superusers")
        fsadmin_group = auth.models.Group.objects.create(name = "filesystem_administrators")

        def grant_write(group, model):
            for perm in auth.models.Permission.objects.filter(content_type = ContentType.objects.get_for_model(model)):
                group.permissions.add(perm)

        grant_write(fsadmin_group, ManagedTarget)
        grant_write(fsadmin_group, ManagedHost)
        grant_write(fsadmin_group, ManagedFilesystem)
        grant_write(fsadmin_group, StorageResourceRecord)
        grant_write(fsadmin_group, Job)
        grant_write(fsadmin_group, Command)
        grant_write(fsadmin_group, Volume)
        grant_write(fsadmin_group, VolumeNode)
        grant_write(fsadmin_group, User)

        fsusers_group = auth.models.Group.objects.create(name = "filesystem_users")
        # For modifying his own account
        grant_write(fsusers_group, User)

    if settings.DEBUG and auth.models.User.objects.count() == 0:
        print "***\n" * 3,
        print "*** SECURITY WARNING: You are running in DEBUG mode and default users have been created"
        print "***\n" * 3
        user = auth.models.User.objects.create_superuser("debug", "<EMAIL>", "chr0m4_d3bug")
        user.groups.add(auth.models.Group.objects.get(name='superusers'))
        user = auth.models.User.objects.create_user("admin", "<EMAIL>", "chr0m4_d3bug")
        user.groups.add(auth.models.Group.objects.get(name='filesystem_administrators'))


post_syncdb.connect(setup_groups, sender=auth.models)<commit_msg>Fix permissions setup: previously the setup_groups function was getting called once after the contrib.auth syncdb, resulting in permissions for 'filesystem_administrators' not getting created for chroma_core models.<commit_after>#
# ========================================================
# Copyright (c) 2012 Whamcloud, Inc.  All rights reserved.
# ========================================================


from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
import django.contrib.auth as auth
from south.signals import post_migrate

from chroma_core.models import ManagedHost, ManagedTarget, ManagedFilesystem, StorageResourceRecord
from chroma_core.models import Job, Command, Volume, VolumeNode

import settings


def setup_groups(app, **kwargs):
    if app != 'chroma_core':
        return

    if auth.models.Group.objects.count() == 0:
        print "Creating groups..."
        auth.models.Group.objects.create(name = "superusers")
        fsadmin_group = auth.models.Group.objects.create(name = "filesystem_administrators")

        def grant_write(group, model):
            for perm in auth.models.Permission.objects.filter(content_type = ContentType.objects.get_for_model(model)):
                group.permissions.add(perm)

        grant_write(fsadmin_group, ManagedTarget)
        grant_write(fsadmin_group, ManagedHost)
        grant_write(fsadmin_group, ManagedFilesystem)
        grant_write(fsadmin_group, StorageResourceRecord)
        grant_write(fsadmin_group, Job)
        grant_write(fsadmin_group, Command)
        grant_write(fsadmin_group, Volume)
        grant_write(fsadmin_group, VolumeNode)
        grant_write(fsadmin_group, User)

        fsusers_group = auth.models.Group.objects.create(name = "filesystem_users")
        # For modifying his own account
        grant_write(fsusers_group, User)

    if settings.DEBUG and auth.models.User.objects.count() == 0:
        print "***\n" * 3,
        print "*** SECURITY WARNING: You are running in DEBUG mode and default users have been created"
        print "***\n" * 3
        user = auth.models.User.objects.create_superuser("debug", "<EMAIL>", "chr0m4_d3bug")
        user.groups.add(auth.models.Group.objects.get(name='superusers'))
        user = auth.models.User.objects.create_user("admin", "<EMAIL>", "chr0m4_d3bug")
        user.groups.add(auth.models.Group.objects.get(name='filesystem_administrators'))


## Ensure that the auto post_syncdb hook is installed
## before our hook, so that Permission objects will be there
## by the time we are called.
import django.contrib.auth.management

post_migrate.connect(setup_groups)
{'fraction_non_alphanumeric': 0.1377222113694081, 'fraction_numerical': 0.005469818323891385, 'mean_word_length': 4.054294175715696, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5444375', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1416, 'n_words': 339}