A few session/cart issues hoping to get help with :-)

47 views
Skip to first unread message

Andrew Evans

unread,
Oct 8, 2012, 12:33:20 PM10/8/12
to web...@googlegroups.com
I am trying to solve an issue in my code and hoping some of you can help

I have a cart functionality for reserving items based on a month / day / year reservation. The problem is an extra entry is being added to the database when I submit the form.

Any ideas how to fix this also will my check_database_entry onvalidation method work if I am now adding the data using sessions?

*cheers

Below is my code

def cart_callback():
    id = int(request.vars.id)
    if request.vars.action == 'add':
        session.cart[id] = session.cart.get(id,0)+1
        session.flash = 'Item added to Cart'
    if request.vars.action == 'sub':
        session.cart[id]=max(0,session.cart.get(id,0)-1)
        session.flash = 'Item removed from Cart'
    return redirect(URL('first_year', 'first_year'))

def check_database_entry(form):
    prod_id = request.args(0)
    project_date = db(db.checkout.product == prod_id).select()
    for project in project_date:
        start = project.date_in
        end = project.date_out
        user_start = datetime.datetime.strptime(form.vars.date_in, '%Y-%m-%d').date()
        user_end = datetime.datetime.strptime(form.vars.date_out, '%Y-%m-%d').date()
        if start <= user_start <= end or start <= user_end <= end:  
            session.flash = 'Those dates have been reserved for those items, please try another'
            redirect(URL('error', 'availability')) 

@auth.requires(auth.has_membership('first_year') or auth.has_membership('admin'))
def cart_checkout():
    form = SQLFORM(db.checkout, formstyle='divs')
    if form.process(onvalidation=check_database_entry).accepted:
        for key, value in session.cart.items():
            db.checkout.insert(userinfo = auth.user.id,
                            product = key,
                            project = form.vars.project,
                            date_in = form.vars.date_in,
                            date_out = form.vars.date_out)
        session.cart.clear()
        session.flash = 'Your item(s) are Reserved'
        redirect(URL('default', 'next'))             
    elif form.errors:
        response.flash = 'form has errors'
    else:
        response.flash = 'please fill the form'
        return dict(form=form)


Massimo Di Pierro

unread,
Oct 8, 2012, 1:05:59 PM10/8/12
to web...@googlegroups.com
In your code:

    form = SQLFORM(db.checkout, formstyle='divs') 
    if form.process(onvalidation=check_database_entry).accepted:
        for key, value in session.cart.items():
            db.checkout.insert(userinfo = auth.user.id,
                            product = key,
                            project = form.vars.project,
                            date_in = form.vars.date_in,
                            date_out = form.vars.date_out)

The function process() does a db.checkout.insert(**form.vars). You can prevent it with dbio=False

    form = SQLFORM(db.checkout, formstyle='divs') 
    if form.process(dbio=False,onvalidation=check_database_entry).accepted:

        for key, value in session.cart.items():
            db.checkout.insert(userinfo = auth.user.id,
                            product = key,
                            project = form.vars.project,
                            date_in = form.vars.date_in,
                            date_out = form.vars.date_out)
Reply all
Reply to author
Forward
0 new messages