def entry(): global logger, path_to_book, s3_bucket_name, s3_client, book_name logger.info('Creating new form inside of the /entry route') form = TransactionForm.new() if form.validate_on_submit(): # Add the transaction to the GnuCash book logger.info('Attempting to open book in /entry route') gnucash_book = open_book(path_to_book) descrip = form.description.data amount = form.amount.data credit = form.credit.data debit = form.debit.data date = form.date.data time = datetime.utcnow().time() enter_datetime = datetime.combine(date, time) added_txn = add_transaction(gnucash_book, descrip, amount, debit, credit, enter_datetime) gnucash_book.close() # Upload the GnuCash book to Scaleway S3 and delete local copy if added_txn: upload_gnucash_file_to_s3_and_delete_local(path_to_book, book_name, s3_bucket_name, s3_client) else: flash('Failed to add transaction to GnuCash book', 'danger') return redirect(url_for('entry')) return render_template('entry.html', form=form)
def new(cls): """Instantiate a new AddAccountForm.""" global logger, path_to_book, book_name, s3_client, s3_bucket_name logger.info('Attempting to download GnuCash file for /accounts.') if downloaded := download_gnucash_file_from_scaleway_s3( book_name, path_to_book, s3_bucket_name, s3_client): logger.info('Successfully downloaded GnuCash file for /accounts.')
def new(cls): """Instantiate a new DeleteTransactionForm.""" global logger, path_to_book, book_name, s3_client, s3_bucket_name logger.info( 'Attempting to download GnuCash file from Scaleway S3 for /delete.' ) if downloaded := download_gnucash_file_from_scaleway_s3( book_name, path_to_book, s3_bucket_name, s3_client): logger.info('Successfully downloaded GnuCash file for /delete.')
def transactions(): global path_to_book global logger logger.info('Attempting to open book inside transactions route') book = open_book(path_to_book) # determine the number of transactions to display based on env var num_transactions = int(get_env_var('NUM_TRANSACTIONS')) if num_transactions is None: transactions = last_n_transactions(book) else: transactions = last_n_transactions(book, n=num_transactions) book.close() return render_template('transactions.html', transactions=transactions, n=num_transactions)
def balances(): global path_to_book global logger logger.info('Attempting to open book inside of balances route.') book = open_book(path_to_book, readonly=True) accounts = [] for acc in book.accounts: account = {} fn = acc.fullname.replace(':', ' ➔ ') bal = acc.get_balance() bal = f'${bal:,.2f}' account['fullname'] = fn account['balance'] = bal accounts.append(account) accounts = sorted(accounts, key=lambda x: x['fullname']) book.close() return render_template('balances.html', accounts=accounts)
def delete(): global logger, path_to_book, book_name, s3_client, s3_bucket_name logger.info('Creating new form inside of the /delete route') form = DeleteTransactionForm.new() if form.validate_on_submit(): # Delete the transaction from the GnuCash book logger.info('Attempting to open book in /delete route') gnucash_book = open_book(path_to_book) txn_to_delete = form.del_transactions.data txn_deleted = delete_transaction(gnucash_book, txn_to_delete) gnucash_book.close() if txn_deleted: upload_gnucash_file_to_s3_and_delete_local(path_to_book, book_name, s3_bucket_name, s3_client) else: message = 'Transaction was NOT deleted from GnuCash file:\n' message += txn_to_delete flash(message, 'danger') return redirect(url_for('entry')) return render_template('delete_txn.html', form=form)
def accounts(): global logger logger.info('Creating new form inside of the /accounts route') delete_form = DeleteAccountForm.new() add_form = AddAccountForm.new() if delete_form.validate_on_submit(): # Delete the account from the GnuCash book logger.info('Attempting to open book in /accounts route') gnucash_book = open_book(path_to_book) acc_to_delete = delete_form.del_account.data acc_deleted = delete_account_with_inheritance(gnucash_book, acc_to_delete) gnucash_book.close() if acc_deleted: upload_gnucash_file_to_s3_and_delete_local(path_to_book, book_name, s3_bucket_name, s3_client) else: message = 'Account was NOT deleted from GnuCash file:\n' message += acc_to_delete flash(message, 'danger') return redirect(url_for('accounts')) elif add_form.validate_on_submit(): logger.info('Attempting to open book in /accounts route') gnucash_book = open_book(path_to_book) acc_to_add = add_form.new_account.data parent_account = add_form.parent_account_select.data acc_added = add_account(gnucash_book, acc_to_add, parent_account) gnucash_book.close() if acc_added: upload_gnucash_file_to_s3_and_delete_local(path_to_book, book_name, s3_bucket_name, s3_client) else: message = f'Account "{acc_to_add}" was NOT added to GnuCash file:\n' flash(message, 'danger') return redirect(url_for('accounts')) return render_template('accounts.html', delete_form=delete_form, add_form=add_form)
def index(): global logger logger.info('Rendering the index page') return render_template('index.html')
@classmethod def new(cls): """Instantiate a new TransactionForm.""" global logger, path_to_book, book_name, s3_client, s3_bucket_name logger.info( f'Attempting to download GnuCash file {book_name} from Scaleway S3.' ) if downloaded := download_gnucash_file_from_scaleway_s3( book_name, path_to_book, s3_bucket_name, s3_client): logger.info( 'Successfully downloaded GnuCash file from S3 for /entry.') else: logger.critical( 'Failed to download GnuCash file from S3 for /entry.') raise SystemExit logger.info( 'Attempting to read GnuCash book to create TransactionForm.') accounts = list_accounts(path_to_book) txn_form = cls() txn_form.debit.choices = accounts txn_form.credit.choices = accounts return txn_form class DeleteTransactionForm(FlaskForm): del_transactions = SelectField('Select a Transaction to Delete', validators=[DataRequired()], validate_choice=True) submit = SubmitField('Delete') @classmethod