From b5a7427f602147725b36701b4c5c7a2b00183995 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Tue, 13 May 2014 12:12:24 +0000 Subject: Take into account number of votes in rating sort --- diff --git a/sugar_network/model/__init__.py b/sugar_network/model/__init__.py index d979772..aa835ed 100644 --- a/sugar_network/model/__init__.py +++ b/sugar_network/model/__init__.py @@ -17,8 +17,6 @@ import os import gettext from os.path import join -import xapian - from sugar_network import db from sugar_network.model.routes import FrontRoutes @@ -62,4 +60,4 @@ class Rating(db.List): def slotting(self, value): rating = float(value[1]) / value[0] if value[0] else 0 - return xapian.sortable_serialise(rating) + return '%.3f%010d' % (rating, value[0]) diff --git a/tests/units/model/model.py b/tests/units/model/model.py index c333653..23e6159 100755 --- a/tests/units/model/model.py +++ b/tests/units/model/model.py @@ -17,7 +17,6 @@ from sugar_network.toolkit.coroutine import this class ModelTest(tests.Test): def test_RatingSort(self): - this.localcast = lambda event: None directory = db.Volume('db', [Post])['post'] directory.create({'guid': '1', 'context': '', 'type': 'post', 'title': {}, 'message': {}, 'rating': [0, 0]}) @@ -36,5 +35,25 @@ class ModelTest(tests.Test): ['3', '5', '2', '4', '1'], [i.guid for i in directory.find(order_by='-rating')[0]]) + def test_RatingSecondarySortByVotes(self): + directory = db.Volume('db', [Post])['post'] + + directory.create({'guid': '1', 'context': '', 'type': 'post', 'title': {}, 'message': {}, 'rating': [10, 10]}) + directory.create({'guid': '2', 'context': '', 'type': 'post', 'title': {}, 'message': {}, 'rating': [1, 1]}) + directory.create({'guid': '3', 'context': '', 'type': 'post', 'title': {}, 'message': {}, 'rating': [10000, 10000]}) + directory.create({'guid': '4', 'context': '', 'type': 'post', 'title': {}, 'message': {}, 'rating': [1000, 1000]}) + directory.create({'guid': '5', 'context': '', 'type': 'post', 'title': {}, 'message': {}, 'rating': [100, 100]}) + + self.assertEqual( + ['1', '2', '3', '4', '5'], + [i.guid for i in directory.find()[0]]) + self.assertEqual( + ['2', '1', '5', '4', '3'], + [i.guid for i in directory.find(order_by='rating')[0]]) + self.assertEqual( + ['3', '4', '5', '1', '2'], + [i.guid for i in directory.find(order_by='-rating')[0]]) + + if __name__ == '__main__': tests.main() -- cgit v0.9.1