# Generated by Django 5.1.2 on 2024-11-04 21:17

import django.db.migrations.operations.special
import metadata.models
from django.db import migrations, models


def gen_ratio(apps, schema_editor):
    Scale = apps.get_model('metadata', 'Scale')
    for row in Scale.objects.all():
        row.ratio_int = metadata.models.calculate_ratio(row.ratio)
        row.save(update_fields=['ratio_int'])


def convert_tarcks(apps, schema_editor):
    Scale = apps.get_model("metadata", "Scale")
    for row in Scale.objects.all():
        row.tracks = "".join(
            filter(
                lambda x: str.isdigit(x) or x == "." or x == ",",
                row.tracks
            )
        )
        row.save(update_fields=["tracks"])


class Migration(migrations.Migration):

    dependencies = [
        ('metadata', '0017_alter_property_private'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='decoder',
            options={'ordering': ['manufacturer__name', 'name']},
        ),
        migrations.AlterModelOptions(
            name='scale',
            options={'ordering': ['ratio_int', 'scale']},
        ),
        migrations.AddField(
            model_name='scale',
            name='ratio_int',
            field=models.SmallIntegerField(default=0, editable=False),
        ),
        migrations.RunPython(
            code=gen_ratio,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        migrations.AlterField(
            model_name='scale',
            name='ratio',
            field=models.CharField(max_length=16, validators=[metadata.models.calculate_ratio]),
        ),
        migrations.AlterModelOptions(
            name='scale',
            options={'ordering': ['-ratio_int', '-tracks', 'scale']},
        ),
        migrations.RunPython(
            code=convert_tarcks,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        migrations.AlterField(
            model_name='scale',
            name='tracks',
            field=models.FloatField(help_text='Distance between model tracks in mm'),
        ),
    ]
