# Generated by Django 2.2.18 on 2021-03-25 10:28

from django.db import migrations
from collections import defaultdict

state_map = defaultdict(
    lambda: "draft",
    {
        300: "accepted",
        200: "submited",
        -1: "refused",
    },
)

old_state_map = defaultdict(
    lambda: 100,
    {
        "accepted": 300,
        "submited": 200,
        "refused": -1,
    },
)


def migrate_picture_states(app, schema_editor):
    Picture = app.get_model("terra_opp", "Picture")
    for picture in Picture.objects.all():
        picture.state = state_map[picture.old_state]
        picture.save()


def reverse_migrate_picture_states(app, schema_editor):
    Picture = app.get_model("terra_opp", "Picture")
    for picture in Picture.objects.all():
        picture.old_state = old_state_map[picture.state]
        picture.save()


class Migration(migrations.Migration):

    dependencies = [
        ("terra_opp", "0007_auto_20210325_1022"),
    ]

    operations = [
        migrations.RunPython(
            migrate_picture_states, reverse_code=reverse_migrate_picture_states
        ),
        migrations.RemoveField(
            model_name="picture",
            name="old_state",
        ),
    ]
