Beispiel #1
0
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,)
Beispiel #2
0
def create(request):
    if request.POST:
        form = ScaleForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/scales/all')
    else:
        form = ScaleForm()

    args = {}
    args.update(csrf(request))

    args['form'] = form

    return render_to_response('create_scale.html', args)