From 176658c31968597b3157c14730a3d3fa4700128b Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 18 Oct 2018 10:04:24 +0200 Subject: [PATCH] Finished rating distribution. --- backend/postings/models.py | 8 ++++++++ .../templates/postings/frontend/entity.html | 20 +++++++++++++++---- backend/postings/views.py | 2 ++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/backend/postings/models.py b/backend/postings/models.py index 0054bed..de4c017 100644 --- a/backend/postings/models.py +++ b/backend/postings/models.py @@ -40,6 +40,14 @@ class RateableEntity(models.Model): return None return rating_sum / reviews.count() + # Gets a 5-item list of the count of each rating. + def getRatingDistribution(self): + reviews = self.review_set.select_related() + distribution = [0, 0, 0, 0, 0] + for review in reviews: + distribution[review.rating-1] += 1 + return distribution + # Simply returns the name as the string representation. def __str__(self): return self.name diff --git a/backend/postings/templates/postings/frontend/entity.html b/backend/postings/templates/postings/frontend/entity.html index 000ecdf..52c58aa 100644 --- a/backend/postings/templates/postings/frontend/entity.html +++ b/backend/postings/templates/postings/frontend/entity.html @@ -9,11 +9,13 @@ {% block content %}
-
-
-

{{ entity.name }} {{ entity.average }}

+ {% block entity_info %} +
+
+

{{ entity.name }}

+
-
+ {% endblock %}
@@ -22,6 +24,16 @@

{{ entity.average_rating|floatformat:"-2" }} / 5

+ {# Rating Distribution Display #} +
+
    +
  • 5: {{ entity.rating_distribution.4 }}
  • +
  • 4: {{ entity.rating_distribution.3 }}
  • +
  • 3: {{ entity.rating_distribution.2 }}
  • +
  • 2: {{ entity.rating_distribution.1 }}
  • +
  • 1: {{ entity.rating_distribution.0 }}
  • +
+
diff --git a/backend/postings/views.py b/backend/postings/views.py index 7ffeeaf..bf668c8 100644 --- a/backend/postings/views.py +++ b/backend/postings/views.py @@ -49,6 +49,8 @@ def rateable_entity(request, entity_id): # Set any auxiliary variables needed, like average rating. # 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.")