From 3c0e8af223c2b04fd1f225835025cb02cab57a54 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 18 Aug 2017 09:22:39 +0200 Subject: [PATCH] Fixed division by 0 error in calc_imputations --- stages/models.py | 6 +++--- stages/tests.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/stages/models.py b/stages/models.py index 1dff52b..1bba56b 100644 --- a/stages/models.py +++ b/stages/models.py @@ -114,13 +114,13 @@ class Teacher(models.Model): for key in imputations: imputations[key] += round(imputations[key] / tot * activities['tot_formation']) - # Split EDE périods in EDEpe and EDEps columns, in proportion + # Split EDE periods in EDEpe and EDEps columns, in proportion ede = courses.filter(imputation='EDE').aggregate(models.Sum('period'))['period__sum'] or 0 if ede > 0: pe = imputations['EDEpe'] ps = imputations['EDEps'] - pe_percent = pe / (pe + ps) - pe_plus = pe * pe_percent + pe_percent = (pe / (pe + ps)) if (pe + ps) > 0 else 0.5 + pe_plus = round(ede * pe_percent) imputations['EDEpe'] += pe_plus imputations['EDEps'] += ede - pe_plus diff --git a/stages/tests.py b/stages/tests.py index 0c9ad8e..02cde00 100644 --- a/stages/tests.py +++ b/stages/tests.py @@ -218,6 +218,16 @@ class TeacherTests(TestCase): result = self.teacher.calc_imputations() self.assertEqual(result[1]['ASSC'], 9) self.assertEqual(result[1]['EDEpe'], 5) + # Test with only EDE data + t2 = Teacher.objects.create( + first_name='Isidore', last_name='Gluck', birth_date='1986-01-01' + ) + Course.objects.create( + teacher=t2, period=5, subject='Cours EDE', imputation='EDE', + ) + result = t2.calc_imputations() + self.assertEqual(result[1]['EDEpe'], 2) + self.assertEqual(result[1]['EDEps'], 3) def test_export_imputations(self): self.client.login(username='me', password='mepassword')