Fixed distribution.

This commit is contained in:
Andrew Lalis 2018-10-18 11:06:55 +02:00
parent 7814cf6626
commit f5dbbd9742
3 changed files with 28 additions and 26 deletions

View File

@ -40,12 +40,27 @@ 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. # Gets a 5-item list of the count of each rating, and the percentage of total votes.
def getRatingDistribution(self): def getRatingDistribution(self):
reviews = self.review_set.select_related() 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: 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 return distribution
# Simply returns the name as the string representation. # Simply returns the name as the string representation.

View File

@ -25,18 +25,6 @@
</div> </div>
</div> </div>
{# Rating Distribution Display #} {# 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 class="col-sm-3"> <div class="col-sm-3">
<h4>Rating breakdown</h4> <h4>Rating breakdown</h4>
<div class="pull-left"> <div class="pull-left">
@ -45,12 +33,12 @@
</div> </div>
<div class="pull-left" style="width:180px;"> <div class="pull-left" style="width:180px;">
<div class="progress" style="height:9px; margin:8px 0;"> <div class="progress" style="height:9px; margin:8px 0;">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="5" aria-valuemin="0" aria-valuemax="5" style="width: 0%"> <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="5" aria-valuemin="0" aria-valuemax="5" style="width: {{ entity.rating_distribution.4.1 }}%">
<span class="sr-only">80% Complete (danger)</span> <span class="sr-only">80% Complete (danger)</span>
</div> </div>
</div> </div>
</div> </div>
<div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.4 }}</div> <div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.4.0 }}</div>
</div> </div>
<div class="pull-left"> <div class="pull-left">
<div class="pull-left" style="width:35px; line-height:1;"> <div class="pull-left" style="width:35px; line-height:1;">
@ -58,12 +46,12 @@
</div> </div>
<div class="pull-left" style="width:180px;"> <div class="pull-left" style="width:180px;">
<div class="progress" style="height:9px; margin:8px 0;"> <div class="progress" style="height:9px; margin:8px 0;">
<div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="4" aria-valuemin="0" aria-valuemax="5" style="width: 33%"> <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="4" aria-valuemin="0" aria-valuemax="5" style="width: {{ entity.rating_distribution.3.1 }}%">
<span class="sr-only">80% Complete (danger)</span> <span class="sr-only">80% Complete (danger)</span>
</div> </div>
</div> </div>
</div> </div>
<div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.3 }}</div> <div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.3.0 }}</div>
</div> </div>
<div class="pull-left"> <div class="pull-left">
<div class="pull-left" style="width:35px; line-height:1;"> <div class="pull-left" style="width:35px; line-height:1;">
@ -71,12 +59,12 @@
</div> </div>
<div class="pull-left" style="width:180px;"> <div class="pull-left" style="width:180px;">
<div class="progress" style="height:9px; margin:8px 0;"> <div class="progress" style="height:9px; margin:8px 0;">
<div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="3" aria-valuemin="0" aria-valuemax="5" style="width: 33%"> <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="3" aria-valuemin="0" aria-valuemax="5" style="width: {{ entity.rating_distribution.2.1 }}%">
<span class="sr-only">80% Complete (danger)</span> <span class="sr-only">80% Complete (danger)</span>
</div> </div>
</div> </div>
</div> </div>
<div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.2 }}</div> <div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.2.0 }}</div>
</div> </div>
<div class="pull-left"> <div class="pull-left">
<div class="pull-left" style="width:35px; line-height:1;"> <div class="pull-left" style="width:35px; line-height:1;">
@ -84,12 +72,12 @@
</div> </div>
<div class="pull-left" style="width:180px;"> <div class="pull-left" style="width:180px;">
<div class="progress" style="height:9px; margin:8px 0;"> <div class="progress" style="height:9px; margin:8px 0;">
<div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="2" aria-valuemin="0" aria-valuemax="5" style="width: 33%"> <div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="2" aria-valuemin="0" aria-valuemax="5" style="width: {{ entity.rating_distribution.1.1 }}%">
<span class="sr-only">80% Complete (danger)</span> <span class="sr-only">80% Complete (danger)</span>
</div> </div>
</div> </div>
</div> </div>
<div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.1 }}</div> <div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.1.0 }}</div>
</div> </div>
<div class="pull-left"> <div class="pull-left">
<div class="pull-left" style="width:35px; line-height:1;"> <div class="pull-left" style="width:35px; line-height:1;">
@ -97,12 +85,12 @@
</div> </div>
<div class="pull-left" style="width:180px;"> <div class="pull-left" style="width:180px;">
<div class="progress" style="height:9px; margin:8px 0;"> <div class="progress" style="height:9px; margin:8px 0;">
<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="1" aria-valuemin="0" aria-valuemax="5" style="width: 0"> <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="1" aria-valuemin="0" aria-valuemax="5" style="width: {{ entity.rating_distribution.0.1 }}%">
<span class="sr-only">80% Complete (danger)</span> <span class="sr-only">80% Complete (danger)</span>
</div> </div>
</div> </div>
</div> </div>
<div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.0 }}</div> <div class="pull-right" style="margin-left:10px;">{{ entity.rating_distribution.0.0 }}</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -50,7 +50,6 @@ def rateable_entity(request, entity_id):
# 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() 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.")