Al/small updates #17
|
@ -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
|
||||
|
|
|
@ -9,11 +9,13 @@
|
|||
|
||||
{% block content %}
|
||||
<div class="container entity">
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
<h1 class="muted text-center">{{ entity.name }} {{ entity.average }}</h1>
|
||||
{% block entity_info %}
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
<h1 class="muted text-center">{{ entity.name }}</h1>
|
||||
</div>
|
||||
</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">
|
||||
|
|
|
@ -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.")
|
||||
|
||||
|
|
Loading…
Reference in New Issue