Migrate route difficulty to use new IntegerChoices
authorDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 13 Mar 2022 17:05:06 +0000 (13:05 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 13 Mar 2022 17:05:06 +0000 (13:05 -0400)
src/climbing/migrations/0006_change_difficulty_name.py [new file with mode: 0644]
src/climbing/migrations/0007_pitch_difficulty.py [new file with mode: 0644]
src/climbing/migrations/0008_remove_pitch_difficulty_old.py [new file with mode: 0644]
src/climbing/models.py

diff --git a/src/climbing/migrations/0006_change_difficulty_name.py b/src/climbing/migrations/0006_change_difficulty_name.py
new file mode 100644 (file)
index 0000000..25d494f
--- /dev/null
@@ -0,0 +1,27 @@
+# Generated by Django 4.0.3 on 2022-03-13 16:23
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('climbing', '0005_mountain_project_urls'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='pitch',
+            options={'verbose_name_plural': 'pitches'},
+        ),
+        migrations.RenameField(
+            model_name='pitch',
+            old_name='difficulty',
+            new_name='difficulty_old',
+        ),
+        migrations.AlterField(
+            model_name='pitch',
+            name='name',
+            field=models.CharField(blank=True, max_length=64, null=True),
+        ),
+    ]
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),
+    ]
diff --git a/src/climbing/migrations/0008_remove_pitch_difficulty_old.py b/src/climbing/migrations/0008_remove_pitch_difficulty_old.py
new file mode 100644 (file)
index 0000000..8f91b87
--- /dev/null
@@ -0,0 +1,17 @@
+# Generated by Django 4.0.3 on 2022-03-13 16:48
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('climbing', '0007_pitch_difficulty'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='pitch',
+            name='difficulty_old',
+        ),
+    ]
index dc1396a..67befd7 100644 (file)
@@ -47,75 +47,67 @@ class Route(models.Model):
             return self.name
 
         if pitch_count == 1:
-            return '{} {}'.format(self.name, self.difficulty)
+            return '{} {}'.format(self.name, self.difficulty_display)
 
         return '{} {} ({} pitches)'.format(
             self.name,
-            self.difficulty,
+            self.difficulty_display,
             pitch_count,
         )
 
     @property
     def difficulty(self):
-        diff = None
-        diff_index = -1
+        diff = -1
 
         for pitch in self.pitches.all():
-            p_diff_index = _route_difficulty_index(pitch.difficulty)
-
-            if p_diff_index > diff_index:
-                diff_index = p_diff_index
-                diff = pitch.difficulty
+            diff = max(diff, pitch.difficulty)
 
         return diff
 
-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 _route_difficulty_index(difficulty):
-    for i, d in enumerate(ROUTE_DIFFICULTY_CHOICES):
-        if difficulty == d[0]:
-            return i
-    return -1
+    @property
+    def difficulty_display(self):
+        return RouteDifficulty(self.difficulty).label
+
+class RouteDifficulty(models.IntegerChoices):
+    YDS_5_0 = 0, '5.0'
+    YDS_5_1 = 1, '5.1'
+    YDS_5_2 = 2, '5.2'
+    YDS_5_3 = 3, '5.3'
+    YDS_5_4 = 4, '5.4'
+    YDS_5_5 = 5, '5.5'
+    YDS_5_6 = 6, '5.6'
+    YDS_5_6p = 7, '5.6+'
+    YDS_5_7 = 8, '5.7'
+    YDS_5_7p = 9, '5.7+'
+    YDS_5_8 = 10, '5.8'
+    YDS_5_8p = 11, '5.8+'
+    YDS_5_9m = 12, '5.9-'
+    YDS_5_9 = 13, '5.9'
+    YDS_5_9p = 14, '5.9+'
+    YDS_5_10a = 15, '5.10a'
+    YDS_5_10b = 16, '5.10b'
+    YDS_5_10c = 17, '5.10c'
+    YDS_5_10d = 18, '5.10d'
+    YDS_5_11a = 19, '5.11a'
+    YDS_5_11b = 20, '5.11b'
+    YDS_5_11c = 21, '5.11c'
+    YDS_5_11d = 22, '5.11d'
+    YDS_5_12a = 23, '5.12a'
+    YDS_5_12b = 24, '5.12b'
+    YDS_5_12c = 25, '5.12c'
+    YDS_5_12d = 26, '5.12d'
+    YDS_5_13a = 27, '5.13a'
+    YDS_5_13b = 28, '5.13b'
+    YDS_5_13c = 29, '5.13c'
+    YDS_5_13d = 30, '5.13d'
+    YDS_5_14a = 31, '5.14a'
+    YDS_5_14b = 32, '5.14b'
+    YDS_5_14c = 33, '5.14c'
+    YDS_5_14d = 34, '5.14d'
+    YDS_5_15a = 35, '5.15a'
+    YDS_5_15b = 36, '5.15b'
+    YDS_5_15c = 37, '5.15c'
+    YDS_5_15d = 38, '5.15d'
 
 SAFETY_CHOICES = (
     ('G', 'G'),
@@ -132,7 +124,7 @@ class Pitch(models.Model):
         related_name='pitches',
     )
     name = models.CharField(max_length=64, blank=True, null=True)
-    difficulty = models.CharField(max_length=5, choices=ROUTE_DIFFICULTY_CHOICES)
+    difficulty = models.PositiveIntegerField(choices=RouteDifficulty.choices)
     safety = models.CharField(max_length=4, choices=SAFETY_CHOICES)
     notes = models.TextField(blank=True, null=True)