Shift over rest of choices to IntegerChoices main
authorDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 13 Mar 2022 17:29:10 +0000 (13:29 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 13 Mar 2022 17:29:10 +0000 (13:29 -0400)
src/climbing/migrations/0009_rename_safety_pitch_safety_old_and_more.py [new file with mode: 0644]
src/climbing/migrations/0010_pitch_safety_problem_difficulty_problem_safety.py [new file with mode: 0644]
src/climbing/migrations/0011_remove_pitch_safety_old_and_more.py [new file with mode: 0644]
src/climbing/models.py

diff --git a/src/climbing/migrations/0009_rename_safety_pitch_safety_old_and_more.py b/src/climbing/migrations/0009_rename_safety_pitch_safety_old_and_more.py
new file mode 100644 (file)
index 0000000..c314934
--- /dev/null
@@ -0,0 +1,28 @@
+# Generated by Django 4.0.3 on 2022-03-13 17:15
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('climbing', '0008_remove_pitch_difficulty_old'),
+    ]
+
+    operations = [
+        migrations.RenameField(
+            model_name='pitch',
+            old_name='safety',
+            new_name='safety_old',
+        ),
+        migrations.RenameField(
+            model_name='problem',
+            old_name='difficulty',
+            new_name='difficulty_old',
+        ),
+        migrations.RenameField(
+            model_name='problem',
+            old_name='safety',
+            new_name='safety_old',
+        ),
+    ]
diff --git a/src/climbing/migrations/0010_pitch_safety_problem_difficulty_problem_safety.py b/src/climbing/migrations/0010_pitch_safety_problem_difficulty_problem_safety.py
new file mode 100644 (file)
index 0000000..c7dd7f7
--- /dev/null
@@ -0,0 +1,97 @@
+# Generated by Django 4.0.3 on 2022-03-13 17:16
+
+from django.db import migrations, models
+
+SAFETY_CHOICES = (
+    ('G', 'G'),
+    ('PG', 'PG'),
+    ('PG13', 'PG13'),
+    ('R', 'R'),
+    ('X', 'X'),
+)
+
+BOULDER_DIFFICULTY_CHOICES = (
+    ('V0', 'V0'),
+    ('V1', 'V1'),
+    ('V2', 'V2'),
+    ('V3', 'V3'),
+    ('V4', 'V4'),
+    ('V5', 'V5'),
+    ('V6', 'V6'),
+    ('V7', 'V7'),
+    ('V8', 'V8'),
+    ('V9', 'V9'),
+    ('V10', 'V10'),
+    ('V11', 'V11'),
+    ('V12', 'V12'),
+    ('V13', 'V13'),
+    ('V14', 'V14'),
+    ('V15', 'V15'),
+    ('V16', 'V16'),
+    ('V17', 'V17'),
+)
+
+def forward(app, schema_editor):
+    Pitch = app.get_model('climbing', 'Pitch')
+    Problem = app.get_model('climbing', 'Problem')
+
+    safety_choices = {
+        s[0]: i
+        for i, s in enumerate(SAFETY_CHOICES)
+    }
+
+    difficulty_choices = {
+        d[0]: i
+        for i, d in enumerate(BOULDER_DIFFICULTY_CHOICES)
+    }
+
+    for p in Pitch.objects.all():
+        p.safety = safety_choices[p.safety_old]
+        p.save()
+
+    for p in Problem.objects.all():
+        p.difficulty = difficulty_choices[p.difficulty_old]
+        p.safety = safety[p.safety_old]
+        p.save()
+
+def backward(app, schema_editor):
+    Pitch = app.get_model('climbing', 'Pitch')
+    Problem = app.get_model('climbing', 'Problem')
+
+    for p in Pitch.objects.all():
+        p.safety_old = SAFETY_CHOICES[p.safety][0]
+        p.save()
+
+    for p in Problem.objects.all():
+        p.difficulty_old = BOULDER_DIFFICULTY_CHOICES[p.difficulty][0]
+        p.safety_old = SAFETY_CHOICES[p.safety][0]
+        p.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('climbing', '0009_rename_safety_pitch_safety_old_and_more'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='pitch',
+            name='safety',
+            field=models.IntegerField(choices=[(0, 'G'), (1, 'PG'), (2, 'PG13'), (3, 'R'), (4, 'X')], default=0),
+            preserve_default=False,
+        ),
+        migrations.AddField(
+            model_name='problem',
+            name='difficulty',
+            field=models.IntegerField(choices=[(0, 'V0'), (1, 'V1'), (2, 'V2'), (3, 'V3'), (4, 'V4'), (5, 'V5'), (6, 'V6'), (7, 'V7'), (8, 'V8'), (9, 'V9'), (10, 'V10'), (11, 'V11'), (12, 'V12'), (13, 'V13'), (14, 'V14'), (15, 'V15'), (16, 'V16'), (17, 'V17')], default=0),
+            preserve_default=False,
+        ),
+        migrations.AddField(
+            model_name='problem',
+            name='safety',
+            field=models.IntegerField(choices=[(0, 'G'), (1, 'PG'), (2, 'PG13'), (3, 'R'), (4, 'X')], default=0),
+            preserve_default=False,
+        ),
+        migrations.RunPython(forward, backward),
+    ]
diff --git a/src/climbing/migrations/0011_remove_pitch_safety_old_and_more.py b/src/climbing/migrations/0011_remove_pitch_safety_old_and_more.py
new file mode 100644 (file)
index 0000000..e84dfa0
--- /dev/null
@@ -0,0 +1,25 @@
+# Generated by Django 4.0.3 on 2022-03-13 17:25
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('climbing', '0010_pitch_safety_problem_difficulty_problem_safety'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='pitch',
+            name='safety_old',
+        ),
+        migrations.RemoveField(
+            model_name='problem',
+            name='difficulty_old',
+        ),
+        migrations.RemoveField(
+            model_name='problem',
+            name='safety_old',
+        ),
+    ]
index 67befd7..69bbc9f 100644 (file)
@@ -109,13 +109,12 @@ class RouteDifficulty(models.IntegerChoices):
     YDS_5_15c = 37, '5.15c'
     YDS_5_15d = 38, '5.15d'
 
-SAFETY_CHOICES = (
-    ('G', 'G'),
-    ('PG', 'PG'),
-    ('PG13', 'PG13'),
-    ('R', 'R'),
-    ('X', 'X'),
-)
+class Safety(models.IntegerChoices):
+    G = 0, 'G'
+    PG = 1, 'PG'
+    PG13 = 2, 'PG13'
+    R = 3, 'R'
+    X = 4, 'X'
 
 class Pitch(models.Model):
     route = models.ForeignKey(
@@ -125,7 +124,7 @@ class Pitch(models.Model):
     )
     name = models.CharField(max_length=64, blank=True, null=True)
     difficulty = models.PositiveIntegerField(choices=RouteDifficulty.choices)
-    safety = models.CharField(max_length=4, choices=SAFETY_CHOICES)
+    safety = models.IntegerField(choices=Safety.choices)
     notes = models.TextField(blank=True, null=True)
 
     class Meta:
@@ -160,26 +159,26 @@ class Boulder(models.Model):
     def __str__(self):
         return self.name
 
-BOULDER_DIFFICULTY_CHOICES = (
-    ('V0', 'V0'),
-    ('V1', 'V1'),
-    ('V2', 'V2'),
-    ('V3', 'V3'),
-    ('V4', 'V4'),
-    ('V5', 'V5'),
-    ('V6', 'V6'),
-    ('V7', 'V7'),
-    ('V8', 'V8'),
-    ('V9', 'V9'),
-    ('V10', 'V10'),
-    ('V11', 'V11'),
-    ('V12', 'V12'),
-    ('V13', 'V13'),
-    ('V14', 'V14'),
-    ('V15', 'V15'),
-    ('V16', 'V16'),
-    ('V17', 'V17'),
-)
+class BoulderDifficylty(models.IntegerChoices):
+    V0 = 0, 'V0'
+    V1 = 1, 'V1'
+    V2 = 2, 'V2'
+    V3 = 3, 'V3'
+    V4 = 4, 'V4'
+    V5 = 5, 'V5'
+    V6 = 6, 'V6'
+    V7 = 7, 'V7'
+    V8 = 8, 'V8'
+    V9 = 9, 'V9'
+    V10 = 10, 'V10'
+    V11 = 11, 'V11'
+    V12 = 12, 'V12'
+    V13 = 13, 'V13'
+    V14 = 14, 'V14'
+    V15 = 15, 'V15'
+    V16 = 16, 'V16'
+    V17 = 17, 'V17'
+
 
 class Problem(models.Model):
     boulder = models.ForeignKey(
@@ -188,8 +187,8 @@ class Problem(models.Model):
         related_name='problems',
     )
     name = models.CharField(max_length=64)
-    difficulty = models.CharField(max_length=3, choices=BOULDER_DIFFICULTY_CHOICES)
-    safety = models.CharField(max_length=4, choices=SAFETY_CHOICES)
+    difficulty = models.IntegerField(choices=BoulderDifficylty.choices)
+    safety = models.IntegerField(choices=Safety.choices)
     mountain_project = models.URLField(blank=True, null=True)
     notes = models.TextField(blank=True, null=True)