# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-06-09 13:47
from __future__ import unicode_literals

from django.db import migrations


def create_basic_groups(apps, schema):
    Permission = apps.get_model("auth", "Permission")
    Group = apps.get_model("auth", "Group")

    oauth_providers_permissions = [
        'view_person',
        'add_authorization',
        'change_authorization',
        'delete_authorization',
        'view_client',
    ]

    workers_permissions = [
        'add_person', 'change_person', 'view_person',
        'add_persontag', 'change_persontag', 'delete_persontag',
        'add_calendar', 'change_calendar', 'delete_calendar',
        'add_event', 'change_event',
        'add_eventtag', 'change_eventtag', 'delete_eventtag',
        'add_rsvp', 'change_rsvp', 'delete_rsvp', 'view_rsvp',
        'add_membership', 'change_membership', 'delete_membership', 'view_membership',
        'add_supportgroup', 'change_supportgroup',
        'add_supportgrouptag', 'change_supportgrouptag', 'delete_supportgrouptag',
    ]

    oauth_providers = Group.objects.create(name='oauth_providers')
    oauth_providers.permissions.add(*Permission.objects.filter(codename__in=oauth_providers_permissions))

    workers = Group.objects.create(name='workers')
    workers.permissions.add(*Permission.objects.filter(codename__in=workers_permissions))


def delete_basic_groups(apps, schema):
    Group = apps.get_model("auth", "Group")

    Group.objects.filter(name__in=['oauth_providers', 'workers']).delete()


class Migration(migrations.Migration):
    dependencies = [
        ('authentication', '0001_initial'),
        # depends on auth models to be created
        ('auth', '0008_alter_user_username_max_length'),
        # depends on needed permissions actually existing
        ('events', '0001_initial'),
        ('groups', '0001_initial'),
        ('people', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_basic_groups, delete_basic_groups, atomic=True)
    ]

@@ -121,28 +121,309 @@
 ngo.
-db import migrations
+contrib.auth.management import create_permissions%0Afrom django.db import migrations%0A%0A%0Adef make_permissions(apps):%0A    for app_config in apps.get_app_configs():%0A        app_config.models_module = True%0A        create_permissions(app_config, apps=apps, verbosity=0)%0A        app_config.models_module = None
 %0A%0A%0Ad
@@ -456,24 +456,51 @@
 s, schema):%0A
+    make_permissions(apps)%0A
     Permissi
@@ -2215,24 +2215,61 @@
 _initial'),%0A
+        ('clients', '0001_initial'),%0A
     %5D%0A%0A    o

fix create basic groups migration