def scale(sid=None): from models import Scale if not sid and 'sid' in request.args: sid = request.args.get('sid') if sid: # edit weight elem = Scale.query.get(sid) if elem: form = ScaleForm(obj=elem) else: # add form = ScaleForm() if form.validate_on_submit(): if not elem: elem = Scale(name=request.form['name']) if 'name' in request.form: elem.name = request.form['name'] if 'owner' in request.form: elem.owner = request.form['owner'] if 'model' in request.form: elem.model = request.form['model'] if 'comment' in request.form: elem.comment = request.form['comment'] db.session.add(elem) db.session.commit() flash('Data saved', 'info') return render_template('scale_edit.html', form=form,) else: # show table of weights page = request.args.get('page', '') if page.isdigit(): page = int(page) else: page = 1 elements = Scale.query.order_by('name').paginate( page, per_page=10) return render_template('scale_list.html', elements=elements.items, paginate=elements,)
def add_scales(doc, username): for elem in doc.xpath("//scales/item"): sc1 = Scale(name=unicode(elem.attrib["id"])) for i in elem: if i.tag=='owner': sc1.owner = unicode(i.text) if i.tag=='model' and i.text: sc1.model = unicode(i.text) db.session.add(sc1) db.session.flush() if 'default' in elem.attrib and \ elem.attrib['default'] == "True" and username: u1 = User.query.get(username) u1.default_scale_name = sc1.name db.session.commit()