def elems(elem_type): form = ElemForm() if elem_type == CONST.ELEM_ORG: elems = current_user.available_orgs() else: elems = current_user.available_agents() if request.method == "POST": if form.validate(elem_type, current_user): if elem_type == CONST.ELEM_ORG: newelem = Organization(form.name.data, form.inn.data, form.kpp.data, current_user.uid, form.is_ip.data) else: newelem = Agent(form.name.data, form.inn.data, form.kpp.data, current_user.uid, form.is_ip.data) db.session.add(newelem) db.session.commit() flash(u"Добавлен новый элемент", "alert-success") if elem_type == CONST.ELEM_ORG: elems = current_user.available_orgs() else: elems = current_user.available_agents() # clean fields lst_fields = ['name', 'inn', 'kpp', 'is_ip'] for fld in lst_fields: form[fld].raw_data = None form[fld].data = None return render_template("elems.html", form=form, elem_type=elem_type, elems=elems, edit=0)
def add_item(item_type=CONST.ELEM_AGENT): form = ElemForm(request.form) if form.validate(item_type, current_user): if item_type == CONST.ELEM_ORG: newitem = Organization(form.name.data, form.inn.data, form.kpp.data, current_user.uid, form.is_ip.data) else: newitem = Agent(form.name.data, form.inn.data, form.kpp.data, current_user.uid, form.is_ip.data) db.session.add(newitem) db.session.commit() return jsonify(result="OK", message=u"Элемент добавлен") else: return jsonify(result="error", fields=form.errors)
def update_elem(elem_type, uid): if elem_type == CONST.ELEM_ORG: elem = Organization.query.get_or_404(uid) else: elem = Agent.query.get_or_404(uid) form = ElemForm(request.form, obj=elem) if request.method == "POST": if form.validate(elem_type, current_user): elem.uid = form.uid.data elem.name = form.name.data elem.inn = form.inn.data elem.kpp = form.kpp.data elem.is_ip = form.is_ip.data db.session.add(elem) db.session.commit() flash(u"Изменения сохранены", "alert-success") return redirect(url_for("elems", elem_type=elem_type)) return render_template('elem.html', form=form, elem_type=elem_type, edit=1)