Refactor calculation of route grade
authorDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 4 Mar 2022 19:06:13 +0000 (14:06 -0500)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 4 Mar 2022 19:06:13 +0000 (14:06 -0500)
src/climbing/models.py

index dd2493c..dc1396a 100644 (file)
@@ -61,12 +61,11 @@ class Route(models.Model):
         diff_index = -1
 
         for pitch in self.pitches.all():
-            for index, diff_choice in enumerate(ROUTE_DIFFICULTY_CHOICES):
-                if pitch.difficulty == diff_choice[0]:
-                    if diff_index < index:
-                        diff = pitch.difficulty
-                        diff_index = index
-                    break
+            p_diff_index = _route_difficulty_index(pitch.difficulty)
+
+            if p_diff_index > diff_index:
+                diff_index = p_diff_index
+                diff = pitch.difficulty
 
         return diff
 
@@ -112,6 +111,12 @@ ROUTE_DIFFICULTY_CHOICES = (
     ('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
+
 SAFETY_CHOICES = (
     ('G', 'G'),
     ('PG', 'PG'),