diff --git a/backend/postings/models.py b/backend/postings/models.py index f6ca417..69af1a2 100644 --- a/backend/postings/models.py +++ b/backend/postings/models.py @@ -40,12 +40,27 @@ class RateableEntity(models.Model): return None return rating_sum / reviews.count() - # Gets a 5-item list of the count of each rating. + # Gets a 5-item list of the count of each rating, and the percentage of total votes. def getRatingDistribution(self): reviews = self.review_set.select_related() - distribution = [0, 0, 0, 0, 0] + distribution = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]] + review_count = len(reviews) + for review in reviews: - distribution[review.rating-1] += 1 + 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] + + print(max_val) + print(distribution) + 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. diff --git a/backend/postings/templates/postings/frontend/entity.html b/backend/postings/templates/postings/frontend/entity.html index 7eb64a0..3977c31 100644 --- a/backend/postings/templates/postings/frontend/entity.html +++ b/backend/postings/templates/postings/frontend/entity.html @@ -25,18 +25,6 @@ {# Rating Distribution Display #} - - - -

Rating breakdown

@@ -45,12 +33,12 @@
-
+
80% Complete (danger)
-
{{ entity.rating_distribution.4 }}
+
{{ entity.rating_distribution.4.0 }}
@@ -58,12 +46,12 @@
-
+
80% Complete (danger)
-
{{ entity.rating_distribution.3 }}
+
{{ entity.rating_distribution.3.0 }}
@@ -71,12 +59,12 @@
-
+
80% Complete (danger)
-
{{ entity.rating_distribution.2 }}
+
{{ entity.rating_distribution.2.0 }}
@@ -84,12 +72,12 @@
-
+
80% Complete (danger)
-
{{ entity.rating_distribution.1 }}
+
{{ entity.rating_distribution.1.0 }}
@@ -97,12 +85,12 @@
-
+
80% Complete (danger)
-
{{ entity.rating_distribution.0 }}
+
{{ entity.rating_distribution.0.0 }}
diff --git a/backend/postings/views.py b/backend/postings/views.py index 51f26f4..c98473d 100644 --- a/backend/postings/views.py +++ b/backend/postings/views.py @@ -50,7 +50,6 @@ def rateable_entity(request, entity_id): # This MUST be done after categorizing the object above. entity.average_rating = entity.getAverageRating() entity.rating_distribution = entity.getRatingDistribution() - print(entity.rating_distribution) except RateableEntity.DoesNotExist: raise Http404("RateableEntity with id " + str(entity_id) + " does not exist.")