>
> In your example above, it's one model defining tables from another model.
> But in my case, what I have is a controller function that needs access to
> the db, which is defined in the model file. But I found the hard way (and
> later also from an old thread:
> https://groups.google.com/forum/#!topic/web2py-developers/5TPI9oYOZHM )
> that exec_environment does not load models. So this puts me kind of back
> to where I was.
>
> The simplest solution I found was to put, in the controller file, code
> that looks like this:
>
> from applications.otherapp.modules.db_definer
> db = db_definer.define_db()
>
> Then every time my controller is run, it will redefine the DB, using the
> code set up in the other app's module to do this in a consistent way (along
> the lines you suggested).
>
> What I don't like about this is that it means the DB-setup code is run
> multiple times on a "normal" request. That is, if I have code to set up
> the DB in my model file, and I visit my db-accessing controller, it runs
> the code in the model file to set up the DB, then runs the code again when
> it runs the controller file. I imagine this is not a huge penalty but it
> is annoying.
>
So, then don't do it that way. You have two options. First, if you have a
controller that doesn't need any of the model definitions from your
application's model files, then you should probably re-architect your
application so only the models that are needed get defined when they are
needed (either using the conditional models functionality or by defining
all models in modules that are imported where needed).
The other option is simply to avoid re-running the DB setup code and
instead use the already defined db object, passing it to the module code
from the other app:
app1/models/db.py:
db = DAL(...)
app2/modules/db_definer.py:
def define_tables(db):
db.define_table('my_app2_table', ...)
app1/controllers/default.py:
from app2.modules.db_definer import define_tables
def myfunc():
define_tables(db)
...
The above assumes both apps are using the same database, but if they are
not, then there is no "re-running the DB setup code" anyway, as you would
be dealing with two different databases, which would each need their own
setup code.
> I do have a couple questions about this: First, am I right that the
> performance impact of re-running the DAL object creation is negligible? Is
> it something I should be concerned about?
>
The impact should be minimal, but see above for alternatives.
> Second, is there anything wrong with using request.requires_https() in
> the top-level code of a model file?
>
That's typically where it would be -- usually one of the first lines in the
first model file to avoid unnecessary processing (as it results in a
redirect). This is assuming you want the whole app over HTTPS.
The problem is that I don't want these internal cross-app calls to be
> considered "requests"; I just want them to be calls to controller
> functions, so anything like requires_https should be ignored (or just
> proxied to the "real" request which is making this sub-call to another app).
>
It should only take effect for HTTP requests, not executions via the shell,
scheduler, or cron.
> The upshot of all this appears to be that there is no real way to get
> web2py to give me access to the model/controller combination without an
> HTTP request. In web2py the models and controllers are tightly coupled to
> the HTTP request. I find this somewhat irritating, as to my mind in an MVC
> framework the models and controllers shouldn't be so closely tied to the
> transport mechanism. That is, I should be able to say "use this model and
> this controller and give me the data", without having to involve HTTP at
> all. It seems that, in web2py this coupling is encouraged because
> controller functions directly access objects like request and response to
> get their arguments, rather than having a separate "routing" mechanism that
> maps HTTP request parameters to Python functions. (This is what the
> "Service" function does, but it requires a level of indirection with a
> "call" entry point.)
>
>
In essence, the way I think of it, when a request comes in, it is routed to
> a controller function. That controller then runs along with its associated
> models. and the controller function is called. It returns some data, which
> is passed to a view. What I would like is the ability to do just the
> middle part of that: take a controller, load its models, and run a
> function. No HTTP. No request. No response. No network. No view. No
> nothing. Just the controller and the model. For now I have a way to do
> it, but it involves duplicating the model-controller linkage that web2py
> already does (by "manually" recreating the DAL object from within the
> controller).
>
If you need to share code across applications, I suggest you put that code
into modules and then import in whatever applications need the
functionality. Of course, you can also make internal HTTP requests from one
application to another.
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 [email protected].
For more options, visit https://groups.google.com/d/optout.