[web2py] Re: SQLFORM.smartgrid selectable return string or list

2019-04-21 Thread icodk
You both are righ:
What's work now (also should worke before):
selectable=[('change price 100', lambda ids: modify_price(ids,100) ), ]

and the function in the same controller:
def modify_price(ids,inc):

print request.vars.records
print ids
print inc 


Both request.vars.records and ids are lists also with only one selected

Thanks for help  

 








On Sunday, April 21, 2019 at 1:56:09 AM UTC+2, Anthony wrote:
>
> On Saturday, April 20, 2019 at 8:52:01 AM UTC-4, icodk wrote:
>>
>> Hi Antony
>>
>> My selectable looks like :
>>
>> selectable = [('Add 100 to price',lambda ids: redirect(URL('product', 
>> 'modify_price', args=request.args, vars=dict(id=ids, inc=100,
>>
>>
> Why do a redirect rather than simply doing the work in the same function? 
> Also, this redirect puts the ids in the URL query string, resulting in the 
> ultimate update being a GET request rather than a POST -- this is 
> undesirable because a refresh of that page will result in the entire 
> operation being repeated, which you don't want.
>  
>
>>
>> and my function in the product controller looks like this:
>>
>> def modify_price():
>>
>> if type(request.vars.id) is str:
>>
>> id_list = request.vars.id.split(',') #convertint to list
>>
>> else:
>>
>> id_list = request.vars.id
>>
>> db(db.product.id.belongs(id_list)).update(product_price=db.product.product_price+request.vars.inc)
>>
>>
>>  Simply following the documentation in the book
>>
>> I also tried:
>>
>> selectable=  lambda ids: modify_price
>>
>>
>> But the function neve called
>>
>
> It's not that the function never gets called but that the function is not 
> written to work this way. The function expects to find ids in 
> request.vars.id, but that is actually None in the case where you call it 
> this way (the real values are in request.vars.records). Is modify_price a 
> controller action that needs to be accessible separately from this 
> particular operation? If not, just make it a helper function that accepts 
> an "ids" argument, and set selectable=modify_price.
>
> Anthony
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: SQLFORM.smartgrid selectable return string or list

2019-04-20 Thread Anthony
On Saturday, April 20, 2019 at 8:52:01 AM UTC-4, icodk wrote:
>
> Hi Antony
>
> My selectable looks like :
>
> selectable = [('Add 100 to price',lambda ids: redirect(URL('product', 
> 'modify_price', args=request.args, vars=dict(id=ids, inc=100,
>
>
Why do a redirect rather than simply doing the work in the same function? 
Also, this redirect puts the ids in the URL query string, resulting in the 
ultimate update being a GET request rather than a POST -- this is 
undesirable because a refresh of that page will result in the entire 
operation being repeated, which you don't want.
 

>
> and my function in the product controller looks like this:
>
> def modify_price():
>
> if type(request.vars.id) is str:
>
> id_list = request.vars.id.split(',') #convertint to list
>
> else:
>
> id_list = request.vars.id
>
> db(db.product.id.belongs(id_list)).update(product_price=db.product.product_price+request.vars.inc)
>
>
>  Simply following the documentation in the book
>
> I also tried:
>
> selectable=  lambda ids: modify_price
>
>
> But the function neve called
>

It's not that the function never gets called but that the function is not 
written to work this way. The function expects to find ids in 
request.vars.id, but that is actually None in the case where you call it 
this way (the real values are in request.vars.records). Is modify_price a 
controller action that needs to be accessible separately from this 
particular operation? If not, just make it a helper function that accepts 
an "ids" argument, and set selectable=modify_price.

Anthony

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: SQLFORM.smartgrid selectable return string or list

2019-04-20 Thread villas
Hi Icodk

I would see exactly what is being submitted by the browser. Open the 'web 
developer' section in your browser and look at the 'network' traffic.  When 
you click your submit button, see what variables etc are being sent.

If your function is really being called, then try some print statements to 
see what is being received by the function (you seem to be doing that).

Or, I guess you can also call your function directly from the lambda.  
...
selectable= (lambda ids: modify_price(ids, 100)),
...

def modify_price(ids, incr):
#print ids, incr
db(db.product.id.belongs(ids)).update(

product_price=db.product.product_price + incr)






On Saturday, 20 April 2019 13:52:01 UTC+1, icodk wrote:
>
> Hi Antony
>
> My selectable looks like :
>
> selectable = [('Add 100 to price',lambda ids: redirect(URL('product', 
> 'modify_price', args=request.args, vars=dict(id=ids, inc=100,
>
>
> and my function in the product controller looks like this:
>
> def modify_price():
>
> if type(request.vars.id) is str:
>
> id_list = request.vars.id.split(',') #convertint to list
>
> else:
>
> id_list = request.vars.id
>
> db(db.product.id.belongs(id_list)).update(product_price=db.product.product_price+request.vars.inc)
>
>
>  Simply following the documentation in the book
>
> I also tried:
>
> selectable=  lambda ids: modify_price
>
>
> But the function neve called
>
> It should be request.vars.records, not request.vars.id.
>
>
> Also tried with 
>
> def modify_rule_end_time():
>
> print request.vars.records 
>
>
> and 
>
> def modify_rule_end_time(ids):
>
> print ids
>
> But again no error and the function was not called
>
> On Friday, April 19, 2019 at 9:39:51 PM UTC+2, Anthony wrote:
>>
>> It should be request.vars.records, not request.vars.id.
>>
>> Note, request.vars is processed by the core framework, not by 
>> SQLFORM.smartgrid, so it will be a single value if the browser sends only 
>> one "records" field in the form data and a list otherwise. The core code 
>> that populates request.vars has no way of knowing that a given field 
>> containing a single value is supposed to be a list -- that has to be 
>> handled elsewhere. SQLFORM.smartgrid itself handles this properly, but if 
>> you are going to intercept request.vars and run some custom code outside of 
>> the grid, then it is up to you to do the check and convert to a list.
>>
>> Keep in mind, the "selectable" argument to the grid should be a function 
>> that takes the list of ids -- presumably you can move your code into that 
>> function, in which case, you won't have to worry about this, as the grid 
>> will handle the conversion to a list.
>>
>> Anthony
>>
>> On Friday, April 19, 2019 at 9:28:21 AM UTC-4, icodk wrote:
>>>
>>> SQLFORM.smartgrid selectable  return string if only one checkbox was 
>>> slected and returns a list if 2 or more was selected.
>>> This  inconsitancy force me to check for type before processing can 
>>> continue
>>> For example, If I have a button that update selected records in the 
>>> database call a function that do:
>>>
>>>
>>> db(db.product.id.belongs(request.vars.id)).update(price=db.product.price+request.vars.inc_price)
>>>
>>>
>>> However if only one line selected in the grid request.vars.id is a string 
>>> and not a list, which cause an error
>>>
>>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: SQLFORM.smartgrid selectable return string or list

2019-04-20 Thread icodk


Hi Antony

My selectable looks like :

selectable = [('Add 100 to price',lambda ids: redirect(URL('product', 
'modify_price', args=request.args, vars=dict(id=ids, inc=100,


and my function in the product controller looks like this:

def modify_price():

if type(request.vars.id) is str:

id_list = request.vars.id.split(',') #convertint to list

else:

id_list = request.vars.id

db(db.product.id.belongs(id_list)).update(product_price=db.product.product_price+request.vars.inc)


 Simply following the documentation in the book

I also tried:

selectable=  lambda ids: modify_price


But the function neve called

It should be request.vars.records, not request.vars.id.


Also tried with 

def modify_rule_end_time():

print request.vars.records 


and 

def modify_rule_end_time(ids):

print ids

But again no error and the function was not called

On Friday, April 19, 2019 at 9:39:51 PM UTC+2, Anthony wrote:
>
> It should be request.vars.records, not request.vars.id.
>
> Note, request.vars is processed by the core framework, not by 
> SQLFORM.smartgrid, so it will be a single value if the browser sends only 
> one "records" field in the form data and a list otherwise. The core code 
> that populates request.vars has no way of knowing that a given field 
> containing a single value is supposed to be a list -- that has to be 
> handled elsewhere. SQLFORM.smartgrid itself handles this properly, but if 
> you are going to intercept request.vars and run some custom code outside of 
> the grid, then it is up to you to do the check and convert to a list.
>
> Keep in mind, the "selectable" argument to the grid should be a function 
> that takes the list of ids -- presumably you can move your code into that 
> function, in which case, you won't have to worry about this, as the grid 
> will handle the conversion to a list.
>
> Anthony
>
> On Friday, April 19, 2019 at 9:28:21 AM UTC-4, icodk wrote:
>>
>> SQLFORM.smartgrid selectable  return string if only one checkbox was 
>> slected and returns a list if 2 or more was selected.
>> This  inconsitancy force me to check for type before processing can 
>> continue
>> For example, If I have a button that update selected records in the 
>> database call a function that do:
>>
>>
>> db(db.product.id.belongs(request.vars.id)).update(price=db.product.price+request.vars.inc_price)
>>
>>
>> However if only one line selected in the grid request.vars.id is a string 
>> and not a list, which cause an error
>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: SQLFORM.smartgrid selectable return string or list

2019-04-19 Thread Anthony
It should be request.vars.records, not request.vars.id.

Note, request.vars is processed by the core framework, not by 
SQLFORM.smartgrid, so it will be a single value if the browser sends only 
one "records" field in the form data and a list otherwise. The core code 
that populates request.vars has no way of knowing that a given field 
containing a single value is supposed to be a list -- that has to be 
handled elsewhere. SQLFORM.smartgrid itself handles this properly, but if 
you are going to intercept request.vars and run some custom code outside of 
the grid, then it is up to you to do the check and convert to a list.

Keep in mind, the "selectable" argument to the grid should be a function 
that takes the list of ids -- presumably you can move your code into that 
function, in which case, you won't have to worry about this, as the grid 
will handle the conversion to a list.

Anthony

On Friday, April 19, 2019 at 9:28:21 AM UTC-4, icodk wrote:
>
> SQLFORM.smartgrid selectable  return string if only one checkbox was 
> slected and returns a list if 2 or more was selected.
> This  inconsitancy force me to check for type before processing can 
> continue
> For example, If I have a button that update selected records in the 
> database call a function that do:
>
>
> db(db.product.id.belongs(request.vars.id)).update(price=db.product.price+request.vars.inc_price)
>
>
> However if only one line selected in the grid request.vars.id is a string and 
> not a list, which cause an error
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.