You can also just import current in the module and then refer to it directly within your function (i.e., no need to pass current as an argument to the function).
Anthony On Sunday, December 14, 2014 11:07:48 PM UTC-5, Mark Graves wrote: > > Right, I knew it looked too easy. > > Thanks, Anthony! > > Would it be viable to say in models (or controllers): > > from gluon import current > current.db = db > > Then in modules: > > def my_function(current): > db = current.db > session = current.session > request = current.request > response = current.response > > Then perhaps futher mimic locals with a decorator by returning them to the > function and/or changing the signature? (Or would that run into the same > threading problem? > > Trying to avoid repeating myself... > > > > > > On Sunday, December 14, 2014 8:32:55 PM UTC-6, Anthony wrote: >> >> No, the book warns to avoid that approach: >> http://www.web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules >> >> You cannot assign the thread local object to a top-level variable in the >> module, as it will only be assigned once upon first import. >> >> Anthony >> >> On Sunday, December 14, 2014 7:32:29 PM UTC-5, Mark Graves wrote: >>> >>> Hey everyone, >>> >>> I could use a sanity check here from the community. >>> >>> In a controller I get a record, then, I want to pass do some database >>> calls from a module. >>> >>> It seems that the least code I could write would be in models: >>> >>> from gluon import current >>> current.db = db >>> >>> Then in the module at the top >>> >>> from gluon import current >>> db = current.db >>> >>> The method I was using was passing the objects in directly to the >>> function defined in the module, but that was overly repetitive. No >>> classes, just functions. >>> >>> Can anyone see where I would run into problems? Is this thread safe? >>> >>> Would it be better to instantiate a class with the handler similar to >>> what I've seen elsewhere regarding model-less apps? >>> >>> Thanks in advance! >>> >>> -Mark >>> >>> >>> -- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.

