# Generated by Django 2.1.7 on 2019-07-07 18:47

from django.db import migrations, models


def change_default_for_existing_users(apps, schema_editor):
    """Change default value in `lesson_publication_consent` for users, who
    have no record of changing their profile after this field was introduced
    in migration 'workshops.0165_person_lesson_publication_consent'.

    This function uses a few tricks for find out who has no recorded changes
    after aforementioned migration was applied."""

    Person = apps.get_model('workshops', 'Person')

    # 1. find out migration application timestamp
    from django.db.migrations.recorder import MigrationRecorder

    # MigrationRecorder.Migration is a model for all migrations, their metadata
    # is stored in DB in a special table
    migration = MigrationRecorder.Migration.objects.get(
        app='workshops',
        name='0165_person_lesson_publication_consent',
    )

    # 2. get all users who still have 'no' as their consent
    no_consent = Person.objects.filter(lesson_publication_consent='no')

    # 3. from these users, find out who has any revision made after migration
    #    application date
    users_no_changes = []
    for user in no_consent.prefetch_related('revision_set'):
        if not user.revision_set.filter(date_created__gt=migration.applied):
            users_no_changes.append(user.pk)

    Person.objects.filter(pk__in=users_no_changes) \
                  .update(lesson_publication_consent='unset')


class Migration(migrations.Migration):

    dependencies = [
        ('workshops', '0184_auto_20190416_1837'),
    ]

    operations = [
        migrations.AlterField(
            model_name='person',
            name='lesson_publication_consent',
            field=models.CharField(blank=True, choices=[('yes-profile', 'Yes, and use the name associated with my profile'), ('yes-orcid', 'Yes, and use the name associated with my ORCID profile'), ('yes-github', 'Yes, and only use my GitHub handle'), ('no', 'No'), ('unset', 'Unset')], default='unset', help_text='When we publish our lessons, we like to include everyone who has contributed via pull request as an author. If you do make any contributions, would you like to be included as an author when we publish the lesson?', max_length=40, verbose_name='Do you consent to have your name or identity associated with lesson publications?'),
        ),
        migrations.RunPython(change_default_for_existing_users),
    ]

# Generated by Django 2.1.7 on 2019-07-07 18:47

from django.db import migrations, models


def change_default_for_existing_users(apps, schema_editor):
    """Change default value in `lesson_publication_consent` for users, who
    have no record of changing their profile after this field was introduced
    in migration 'workshops.0165_person_lesson_publication_consent'.

    This function uses a few tricks for find out who has no recorded changes
    after aforementioned migration was applied."""

    Person = apps.get_model('workshops', 'Person')

    # 1. find out migration application timestamp
    from django.db.migrations.recorder import MigrationRecorder

    # MigrationRecorder.Migration is a model for all migrations, their metadata
    # is stored in DB in a special table
    migration = MigrationRecorder.Migration.objects.get(
        app='workshops',
        name='0165_person_lesson_publication_consent',
    )

    # 2. get all users who still have 'no' as their consent
    no_consent = Person.objects.filter(lesson_publication_consent='no')

    # 3. from these users, find out who has any revision made after migration
    #    application date

    # CAUTION: this nice set of instructions is INCOMPATIBLE with SQLite
    #          when the number of objects is just huge... Therefore I had
    #          to switch to lesser-SQL-greedy version, much much slower too,
    #          below.
    # users_no_changes = []
    # for user in no_consent.prefetch_related('revision_set'):
    #     if not user.revision_set.filter(date_created__gt=migration.applied):
    #         users_no_changes.append(user.pk)
    # Person.objects.filter(pk__in=users_no_changes) \
    #               .update(lesson_publication_consent='unset')

    for user in no_consent:
        if not user.revision_set.filter(date_created__gt=migration.applied):
            user.lesson_publication_consent = 'unset'
            user.save()


class Migration(migrations.Migration):

    dependencies = [
        ('workshops', '0184_auto_20190416_1837'),
    ]

    operations = [
        migrations.AlterField(
            model_name='person',
            name='lesson_publication_consent',
            field=models.CharField(blank=True, choices=[('yes-profile', 'Yes, and use the name associated with my profile'), ('yes-orcid', 'Yes, and use the name associated with my ORCID profile'), ('yes-github', 'Yes, and only use my GitHub handle'), ('no', 'No'), ('unset', 'Unset')], default='unset', help_text='When we publish our lessons, we like to include everyone who has contributed via pull request as an author. If you do make any contributions, would you like to be included as an author when we publish the lesson?', max_length=40, verbose_name='Do you consent to have your name or identity associated with lesson publications?'),
        ),
        migrations.RunPython(change_default_for_existing_users),
    ]

Switch migration to way slower code