# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2018-06-07 12:19
from __future__ import unicode_literals

from django.contrib.auth.models import Group, Permission, User
from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations

def create_group(name):
    group, created = Group.objects.get_or_create(name=name);
    if created:
        print ('... created group "{}"'.format (name))
        return group
    else:
        print ('!!! group "{}" already exists'.format(name))
        return False

def delete_group(name):
    try:
        Group.objects.get(name=name).delete ();
        print ('... deleted group "{}"'.format(name));
    except ObjectDoesNotExist:
        print ('!!! group "{}" doesn\'t exist'.format(name));

def init_teams_and_permissions(apps, schema_editor):
    print ();

    board = create_group('Bundesvorstand');
    team  = create_group('Prüfungsteam');

    if team:
        for user in User.objects.filter(is_staff=True, is_active=True):
            user.groups.add (team);
            print ('... added all staff members to group "Prüfungsteam"');

        permission = Permission.objects.get(name='Can add moderation')
        team.permissions.add (permission);
        print ('... added permission for group "Prüfungsteam" to add moderations');

def reverse_teams_and_permissions(apps, schema_editor):
    print ();

    for user in User.objects.filter(groups__name='Prüfungsteam', is_active=True):
        user.is_staff = True;
        user.save();
    print ('... made all members of group "Prüfungsteam" staff members');

    delete_group('Prüfungsteam');
    delete_group('Bundesvorstand');

class Migration(migrations.Migration):

    dependencies = [
        ('initproc', '0024_merge_20180510_1558'),
    ]

    operations = [
        migrations.RunPython(init_teams_and_permissions,
                             reverse_code=reverse_teams_and_permissions),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2018-06-07 12:19
from __future__ import unicode_literals

from django.contrib.auth.models import Group, Permission, User
from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations

def create_group(name):
    group, created = Group.objects.get_or_create(name=name);
    if created:
        print ('... created group "{}"'.format (name))
        return group
    else:
        print ('!!! group "{}" already exists'.format(name))
        return False

def delete_group(name):
    try:
        Group.objects.get(name=name).delete ();
        print ('... deleted group "{}"'.format(name));
    except ObjectDoesNotExist:
        print ('!!! group "{}" doesn\'t exist'.format(name));

def init_teams_and_permissions(apps, schema_editor):
    print ();

    board = create_group('Bundesvorstand');
    team  = create_group('Prüfungsteam');

    if team:
        for user in User.objects.filter(is_staff=True, is_active=True):
            user.groups.add (team);
            print ('... added all staff members to group "Prüfungsteam"');

        permission = Permission.objects.get(content_type__app_label='initproc', codename='add_moderation')
        team.permissions.add (permission);
        print ('... added permission for group "Prüfungsteam" to add moderations');

def reverse_teams_and_permissions(apps, schema_editor):
    print ();

    for user in User.objects.filter(groups__name='Prüfungsteam', is_active=True):
        user.is_staff = True;
        user.save();
    print ('... made all members of group "Prüfungsteam" staff members');

    delete_group('Prüfungsteam');
    delete_group('Bundesvorstand');

class Migration(migrations.Migration):

    dependencies = [
        ('initproc', '0024_merge_20180510_1558'),
    ]

    operations = [
        migrations.RunPython(init_teams_and_permissions,
                             reverse_code=reverse_teams_and_permissions),
    ]

use codename instead of name for moderation permission