response.view as a decorator

63 views
Skip to first unread message

samuel bonill

unread,
Aug 22, 2013, 2:52:33 PM8/22/13
to web...@googlegroups.com
hi all.

i'm trying implement response.view as a decorator, but not work my code:
for example i wanna do it:

@View('path/sub_path/view.html')
def index()
      return locals()

in modules i have :  decorator.py

from gluon.globals import Response
response = Response()

class Error(Exception):
    ''' Error if path != str''

class View(object):
      
    def __init__(self, path):
        if isinstance(path, str):
            self.path = path
        else:
            raise Error('{} no es una cadena'.format(path))
           
    def __call__(self, fn):
        def _call():
           ## fn.func_globals[response.view] = self.path
           respomse.view = self.path
           return fn()
        return _call

in default.py :

from decorador import View

@View('layout.html')
def exampler():
    '''para llamar una vista de cualquier directorio'''
    return locals()

but not found !!!.... why ?

not want to put on each controller response.view = 'example'.... ! help !!



Niphlod

unread,
Aug 22, 2013, 3:20:44 PM8/22/13
to web...@googlegroups.com
uhm. I'm totally ok for decorators that save lots of typing.
What's so different between
@View('whatever')
and
response.view = 'whatever'
?

Niphlod

unread,
Aug 22, 2013, 3:39:01 PM8/22/13
to web...@googlegroups.com
BTW, I'm a fan of functional.

from gluon import current
def outer(view):
    def wrap(f):
        def wrapped_f():
            current.response.view = view
            return f()
        return wrapped_f
    return wrap

@outer('path/to/view')
def index():
      return locals()

samuel bonill

unread,
Aug 22, 2013, 5:13:42 PM8/22/13
to web...@googlegroups.com
thanks... the error is in import gluon.globals :

from gluon.globals import Response
response = Response() # import error

now:

from gluon import current
response = current.response

class View(object):
       def __init__(self, path):
            ........
        def __call__(self, fn):
            def _call():
                 response.view = self.path
                ......
                 ...... etc....

now work....


2013/8/22 Niphlod <nip...@gmail.com>

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/ljp1IcabuSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Anthony

unread,
Aug 22, 2013, 9:00:15 PM8/22/13
to

from gluon import current
response = current.response

class View(object):
       def __init__(self, path):
            ........
        def __call__(self, fn):
            def _call():
                 response.view = self.path

No, don't do that. If you assign current.response to response at the top level in the module, it will not be updated on each request. Instead, in the _call() function, do current.response.view = self.path.

Anyway, I agree with Niphlod -- this seems like a completely gratuitous use of a decorator.

Anthony 

samuel bonill

unread,
Aug 23, 2013, 10:41:50 AM8/23/13
to web...@googlegroups.com
thanks Anthony....

@View('path/to/file')
def example():
     return locals()

implement this decorator is based on my personal taste....


2013/8/22 Anthony <abas...@gmail.com>

from gluon import current
response = current.response

class View(object):
       def __init__(self, path):
            ........
        def __call__(self, fn):
            def _call():
                 response.view = self.path

No, don't do that. If you assign current.response to response at the top level in the module, it will not be updated on each request. Instead, in the _call() function, do current.response.view = self.path.

Anyway, I agree with Niphod -- this seems like a completely gratuitous use of a decorator.

Anthony 

--
Reply all
Reply to author
Forward
0 new messages