I modified web2py_ajax.html (in trunk) so that it now takes a new
response.headers['web2py-component-content']
which can be 'prepend', 'append' or 'hide' and you can do
def index():
return dict(check=LOAD('default','test.load',ajax=True))
@auth.requires_login()
def test(): return "hello world"
def user():
if request.extension=='load' and not auth.user:
response.flash = 'hey! login please'
response.headers['web2py-component-content']='prepend'
return 'oops, you must login'
return dict(form=auth())
Is this acceptable?
On Aug 20, 3:36 pm, Jurgis Pralgauskis <[email protected]>
wrote:
> hello, by default, if some function needs auth, w2p gives back some
> text/html:
> When I work with components ajax way, I don't want my content parts to
> get mangled with it...
> 1) if I am not logged in, it would flood my tiny content area with big
> login page clone :)
> 2) in my case, I want my current content (of target div) to stay in
> place if I don't have permisions, (and new content should replace it
> only if accessible).
>
> so I made a small patch, You can test it
> hearhttps://web2py-gae-test.appspot.com/auth_for_web2py_components/defaul...
> its default new app with several changes in
> /controllers/default.py : user()
> /views/web2py_ajax.html : web2py_ajax_page()
>
> is it a good way? or how would be better?
>
> /controllers/default.py
>
> def user():
> if request.ajax:
> result = auth()
> if not auth.user:
> return "ERROR" +"<br> You should %s first <br>\n" %
> ( A('login', _target="blank", _href=auth.settings.login_url).xml())
> if isinstance(result, str):
> response.flash = result
> return "ERROR"
> return result # no dict(form=result)
> else:
> return dict(form=auth())
>
> /views/web2py_ajax.html
>
> 'success': function(text) {
> if (text.startsWith( "ERROR" )) {
> jQuery('#'+target).prepend( text.substr( "ERROR".length ) );
> }
> else {
> jQuery('#'+target).html(text);
> }