def test_sync(self): mock_sess = Mock() with patch('%s.inspect' % pbm) as mock_inspect: type(mock_inspect.return_value).session = mock_sess b1 = Budget(name='my_budg') b2 = Budget(name='my_budg2') b3 = Budget(name='my_budg3') t = Transaction(budget_amounts={ b1: Decimal('10.00'), b2: Decimal('90.00') }) mock_sess.reset_mock() t.set_budget_amounts({b1: Decimal('40.00'), b3: Decimal('60.00')}) assert len(t.budget_transactions) == 3 for bt in t.budget_transactions: assert isinstance(bt, BudgetTransaction) assert bt.transaction == t assert {bt.budget: bt.amount for bt in t.budget_transactions} == { b1: Decimal('40.00'), b2: Decimal('90.00'), b3: Decimal('60.00') } assert len(mock_sess.mock_calls) == 1 assert mock_sess.mock_calls[0][0] == 'delete' assert mock_sess.mock_calls[0][1][0].budget == b2 assert mock_sess.mock_calls[0][1][0].amount == Decimal('90.00')
def test_none(self): mock_sess = Mock() with patch('%s.inspect' % pbm) as mock_inspect: type(mock_inspect.return_value).session = mock_sess t = Transaction() with pytest.raises(AssertionError): t.set_budget_amounts({}) assert mock_sess.mock_calls == []
def test_13_add_transactions(self, testdb): acct = testdb.query(Account).get(1) budgets = {x.id: x for x in testdb.query(Budget).all()} # Budget 3 Income Transaction t1 = Transaction(date=date(2017, 4, 7), budget_amounts={ budgets[3]: Decimal('100.00'), budgets[4]: Decimal('50.00') }, budgeted_amount=Decimal('100.00'), description='B3 Income', account=acct) testdb.add(t1) # Budget 3 Income ST testdb.add( ScheduledTransaction(amount=Decimal('99.00'), description='B3 Income ST', account=acct, budget=budgets[3], num_per_period=1)) # Budget 4 allocated greater than budgeted (500.00) testdb.add( ScheduledTransaction(amount=Decimal('250.00'), description='B4 ST', account=acct, budget=budgets[4], date=date(2017, 4, 10))) t2 = Transaction(date=date(2017, 4, 11), budget_amounts={budgets[4]: Decimal('250.00')}, description='B4 T no budgeted', account=acct) testdb.add(t2) t3 = Transaction(date=date(2017, 4, 12), budget_amounts={budgets[4]: Decimal('600.00')}, budgeted_amount=Decimal('500.00'), description='B4 T budgeted', account=acct, planned_budget=budgets[4]) testdb.add(t3) # Budget 5 budgeted greater than allocated (100) testdb.add( ScheduledTransaction(amount=Decimal('2.00'), description='B5 ST', account=acct, budget=budgets[5], day_of_month=9)) t4 = Transaction(date=date(2017, 4, 13), description='B5 T', budget_amounts={budgets[5]: Decimal('3.00')}, budgeted_amount=Decimal('1.00'), account=acct, planned_budget=budgets[5]) testdb.add(t4) testdb.flush() testdb.commit()
def test_3_add_transactions(self, testdb): acct = testdb.query(Account).get(1) budgets = {x.id: x for x in testdb.query(Budget).all()} # Budget 3 Income Transaction testdb.add( Transaction(date=date(2017, 4, 7), actual_amount=100.00, budgeted_amount=100.00, description='B3 Income', account=acct, budget=budgets[3])) # Budget 3 Income ST testdb.add( ScheduledTransaction(amount=99.00, description='B3 Income ST', account=acct, budget=budgets[3], num_per_period=1)) # Budget 4 allocated greater than budgeted (500.00) testdb.add( ScheduledTransaction(amount=250.00, description='B4 ST', account=acct, budget=budgets[4], date=date(2017, 4, 10))) testdb.add( Transaction(date=date(2017, 4, 11), actual_amount=250.00, description='B4 T no budgeted', account=acct, budget=budgets[4])) testdb.add( Transaction(date=date(2017, 4, 12), actual_amount=600.00, budgeted_amount=500.00, description='B4 T budgeted', account=acct, budget=budgets[4])) # Budget 5 budgeted greater than allocated (100) testdb.add( ScheduledTransaction(amount=2.00, description='B5 ST', account=acct, budget=budgets[5], day_of_month=9)) testdb.add( Transaction(date=date(2017, 4, 13), description='B5 T', actual_amount=3.00, budgeted_amount=1.00, account=acct, budget=budgets[5])) testdb.flush() testdb.commit()
def submit(self, data): """ Handle form submission; create or update models in the DB. Raises an Exception for any errors. :param data: submitted form data :type data: dict :return: message describing changes to DB (i.e. link to created record) :rtype: str """ st_id = int(data['id']) st = db_session.query(ScheduledTransaction).get(st_id) d = datetime.strptime(data['payperiod_start_date'], '%Y-%m-%d').date() desc = 'Skip ScheduledTransaction %d in period %s' % ( st_id, data['payperiod_start_date']) t = Transaction(date=d, budget_amounts={st.budget: Decimal('0.0')}, budgeted_amount=Decimal('0.0'), description=desc, notes=data['notes'], account=st.account, scheduled_trans=st, planned_budget=st.budget) db_session.add(t) db_session.add(TxnReconcile(transaction=t, note=desc)) db_session.commit() logger.info( 'Created Transaction %d to skip ' 'ScheduledTransaction %d', t.id, st.id) return 'Successfully created Transaction %d to skip ' \ 'ScheduledTransaction %d.' % (t.id, st.id)
def _add_transactions(self, data_sess, pp): budgets = list(data_sess.query(Budget).filter( Budget.is_active.__eq__(True), Budget.is_income.__eq__(False), Budget.is_periodic.__eq__(True) ).all()) target = 1950 total = 0 count = 0 while total < target: count += 1 amt = uniform(0, target * 0.2) if total + amt > target: amt = target - total total += amt amt = Decimal(Decimal(amt).quantize( Decimal('.001'), rounding=ROUND_HALF_UP )) data_sess.add(Transaction( account_id=1, budgeted_amount=amt, date=pp.start_date + timedelta(days=randrange(0, 12)), description='Transaction %d' % count, budget_amounts={ choice(budgets): amt } )) data_sess.flush() data_sess.commit()
def submit(self, data): """ Handle form submission; create or update models in the DB. Raises an Exception for any errors. :param data: submitted form data :type data: dict :return: message describing changes to DB (i.e. link to created record) :rtype: str """ st_id = int(data['id']) st = db_session.query(ScheduledTransaction).get(st_id) d = datetime.strptime(data['date'], '%Y-%m-%d').date() t = Transaction(date=d, budget_amounts={st.budget: Decimal(data['amount'])}, budgeted_amount=st.amount, description=data['description'], notes=data['notes'], account=st.account, planned_budget=st.budget, scheduled_trans=st) db_session.add(t) db_session.commit() logger.info('Created Transaction %d for ScheduledTransaction %d', t.id, st.id) return 'Successfully created Transaction %d ' \ 'for ScheduledTransaction %d.' % (t.id, st.id)
def test_add_one(self): mock_sess = Mock() with patch('%s.inspect' % pbm) as mock_inspect: type(mock_inspect.return_value).session = mock_sess b1 = Budget(name='my_budg') t = Transaction(budget_amounts={b1: Decimal('10.00')}) assert len(t.budget_transactions) == 1 assert t.budget_transactions[0].transaction == t assert t.budget_transactions[0].budget == b1 assert t.budget_transactions[0].amount == Decimal('10.00') assert mock_sess.mock_calls == []
def test_unreconciled(self): Transaction() m_db = Mock() m_q = Mock(spec_set=Query) m_filt = Mock(spec_set=Query) m_db.query.return_value = m_q m_q.filter.return_value = m_filt res = Transaction.unreconciled(m_db) assert res == m_filt assert len(m_db.mock_calls) == 2 assert m_db.mock_calls[0] == call.query(Transaction) kall = m_db.mock_calls[1] assert kall[0] == 'query().filter' expected1 = Transaction.reconcile.__eq__(null()) expected2 = Transaction.date.__ge__(date(2017, 3, 17)) expected3 = Transaction.account.has(reconcile_trans=True) assert len(kall[1]) == 3 assert str(expected1) == str(kall[1][0]) assert binexp_to_dict(expected2) == binexp_to_dict(kall[1][1]) assert str(expected3) == str(kall[1][2])
def test_3_add_trans_standing_budget(self, testdb): """add a transaction against a standing budget""" testdb.add( Transaction(actual_amount=222.22, budgeted_amount=123.45, description='T5', notes='notesT5', account=testdb.query(Account).get(1), budget=testdb.query(Budget).get(5))) testdb.flush() testdb.commit()
def test_1_add_trans_periodic_budget(self, testdb): """add a transaction against a periodic budget""" testdb.add( Transaction(actual_amount=222.22, budgeted_amount=123.45, description='T4', notes='notesT4', account=testdb.query(Account).get(1), budget=testdb.query(Budget).get(2))) testdb.flush() testdb.commit()
def test_7_spent_greater_than_allocated(self, testdb): acct = testdb.query(Account).get(1) budget = testdb.query(Budget).get(5) testdb.add( Transaction(date=date(2017, 4, 13), description='B6 T', actual_amount=2032.0, budgeted_amount=32.0, account=acct, budget=budget)) testdb.flush() testdb.commit() pp = BiweeklyPayPeriod.period_for_date(date(2017, 4, 10), testdb) assert pp._data['budget_sums'] == { 2: { 'budget_amount': Decimal('123.45'), 'allocated': Decimal('0.0'), 'spent': Decimal('0.0'), 'trans_total': Decimal('0.0'), 'is_income': True, 'remaining': Decimal('123.45') }, 3: { 'budget_amount': Decimal('0.0'), 'allocated': Decimal('199.0'), 'spent': Decimal('100.0'), 'trans_total': Decimal('199.0'), 'is_income': True, 'remaining': Decimal('199.0') }, 4: { 'budget_amount': Decimal('500.00'), 'allocated': Decimal('1000.0'), 'spent': Decimal('850.0'), 'trans_total': Decimal('1100.0'), 'is_income': False, 'remaining': Decimal('-600.0') }, 5: { 'budget_amount': Decimal('100.0'), 'allocated': Decimal('35.0'), 'spent': Decimal('2035.0'), 'trans_total': Decimal('2037.0'), 'is_income': False, 'remaining': Decimal('-1937.0') } } assert pp._data['overall_sums'] == { 'allocated': Decimal('1100.0'), 'spent': Decimal('2885.0'), 'income': Decimal('322.45'), 'remaining': Decimal('-2562.55') }
def test_1_add_trans_periodic_budget(self, testdb): """add a transaction against a periodic budget""" t = Transaction( budget_amounts={testdb.query(Budget).get(2): Decimal('222.22')}, budgeted_amount=Decimal('123.45'), description='T5', notes='notesT5', account=testdb.query(Account).get(1), planned_budget=testdb.query(Budget).get(2)) testdb.add(t) testdb.flush() testdb.commit()
def test_3_add_trans_standing_budget(self, testdb): """add a transaction against a standing budget""" t = Transaction( budget_amounts={testdb.query(Budget).get(5): Decimal('222.22')}, budgeted_amount=Decimal('123.45'), description='T6', notes='notesT6', account=testdb.query(Account).get(1), planned_budget=testdb.query(Budget).get(5)) testdb.add(t) testdb.flush() testdb.commit()
def test_0_update_db(self, testdb): b = testdb.query(Budget).get(4) b.current_balance = 1617.56 testdb.add(b) acct = testdb.query(Account).get(1) budget = testdb.query(Budget).get(1) pp = BiweeklyPayPeriod.period_for_date(dtnow(), testdb) tdate = pp.start_date + timedelta(days=2) stmt1 = OFXStatement(account=acct, filename='a1.ofx', file_mtime=dtnow(), as_of=dtnow(), currency='USD', acctid='1', bankid='b1', routing_number='r1') testdb.add(stmt1) o = OFXTransaction(account=acct, statement=stmt1, fitid='OFX8', trans_type='Purchase', date_posted=dtnow(), amount=-600.0, name='ofx8-trans4') testdb.add(o) t = Transaction(date=tdate, actual_amount=600.00, description='trans6', account=acct, budget=budget) testdb.add(t) testdb.add(TxnReconcile(transaction=t, ofx_trans=o)) testdb.add( Transaction(date=tdate, actual_amount=34000.00, description='transFoo', account=acct, budget=budget)) testdb.flush() testdb.commit()
def _payperiods_preshot(self): logger.info('payperiods preshot') # BEGIN DB update conn = engine.connect() data_sess = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=conn) ) pp = BiweeklyPayPeriod.period_for_date(dtnow(), data_sess).previous data_sess.add(Budget( name='Budget3', is_periodic=True, starting_balance=Decimal('0') )) data_sess.add(Budget( name='Budget4', is_periodic=True, starting_balance=Decimal('0') )) data_sess.flush() data_sess.commit() budgets = list(data_sess.query(Budget).filter( Budget.is_active.__eq__(True), Budget.is_income.__eq__(False), Budget.is_periodic.__eq__(True) ).all()) for i in range(0, 12): # payperiods mult = choice([-1, 1]) target = 2011.67 + (mult * uniform(0, 500)) total = 0 count = 0 while total < target: count += 1 amt = uniform(0, target * 0.2) if total + amt > target: amt = target - total total += amt amt = Decimal(Decimal(amt).quantize( Decimal('.001'), rounding=ROUND_HALF_UP )) data_sess.add(Transaction( account_id=1, budgeted_amount=amt, date=pp.start_date + timedelta(days=1), description='Transaction %d.%d' % (i, count), budget_amounts={ choice(budgets): amt } )) data_sess.flush() data_sess.commit() pp = pp.next data_sess.close() conn.close() # END DB update self.get('/payperiods') sleep(1)
def test_82_issue201_st_to_trans(self, testdb): budg = testdb.query(Budget).get(6) st = testdb.query(ScheduledTransaction).get(4) testdb.add( Transaction(date=date(2020, 4, 14), description='B6 T6 from ST4', budget_amounts={budg: Decimal('200.00')}, budgeted_amount=Decimal('2.00'), account=testdb.query(Account).get(1), planned_budget=budg, scheduled_trans=st)) testdb.flush() testdb.commit()
def test_none_but_existing(self): mock_sess = Mock() with patch('%s.inspect' % pbm) as mock_inspect: type(mock_inspect.return_value).session = mock_sess b1 = Budget(name='my_budg') b2 = Budget(name='my_budg2') t = Transaction(budget_amounts={ b1: Decimal('10.00'), b2: Decimal('90.00') }) assert len(t.budget_transactions) == 2 for bt in t.budget_transactions: assert isinstance(bt, BudgetTransaction) assert bt.transaction == t assert {bt.budget: bt.amount for bt in t.budget_transactions} == { b1: Decimal('10.00'), b2: Decimal('90.00') } mock_sess.reset_mock() with pytest.raises(AssertionError): t.set_budget_amounts({}) assert mock_sess.mock_calls == []
def get(self): res = [] for t in Transaction.unreconciled(db_session).order_by( Transaction.date).all(): d = t.as_dict d['budgets'] = [{ 'name': bt.budget.name, 'id': bt.budget_id, 'amount': bt.amount, 'is_income': bt.budget.is_income } for bt in sorted( t.budget_transactions, key=lambda x: x.amount, reverse=True)] d['account_name'] = t.account.name res.append(d) return jsonify(res)
def do_budget_transfer(db_sess, txn_date, amount, account, from_budget, to_budget, notes=None): """ Transfer a given amount from ``from_budget`` to ``to_budget`` on ``txn_date``. This method does NOT commit database changes. There are places where we rely on this function not committing changes. :param db_sess: active database session to use for queries :type db_sess: sqlalchemy.orm.session.Session :param txn_date: date to make the transfer Transactions on :type txn_date: datetime.date :param amount: amount of money to transfer :type amount: float :param account: :type account: biweeklybudget.models.account.Account :param from_budget: :type from_budget: biweeklybudget.models.budget_model.Budget :param to_budget: :type to_budget: biweeklybudget.models.budget_model.Budget :param notes: Notes to add to the Transaction :type notes: str :return: list of Transactions created for the transfer :rtype: :py:obj:`list` of :py:class:`~.Transaction` objects """ desc = 'Budget Transfer - %s from %s (%d) to %s (%d)' % ( amount, from_budget.name, from_budget.id, to_budget.name, to_budget.id) logger.info(desc) t1 = Transaction(date=txn_date, budget_amounts={from_budget: amount}, budgeted_amount=amount, description=desc, account=account, notes=notes, planned_budget=from_budget) db_sess.add(t1) t2 = Transaction(date=txn_date, budget_amounts={to_budget: (-1 * amount)}, budgeted_amount=(-1 * amount), description=desc, account=account, notes=notes, planned_budget=to_budget) db_sess.add(t2) t1.transfer = t2 db_sess.add(t1) t2.transfer = t1 db_sess.add(t2) db_sess.add(TxnReconcile(transaction=t1, note=desc)) db_sess.add(TxnReconcile(transaction=t2, note=desc)) return [t1, t2]
def submit(self, data): """ Handle form submission; create or update models in the DB. Raises an Exception for any errors. :param data: submitted form data :type data: dict :return: message describing changes to DB (i.e. link to created record) :rtype: str """ if 'id' in data and data['id'].strip() != '': # updating an existing budget trans = db_session.query(Transaction).get(int(data['id'])) if trans is None: raise RuntimeError("Error: no Transaction with ID " "%s" % data['id']) if trans.reconcile is not None: raise RuntimeError( "Transaction %d is already reconciled; cannot be edited." "" % trans.id) action = 'updating Transaction ' + data['id'] else: trans = Transaction() action = 'creating new Transaction' trans.description = data['description'].strip() trans.date = datetime.strptime(data['date'], '%Y-%m-%d').date() trans.account_id = int(data['account']) trans.notes = data['notes'].strip() budg_amts = {} for bid, budg_amt in data['budgets'].items(): budg = db_session.query(Budget).get(int(bid)) budg_amts[budg] = Decimal(budg_amt) trans.set_budget_amounts(budg_amts) logger.info('%s: %s', action, trans.as_dict) db_session.add(trans) db_session.commit() return { 'success_message': 'Successfully saved Transaction %d in database.' '' % trans.id, 'success': True, 'trans_id': trans.id }
def submit(self, data): """ Handle form submission; create or update models in the DB. Raises an Exception for any errors. :param data: submitted form data :type data: dict :return: message describing changes to DB (i.e. link to created record) :rtype: str """ # get the data trans_date = datetime.strptime(data['date'], '%Y-%m-%d').date() amt = Decimal(data['amount']) from_acct = db_session.query(Account).get(int(data['from_account'])) if from_acct is None: raise RuntimeError("Error: no Account with ID %s" % data['from_account']) to_acct = db_session.query(Account).get(int(data['to_account'])) if to_acct is None: raise RuntimeError("Error: no Account with ID %s" % data['to_account']) budget = db_session.query(Budget).get(int(data['budget'])) if budget is None: raise RuntimeError("Error: no Budget with ID %s" % data['budget']) notes = data['notes'].strip() desc = 'Account Transfer - %s from %s (%d) to %s (%d)' % ( amt, from_acct.name, from_acct.id, to_acct.name, to_acct.id) logger.info(desc) t1 = Transaction(date=trans_date, budget_amounts={budget: amt}, budgeted_amount=amt, description=desc, account=from_acct, notes=notes, planned_budget=budget) db_session.add(t1) t2 = Transaction(date=trans_date, budget_amounts={budget: (-1 * amt)}, budgeted_amount=(-1 * amt), description=desc, account=to_acct, notes=notes, planned_budget=budget) db_session.add(t2) t1.transfer = t2 db_session.add(t1) t2.transfer = t1 db_session.add(t2) db_session.commit() return 'Successfully saved Transactions %d and %d in database.' % ( t1.id, t2.id)
def test_add_three(self): mock_sess = Mock() with patch('%s.inspect' % pbm) as mock_inspect: type(mock_inspect.return_value).session = mock_sess b1 = Budget(name='my_budg') b2 = Budget(name='my_budg2') b3 = Budget(name='my_budg3') t = Transaction(budget_amounts={ b1: Decimal('50.00'), b2: Decimal('10.00'), b3: Decimal('40.00') }) assert len(t.budget_transactions) == 3 for bt in t.budget_transactions: assert isinstance(bt, BudgetTransaction) assert bt.transaction == t assert {bt.budget: bt.amount for bt in t.budget_transactions} == { b1: Decimal('50.00'), b2: Decimal('10.00'), b3: Decimal('40.00') } assert mock_sess.mock_calls == []
def test_2_add_data(self, testdb): acct = testdb.query(Account).get(1) budg = testdb.query(Budget).get(1) budg2 = testdb.query(Budget).get(2) st_daynum = ScheduledTransaction(amount=Decimal('111.11'), description='ST_day_9', account=acct, budget=budg, day_of_month=9) testdb.add(st_daynum) t_daynum = Transaction( budget_amounts={budg: Decimal('111.33')}, budgeted_amount=Decimal('111.11'), date=date(2017, 4, 9), description='Trans_ST_day_9', account=acct, planned_budget=budg, scheduled_trans=st_daynum, ) testdb.add(t_daynum) testdb.add(TxnReconcile(note='foo', transaction=t_daynum)) st_pp1 = ScheduledTransaction(amount=Decimal('222.22'), description='ST_pp_1', account=acct, budget=budg, num_per_period=1) testdb.add(st_pp1) st_pp3 = ScheduledTransaction(amount=Decimal('333.33'), description='ST_pp_3', account=acct, budget=budg, num_per_period=3) testdb.add(st_pp3) t_pp3A = Transaction( budget_amounts={budg: Decimal('333.33')}, budgeted_amount=Decimal('333.33'), date=date(2017, 4, 14), description='Trans_ST_pp_3_A', account=acct, planned_budget=budg, scheduled_trans=st_pp3, ) testdb.add(t_pp3A) t_pp3B = Transaction(budget_amounts={budg: Decimal('333.33')}, budgeted_amount=Decimal('333.33'), date=date(2017, 4, 15), description='Trans_ST_pp_3_B', account=acct, planned_budget=budg, scheduled_trans=st_pp3) testdb.add(t_pp3B) st_date = ScheduledTransaction(amount=Decimal('444.44'), description='ST_date', account=acct, budget=budg, date=date(2017, 4, 12)) testdb.add(st_date) t_date = Transaction(budget_amounts={budg: Decimal('444.44')}, budgeted_amount=Decimal('444.44'), date=date(2017, 4, 12), description='Trans_ST_date', account=acct, planned_budget=budg, scheduled_trans=st_date) testdb.add(t_date) t_foo = Transaction(budget_amounts={budg: Decimal('555.55')}, date=date(2017, 4, 8), description='Trans_foo', account=acct) testdb.add(t_foo) t_bar = Transaction(budget_amounts={ budg: Decimal('666.66'), budg2: Decimal('100.00') }, date=date(2017, 4, 16), description='Trans_bar', account=acct) testdb.add(t_bar) testdb.flush() testdb.commit()
def test_2_add_data(self, testdb): acct = testdb.query(Account).get(1) budg = testdb.query(Budget).get(1) st_daynum = ScheduledTransaction(amount=111.11, description='ST_day_9', account=acct, budget=budg, day_of_month=9) testdb.add(st_daynum) t_daynum = Transaction( actual_amount=111.33, budgeted_amount=111.11, date=date(2017, 4, 9), description='Trans_ST_day_9', account=acct, budget=budg, scheduled_trans=st_daynum, ) testdb.add(t_daynum) testdb.add(TxnReconcile(note='foo', transaction=t_daynum)) st_pp1 = ScheduledTransaction(amount=222.22, description='ST_pp_1', account=acct, budget=budg, num_per_period=1) testdb.add(st_pp1) st_pp3 = ScheduledTransaction(amount=333.33, description='ST_pp_3', account=acct, budget=budg, num_per_period=3) testdb.add(st_pp3) t_pp3A = Transaction( actual_amount=333.33, budgeted_amount=333.33, date=date(2017, 4, 14), description='Trans_ST_pp_3_A', account=acct, budget=budg, scheduled_trans=st_pp3, ) testdb.add(t_pp3A) t_pp3B = Transaction(actual_amount=333.33, budgeted_amount=333.33, date=date(2017, 4, 15), description='Trans_ST_pp_3_B', account=acct, budget=budg, scheduled_trans=st_pp3) testdb.add(t_pp3B) st_date = ScheduledTransaction(amount=444.44, description='ST_date', account=acct, budget=budg, date=date(2017, 4, 12)) testdb.add(st_date) t_date = Transaction(actual_amount=444.44, budgeted_amount=444.44, date=date(2017, 4, 12), description='Trans_ST_date', account=acct, budget=budg, scheduled_trans=st_date) testdb.add(t_date) t_foo = Transaction(actual_amount=555.55, date=date(2017, 4, 8), description='Trans_foo', account=acct, budget=budg) testdb.add(t_foo) t_bar = Transaction(actual_amount=666.66, date=date(2017, 4, 16), description='Trans_bar', account=acct, budget=budg) testdb.add(t_bar) testdb.flush() testdb.commit()
def submit(self, data): """ Handle form submission; create or update models in the DB. Raises an Exception for any errors. :param data: submitted form data :type data: dict :return: message describing changes to DB (i.e. link to created record) :rtype: str """ total = float(data['total_cost']) dt = datetime.strptime(data['date'], '%Y-%m-%d').date() veh_name = db_session.query(Vehicle).get(int(data['vehicle'])).name fill = FuelFill() fill.date = dt fill.vehicle_id = int(data['vehicle']) fill.odometer_miles = int(data['odometer_miles']) fill.reported_miles = int(data['reported_miles']) fill.level_before = int(data['level_before']) fill.level_after = int(data['level_after']) fill.fill_location = data['fill_location'].strip() fill.cost_per_gallon = float(data['cost_per_gallon']) fill.total_cost = total fill.gallons = float(data['gallons']) fill.reported_mpg = float(data['reported_mpg']) fill.notes = data['notes'].strip() logger.info('Creating new FuelFill: %s', fill.as_dict) db_session.add(fill) db_session.commit() fill.calculate_mpg() db_session.commit() if data['add_trans'] != 'true': return { 'success_message': 'Successfully saved FuelFill %d ' 'in database.' % fill.id, 'success': True, 'fill_id': fill.id, 'calculated_mpg': fill.calculated_mpg } trans = Transaction() budg = db_session.query(Budget).get(int(data['budget'])) trans.description = '%s - FuelFill #%d (%s)' % ( data['fill_location'].strip(), fill.id, veh_name) trans.date = dt trans.actual_amount = total trans.account_id = int(data['account']) trans.budget = budg trans.notes = data['notes'].strip() logger.info('Creating new Transaction for FuelFill: %s', trans.as_dict) db_session.add(trans) db_session.commit() return { 'success_message': 'Successfully saved FuelFill %d ' ' and Transaction %d in database.' % (fill.id, trans.id), 'success': True, 'trans_id': trans.id, 'fill_id': fill.id, 'calculated_mpg': fill.calculated_mpg }