Al/small updates #18

Merged
andrewlalis merged 12 commits from al/small_updates into develop 2018-10-18 11:13:50 +00:00
3 changed files with 26 additions and 4 deletions
Showing only changes of commit 176658c319 - Show all commits

View File

@ -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

View File

@ -9,11 +9,13 @@
{% block content %}
<div class="container entity">
{% block entity_info %}
<div class="row">
<div class="col-sm-6">
<h1 class="muted text-center">{{ entity.name }} {{ entity.average }}</h1>
<h1 class="muted text-center">{{ entity.name }}</h1>
</div>
</div>
{% endblock %}
<hr>
<div class="row">
<div class="col-sm-3">
@ -22,6 +24,16 @@
<h2 class="bold padding-bottom-7">{{ entity.average_rating|floatformat:"-2" }} <small>/ 5</small></h2>
</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 class="row">

View File

@ -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.")