def player(request, player_id): player_id = int(player_id) p = Player.objects.get(id=player_id) if request.method == 'POST' and request.POST['form_name'] == 'transfer': tf = TransferForm(request.POST) tf.fields['to'].choices = get_transfer_choices(player_id) if tf.is_valid(): to_name = do_transfer(player_id, tf.cleaned_data['to'], Decimal(tf.cleaned_data['amount'])) messages.success( request, "${} transferred to {}.".format(tf.cleaned_data['amount'], to_name)) return redirect('bank.views.player', player_id) iform = IncomeForm() elif request.method == 'POST' and request.POST['form_name'] == 'income': iform = IncomeForm(request.POST) if iform.is_valid(): p.balance += Decimal(iform.cleaned_data['amount']) p.save() messages.success( request, "Added ${} income..".format(iform.cleaned_data['amount'])) return redirect('bank.views.player', player_id) tf = TransferForm() else: tf = TransferForm() iform = IncomeForm() tf.fields['to'].choices = get_transfer_choices(player_id) return render( request, "player.html", { 'player': p, 'transfer_form': tf, 'players': Player.objects.all(), 'income_form': iform })
def add_income(): form = IncomeForm() users = User.query.filter_by(bmID=current_user.bmID) form.owner.choices = [(user.id, user.firstname) for user in users] banks = Bank.query.filter_by(bmID=current_user.bmID, accounttype="Current") form.paidinto.choices = [(bank.bankID, bank.bankname + " - " + bank.accounttype) for bank in banks] if form.validate_on_submit(): new_income = Income(bmID=current_user.bmID, owner=form.owner.data, amount=form.amount.data, frequency=form.frequency.data, paidinto=form.paidinto.data, nextduedate=form.nextduedate.data) db.session.add(new_income) db.session.commit() return redirect(url_for("view_incomes")) return render_template("add-income.html", form=form)
def edit_income(income_id): income = Income.query.get(income_id) edit_form = IncomeForm(owner=get_member_name(income.owner), amount=income.amount, frequency=income.frequency, paidinto=get_bank_name(income.paidinto), nextduedate=income.nextduedate) users = User.query.filter_by(bmID=current_user.bmID) edit_form.owner.choices = [(user.id, user.firstname) for user in users] banks = Bank.query.filter_by(bmID=current_user.bmID, accounttype="Current") edit_form.paidinto.choices = [(bank.bankID, bank.bankname + " - " + bank.accounttype) for bank in banks] if edit_form.validate_on_submit(): income.owner = edit_form.owner.data, income.amount = edit_form.amount.data, income.frequency = edit_form.frequency.data, income.paidinto = edit_form.paidinto.data, income.nextduedate = edit_form.nextduedate.data db.session.commit() return redirect(url_for("view_income")) return render_template("add-income.html", form=edit_form)