def transaction(tr_type): """Create new transaction.""" if tr_type == 'minus': tr_type = True else: tr_type = False dbs = db.session form = TransactionForm() # form.tdate.data = date.today() form.account_id.choices = active_accounts(True) form.group_id.choices = active_groups(True) form.category_id.choices = active_categories(True) form.party_id.choices = active_parties(True) if form.validate_on_submit(): tr = Transaction() tr.tdate = form.tdate.data tr.amount = round(float(form.amount.data), 2) tr.minus = tr_type tr.owner = current_user tr.account_id = form.account_id.data tr.group_id = form.group_id.data tr.category_id = form.category_id.data tr.party_id = form.party_id.data tr.comment = form.comment.data.strip() try: # Save transaction to databse dbs.add(tr) dbs.commit() # Increase time_used for account tr.account.times_used += 1 # Increase times_used for group tr.group.times_used += 1 # Increase times_used for category tr.category.times_used += 1 # Increase times_used for party tr.party.times_used += 1 # Subtract expense or add balance from balance if tr_type: # a.balance = a.balance - tr.amount tr.account.balance = tr.account.balance - tr.amount else: # a.balance = a.balance + tr.amount tr.account.balance = tr.account.balance + tr.amount dbs.commit() flash('Transaction created!', 'success') return redirect(url_for('main.index')) except IntegrityError: dbs.rollback() flash('Transaction was not created', 'warning') return render_template('transactions/transaction.html', form=form)
def upload_file(): form = UploadForm() if request.method == 'GET': form.user_id.choices = [] user = load_user(current_user.id) if user.is_parent_user: children = User.query.filter_by(parent_id=current_user.id).all() for child in children: account_name = "No account" account_id = 0 if len(child.accounts.all()) > 0: account_name = child.accounts[0].name account_id = child.accounts[0].id account_balance = child.accounts[0].balance form.user_id.choices.append((child.id, child.username)) if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': flash('No selected file') return redirect(request.url) if file: content = file.read() content = content.decode('utf-8') print(content) csv_reader = csv.reader(StringIO(content), delimiter=',') user = load_user(form.user_id.data) current_account = user.accounts[0] print(form.user_id.data) print(user.username) print(current_account.balance) for row in csv_reader: transaction_date = row[0] description = row[1] credit = row[2] debit = row[3] transaction = Transaction(created_date=transaction_date, description=description, account=current_account) if credit: transaction.type = TransactionType.CREDIT.value transaction.amount = credit current_account.balance = current_account.balance + decimal.Decimal( credit) else: transaction.type = TransactionType.DEBIT.value transaction.amount = debit current_account.balance = current_account.balance - decimal.Decimal( debit) print(transaction) db.session.add(transaction) print(current_account.balance) db.session.commit() return redirect(url_for('main.index')) return render_template('upload_transactions.html', form=form)