# Generated by Django 2.1 on 2018-08-26 17:27

from django.db import migrations, models
from django.db.models import Q


def determine_entry_state(apps, schema_editor):
    """Go through all EventRequests, EventSubmissions and DC Self-org event
    requests and determine their new `state` field value from `active`
    field."""

    EventRequest = apps.get_model('workshops', 'EventRequest')
    EventSubmission = apps.get_model('workshops', 'EventSubmission')
    DCSelfOrganizedEventRequest = apps.get_model('workshops',
                                                 'DCSelfOrganizedEventRequest')

    q1 = Q(active=True)
    q2 = Q(active=False, event=None)
    q3 = Q(active=False) & ~Q(event=None)

    # case 1: entry is active (== pending)
    EventRequest.objects.filter(q1).update(state='p')
    EventSubmission.objects.filter(q1).update(state='p')
    DCSelfOrganizedEventRequest.objects.filter(q1).update(state='p')

    # case 2: entry is not active, and has no related event (== discarded)
    EventRequest.objects.filter(q2).update(state='d')
    EventSubmission.objects.filter(q2).update(state='d')
    DCSelfOrganizedEventRequest.objects.filter(q2).update(state='d')

    # case 3: entry is not active, but has a related event (== accepted)
    EventRequest.objects.filter(q3).update(state='a')
    EventSubmission.objects.filter(q3).update(state='a')
    DCSelfOrganizedEventRequest.objects.filter(q3).update(state='a')


class Migration(migrations.Migration):

    dependencies = [
        ('workshops', '0148_event_link_field'),
    ]

    operations = [
        migrations.AddField(
            model_name='dcselforganizedeventrequest',
            name='state',
            field=models.CharField(choices=[('p', 'Pending'), ('d', 'Discarded'), ('a', 'Accepted')], default='p', max_length=1),
        ),
        migrations.AddField(
            model_name='eventrequest',
            name='state',
            field=models.CharField(choices=[('p', 'Pending'), ('d', 'Discarded'), ('a', 'Accepted')], default='p', max_length=1),
        ),
        migrations.AddField(
            model_name='eventsubmission',
            name='state',
            field=models.CharField(choices=[('p', 'Pending'), ('d', 'Discarded'), ('a', 'Accepted')], default='p', max_length=1),
        ),
        migrations.RunPython(determine_entry_state),
        migrations.RemoveField(
            model_name='dcselforganizedeventrequest',
            name='active',
        ),
        migrations.RemoveField(
            model_name='eventrequest',
            name='active',
        ),
        migrations.RemoveField(
            model_name='eventsubmission',
            name='active',
        ),
        migrations.AlterField(
            model_name='trainingrequest',
            name='underresourced',
            field=models.BooleanField(blank=True, default=False, help_text='The Carpentries strive to make workshops accessible to as many people as possible, in as wide a variety of situations as possible.', verbose_name='This is a small, remote, or under-resourced institution'),
        ),
    ]
