def roast_add(roast_id=None): user = g.user form = RoastForm() form.bean_id.choices = [ (b.id, "%s - %s (%dg)" % (b.purchase_date, b.name, b.weight)) for b in Bean.query.filter_by(user_id=user.id).order_by(Bean.purchase_date.desc()).limit(10).all() ] if roast_id: roast = Roast.query.get(roast_id) form.bean_dose.data = roast.bean_dose form.drop_temp.data = roast.drop_temp form.dry_end_time.data = seconds_to_timestring(roast.dry_end_time) form.dry_end_temp.data = roast.dry_end_temp form.fc_begin_time.data = seconds_to_timestring(roast.fc_begin_time) form.fc_begin_temp.data = roast.fc_begin_temp form.fc_end_time.data = seconds_to_timestring(roast.fc_end_time) form.fc_end_temp.data = roast.fc_end_temp form.sc_begin_time.data = seconds_to_timestring(roast.sc_begin_time) form.sc_begin_temp.data = roast.sc_begin_temp form.sc_end_time.data = seconds_to_timestring(roast.sc_end_time) form.sc_end_temp.data = roast.sc_end_temp form.end_time.data = seconds_to_timestring(roast.end_time) form.end_temp.data = roast.end_temp form.end_weight.data = roast.end_weight form.roaster_machine.data = roast.roaster_machine form.roast_date.data = roast.roast_date form.notes.data = roast.notes form.bean_id.data = roast.bean_id form.roast_id.data = roast.id if form.validate_on_submit(): form.roast_id.data = int(form.roast_id.data) if form.roast_id.data == 0: roast = Roast() else: roast = Roast.query.get(form.roast_id.data) if roast.user_id != user.id: return redirect(url_for("roast_list")) roast.bean_dose = form.bean_dose.data roast.drop_temp = form.drop_temp.data roast.dry_end_time = timestring_to_seconds(form.dry_end_time.data) roast.dry_end_temp = form.dry_end_temp.data roast.fc_begin_time = timestring_to_seconds(form.fc_begin_time.data) roast.fc_begin_temp = form.fc_begin_temp.data roast.fc_end_time = timestring_to_seconds(form.fc_end_time.data) roast.fc_end_temp = form.fc_end_temp.data roast.sc_begin_time = timestring_to_seconds(form.sc_begin_time.data) roast.sc_begin_temp = form.sc_begin_temp.data roast.sc_end_time = timestring_to_seconds(form.sc_end_time.data) roast.sc_end_temp = form.sc_end_temp.data roast.end_time = timestring_to_seconds(form.end_time.data) roast.end_temp = form.end_temp.data roast.end_weight = form.end_weight.data roast.roaster_machine = form.roaster_machine.data roast.roast_date = form.roast_date.data roast.notes = form.notes.data roast.bean_id = form.bean_id.data roast.user_id = user.id Roast.add_roast(roast) return redirect(url_for("roast_list")) return render_template("roast_add.html", form=form)
def roast_add(roast_id=None): user = g.user form = RoastForm() form.bean_id.choices = [ (b.id, '%s - %s (%dg)' % (b.purchase_date, b.name, b.weight)) for b in Bean.query.filter_by(user_id=user.id).order_by( Bean.purchase_date.desc()).limit(10).all() ] if roast_id: roast = Roast.query.get(roast_id) form.bean_dose.data = Decimal(roast.bean_dose) / 100 form.drop_temp.data = roast.drop_temp form.dry_end_time.data = seconds_to_timestring(roast.dry_end_time) form.dry_end_temp.data = roast.dry_end_temp form.fc_begin_time.data = seconds_to_timestring(roast.fc_begin_time) form.fc_begin_temp.data = roast.fc_begin_temp form.fc_end_time.data = seconds_to_timestring(roast.fc_end_time) form.fc_end_temp.data = roast.fc_end_temp form.sc_begin_time.data = seconds_to_timestring(roast.sc_begin_time) form.sc_begin_temp.data = roast.sc_begin_temp form.sc_end_time.data = seconds_to_timestring(roast.sc_end_time) form.sc_end_temp.data = roast.sc_end_temp form.end_time.data = seconds_to_timestring(roast.end_time) form.end_temp.data = roast.end_temp form.end_weight.data = Decimal(roast.end_weight) / 100 form.roaster_machine.data = roast.roaster_machine form.roast_date.data = roast.roast_date form.notes.data = roast.notes form.bean_id.data = roast.bean_id form.roast_id.data = roast.id if form.validate_on_submit(): form.roast_id.data = int(form.roast_id.data) if form.roast_id.data == 0: roast = Roast() else: roast = Roast.query.get(form.roast_id.data) if roast.user_id != user.id: return redirect(url_for('roast_list')) roast.bean_dose = int(form.bean_dose.data * 100) roast.drop_temp = form.drop_temp.data roast.dry_end_time = timestring_to_seconds(form.dry_end_time.data) roast.dry_end_temp = form.dry_end_temp.data roast.fc_begin_time = timestring_to_seconds(form.fc_begin_time.data) roast.fc_begin_temp = form.fc_begin_temp.data roast.fc_end_time = timestring_to_seconds(form.fc_end_time.data) roast.fc_end_temp = form.fc_end_temp.data roast.sc_begin_time = timestring_to_seconds(form.sc_begin_time.data) roast.sc_begin_temp = form.sc_begin_temp.data roast.sc_end_time = timestring_to_seconds(form.sc_end_time.data) roast.sc_end_temp = form.sc_end_temp.data roast.end_time = timestring_to_seconds(form.end_time.data) roast.end_temp = form.end_temp.data roast.end_weight = int(form.end_weight.data * 100) roast.roaster_machine = form.roaster_machine.data roast.roast_date = form.roast_date.data roast.notes = form.notes.data roast.bean_id = form.bean_id.data roast.user_id = user.id Roast.add_roast(roast) return redirect(url_for('roast_list')) return render_template('roast_add.html', form=form)