Migrate route difficulty to use new IntegerChoices
[climbing.kerkeslager.com] / src / climbing / migrations / 0007_pitch_difficulty.py
diff --git a/src/climbing/migrations/0007_pitch_difficulty.py b/src/climbing/migrations/0007_pitch_difficulty.py
new file mode 100644 (file)
index 0000000..0f95548
--- /dev/null
@@ -0,0 +1,80 @@
+# Generated by Django 4.0.3 on 2022-03-13 16:38
+
+from django.db import migrations, models
+
+ROUTE_DIFFICULTY_CHOICES = (
+    ('5.0', '5.0'),
+    ('5.1', '5.1'),
+    ('5.2', '5.2'),
+    ('5.3', '5.3'),
+    ('5.4', '5.4'),
+    ('5.5', '5.5'),
+    ('5.6', '5.6'),
+    ('5.6+', '5.6+'),
+    ('5.7', '5.7'),
+    ('5.7+', '5.7+'),
+    ('5.8', '5.8'),
+    ('5.8+', '5.8+'),
+    ('5.9-', '5.9-'),
+    ('5.9', '5.9'),
+    ('5.9+', '5.9+'),
+    ('5.10a', '5.10a'),
+    ('5.10b', '5.10b'),
+    ('5.10c', '5.10c'),
+    ('5.10d', '5.10d'),
+    ('5.11a', '5.11a'),
+    ('5.11b', '5.11b'),
+    ('5.11c', '5.11c'),
+    ('5.11d', '5.11d'),
+    ('5.12a', '5.12a'),
+    ('5.12b', '5.12b'),
+    ('5.12c', '5.12c'),
+    ('5.12d', '5.12d'),
+    ('5.13a', '5.13a'),
+    ('5.13b', '5.13b'),
+    ('5.13c', '5.13c'),
+    ('5.13d', '5.13d'),
+    ('5.14a', '5.14a'),
+    ('5.14b', '5.14b'),
+    ('5.14c', '5.14c'),
+    ('5.14d', '5.14d'),
+    ('5.15a', '5.15a'),
+    ('5.15b', '5.15b'),
+    ('5.15c', '5.15c'),
+    ('5.15d', '5.15d'),
+)
+
+def forward(apps, schema_editor):
+    Pitch = apps.get_model('climbing', 'Pitch')
+
+    difficulties = {}
+
+    for i, d in enumerate(ROUTE_DIFFICULTY_CHOICES):
+        difficulties[d[0]] = i
+
+    for p in Pitch.objects.all():
+        p.difficulty = difficulties[p.difficulty_old]
+        p.save()
+
+def backward(apps, schema_editor):
+    Pitch = apps.get_model('climbing', 'Pitch')
+
+    for p in Pitch.objects.all():
+        p.difficulty_old = ROUTE_DIFFICULTY_CHOICES[p.difficulty][0]
+        p.save()
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('climbing', '0006_change_difficulty_name'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='pitch',
+            name='difficulty',
+            field=models.PositiveIntegerField(choices=[(0, '5.0'), (1, '5.1'), (2, '5.2'), (3, '5.3'), (4, '5.4'), (5, '5.5'), (6, '5.6'), (7, '5.6+'), (8, '5.7'), (9, '5.7+'), (10, '5.8'), (11, '5.8+'), (12, '5.9-'), (13, '5.9'), (14, '5.9+'), (15, '5.10a'), (16, '5.10b'), (17, '5.10c'), (18, '5.10d'), (19, '5.11a'), (20, '5.11b'), (21, '5.11c'), (22, '5.11d'), (23, '5.12a'), (24, '5.12b'), (25, '5.12c'), (26, '5.12d'), (27, '5.13a'), (28, '5.13b'), (29, '5.13c'), (30, '5.13d'), (31, '5.14a'), (32, '5.14b'), (33, '5.14c'), (34, '5.14d'), (35, '5.15a'), (36, '5.15b'), (37, '5.15c'), (38, '5.15d')], default=0),
+            preserve_default=False,
+        ),
+        migrations.RunPython(forward, backward),
+    ]