# -*- coding: utf-8 -*-
# Generated by Django 1.11.17 on 2021-01-06 14:09
from __future__ import unicode_literals

from django.db import migrations


def map_status(status):

    mapping = {
        'draft': 'draft',
        'submitted': 'draft',
        'needs_work': 'draft',
        'rejected': 'draft',
        'deleted': 'draft',
        'cancelled': 'draft',
        'expired': 'finished',
        'open': 'ready',
        'succeeded': 'finished',
        'full': 'full',
        'running': 'running'

    }
    return getattr(mapping, status, 'draft')

def migrate_to_slots(apps, schema_editor):
    DateActivity = apps.get_model('time_based', 'DateActivity')
    DateActivitySlot = apps.get_model('time_based', 'DateActivitySlot')

    for activity in DateActivity.object.all():
        status = map_status(activity.status)
        slot = DateActivitySlot.objects.create(
            status=status,
            activity_id=activity.id,
            start=activity.start,
            duration=activity.duration,
            is_online=activity.is_online,
            online_meeting_url=activity.online_meeting_url,
            location=activity.location,
            location_hint=activity.location_hint
        )

        if slot.status == 'draft' and slot.is_complete:
            slot.states.complete(save=True)


class Migration(migrations.Migration):

    dependencies = [
        ('time_based', '0046_auto_20210106_1507'),
    ]

    operations = [
        migrations.RunPython(
            migrate_to_slots,
            migrations.RunPython.noop
        )
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.17 on 2021-01-06 14:09
from __future__ import unicode_literals

from django.db import migrations


def map_status(status):

    mapping = {
        'draft': 'draft',
        'submitted': 'draft',
        'needs_work': 'draft',
        'rejected': 'draft',
        'deleted': 'draft',
        'cancelled': 'draft',
        'expired': 'finished',
        'open': 'ready',
        'succeeded': 'finished',
        'full': 'full',
        'running': 'running'

    }
    return getattr(mapping, status, 'draft')

def migrate_to_slots(apps, schema_editor):
    DateActivity = apps.get_model('time_based', 'DateActivity')
    DateActivitySlot = apps.get_model('time_based', 'DateActivitySlot')

    for activity in DateActivity.objects.all():
        status = map_status(activity.status)
        slot = DateActivitySlot.objects.create(
            status=status,
            activity_id=activity.id,
            start=activity.start,
            duration=activity.duration,
            is_online=activity.is_online,
            online_meeting_url=activity.online_meeting_url,
            location=activity.location,
            location_hint=activity.location_hint
        )

        if slot.status == 'draft' and slot.is_complete:
            slot.status = 'ready'
            slot.save()


class Migration(migrations.Migration):

    dependencies = [
        ('time_based', '0046_auto_20210106_1507'),
    ]

    operations = [
        migrations.RunPython(
            migrate_to_slots,
            migrations.RunPython.noop
        )
    ]

Fix pmigration