[web2py] SQLFORM widgets and drop-down filtering

2018-04-08 Thread Dave S
I have a table that follows the following structure:

db.define_table('collection', 
Field('IssueYr', 'integer',
 requires = [IS_NOT_EMPTY(), IS_INT_IN_RANGE(1965,2018, 
"Not 
a valid year of issue")],
 widget = lambda f, v: SQLFORM.widgets.integer.widget(f, 
v, _autofocus=True)),
Field('State', 'string', default=None, represent=lambda 
state: state or '',
  requires=IS_EMPTY_OR(IS_IN_SET(State_list))),
Field('Other', 'string', default=None, represent=lambda 
Other: Other or '', 
  requires=IS_EMPTY_OR(IS_IN_SET(other_list)))
)


And I use use SQLFORM() to fill out the fields..  "State" is defined for 
certain issueYrs, for other years a javascript trigger skips to the "Other" 
field.  This works well, and the State_list dropdown works as expected.  
But it turns out that the State_list can be subdivided with 4 or 5 
corresponding to a given year.  I do additional validation on the basis of 
this (to catch typo'd years).  It would be nice to take advantage of that 
subdivision in the dropdown, only offering the, um, slice of states for the 
year that has been entered.  For the validation, I have a dictionary where 
the keys are years and the values are the matching states.  Is there a 
widget that I can pass the value-list to for use in the dropdown, using the 
javascript trigger to handle the key lookup?

I looked at the autocomplete widget, and it's tied to a database field, so 
that's not very convenient for this use-case.  I'm not totally against 
having essentially static tables when appropriate, but it seems like 
overkill for this.

/dps


-- 
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] Using variables in db.table.field expressions

2018-04-08 Thread xelomac
Hi all,

in a controller function I want to check if a field is set as writable in 
the model before updating it. "if db[table][column].writable == False" did 
not work. I have the following:

def upd_field_value():
id,table,column = request.post_vars.id.split('.')
value = request.post_vars.value
my_field = db().db[table][column]
if LOOKING_FOR_THE_CORRECT_SYNTAX_HERE_USING_THE 
VARS_table_AND_column.writable == False:
do something
db(db[table].id == id).update(**{column:value})
return value

Thanks for any help

-- 
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: Using variables in db.table.field expressions

2018-04-08 Thread 黄祥
perhaps you can print the value of fields writable then compare it
*e.g.*
field_writable = db.table.field.writable

*ref:*
http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Run-time-field-and-table-modification

best regards,
stifan

-- 
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] problem export virtual field to CSV using grid

2018-04-08 Thread Vic Ding
Hi all,
I am having issue exporting virtual fields to CSV using grid. It displays 
fine though. 

I defined field in this way

Field.Virtual('cost_value', lambda row: row.inventory.cost_price_avg * 
row.inventory.qty, label=T('Stock value')),


When I export it to CSV in grid I got the following error
 '"inventory"."cost_value"'

The grid was a simple one

fields = [db.inventory.qty, db.inventory.cost_price_avg,db.inventory.cost_value]
grid = SQLFORM.grid(db.inventory, maxtextlength=64, paginate=20, create=False,
details=False, editable=editable, deletable=deletable,
fields=fields,
links=links,
)


Any clue?

Thanks!

Vic

-- 
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: Using variables in db.table.field expressions

2018-04-08 Thread Anthony
On Sunday, April 8, 2018 at 4:27:59 AM UTC-4, xelomac wrote:
>
> Hi all,
>
> in a controller function I want to check if a field is set as writable in 
> the model before updating it. "if db[table][column].writable == False" did 
> not work.
>

What do you mean by "did not work"? That is the correct way to check the 
writable attribute. We need more info.

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: Using variables in db.table.field expressions

2018-04-08 Thread xelomac
Sorry - I cannot comprehend what I have done wrong before. 
"if db[table][column].writable == False:" works now as expected.

On Sunday, April 8, 2018 at 5:44:55 PM UTC+2, Anthony wrote:
>
> On Sunday, April 8, 2018 at 4:27:59 AM UTC-4, xelomac wrote:
>>
>> Hi all,
>>
>> in a controller function I want to check if a field is set as writable in 
>> the model before updating it. "if db[table][column].writable == False" did 
>> not work.
>>
>
> What do you mean by "did not work"? That is the correct way to check the 
> writable attribute. We need more info.
>
> 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] How to retrieve info about sessions when they are stored in Redis?

2018-04-08 Thread Lisandro
Recently I moved the sessions from the database to Redis, and I'm 
wondering: is there a way to retrieve info about sessions when they are 
stored in Redis? 
For example, when sessions are stored in the database, you have the option 
to use SQL to do some stuff like counting or deleting sessions. How to do 
it when sessions are stored in Redis?

I also use Redis to cache HTML responses from web2py and any other stuff 
that can be cached (lists, dictionaries, etc). In order to be able to list 
the keys cached by one specific web2py application, I have written this 
custom function to retrieve those keys. 
I've read that it's not a good idea to use cache.redis.r_server.keys() 
method on production 
,
 
so I written this code based on what I saw in the clear() method at 
gluon.contrib.redis_cache 

:

def get_cache_keys(application, prefix=''):
import re
result = []
regex = ':%s*' % prefix
prefix = 'w2p:%s' % application
cache_set = 'w2p:%s:___cache_set' % application
r = re.compile(regex)
buckets = current.cache.redis.r_server.smembers(cache_set)  # get all 
buckets
if buckets:  # get all keys in buckets
keys = current.cache.redis.r_server.sunion(buckets)
else:
return result
for a in keys:
if r.match(str(a).replace(prefix, '', 1)):
result.append(a)
return result


With that code, I'm able to list all the keys cached by a web2py 
application.
As I'm also using Redis to store sessions, I want to be able to list all 
the session keys.
I've tried a similar code to the one showed above, replacing this:

prefix = 'w2p:sess:%s' % application
cache_set = 'w2p:sess:%s:id_idx' % application

But that doesn't work. Is it possible to achieve what I want? Any 
suggestion will be much appreciated.

Regards,
Lisandro.

-- 
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.


Re: [web2py] Re: Error loging in after authentication changes

2018-04-08 Thread Maurice Waka
I moved the iteration to the controller side and it worked.
Regards

On Sat, Apr 7, 2018 at 5:36 PM, Anthony  wrote:

> Also, there is not point to either of the for loops at the top of the view
> -- each loop is simply assigning a new value to the same variable, so the
> final value of the variable is just that of the final element in the
> iteration. You might as well just make that final assignment without
> bothering with iteration over all the previous items.
>
> Anthony
>
>
> On Saturday, April 7, 2018 at 10:34:28 AM UTC-4, Anthony wrote:
>>
>> {{for q in question:}}
>>> {{q=XML(q.quest.replace('\n','').replace('(','{').replace(')','}'),
>>> sanitize=True)}}
>>> {{pass}}
>>> ...
>>> $(function () {
>>>...
>>>
>> sendMessage('{{=q}}');
>>>
>>
>> When "question" is empty (i.e., there are no answers from the current
>> user), the variable "q" will not be defined in the for loop, and therefore
>> will result in an exception later where you have {{=q}}.
>>
>> This doesn't have anything to do with @auth.has_membership('managers'),
>> as the error is not even occurring in the function with that decorator. If
>> you are expecting "question" to always include some records, you have to
>> figure out why you aren't getting any in this case. Otherwise, adjust your
>> code to account for the possibility of an empty "question".
>>
>> 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 a topic in the
> Google Groups "web2py-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/web2py/YQhEantsews/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> web2py+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.