# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2017-08-04 20:35
from __future__ import unicode_literals

from django.db import migrations
from apps.user_group.models import UserGroupType


def add_owner_group_for_app(apps, schema_editor):
    type_model = apps.get_model('user_group', 'UserGroupType')
    group_model = apps.get_model('user_group', 'UserGroup')
    app_model = apps.get_model('app', 'App')

    owner_type = type_model.objects.get(name=UserGroupType.OWNER)
    for app in app_model.objects.all():
        if not group_model.objects.filter(type=owner_type, app=app).exists():
            group_model.objects.create(type=owner_type, app=app, name=app.name + '_owner', creator=app.creator)


class Migration(migrations.Migration):

    dependencies = [
        ('user_group', '0009_auto_20170802_1818'),
    ]

    operations = [
        migrations.RunPython(add_owner_group_for_app, reverse_code=migrations.RunPython.noop),
    ]
