Example #1
0
def test_info_parse(transactions_json, api_key, sdk_key):
    client = FioBank('...')

    api_info = transactions_json['accountStatement']['info']
    sdk_info = client._parse_info(transactions_json)

    assert sdk_info[sdk_key] == api_info[api_key]
Example #2
0
def test_statement(transactions_json):
    client = FioBank('...')

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.statement(2016, 308)
        stub.assert_called_once_with('by-id', year=2016, number=308)
Example #3
0
def main(arguments):
    client = FioBank(token=arguments['--token'])

    if '<month>' in arguments:
        month = strtodatetime(arguments['<month>'])
        month_range = calendar.monthrange(month.year, month.month)
        
        month_start = datetime.date(month.year, month.month, month_range[0])
        month_end = datetime.date(month.year, month.month, month_range[1])

        transactions = client.period(month_start, month_end)
    else:
        transactions = client.period(arguments['<from>'], arguments['<to>'])
    
    print table_header()

    for transaction in transactions:
        try:
            transaction['recipient_message']
        except KeyError:
            transaction['recipient_message'] = ''
        try:
            transaction['variable_symbol']
        except KeyError:
            transaction['variable_symbol'] = ''
        
        print "||%s\t||%s\t||%s\t||%s\t||%s\t||" % (transaction['date'], transaction['user_identifiaction'], transaction['recipient_message'], transaction['variable_symbol'], transaction['amount'])
Example #4
0
def test_statement(transactions_json):
    client = FioBank('...')

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.statement(2016, 308)
        stub.assert_called_once_with('by-id', year=2016, number=308)
Example #5
0
def test_info_parse(transactions_json, api_key, sdk_key):
    client = FioBank('...')

    api_info = transactions_json['accountStatement']['info']
    sdk_info = client._parse_info(transactions_json)

    assert sdk_info[sdk_key] == api_info[api_key]
Example #6
0
def main():
    doc_key = '1TO5Yzk0-4V_RzRK5Jr9I_pF5knZsEZrNn2HKTXrHgls'

    client = FioBank(token=FIOBANK_API_KEY)
    to_date = date.today().strftime('%Y-%m-%d')

    records = []
    for transaction in client.period(from_date='2020-01-01', to_date=to_date):
        transaction['message'] = ', '.join(
            filter(None, [
                transaction.get('comment'),
                transaction.get('user_identification'),
                transaction.get('recipient_message'),
            ]))

        for category_fn in CATEGORIES:
            category = category_fn(transaction)
            if category:
                transaction['category'] = category
                break

        records.append({
            'Date': transaction['date'].strftime('%Y-%m-%d'),
            'Category': transaction['category'],
            'Amount': transaction['amount'],
            'Currency': transaction['currency'],
            'Message': transaction['message'],
            'Variable Symbol': transaction['variable_symbol'],
        })
    google_sheets.upload(google_sheets.get(doc_key, 'transactions'), records)
Example #7
0
def test_transactions_parse_none(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert api_transaction['column10'] is None
    assert sdk_transaction['account_name'] is None
Example #8
0
def test_info_parse_account_number_full(transactions_json):
    client = FioBank('...')

    api_info = transactions_json['accountStatement']['info']
    sdk_info = client._parse_info(transactions_json)

    expected_value = '{}/{}'.format(api_info['accountId'], api_info['bankId'])
    assert sdk_info['account_number_full'] == expected_value
Example #9
0
def test_info_uses_today(transactions_json):
    client = FioBank('...')
    today = date.today()

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.info()
        stub.assert_called_once_with('periods', from_date=today, to_date=today)
Example #10
0
def test_transactions_parse_none(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert api_transaction['column10'] is None
    assert sdk_transaction['account_name'] is None
Example #11
0
def test_info_parse_account_number_full(transactions_json):
    client = FioBank('...')

    api_info = transactions_json['accountStatement']['info']
    sdk_info = client._parse_info(transactions_json)

    expected_value = '{}/{}'.format(api_info['accountId'], api_info['bankId'])
    assert sdk_info['account_number_full'] == expected_value
Example #12
0
def test_info_uses_today(transactions_json):
    client = FioBank('...')
    today = date.today()

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.info()
        stub.assert_called_once_with('periods', from_date=today, to_date=today)
Example #13
0
def test_transactions_parse_unsanitized(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    api_transaction['column10'] = {'value': '             Honza\n'}

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert sdk_transaction['account_name'] == 'Honza'
Example #14
0
def test_transactions_parse_convert(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    api_transaction['column0'] = {'value': '2015-08-30'}

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert sdk_transaction['date'] == date(2015, 8, 30)
Example #15
0
def test_transactions_parse_unsanitized(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    api_transaction['column10'] = {'value': '             Honza\n'}

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert sdk_transaction['account_name'] == 'Honza'
Example #16
0
def test_info_parse_no_account_number_full(transactions_json):
    client = FioBank('...')

    api_info = transactions_json['accountStatement']['info']
    del api_info['bankId']

    sdk_info = client._parse_info(transactions_json)

    assert sdk_info['account_number_full'] is None
Example #17
0
def test_transactions_parse_convert(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    api_transaction['column0'] = {'value': '2015-08-30'}

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert sdk_transaction['date'] == date(2015, 8, 30)
Example #18
0
def test_info_parse_no_account_number_full(transactions_json):
    client = FioBank('...')

    api_info = transactions_json['accountStatement']['info']
    del api_info['bankId']

    sdk_info = client._parse_info(transactions_json)

    assert sdk_info['account_number_full'] is None
Example #19
0
def test_transactions_parse_no_account_number_full(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    api_transaction['column2'] = {'value': 10000000002}
    api_transaction['column3'] = {'value': None}

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert sdk_transaction['account_number_full'] is None
Example #20
0
def test_last_from_date(transactions_json, test_input):
    client = FioBank('...')

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.last(from_date=test_input)
        stub.assert_has_calls([
            mock.call('set-last-date', from_date=date(2016, 8, 30)),
            mock.call('last'),
        ])
Example #21
0
def test_last_from_id(transactions_json):
    client = FioBank('...')

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.last(from_id=308)
        stub.assert_has_calls([
            mock.call('set-last-id', from_id=308),
            mock.call('last'),
        ])
Example #22
0
def test_transactions_parse_missing(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    del api_transaction['column10']

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert 'column10' not in api_transaction
    assert sdk_transaction['account_name'] is None
    def handle(self, *args, **kwargs):
        client = FioBank(token=settings.FIO_TOKEN)
        payments = client.period(
            datetime.datetime.now() -
            datetime.timedelta(days=kwargs['days_back']),
            datetime.datetime.now(),
        )

        for payment in payments:
            self.pair_payment(payment)
Example #24
0
def test_transactions_parse_no_account_number_full(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    api_transaction['column2'] = {'value': 10000000002}
    api_transaction['column3'] = {'value': None}

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert sdk_transaction['account_number_full'] is None
Example #25
0
def test_transactions_parse_missing(transactions_json):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    del api_transaction['column10']

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert 'column10' not in api_transaction
    assert sdk_transaction['account_name'] is None
Example #26
0
def test_last_from_id(transactions_json):
    client = FioBank('...')

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.last(from_id=308)
        stub.assert_has_calls([
            mock.call('set-last-id', from_id=308),
            mock.call('last'),
        ])
Example #27
0
def test_last_from_date(transactions_json, test_input):
    client = FioBank('...')

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.last(from_date=test_input)
        stub.assert_has_calls([
            mock.call('set-last-date', from_date=date(2016, 8, 30)),
            mock.call('last'),
        ])
Example #28
0
def test_info_is_case_insensitive(transactions_json):
    client = FioBank('...')

    api_info = transactions_json['accountStatement']['info']
    value = api_info['accountId']
    del api_info['accountId']
    api_info['acCOUNTid'] = value

    sdk_info = client._parse_info(transactions_json)

    assert sdk_info['account_number'] == value
Example #29
0
def test_info_is_case_insensitive(transactions_json):
    client = FioBank('...')

    api_info = transactions_json['accountStatement']['info']
    value = api_info['accountId']
    del api_info['accountId']
    api_info['acCOUNTid'] = value

    sdk_info = client._parse_info(transactions_json)

    assert sdk_info['account_number'] == value
Example #30
0
def test_period_coerces_date(transactions_json):
    client = FioBank('...')

    from_date = '2016-08-04T09:36:42'
    to_date = '2016-08-30T11:45:38'

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.period(from_date, to_date)
        stub.assert_called_once_with('periods',
                                     from_date=date(2016, 8, 4),
                                     to_date=date(2016, 8, 30))
Example #31
0
def test_transactions_parse_amount(transactions_json, test_input,
                                   amount, currency):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    api_transaction['column18'] = {'value': test_input}

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert sdk_transaction['specification'] == test_input
    assert sdk_transaction['original_amount'] == amount
    assert sdk_transaction['original_currency'] == currency
Example #32
0
def test_transactions_parse_amount(transactions_json, test_input,
                                   amount, currency):
    client = FioBank('...')

    api_transaction = transactions_json['accountStatement']['transactionList']['transaction'][0]  # NOQA
    api_transaction['column18'] = {'value': test_input}

    sdk_transaction = list(client._parse_transactions(transactions_json))[0]

    assert sdk_transaction['specification'] == test_input
    assert sdk_transaction['original_amount'] == amount
    assert sdk_transaction['original_currency'] == currency
Example #33
0
def test_period_coerces_date(transactions_json):
    client = FioBank('...')

    from_date = '2016-08-04T09:36:42'
    to_date = '2016-08-30T11:45:38'

    options = {'return_value': transactions_json}
    with mock.patch.object(client, '_request', **options) as stub:
        client.period(from_date, to_date)
        stub.assert_called_once_with('periods',
                                     from_date=date(2016, 8, 4),
                                     to_date=date(2016, 8, 30))
Example #34
0
def _get_last_payments():
    """ Get list of payments for last three days from FioBank """
    client = FioBank(token=settings.FIO_BANK_TOKEN)

    today = timezone.now()
    date_from = (today - timedelta(days=settings.FIO_BANK_PROCESS_DAYS)).strftime(DATE_FORMAT)
    date_to = today.strftime(DATE_FORMAT)

    try:
        data = list(client.period(date_from, date_to))
    except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as e:
        logger.error('{} in _get_last_payments'.format(e))
        data = []

    return data
Example #35
0
 def handle(self, *args, **options):
     if settings.FIO_TOKEN:
         client = FioBank(token=settings.FIO_TOKEN)
         payments = client.period(from_date=datetime.date.today()-datetime.timedelta(30), to_date=datetime.date.today())
         for payment in payments:
             date = payment.get("date")
             vs = payment.get("variable_symbol")
             value = payment.get("amount", 0.0)
             trans = payment.get("transaction_id")
             if vs and value and date and trans:
                 try:
                     user = User.objects.get(id=(int(vs) - 100000))
                 except ObjectDoesNotExist:
                     continue
                 if not TransId.objects.filter(trans_id=trans).count() and value > 0:
                     self.pay(date, user, value, trans)
     else:
         sys.stderr.write("Error: not FIO TOKEN found")
Example #36
0
 def handle(self, *args, **options):
     if settings.FIO_TOKEN:
         client = FioBank(token=settings.FIO_TOKEN)
         payments = client.period(from_date=datetime.date.today() -
                                  datetime.timedelta(30),
                                  to_date=datetime.date.today())
         for payment in payments:
             date = payment.get("date")
             vs = payment.get("variable_symbol")
             value = payment.get("amount", 0.0)
             trans = payment.get("transaction_id")
             if vs and value and date and trans:
                 try:
                     user = User.objects.get(id=(int(vs) - 100000))
                 except ObjectDoesNotExist:
                     continue
                 if not TransId.objects.filter(
                         trans_id=trans).count() and value > 0:
                     self.pay(date, user, value, trans)
     else:
         sys.stderr.write("Error: not FIO TOKEN found")
Example #37
0
def client(token, transactions_json):
    with responses.RequestsMock(assert_all_requests_are_fired=False) as resps:
        url = re.compile(
            re.escape(FioBank.base_url) +
            r'[^/]+/{token}/([^/]+/)*transactions\.json'.format(token=token))
        resps.add(responses.GET, url, json=transactions_json)

        url = re.compile(
            re.escape(FioBank.base_url) +
            r'set-last-\w+/{token}/[^/]+/'.format(token=token))
        resps.add(responses.GET, url)

        yield FioBank(token)
Example #38
0
def test_transactions_parse(transactions_json, api_key, sdk_key, sdk_type):
    client = FioBank('...')

    api_transactions = transactions_json['accountStatement'][
        'transactionList']['transaction']  # NOQA

    # The 'transactions.json' file is based on real data, so it doesn't
    # contain some values. To test all values, we use dummy data here.
    dummy_mapping = {'column0': '2015-08-30'}
    dummy_default = 30.8
    for api_transaction in api_transactions:
        dummy_value = dummy_mapping.get(api_key, dummy_default)
        api_transaction[api_key] = {'value': dummy_value}

    sdk_transactions = list(client._parse_transactions(transactions_json))
    assert len(sdk_transactions) == len(api_transactions)

    for i in range(len(api_transactions)):
        api_transaction = api_transactions[i]
        sdk_transaction = sdk_transactions[i]

        assert (sdk_transaction[sdk_key] == sdk_type(
            api_transaction[api_key]['value']))
Example #39
0
def test_transactions_parse(transactions_json, api_key, sdk_key, sdk_type):
    client = FioBank('...')

    api_transactions = transactions_json['accountStatement']['transactionList']['transaction']  # NOQA

    # The 'transactions.json' file is based on real data, so it doesn't
    # contain some values. To test all values, we use dummy data here.
    dummy_mapping = {'column0': '2015-08-30'}
    dummy_default = 30.8
    for api_transaction in api_transactions:
        dummy_value = dummy_mapping.get(api_key, dummy_default)
        api_transaction[api_key] = {'value': dummy_value}

    sdk_transactions = list(client._parse_transactions(transactions_json))
    assert len(sdk_transactions) == len(api_transactions)

    for i in range(len(api_transactions)):
        api_transaction = api_transactions[i]
        sdk_transaction = sdk_transactions[i]

        assert (
            sdk_transaction[sdk_key] ==
            sdk_type(api_transaction[api_key]['value'])
        )
 def __init__(self):
     db_client = AsyncIOMotorClient(config["MONGO_CONNECTION_STRING"])
     self.db = db_client[config["MONGO_DATABASE"]]
     self.bank = FioBank(config["FIO_API_TOKEN"])
Example #41
0
def test_last_conflicting_params():
    client = FioBank('...')
    with pytest.raises(ValueError):
        client.last(from_id=308, from_date=date(2016, 8, 30))
Example #42
0
def test_last_conflicting_params():
    client = FioBank('...')
    with pytest.raises(ValueError):
        client.last(from_id=308, from_date=date(2016, 8, 30))
from django.core.exceptions import ObjectDoesNotExist

from decimal import Decimal

from time import sleep
import datetime
import logging

#FIRST_DATE = datetime.date(2010, 9, 1)
FIRST_DATE = datetime.date(2015,1,1)
logger = logging.getLogger(__name__)



for token in settings.BANK_TOKENS:
    client = FioBank(token=token)
    info = client.info()
    curr,created = Currency.objects.get_or_create(symbol=info.get('currency'))
    if created:
        curr.name = curr.symbol
        curr.save()
    my,created = BankAccount.objects.get_or_create(account_number=info['account_number'], bank_code=info['bank_code'])
    if created:
        my.currency = curr
        my.save()
    logger.info('Processing account %s...' % my)
    try:
        from_id = BankTransaction.objects.filter(my_account=my).latest('date').tid
        sleep(30) # to avoid HTTP 409 - "Conflict" response
        trans = client.last(from_id=from_id)
    except ObjectDoesNotExist: