From ba9ef0371a9aa4cb2b76df36fa392eb01d8e5551 Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <ynerant@crans.org>
Date: Sun, 5 Sep 2021 20:36:17 +0200
Subject: [PATCH] [WEI] Run algorithm only on valid surveys

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
---
 apps/wei/forms/surveys/wei2021.py             | 1 +
 apps/wei/management/commands/wei_algorithm.py | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/apps/wei/forms/surveys/wei2021.py b/apps/wei/forms/surveys/wei2021.py
index b0cfb0cb..a6a241cb 100644
--- a/apps/wei/forms/surveys/wei2021.py
+++ b/apps/wei/forms/surveys/wei2021.py
@@ -170,6 +170,7 @@ class WEISurveyAlgorithm2021(WEISurveyAlgorithm):
         We modify it to allow buses to have multiple "weddings".
         """
         surveys = list(self.get_survey_class()(r) for r in self.get_registrations())  # All surveys
+        surveys = [s for s in surveys if s.is_complete()]
         free_surveys = [s for s in surveys if not s.information.valid]  # Remaining surveys
         while free_surveys:  # Some students are not affected
             survey = free_surveys[0]
diff --git a/apps/wei/management/commands/wei_algorithm.py b/apps/wei/management/commands/wei_algorithm.py
index 558dfae4..238bf13c 100644
--- a/apps/wei/management/commands/wei_algorithm.py
+++ b/apps/wei/management/commands/wei_algorithm.py
@@ -28,7 +28,8 @@ class Command(BaseCommand):
 
         output = options['output']
         registrations = algorithm.get_registrations()
-        per_bus = {bus: [r for r in registrations if r.information['selected_bus_pk'] == bus.pk]
+        per_bus = {bus: [r for r in registrations if 'selected_bus_pk' in r.information
+                         and r.information['selected_bus_pk'] == bus.pk]
                    for bus in algorithm.get_buses()}
         for bus, members in per_bus.items():
             output.write(bus.name + "\n")
-- 
GitLab