# Generated by Django 4.0.4 on 2022-05-31 14:10

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
from django_scopes import scopes_disabled


def migrate_space_permissions(apps, schema_editor):
    with scopes_disabled():
        UserPreference = apps.get_model('cookbook', 'UserPreference')
        UserSpace = apps.get_model('cookbook', 'UserSpace')

        for up in UserPreference.objects.exclude(space=None).all():
            us = UserSpace.objects.create(user=up.user, space=up.space, active=True)
            us.groups.set(up.user.groups.all())


class Migration(migrations.Migration):
    dependencies = [
        ('auth', '0012_alter_user_first_name_max_length'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('cookbook', '0173_recipe_source_url'),
    ]

    operations = [
        migrations.AlterField(
            model_name='food',
            name='substitute',
            field=models.ManyToManyField(blank=True, to='cookbook.food'),
        ),
        migrations.CreateModel(
            name='UserSpace',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('active', models.BooleanField(default=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('groups', models.ManyToManyField(to='auth.group')),
                ('space', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cookbook.space')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.RunPython(migrate_space_permissions)
    ]
