> The simplest approach is to put shared functionality into modules. Of > course, if you don't know in what context a given bit of code will run, you > can easily check whether particular tables have been defined and then call > the table defining code if necessary. > > Maybe it would help to show some actual code from two apps that you would > like to share. >
Yeah, you're right, it is getting a bit abstract. I made a simple example. This is with only one app (with two controllers) but it seems to illustrate the same kind of problem I'm having with multiple apps, so maybe we can start with this. I created a sample app called test_env. Everything in db.py is left as the default, except I uncommented request.requires_https(). I then made two controller files: # db_worker.py def call(): return service() @service.json def work_service(a, b=2): return a, b, str(db) def work_plain(): return str(db) # boss.py import gluon.shell def order_service(): othermod = gluon.shell.exec_environment( 'applications/test_env/controllers/db_worker.py') return "Hello", othermod.work(a=1, b=2) def order_plain(): othermod = gluon.shell.exec_environment( 'applications/test_env/controllers/db_worker.py') return "Hello", othermod.work_plain() I just used "print the db object" as a dummy "do something with the db" usage. If I visit /test_env/db_worker/call/json/work_service?a=1&b=2, I get the right result. What I want is to be able to visit /test_env/boss/order_service and get the exact same result. Right now, I get "name 'service' is not defined" (because db.py is not run, and "service" is defined there). Likewise, work_plain works, but order_plain gives the same error. If I comment out work_service (to bypass the "name service not found" error) and try order_plain, I now get "name 'db' is not defined" (because "db" is also defined in db.py, which is still not run). Now, as near as I can tell, everything I've done above in db_worker.py is straight out of the book as basic web2py stuff. But what you seem to be saying is that, if I want to make "order" work, I have to significantly change the way I've written db.py and worker.py. Namely, I have to abandon the idea of the normal execution framework whereby model files are automatically executed and dump their contents into the namespace of controllers, and instead I have to factor out the db-creation code into a separate module, and then, instead of having it automatically executed and provided to the db_worker controller, I have to manually import and run that db-creation code from within db_worker.py. Is that correct? If so, I understand what you're saying, but I guess it seems a bit perverse. Web2py has all this nice magic for automatically deciding which model files to run, running them, and automatically providing the names they define to the controller files. It's great and makes things very convenient and reduces boilerplate. But that all has to go out the window for one controller to access another (even within the same app)? Is there no way to say "do everything just as you would if a request came in for it -- that is, run the model files and the controller file -- but just don't actually create a request or call any of the controller functions yet, just return me an object representing the controller file"? -- 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.