Finished rating distribution.

This commit is contained in:
Andrew Lalis 2018-10-18 10:04:24 +02:00
parent 9598983c15
commit 176658c319
3 changed files with 26 additions and 4 deletions

View File

@ -40,6 +40,14 @@ class RateableEntity(models.Model):
return None return None
return rating_sum / reviews.count() 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. # Simply returns the name as the string representation.
def __str__(self): def __str__(self):
return self.name return self.name

View File

@ -9,11 +9,13 @@
{% block content %} {% block content %}
<div class="container entity"> <div class="container entity">
<div class="row"> {% block entity_info %}
<div class="col-sm-6"> <div class="row">
<h1 class="muted text-center">{{ entity.name }} {{ entity.average }}</h1> <div class="col-sm-6">
<h1 class="muted text-center">{{ entity.name }}</h1>
</div>
</div> </div>
</div> {% endblock %}
<hr> <hr>
<div class="row"> <div class="row">
<div class="col-sm-3"> <div class="col-sm-3">
@ -22,6 +24,16 @@
<h2 class="bold padding-bottom-7">{{ entity.average_rating|floatformat:"-2" }} <small>/ 5</small></h2> <h2 class="bold padding-bottom-7">{{ entity.average_rating|floatformat:"-2" }} <small>/ 5</small></h2>
</div> </div>
</div> </div>
{# Rating Distribution Display #}
<div class="col-sm-3">
<ul>
<li>5: {{ entity.rating_distribution.4 }}</li>
<li>4: {{ entity.rating_distribution.3 }}</li>
<li>3: {{ entity.rating_distribution.2 }}</li>
<li>2: {{ entity.rating_distribution.1 }}</li>
<li>1: {{ entity.rating_distribution.0 }}</li>
</ul>
</div>
</div> </div>
<div class="row"> <div class="row">

View File

@ -49,6 +49,8 @@ def rateable_entity(request, entity_id):
# Set any auxiliary variables needed, like average rating. # Set any auxiliary variables needed, like average rating.
# This MUST be done after categorizing the object above. # This MUST be done after categorizing the object above.
entity.average_rating = entity.getAverageRating() entity.average_rating = entity.getAverageRating()
entity.rating_distribution = entity.getRatingDistribution()
print(entity.rating_distribution)
except RateableEntity.DoesNotExist: except RateableEntity.DoesNotExist:
raise Http404("RateableEntity with id " + str(entity_id) + " does not exist.") raise Http404("RateableEntity with id " + str(entity_id) + " does not exist.")