# Generated by Django 3.1.7 on 2021-04-09 09:04
from django.conf import settings
from django.db import migrations


def populate_picture_identifier(apps, schema_editor):
    obs_id = settings.TROPP_OBSERVATORY_ID or ""
    Picture = apps.get_model("terra_opp", "Picture")

    for picture in Picture.objects.all():
        viewpoint = picture.viewpoint
        pic_index = list(
            viewpoint.pictures.order_by("date").values_list("id", flat=True)
        ).index(picture.id)
        pic_index += 1  # list index start at 0 & we want the first to be 1
        picture.identifier = int(f"{obs_id}0{viewpoint.id:03}{pic_index:02}")
        picture.save()


class Migration(migrations.Migration):

    dependencies = [
        ("terra_opp", "0010_picture_identifier"),
    ]

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