Skip to content

Instantly share code, notes, and snippets.

@bmease
Created December 21, 2017 22:49
Show Gist options
  • Save bmease/3429b9ed4955db43f891c6ceabd4204a to your computer and use it in GitHub Desktop.
Save bmease/3429b9ed4955db43f891c6ceabd4204a to your computer and use it in GitHub Desktop.
def pct_breakdown(self, grouping):
# provide the grouping and this function will return the percent upvoted for each group
rating_breakdown = {}
groups = IdeaInteraction.objects.filter(idea=self.pk).values(grouping)
for item in groups:
for key, value in item.items():
upvotes = IdeaInteraction.objects.filter(idea=self.pk, vote='up-vote', **{key: value}).count()
downvotes = IdeaInteraction.objects.filter(idea=self.pk, vote='down-vote', **{key: value}).count()
rating = (upvotes / (upvotes + downvotes)) * 100
rating_breakdown[value] = rating
return rating_breakdown
def pct_breakdown(self, grouping):
# declare variables
rating_breakdown = {}
groups = IdeaInteraction.objects.filter(idea=self.pk).values(grouping)
# do your work
for item in groups:
for key, value in item.items():
# grab some data
upvotes = IdeaInteraction.objects.filter(idea=self.pk, vote='up-vote', **{key: value}).count()
downvotes = IdeaInteraction.objects.filter(idea=self.pk, vote='down-vote', **{key: value}).count()
# do some work
rating = (upvotes / (upvotes + downvotes)) * 100
rating_breakdown[value] = rating
# return results
return rating_breakdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment