# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2019-06-21 14:53
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
from django.utils.crypto import get_random_string


def populate_multi_use_invitations(apps, schema_editor):
    MultiUseInvitation = apps.get_model('itk_invitations', 'MultiUseInvitation')
    Group = apps.get_model('auth', 'Group')

    for group in Group.objects.all():
        multi_use_invitation = MultiUseInvitation(group=group)
        multi_use_invitation.key = get_random_string(64).lower()
        multi_use_invitation.save()


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('auth', '0008_alter_user_username_max_length'),
        ('itk_invitations', '0016_auto_20180226_1538'),
    ]

    operations = [
        migrations.CreateModel(
            name='MultiUseInvitation',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('key', models.CharField(max_length=64, unique=True)),
                ('group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='auth.Group')),
            ],
        ),
        migrations.CreateModel(
            name='MultiUseInvitationUsers',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('date_added', models.DateTimeField(auto_now_add=True)),
                ('group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='auth.Group')),
                ('multi_use_invitation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='itk_invitations.MultiUseInvitation')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.RunPython(populate_multi_use_invitations)
    ]
