Exemplo n.º 1
0
def state_edit(id, country):
    state_form = EditStateForm()
    if request.method == 'POST':
        if not state_form.validate():
            flash('All fields are required.')
            return render_template('crud/state/edit.html', form=state_form)
        else:
            result = State.edit(request.form["id"], request.form["name"],
                                request.form["country"])
            if result:
                flash('State edited successfully!!!')
                return redirect(url_for('state'))
            else:
                flash('Unable to edit state.')
                return render_template('crud/state/edit.html', form=state_form)
    else:
        state = State.get(id)
        if not state:
            return redirect(url_for('state'))
        state_form.name.data = state["name"]
        state_form.country.data = state["parent"]
        state_form.id.data = state["id"]
        state_form.country_id.data = country
        return render_template('crud/state/edit.html', form=state_form)