def book(): form = TransactionForm(current_user.username) if form.validate_on_submit(): trans = Transaction(store=form.store.data, date=pd.to_datetime(form.date.data), flexible=form.flexible.data, payment_type=form.payment_type.data, payment_notes=form.payment_notes.data, list=form.grocery_list.data, notes=form.other_notes.data, booking_date=dt.date.today()) trans.assign_recipient(current_user) if current_user.buddy is not None: trans.claimed = True send_confirmation(current_user.buddy, 'volunteer_buddy', transaction=trans) db.session.add(trans) db.session.commit() day_of_week = calendar.day_name[pd.to_datetime( form.date.data).weekday()] str_date = form.date.data.strftime('%m/%d') flash(f'Delivery booked for {day_of_week}, {str_date}!') send_confirmation(current_user, 'recipient_booking', transaction=trans) return redirect(url_for('deliveries', username=current_user.username)) elif request.method == 'GET': d = dt.datetime.today() # default to next Friday while d.weekday() != 4: d += dt.timedelta(1) form.date.data = d # fetch most recent store, most recent payment type, most recent payment notes most_recent_trans = db.session.query(Transaction).join( Recipient).filter( Recipient.username == current_user.username).order_by( desc(Transaction.date)).first() if most_recent_trans is not None: form.store.data = most_recent_trans.store form.payment_type.data = most_recent_trans.payment_type form.payment_notes.data = most_recent_trans.payment_notes return render_template('standard_form.html', header='Book Delivery', form=form)