def backlogs_put(**kwargs): """ actually insert transaction """ obj = json.loads(request.data.decode('utf-8', 'strict')) origin_time = datetime.datetime.strptime( obj['origin_time'], '%Y-%m-%d').date() operation_time = datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date() transaction = Transaction( time=operation_time, account_id=int(obj['account.id']), sum=strip_numbers(obj['sum']), income_id=obj['income.id'], comment=obj['comment']) DB.add(transaction) DB.flush() if origin_time != operation_time: # payment before DB.add( Payforward( income_id=int(obj['income.id']), income_date=origin_time, payment_date=operation_time, transaction_id=transaction.record_id ) ) DB.commit() return transaction
def transaction_put(**kwargs): # pylint: disable=C0111,W0613 obj = json.loads(request.data.decode('utf-8', 'strict')) print(obj) # try: i = Transaction( time=datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date(), account_id=int(obj['account.id']), user_id=session['user'][0], summ=decimal.Decimal(strip_numbers(obj['summ'])), transfer=int(obj['transfer']) if (obj['transfer'] != '' and int(obj['transfer']) > 0) else None, income_id=int(obj['income.id']) if (obj['income.id'] != '' and int(obj['income.id']) > 0) else None, comments=obj['comments']) DB.add(i) DB.flush() if 'new_account.id' in obj: transfer = Transaction( time=datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date(), account_id=int(obj['new_account.id']), user_id=session['user'][0], summ=decimal.Decimal(strip_numbers(obj['new_sum'])), transfer=int(obj['transfer']) if int(obj['transfer']) > 0 else None, income_id=int(obj['income.id']) if int(obj['income.id']) > 0 else None, comments=obj['comments']) DB.add(transfer) DB.flush() i.transfer = transfer.record_id transfer.transfer = i.record_id DB.commit() # except: # abort(400) return i
def transactions_post(**kwargs): obj = json.loads(request.data.decode('utf-8', 'strict')) # try: i = Transaction( time=datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date(), account_id=int(obj['account.id']), sum=decimal.Decimal(strip_numbers(obj['sum'])), transfer=int(obj['transfer']) if int(obj['transfer']) > 0 else None, income_id=int(obj['income.id']) if int(obj['income.id']) > 0 else None, comment=obj['comment'] ) DB.add(i) DB.flush() if 'new_account.id' in obj: transfer = Transaction( time=datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date(), account_id=int(obj['new_account.id']), sum=decimal.Decimal(strip_numbers(obj['new_sum'])), transfer=int(obj['transfer']) if int(obj['transfer']) > 0 else None, comment=obj['comment'], income_id=int(obj['income.id']) if int(obj['income.id']) > 0 else None ) DB.add(transfer) DB.flush() i.transfer = transfer.record_id transfer.transfer = i.record_id DB.commit() # except: # abort(400) return i
def usercurrency_delete(**kwargs): ''' delete user currency ''' DB.query(UserCurrencies).filter( and_( UserCurrencies.user_id == session['user'][0], UserCurrencies.currency_id == kwargs['id']) ).delete(synchronize_session=False) DB.commit() return {'result': 'Ok'}
def accounts_delete(**kwargs): income = DB.query(Account).get(kwargs['id']) _count = DB.query( Transaction).filter( Transaction.account_id == kwargs['id']).count() if _count > 0: income.deleted = 'y' income.show = 'n' else: DB.query(Account).filter(Account.record_id == kwargs['id']).delete() DB.commit() return {'deleted': kwargs['id']}
def backlogs_delete(**kwargs): # just create transaction with sum zero obj = json.loads(request.data.decode('utf-8', 'strict')) t = Transaction( time=datetime.datetime.strptime(obj['origin_time'], '%Y-%m-%d').date(), account_id=0, sum=0, income_id=obj['income.id'], comment='cancelled') DB.add(t) DB.flush() DB.commit() return t
def accounts_put(**kwargs): a = DB.query(Account).get(kwargs['id']) obj = json.loads(request.data.decode('utf-8', 'strict')) a.title = obj['title'] a.show = obj['show'] a.currency_id = obj['currency.id'] delta_sum = decimal.Decimal(strip_numbers(obj['sum'])) - a.sum() if delta_sum != 0: t = Transaction(time=datetime.date.today(), sum=delta_sum, account_id=kwargs['id'], comment='fix account summ') DB.add(t) DB.commit() return {'updated': DB.query(Account).get(kwargs['id']), "previous": a}
def transactions_put(**kwargs): i = DB.query(Transaction).get(kwargs['id']) obj = json.loads(request.data.decode('utf-8', 'strict')) # try: i.time = datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date() i.account_id = int(obj['account.id']) if obj['account.id'] != '' else None i.sum = decimal.Decimal(strip_numbers(obj['sum'])) i.transfer = int(obj['transfer']) if obj['transfer'] not in [ '0', ''] else None i.income_id = int(obj['income.id']) if obj['income.id'] not in [ '0', ''] else None i.comment = obj['comment'] # except: # abort(400) DB.commit() return {'updated': DB.query(Transaction).get(kwargs['id']), "previous": i}
def accounts_post(**kwargs): """ add new account and set first transaction with rests of money """ obj = json.loads(request.data.decode('utf-8', 'strict')) new_account = Account( title=obj['title'], currency_id=int(obj['currency.id'])) DB.add(new_account) DB.flush() if float(strip_numbers(obj['sum'])) > 0: DB.add(Transaction(account_id=new_account.record_id, show=obj['show'], comment='initial summ', time=datetime.date.today(), sum=strip_numbers(obj['sum']))) DB.commit() return new_account
def incomes_put(**kwargs): i = DB.query(Income).get(kwargs['id']) obj = json.loads(request.data.decode('utf-8', 'strict')) # try: i.title = obj['title'] i.currency_id = int(obj['currency.id']) i.sum = decimal.Decimal(strip_numbers(obj['sum'])) i.start_date = datetime.datetime.strptime( obj['start_date'], '%Y-%m-%d').date() i.end_date = ( None if obj['end_date'] == '' else datetime.datetime.strptime( obj['end_date'], '%Y-%m-%d').date()) i.period_id = int(obj['period.id']) # except: # abort(400) DB.commit() return {'updated': DB.query(Income).get(kwargs['id']), "previous": i}
def update_rates(): default_currency = DB.query(Currency).filter_by(default=1).first() http = PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) c_title = {} objects = [] for currency in DB.query(Currency).all(): if currency == default_currency: continue c_title[currency.title] = currency.record_id _request = http.request( 'GET', "https://finance.google.com/finance/converter" "?a=1&from={}&to={}".format( currency, default_currency)) if _request.status == 200: for line in _request.data.decode('utf-8', 'replace').split('\n'): if "id=currency_converter_result" in line: match = RE_C_EXCHANGE.match(line) if match: print("{} {} {}".format(match.group('currency_a'), match.group('currency_b'), match.group('rate'))) objects.append( Rate( rate_date=datetime.datetime.now(), currency_a_id=default_currency.record_id, currency_b_id=c_title[match.group('currency_b')], rate=match.group('rate'))) else: print("cannot get rate {}:{}".format(currency, default_currency)) DB.bulk_save_objects(objects) DB.commit() try: if request.method == 'GET': return redirect(url_for( 'dispatcher', api='currency', _external=True, _scheme='https')) return True except (RuntimeError, AttributeError): return True
def usercurrency_put(**kwargs): """ update user currencies Arguments: **kwargs id -- currency id Returns: json -- result code """ obj = json.loads(request.data.decode('utf-8', 'strict')) updated = 0 if 'default' in obj and obj['default']: # clean previous default updated = DB.query(UserCurrencies).filter( and_( UserCurrencies.user_id == session['user'][0], UserCurrencies.currency_id != kwargs['id']) ).update( {UserCurrencies.default: False}, synchronize_session='evaluate') updated = DB.query(UserCurrencies).filter( and_( UserCurrencies.user_id == session['user'][0], UserCurrencies.currency_id == kwargs['id'] ) ).update( {UserCurrencies.default: True}, synchronize_session='evaluate') if updated == 0: # if not updated nothing - inser new record DB.add(UserCurrencies( user_id=session['user'][0], currency_id=kwargs['id'], default=True )) else: DB.add(UserCurrencies( user_id=session['user'][0], currency_id=kwargs['id'] )) DB.commit() return {'result': 'Ok'}
def incomes_post(**kwargs): obj = json.loads(request.data.decode('utf-8', 'strict')) # try: i = Income( title=obj['title'], currency_id=int(obj['currency.id']), sum=decimal.Decimal(strip_numbers(obj['sum'])), start_date=datetime.datetime.strptime( obj['start_date'], '%Y-%m-%d').date(), end_date=(None if obj['end_date'] == '' else datetime.datetime.strptime( obj['end_date'], '%Y-%m-%d').date()), period_id=int(obj['period.id']) ) DB.add(i) DB.flush() DB.commit() # except: # abort(400) return i
def transaction_post(**kwargs): # pylint: disable=C0111 i = DB.query(Transaction).get(kwargs['id']) obj = json.loads(request.data.decode('utf-8', 'strict')) print(obj) print(obj['time']) # try: i.time = datetime.datetime.strptime(obj['time'], '%Y-%m-%d').date() # TODO: check if account valid for this user i.account_id = int(obj['account.id']) if obj['account.id'] != '' else None i.user_id = session['user'][0] i.summ = decimal.Decimal(strip_numbers(obj['summ'])) # TODO: check if transfer transaction valid for this user i.transfer = int(obj['transfer']) if obj['transfer'] not in ['0', '' ] else None i.income_id = int(obj['income.id']) if obj['income.id'] not in ['0', '' ] else None i.comments = obj['comments'] # except: # abort(400) DB.commit() return {'updated': DB.query(Transaction).get(kwargs['id']), "previous": i}
def transaction_delete(**kwargs): # pylint: disable=C0111 income = DB.query(Transaction).filter_by(record_id=kwargs['id']).delete() DB.query(Payforward).filter_by(transaction_id=kwargs['id']).delete() DB.commit() return {'deleted': income}
def incomes_delete(**kwargs): DB.query(Income).filter_by(id=kwargs['id']).delete() DB.commit() return {'deleted': kwargs['id']}