def quote_for_template(quotes, user, page=0): """Convert a Quote object into a suitable dictionary for a template. Does some processing on parameters and adds an index for paging. Args quotes: A list of Quote objects. Returns A list of dictionaries, one per Quote object. """ quotes_tpl = [] index = 1 + page * models.PAGE_SIZE for quote in quotes: quotes_tpl.append({ 'id': quote.key().id(), 'uri': quote.uri, 'voted': models.voted(quote, user), 'quote': quote.quote, 'creator': quote.creator, 'created': quote.creation_order[:10], 'created_long': quote.creation_order[:19], 'votesum': quote.votesum, 'index': index }) index += 1 return quotes_tpl
def test_voting(self): """ Test the voting system behaves as defined in the design document. """ user = users.User('*****@*****.**') user2 = users.User('*****@*****.**') # Day 1 - [quote 0 and 1 are added on Day 1 and # get 5 and 3 votes respectively. Rank is q0, q1.] # q0 (5) = 1 * 4 + 5 = 9 # q1 (3) = 1 * 4 + 3 = 7 quoteid0 = models.add_quote('This is a test.', user, _created=1) quoteid1 = models.add_quote('This is a test.', user, _created=1) models.set_vote(quoteid0, user, 1) models.set_vote(quoteid1, user, 3) quotes, next = models.get_quotes() self.assertEqual(models.voted(quotes[1], user), 1) self.assertEqual(models.voted(quotes[0], user), 3) self.assertEqual(quotes[0].key().id(), quoteid1) self.assertEqual(quotes[1].key().id(), quoteid0) models.set_vote(quoteid0, user, 5) quotes, next = models.get_quotes() self.assertEqual(quotes[0].key().id(), quoteid0) self.assertEqual(quotes[1].key().id(), quoteid1) # q0 (5) + (3) = 1 * 4 + 8 = 12 # q1 (3) + (0) = 1 * 4 + 3 = 7 # q2 (3) = 2 * 4 + 3 = 11 quoteid2 = models.add_quote('This is a test.', user, _created=2) models.set_vote(quoteid0, user, 8) models.set_vote(quoteid1, user, 3) models.set_vote(quoteid2, user, 3) quotes, next = models.get_quotes() self.assertEqual(quotes[0].key().id(), quoteid0) self.assertEqual(quotes[1].key().id(), quoteid2) self.assertEqual(quotes[2].key().id(), quoteid1) # q0 (5) + (3) = 1 * 4 + 8 = 12 # q1 (3) + (0) = 1 * 4 + 3 = 7 # q2 (3) + (2) = 2 * 5 + 4 = 14 # q3 (5) = 3 * 4 + 5 = 17 quoteid3 = models.add_quote('This is a test.', user, _created=3) models.set_vote(quoteid0, user, 8) models.set_vote(quoteid1, user, 3) models.set_vote(quoteid2, user, 5) models.set_vote(quoteid3, user, 5) quotes, next = models.get_quotes() self.assertEqual(quotes[0].key().id(), quoteid3) self.assertEqual(quotes[1].key().id(), quoteid2) self.assertEqual(quotes[2].key().id(), quoteid0) self.assertEqual(quotes[3].key().id(), quoteid1) # q0 (5) + (3) + (1) = 1 * 4 + 9 = 13 # q1 (3) + (0) = 1 * 4 + 3 = 7 # q2 (3) + (2) = 2 * 5 + 4 = 14 # q3 (0) = 3 * 4 + 0 = 12 models.set_vote(quoteid0, user, 9) models.set_vote(quoteid3, user, 0) quotes, next = models.get_quotes() self.assertEqual(quotes[0].key().id(), quoteid2) self.assertEqual(quotes[1].key().id(), quoteid0) self.assertEqual(quotes[2].key().id(), quoteid3) self.assertEqual(quotes[3].key().id(), quoteid1) models.del_quote(quoteid0, user) models.del_quote(quoteid1, user) models.del_quote(quoteid2, user) models.del_quote(quoteid3, user)