def test_micro_usd_to_usd_float(self): self.assertEqual(currency.micro_usd_to_usd_float(5050500), 5.05) self.assertEqual(currency.micro_usd_to_usd_float(150500), 0.15) self.assertEqual(currency.micro_usd_to_usd_float(-500), 0) self.assertEqual(currency.micro_usd_to_usd_float(500), 0)
def send_updates(self, updates, progress, ignore_category=False): mint_client = self.get_mintapi() num_requests = 0 for (orig_trans, new_trans) in updates: if len(new_trans) == 1: # Update the existing transaction. trans = new_trans[0] modify_trans = { 'task': 'txnedit', 'txnId': '{}:0'.format(trans.id), 'note': trans.note, 'merchant': trans.merchant, 'token': mint_client.token, } if not ignore_category: modify_trans = { **modify_trans, 'category': trans.category, 'catId': trans.category_id, } logger.debug( 'Sending a "modify" transaction request: {}'.format( modify_trans)) response = mint_client.post( '{}{}'.format( MINT_ROOT_URL, UPDATE_TRANS_ENDPOINT), data=modify_trans).text progress.next() logger.debug('Received response: {}'.format(response)) num_requests += 1 else: # Split the existing transaction into many. # If the existing transaction is a: # - credit: positive amount is credit, negative debit # - debit: positive amount is debit, negative credit itemized_split = { 'txnId': '{}:0'.format(orig_trans.id), 'task': 'split', 'data': '', # Yup this is weird. 'token': mint_client.token, } all_credit = all(not trans.is_debit for trans in new_trans) for (i, trans) in enumerate(new_trans): amount = trans.amount # If it's a split credit, everything should be positive if all_credit and amount < 0: amount = -amount amount = micro_usd_to_usd_float(amount) itemized_split['amount{}'.format(i)] = amount # Yup. Weird: itemized_split['percentAmount{}'.format(i)] = amount itemized_split['merchant{}'.format(i)] = trans.merchant # Yup weird. '0' means new? itemized_split['txnId{}'.format(i)] = 0 if not ignore_category: itemized_split['category{}'.format(i)] = trans.category itemized_split['categoryId{}'.format(i)] = ( trans.category_id) else: itemized_split['category{}'.format(i)] = ( orig_trans.category) itemized_split['categoryId{}'.format(i)] = ( orig_trans.category_id) logger.debug( 'Sending a "split" transaction request: {}'.format( itemized_split)) response = mint_client.post( '{}{}'.format( MINT_ROOT_URL, UPDATE_TRANS_ENDPOINT), data=itemized_split) json_resp = response.json() # The first id is always the original transaction (now # parent transaction id). new_trans_ids = json_resp['txnId'][1:] assert len(new_trans_ids) == len(new_trans) for itemized_id, trans in zip(new_trans_ids, new_trans): # Now send the note for each itemized transaction. itemized_note = { 'task': 'txnedit', 'txnId': '{}:0'.format(itemized_id), 'note': trans.note, 'token': mint_client.token, } note_response = mint_client.post( '{}{}'.format( MINT_ROOT_URL, UPDATE_TRANS_ENDPOINT), data=itemized_note) logger.debug( 'Received note response: {}'.format( note_response.text)) progress.next() logger.debug('Received response: {}'.format(response.text)) num_requests += 1 progress.finish() return num_requests
def send_updates(self, updates, ignore_category=False): mint_client = self.get_mintapi() updateProgress = IncrementalBar( 'Updating Mint', max=len(updates)) num_requests = 0 for (orig_trans, new_trans) in updates: if len(new_trans) == 1: # Update the existing transaction. trans = new_trans[0] modify_trans = { 'task': 'txnedit', 'txnId': '{}:0'.format(trans.id), 'note': trans.note, 'merchant': trans.merchant, 'token': mint_client.token, } if not ignore_category: modify_trans = { **modify_trans, 'category': trans.category, 'catId': trans.category_id, } logger.debug( 'Sending a "modify" transaction request: {}'.format( modify_trans)) response = mint_client.post( '{}{}'.format( MINT_ROOT_URL, UPDATE_TRANS_ENDPOINT), data=modify_trans).text updateProgress.next() logger.debug('Received response: {}'.format(response)) num_requests += 1 else: # Split the existing transaction into many. # If the existing transaction is a: # - credit: positive amount is credit, negative debit # - debit: positive amount is debit, negative credit itemized_split = { 'txnId': '{}:0'.format(orig_trans.id), 'task': 'split', 'data': '', # Yup this is weird. 'token': mint_client.token, } for (i, trans) in enumerate(new_trans): amount = trans.amount # Based on the comment above, if the original transaction # is a credit, flip the amount sign for things to work out! if not orig_trans.is_debit: amount *= -1 amount = micro_usd_to_usd_float(amount) itemized_split['amount{}'.format(i)] = amount # Yup. Weird: itemized_split['percentAmount{}'.format(i)] = amount itemized_split['merchant{}'.format(i)] = trans.merchant # Yup weird. '0' means new? itemized_split['txnId{}'.format(i)] = 0 if not ignore_category: itemized_split['category{}'.format(i)] = trans.category itemized_split['categoryId{}'.format(i)] = ( trans.category_id) logger.debug( 'Sending a "split" transaction request: {}'.format( itemized_split)) response = mint_client.post( '{}{}'.format( MINT_ROOT_URL, UPDATE_TRANS_ENDPOINT), data=itemized_split) json_resp = response.json() # The first id is always the original transaction (now # parent transaction id). new_trans_ids = json_resp['txnId'][1:] assert len(new_trans_ids) == len(new_trans) for itemized_id, trans in zip(new_trans_ids, new_trans): # Now send the note for each itemized transaction. itemized_note = { 'task': 'txnedit', 'txnId': '{}:0'.format(itemized_id), 'note': trans.note, 'token': mint_client.token, } note_response = mint_client.post( '{}{}'.format( MINT_ROOT_URL, UPDATE_TRANS_ENDPOINT), data=itemized_note) logger.debug( 'Received note response: {}'.format( note_response.text)) updateProgress.next() logger.debug('Received response: {}'.format(response.text)) num_requests += 1 updateProgress.finish() logger.info('Sent {} updates to Mint'.format(num_requests))