Checking the database items and seeing if they satisfy the condition before taking an action

19 views
Skip to first unread message

mostwanted

unread,
Sep 21, 2019, 12:23:46 AM9/21/19
to web2py-users
What i want to achieve is to disable the quantity button in my shopping application that allows buyers to select the number of items they want if their number exceeds the number of available items. In my mind I can only achieve this if i were able to check first the quantity of the items we have and if they are not smaller than the client's requested amount as the client is still in the process of buying while clicking the quantity button, I am not sure on how to achieve this!

VIEW
<style>
   
.disabled {
  opacity
: 0.65;
  cursor
: not-allowed;
}
</style>

<h7 class="amt {{='red' if p.amount<=5 else ''}}">{{=p.amount}} available in stock
</h7>
   
<h5>
       
<span style="color: aqua; font-weight: bold;">{{=MoneyFormat(p.price)}}</span>
   
</h5>
   
<img id="item" class="magnify" src="{{=URL('download',args=p.image)}}" height="200px"/>
   
<br />
    {{=A('Remove',callback=URL('cart_callback',vars=dict(id=p.id,action='sub')),target='item%s'%p.id,_class='button pill')}} -
<span id="{{='item%s'%p.id}}" style="font-weight: bold; color: red;">{{=session.cart.get(p.id,0)}}</span> in cart -
   
<span class="{{='disabled' if p.amount > 'item%s'%p.id else ''}}">{{=A('Add to cart',callback=URL('cart_callback',vars=dict(id=p.id,action='add')),target='item%s'%p.id,_class='button pill')}}</span>
The highlighted part is where the button span should be getting disabled if the condition is satisfied the problem is i have not figured out a way to check first if the client's amount is higher than what we have in our stock!

CART_CALLBACK FUNCTION
The cart_callback function is the one that counts the number of items and adds them to the cart as the client clicks the button
def cart_callback():
    id
= int(request.vars.id)
   
if request.vars.action == 'add':
        session
.cart[id]=session.cart.get(id,0)+1
   
if request.vars.action == 'sub':
        session
.cart[id]=max(0,session.cart.get(id,0)-1)
   
return str(session.cart[id])


Reply all
Reply to author
Forward
0 new messages