Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PNI] Fix bug with "people voted" label on product pages #11389

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% load i18n %}

<span class="tw-body-small">
{% with vote_data=product.votes.get_vote_average %}
{% with vote_data=product.average_bin %}
<a class="tw-body-small" href="#creepiness-vote">{% trans "People voted:" %}</a>
<span class="people-voted {{ vote_data.label|slugify }}">{{ vote_data }}</span>
<span class="people-voted {{ vote_data.label|slugify }}">{{ vote_data.localized }}</span>
{% endwith %}
</span>
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,26 @@ def labelled_creepiness_per_bin(self):
}
return creepiness_per_bin

@property
def average_bin(self):
"""Name of the bin corresponding to the average vote"""
total_votes = self.total_votes
# If there's no votes, let the user know (also protects from division by zero errors)
if total_votes == 0:
return {
"label": "No votes",
"localized": gettext("No votes"),
}

average_vote = self.average_creepiness
mode_bin = round(average_vote / 20) - 1
label = self.BIN_LABELS[f"bin_{mode_bin}"]

return {
"label": label["key"],
"localized": label["label"],
}


class ProductPageCategory(TranslatableMixin, Orderable):
product = ParentalKey(
Expand Down Expand Up @@ -741,7 +761,10 @@ def annotated_evaluation(self):
if not self.evaluation:
return None
return (
ProductPageEvaluation.objects.with_total_creepiness().with_average_creepiness().get(pk=self.evaluation.pk)
ProductPageEvaluation.objects.with_total_votes()
.with_total_creepiness()
.with_average_creepiness()
.get(pk=self.evaluation.pk)
)

@property
Expand All @@ -756,6 +779,10 @@ def creepiness(self):
except AttributeError:
return self.annotated_evaluation.average_creepiness

@property
def average_bin(self):
return self.annotated_evaluation.average_bin

@property
def get_voting_json(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ def test_creepiness_per_bin(self):
self.assertEqual(evaluation.average_creepiness, 50)
self.assertEqual(evaluation.total_creepiness, 250)
self.assertEqual(evaluation.total_votes, 5)
self.assertDictEqual(
evaluation.average_bin, {"label": "A little creepy", "localized": gettext("A little creepy")}
)

def test_creepiness_per_bin_limits(self):
product_page = buyersguide_factories.ProductPageFactory(parent=self.bg)
Expand Down Expand Up @@ -189,6 +192,9 @@ def test_creepiness_per_bin_limits(self):
self.assertEqual(evaluation.average_creepiness, 49.5)
self.assertEqual(evaluation.total_creepiness, 495)
self.assertEqual(evaluation.total_votes, 10)
self.assertDictEqual(
evaluation.average_bin, {"label": "A little creepy", "localized": gettext("A little creepy")}
)


class TestProductPageEvaluationPrefetching(BuyersGuideTestCase):
Expand Down