def send_updates_to_mint(updates, mint_client, ignore_category=False): updateProgress = IncrementalBar('Updating Mint', max=len(updates)) start_time = time.time() 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() dur = s_to_time(time.time() - start_time) logger.info('Sent {} updates to Mint in {}'.format(num_requests, dur))
def send_updates_to_mint(updates, mint_client, ignore_category=False): # TODO: # Unsplits # Send notes for everything logger.info('Sending {} updates to Mint.'.format(len(updates))) start_time = time.time() 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 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).text logger.debug('Received response: {}'.format(response)) num_requests += 1 dur = s_to_time(time.time() - start_time) logger.info('Sent {} updates to Mint in {}'.format(num_requests, dur))
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)