Removing all tables filter_out. before_inset and before_update works?

48 views
Skip to first unread message

Diogo Munaro

unread,
Apr 30, 2014, 10:03:48 AM4/30/14
to web...@googlegroups.com
Hey guys! Nowadays I'm using a some filter_outs but they got update and insert erros on record versioning.

I'm solving update issue using before_update like this:

    def remove_filter(s,field):
        field_obj = s.query.db[str(s.query).split('.')[0][1:]][field]
        field_obj.filter_out = ''

    for t in db.tables:
        db[t]._before_update.insert(0, lambda s, f: not [remove_filter(s, field) for field in f])

s.query.db is the returned db;

str(s.query).split('.')[0][1:] is an ugly way to get table name

field is the field_name.

With update I got an ugly solution, but I got a solution. With insert I can't do it because before_insert don't have a Query object and I can't get table name

Is there a better way to solve it and remove filter_outs before inserts and updates?

Diogo Munaro

unread,
May 6, 2014, 7:25:00 PM5/6/14
to web...@googlegroups.com
No answer?

Anthony

unread,
May 7, 2014, 2:22:38 AM5/7/14
to web...@googlegroups.com
You're already looping over the tables when you define your functions, so just use the table value from the loop in your lambda function definitions. Not tested, but maybe something like:

def remove_filters(table):
   
for field in table:
        field
.filter_out = None

for t in db.tables:
    db
[t]._before_insert.insert(0, lambda f, table=db[t]: remove_filters(table))
    db
[t]._before_update.insert(0, lambda s, f, table=db[t]: remove_filters(table))

Instead, though, it might be worth figuring out why you're getting the errors and seeing if that can be fixed.

Anthony

Diogo Munaro

unread,
May 7, 2014, 9:31:29 AM5/7/14
to web...@googlegroups.com
Hi Anthony, thx for your help, I was using table inside lambda then table value was always the last table.

Good point! I'll use as you said


--
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/hml1Oyi3tpg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
May 7, 2014, 9:52:41 AM5/7/14
to web...@googlegroups.com
On Wednesday, May 7, 2014 9:31:29 AM UTC-4, Diogo Munaro wrote:
Hi Anthony, thx for your help, I was using table inside lambda then table value was always the last table.

If you are iterating over some items to create lambdas, you must pass each item to the lambda as an argument with a default value -- otherwise, all lambdas get the last value of the iteration (because the items are defined in the scope outside of the lambda). So, instead of:

for t in tables:
    mycallbacks
.append(lambda: do_something_with(t))

it should be:

for t in tables:
    mycallbacks
.append(lambda table=t: do_something_with(table))

Anthony
Reply all
Reply to author
Forward
0 new messages