diff options
-rw-r--r-- | hyperkitty/lib/voting.py | 23 | ||||
-rw-r--r-- | hyperkitty/tests/test_lib.py | 61 | ||||
-rw-r--r-- | hyperkitty/tests/test_views.py | 4 | ||||
-rw-r--r-- | hyperkitty/views/list.py | 4 |
4 files changed, 74 insertions, 18 deletions
diff --git a/hyperkitty/lib/voting.py b/hyperkitty/lib/voting.py index 56fee7e..da3e2a4 100644 --- a/hyperkitty/lib/voting.py +++ b/hyperkitty/lib/voting.py @@ -59,26 +59,19 @@ def set_message_votes(message, user=None): # message.likestatus = "dislike" -def set_thread_votes(thread, user=None): - total = myvote = 0 +def set_thread_votes(thread): likes = dislikes = None cache_key = "list:%s:thread:%s:votes" % (thread.list_name, thread.thread_id) - if user is None or not user.is_authenticated(): - likes, dislikes = cache.get(cache_key, (None, None)) - myvote = 0 + likes, dislikes = cache.get(cache_key, (None, None)) if likes is None or dislikes is None: # XXX: 1 SQL request per thread, possible optimization here - likes, dislikes, myvote = get_votes(thread.email_id_hashes) + likes, dislikes, _myvote = get_votes(thread.email_id_hashes) cache.set(cache_key, (likes, dislikes)) - total = likes + dislikes - try: - thread.likes = likes / total - except ZeroDivisionError: - thread.likes = 0 - try: - thread.dislikes = dislikes / total - except ZeroDivisionError: - thread.dislikes = 0 + # How should thread likes and dislikes be counted? + #thread.likes = likes / thread.emails_count + #thread.dislikes = dislikes / thread.emails_count + thread.likes = likes + thread.dislikes = dislikes thread.likestatus = "neutral" if thread.likes - thread.dislikes >= 10: thread.likestatus = "likealot" diff --git a/hyperkitty/tests/test_lib.py b/hyperkitty/tests/test_lib.py index 19f31d1..6b6a9e1 100644 --- a/hyperkitty/tests/test_lib.py +++ b/hyperkitty/tests/test_lib.py @@ -21,9 +21,14 @@ import datetime -from hyperkitty.tests.utils import TestCase +from django.contrib.auth.models import User, AnonymousUser +from django.core.cache import cache +from hyperkitty.models import Rating from hyperkitty.lib.view_helpers import get_display_dates, paginate +from hyperkitty.lib.voting import get_votes, set_thread_votes + +from hyperkitty.tests.utils import TestCase class GetDisplayDatesTestCase(TestCase): @@ -95,3 +100,57 @@ class PaginateTestCase(TestCase): [1, '...', 96, 97, 98, 99, 100]) self.assertEqual(paginate(objects, 100).page_range, [1, '...', 97, 98, 99, 100]) + + + +# +# Voting +# + +class DummyThread(object): + thread_id = "dummy" + list_name = "test@example.com" + email_id_hashes = [] + @property + def emails_count(self): + return len(email_id_hashes) + +class VotingTestCase(TestCase): + + def setUp(self): + self.thread = DummyThread() + self.user = User.objects.create_user('testuser', 'test@example.com', 'testPass') + + def tearDown(self): + cache.clear() + + def test_msg1(self): + # First message in thread is voted for + self.thread.email_id_hashes = ["msg1", "msg2", "msg3"] + Rating(list_address=self.thread.list_name, messageid="msg1", + user=self.user, vote=1).save() + set_thread_votes(self.thread) + self.assertEqual(self.thread.likes, 1) + self.assertEqual(self.thread.dislikes, 0) + self.assertEqual(self.thread.likestatus, "like") + + def test_msg2(self): + # Second message in thread is voted against + self.thread.email_id_hashes = ["msg1", "msg2", "msg3"] + Rating(list_address=self.thread.list_name, messageid="msg2", + user=self.user, vote=-1).save() + set_thread_votes(self.thread) + self.assertEqual(self.thread.likes, 0) + self.assertEqual(self.thread.dislikes, 1) + self.assertEqual(self.thread.likestatus, "neutral") + + def test_likealot(self): + # All messages in thread are voted for + self.thread.email_id_hashes = [ "msg%s" % num for num in range(1, 11) ] + for msgid in self.thread.email_id_hashes: + Rating(list_address=self.thread.list_name, messageid=msgid, + user=self.user, vote=1).save() + set_thread_votes(self.thread) + self.assertEqual(self.thread.likes, 10) + self.assertEqual(self.thread.dislikes, 0) + self.assertEqual(self.thread.likestatus, "likealot") diff --git a/hyperkitty/tests/test_views.py b/hyperkitty/tests/test_views.py index 948b621..f9a1ac7 100644 --- a/hyperkitty/tests/test_views.py +++ b/hyperkitty/tests/test_views.py @@ -179,10 +179,14 @@ class MessageViewsTestCase(TestCase): self.user = User.objects.create_user( 'testuser', 'test@example.com', 'testPass') # Fake KittStore + class FakeThread(object): + thread_id = None class FakeMessage(object): def __init__(self, h): self.message_id_hash = h self.list_name = "list@example.com" + self.thread = FakeThread() + self.thread.thread_id = h self.store = Mock() self.store.get_message_by_hash_from_list.side_effect = \ lambda l, h: FakeMessage(h) diff --git a/hyperkitty/views/list.py b/hyperkitty/views/list.py index 8feff11..d9a0ace 100644 --- a/hyperkitty/views/list.py +++ b/hyperkitty/views/list.py @@ -89,7 +89,7 @@ def _thread_list(request, mlist, threads, template_name='thread_list.html', extr participants.update(thread.participants) # Votes - set_thread_votes(thread, request.user) + set_thread_votes(thread) # Favorites thread.favorite = False @@ -153,7 +153,7 @@ def overview(request, mlist_fqdn=None): threads = [] for thread_obj in threads_result: # Votes - set_thread_votes(thread_obj, request.user) + set_thread_votes(thread_obj) thread_obj.category_widget = get_category_widget( None, thread_obj.category)[0] thread_obj.unread = is_thread_unread(request, mlist.name, thread_obj) |