run controller function and ommit models?

51 views
Skip to first unread message

Mirek Zvolský

unread,
Oct 19, 2016, 2:52:08 AM10/19/16
to web2py-users
Hi,
I am trying implement fine-uploader for upload files (used in import from other software).

Fine-uploader generates lot of ajax calls with each chunk of each imported file.
These Ajax calls just need read and save the chunk (or join chunks with the last call).
They don't need any model.

I have no problem to have overhead with running of models.
However I am curious, if Web2py has the possibility skip models for some (Ajax) calls.
Or if other frameworks (Django?) give such possibility.

When I think about my use-case, it looks like it could be useful sometimes.

Nico de Groot

unread,
Oct 19, 2016, 5:26:18 AM10/19/16
to web2py-users
You could use conditional models. Just make an empty folder in /models with the name of a (new) controller. Put the functions that don't need any models inside that controller.

See http://www.web2py.com/books/default/search/29?search=Conditional+models

Nico de Groot

Mirek Zvolský

unread,
Oct 19, 2016, 7:12:16 AM10/19/16
to web2py-users
Great !!!
Thanks.

Mirek Zvolský

unread,
Oct 19, 2016, 7:34:40 AM10/19/16
to web2py-users
I think in the alphabetically first model I will use

if request.controller == '':
   
response.models_to_run = ...

黄祥

unread,
Oct 19, 2016, 10:03:14 AM10/19/16
to web2py-users
e.g.
models/db.py
if request.function == 'user' :
response.models_to_run = ['db_schema_0_auth.py', 'db_schema_1_test.py', 'menu.py']
elif 'order' in request.function :
response.models_to_run = ['db_schema_0_auth.py', 'db_schema_1_test.py', 'menu.py']
elif request.controller == 'settings' :
response.models_to_run = ['db_schema_0_auth.py', 'menu.py']
else:
response.models_to_run = ['.*']

best regards,
stifan

Anthony

unread,
Oct 19, 2016, 3:22:36 PM10/19/16
to web2py-users
On Wednesday, October 19, 2016 at 5:26:18 AM UTC-4, Nico de Groot wrote:
You could use conditional models. Just make an empty folder in /models with the name of a (new) controller. Put the functions that don't need any models inside that controller.

Making an empty model folder associated with a given controller is unnecessary, as it will have no effect. All files in the top level of the /models folder will still be executed, and by default (without changing response.models_to_run), any models in subfolders not associated with the current request will not be run anyway. So, no need for the empty folder.

Anthony

Anthony

unread,
Oct 19, 2016, 3:28:15 PM10/19/16
to web2py-users
On Wednesday, October 19, 2016 at 7:34:40 AM UTC-4, Mirek Zvolský wrote:
I think in the alphabetically first model I will use

if request.controller == '':
   
response.models_to_run = ...


Are you saying you will specifically use the value '' for request.controller? Your code should look like this:

if request.controller == 'my_upload_controller':
    response
.models_to_run = []

To prevent any model code from being executed, create a model file that comes first alphabetically, and include only the above two lines in it.

If all the Ajax calls go to a single function within a given controller, then the above condition could instead specify request.function.

Anthony
Reply all
Reply to author
Forward
0 new messages