# Generated by Django 2.2.3 on 2019-07-15 19:24

from django.db import migrations, models


def backpopulate_incomplete_profiles(apps, schema):
    """Backpopulate users who don't have a profile record"""
    User = apps.get_model("users", "User")
    Profile = apps.get_model("users", "Profile")

    for user in User.objects.annotate(
        has_profile=models.Exists(Profile.objects.filter(user=models.OuterRef("pk")))
    ).filter(has_profile=False):
        Profile.objects.get_or_create(user=user)


def remove_incomplete_profiles(apps, schema):
    """Delete records that will cause rollbacks on nullable/blankable fields to fail"""
    Profile = apps.get_model("users", "Profile")

    Profile.objects.filter(
        models.Q(birth_year__isnull=True)
        | models.Q(gender__exact="")
        | models.Q(job_title__exact="")
        | models.Q(company__exact="")
    ).delete()


class Migration(migrations.Migration):

    dependencies = [("users", "0007_validate_country_and_state")]

    operations = [
        migrations.AlterField(
            model_name="profile",
            name="birth_year",
            field=models.IntegerField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name="profile",
            name="company",
            field=models.CharField(blank=True, default="", max_length=128),
        ),
        migrations.AlterField(
            model_name="profile",
            name="gender",
            field=models.CharField(
                blank=True,
                choices=[
                    ("m", "Male"),
                    ("f", "Female"),
                    ("o", "Other/Prefer Not to Say"),
                ],
                default="",
                max_length=10,
            ),
        ),
        migrations.AlterField(
            model_name="profile",
            name="industry",
            field=models.CharField(blank=True, default="", max_length=60),
        ),
        migrations.AlterField(
            model_name="profile",
            name="job_function",
            field=models.CharField(blank=True, default="", max_length=60),
        ),
        migrations.AlterField(
            model_name="profile",
            name="job_title",
            field=models.CharField(blank=True, default="", max_length=128),
        ),
        migrations.AlterField(
            model_name="profile",
            name="leadership_level",
            field=models.CharField(blank=True, default="", max_length=60),
        ),
        migrations.RunPython(
            backpopulate_incomplete_profiles, reverse_code=remove_incomplete_profiles
        ),
    ]

[(78, 'django.db.migrations.RunPython', 'migrations.RunPython', False, 'from django.db import migrations, models\n'), (37, 'django.db.models.IntegerField', 'models.IntegerField', False, 'from django.db import migrations, models\n'), (42, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import migrations, models\n'), (47, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import migrations, models\n'), (61, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import migrations, models\n'), (66, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import migrations, models\n'), (71, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import migrations, models\n'), (76, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import migrations, models\n'), (25, 'django.db.models.Q', 'models.Q', False, 'from django.db import migrations, models\n'), (24, 'django.db.models.Q', 'models.Q', False, 'from django.db import migrations, models\n'), (22, 'django.db.models.Q', 'models.Q', False, 'from django.db import migrations, models\n'), (23, 'django.db.models.Q', 'models.Q', False, 'from django.db import migrations, models\n'), (12, 'django.db.models.OuterRef', 'models.OuterRef', False, 'from django.db import migrations, models\n')]