From 76e48cc49fc7c73ba9642129059c2e51234c3fe6 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Thu, 7 Feb 2013 11:29:04 +0100 Subject: Fix votes (#34) - make sure we always use the message_id_hash - factor some code --- hyperkitty/lib/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'hyperkitty/lib') diff --git a/hyperkitty/lib/__init__.py b/hyperkitty/lib/__init__.py index 52b6bdc..b3fa6c4 100644 --- a/hyperkitty/lib/__init__.py +++ b/hyperkitty/lib/__init__.py @@ -25,6 +25,8 @@ import datetime from django.conf import settings +from hyperkitty.models import Rating + def gravatar_url(email): '''Return a gravatar url for an email address''' @@ -86,3 +88,18 @@ def get_display_dates(year, month, day): end_date = begin_date + datetime.timedelta(days=1) return begin_date, end_date + + +def get_votes(message_id_hash): + """Extract all the votes for this message""" + likes = dislikes = 0 + try: + votes = Rating.objects.filter(messageid=message_id_hash) + except Rating.DoesNotExist: + votes = {} + for vote in votes: + if vote.vote == 1: + likes += 1 + elif vote.vote == -1: + dislikes += 1 + return likes, dislikes -- cgit