From e400980103a63b325703670ca434a8f8be1cba40 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Tue, 21 Nov 2023 11:07:36 +0100
Subject: [PATCH] Frictions: Fix plot drawing crash.

---
 src/Model/Friction/Friction.py       |  9 +++++++++
 src/View/Frictions/PlotStricklers.py | 13 +++++++++++--
 src/tools.py                         |  5 +----
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/Model/Friction/Friction.py b/src/Model/Friction/Friction.py
index 22c4a789..db1a60db 100644
--- a/src/Model/Friction/Friction.py
+++ b/src/Model/Friction/Friction.py
@@ -129,6 +129,15 @@ class Friction(SQLSubModel):
     def has_edge(self):
         return self._edge is not None
 
+    def has_coefficient(self):
+        return (
+            self._begin_strickler is not None and
+            self._end_strickler is not None
+        )
+
+    def is_full_defined(self):
+        return self.has_edge() and self.has_coefficient()
+
     @property
     def begin_kp(self):
         return self._begin_kp
diff --git a/src/View/Frictions/PlotStricklers.py b/src/View/Frictions/PlotStricklers.py
index 2f180e68..6e5c4e0c 100644
--- a/src/View/Frictions/PlotStricklers.py
+++ b/src/View/Frictions/PlotStricklers.py
@@ -16,6 +16,8 @@
 
 # -*- coding: utf-8 -*-
 
+import logging
+
 from tools import timer, flatten
 from View.Tools.PamhyrPlot import PamhyrPlot
 
@@ -23,13 +25,20 @@ from PyQt5.QtCore import (
     QCoreApplication
 )
 
+logger = logging.getLogger()
+
 _translate = QCoreApplication.translate
 
 
 class PlotStricklers(PamhyrPlot):
     def draw_frictions(self, frictions, color="r"):
-        lst = frictions
-        lst.sort(key=lambda s: s.begin_kp)
+        lst = sorted(
+            filter(
+                lambda f: f.is_full_defined(),
+                frictions
+            ),
+            key=lambda s: s.begin_kp
+        )
 
         coef = flatten(
             map(
diff --git a/src/tools.py b/src/tools.py
index bd413d37..d3c2ddcc 100644
--- a/src/tools.py
+++ b/src/tools.py
@@ -199,10 +199,7 @@ def flatten(lst):
     Returns:
         returns a list of element
     """
-    if not lst:
-        return []
-
-    return reduce(list.__add__, lst)
+    return reduce(list.__add__, lst, [])
 
 
 def timestamp(dt: datetime):
-- 
GitLab