From ef24a303e3e019acb08e72f1ab26dd144c299579 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 18 Oct 2018 11:28:21 +0200 Subject: [PATCH] Fixed a divide by zero error. --- backend/postings/models.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/backend/postings/models.py b/backend/postings/models.py index 69af1a2..cc3ffdc 100644 --- a/backend/postings/models.py +++ b/backend/postings/models.py @@ -44,23 +44,21 @@ class RateableEntity(models.Model): def getRatingDistribution(self): reviews = self.review_set.select_related() distribution = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]] - review_count = len(reviews) + review_count = reviews.count() for review in reviews: distribution[review.rating-1][0] += 1 - max_val = 0 - for rating_dist in distribution: - rating_dist[1] = (rating_dist[0] / review_count) * 100 - if (rating_dist[1] > max_val): - max_val = rating_dist[1] + if (review_count > 0): + max_val = 0 + for rating_dist in distribution: + rating_dist[1] = (rating_dist[0] / review_count) * 100 + if (rating_dist[1] > max_val): + max_val = rating_dist[1] - print(max_val) - print(distribution) - for rating_dist in distribution: - rating_dist[1] = (rating_dist[1] / max_val) * 100 + for rating_dist in distribution: + rating_dist[1] = (rating_dist[1] / max_val) * 100 - print(distribution) return distribution # Simply returns the name as the string representation.