def test_url(self): a = ObjectId("123456789012") self.assertEqual(a.url_encode(), "313233343536373839303132") self.assertEqual(a, ObjectId.url_decode("313233343536373839303132")) b = ObjectId() encoded = b.url_encode() self.assertEqual(b, ObjectId.url_decode(encoded))
def get_transaction_details(db, user, values_as_nums=False): #Get transactions from_transactions = [t for t in db.transactions.find({'from_user':user['_id']}).sort('timestamp', pymongo.ASCENDING)] to_transactions = [t for t in db.transactions.find({'to_user':user['_id']}).sort('timestamp', pymongo.ASCENDING)] #Get totals and balance total_from = sum([t['value'] for t in from_transactions]) total_to = sum([t['value'] for t in to_transactions]) balance = total_from-total_to #Put into a single sorted list, using clever merging transactions = [] totals = {} while from_transactions and to_transactions: if from_transactions[-1]['timestamp'] > to_transactions[-1]['timestamp']: trans = from_transactions.pop() totals = update_totals(totals, 'to', trans) else: trans = to_transactions.pop() totals = update_totals(totals, 'from', trans) transactions.append(format_transaction(db, trans)) if to_transactions: for t in to_transactions: totals = update_totals(totals, 'from', t) transactions.append(format_transaction(db, t)) else: for t in from_transactions: totals = update_totals(totals, 'to', t) transactions.append(format_transaction(db, t)) #Build up the totals into nice to handle lists people_owe_you = {} you_owe_people = {} for user_id, total in totals.iteritems(): total = round(total,2) user_id = ObjectId.url_decode(user_id) user = db.users.find_one({'_id': user_id}) if total > 0: if values_as_nums: val = total else: val = u'£%.2f' % total people_owe_you[user['username']] = [user, val] elif total < 0: if values_as_nums: val = abs(total) else: val = u'£%.2f' % abs(total) you_owe_people[user['username']] = [user, val] print you_owe_people return transactions, balance, people_owe_you, you_owe_people
def test_url_legacy(self): a = ObjectId() self.assertNotEqual(a.url_encode(), a.url_encode(legacy=True)) self.assertEqual(a, ObjectId.url_decode(a.url_encode(legacy=True), legacy=True))