# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-10-07 14:55
from __future__ import unicode_literals

from django.db import migrations
from django.conf import settings
from django.contrib.contenttypes.models import ContentType


def create_groups(apps, schema_editor):
    Version = apps.get_model('version', 'Version')
    Group = apps.get_model('auth', 'Group')
    Permission = apps.get_model('auth', 'Permission')

    group, _ = Group.objects.get_or_create(
        name=settings.REGISTERED_GROUP
    )
    try:
        perm = Permission.objects.get(codename="view_version")
        group.permissions.add(perm)
        group.save()
    except Permission.DoesNotExist:
        pass

def revert_groups(apps, schema_editor):
    Group = apps.get_model('auth', 'Group')
    Group.objects.filter(name=settings.REGISTERED_GROUP).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('version', '0016_register-group'),
    ]

    operations = [
        migrations.RunPython(
            create_groups,
            revert_groups
        ),
    ]
