On Mon, Apr 29, 2013 at 10:13 AM, David Marko <dma...@tiscali.cz> wrote: > Is there a way how to use/crrate a viewScope on conversationScope like > variable in web2py? In JAVA based frameworks (e.g. JSF, Spring Framework) > there are scoped environments available. In web2py we have requestScope, and > sessionScope (if I use this naming) but cant figure out how to create a > viewSope(or conversationScope) which is like session but bound to one > page(its instance.) only. So when user submits page without redirect the > viewScope variable is still available. This is very nice scenario e.g. how > to create nice wizards within a one page as viewScope holds position > 'securely' . And there are other use casesas well. (btw. applicationScope > would be very ncie as well but I know we can use cache here, but these scope > are more transparent). >
You can implement yourself using sessions. If I understood correctly, a quick implementation could look like this, in a model: scope_key = '/%s/%s/%s' % (request.application, request.controller, request.function) if session.page_scope is None: session.page_scope = Storage() if session.page_scope[scope_key] is None: session.page_scope[scope_key] = Storage() myscope = session.page_scope[scope_key] Then use myscope.var instead of session.var A more abstract way could be creating some helper classes, something like: from gluon.globals import current from gluon.storage import Storage SCOPE_ROOT = 'scopes' class Scope(dict): __slots__ = () def __init__(self, namespace): session = current.session if not session[SCOPE_ROOT]: session[SCOPE_ROOT] = Storage() if not session[SCOPE_ROOT][namespace]: session[SCOPE_ROOT][namespace] = Storage() self['_namespace'] = namespace @property def namespace(self): return self['_namespace'] def __getattr__(self, key): return current.session[SCOPE_ROOT][self.namespace][key] def __setattr__(self, key, value): current.session[SCOPE_ROOT][self.namespace][key] = value def __delattr__(self, key): try: del current.session[SCOPE_ROOT][self.namespace][key] except KeyError: pass class PageScope(Scope): __slots__ = () def __init__(self): request = current.request namespace = '/%s/%s/%s' % (request.application, request.controller, request.function) super(PageScope, self).__init__(namespace) save it in a module and then you can use it in controllers: from myscope_module import Scope, PageScope def index(): wiz = Scope('wizard_multi_page') wiz.var1 = 'var1' wiz.var2 = 'var2' wiz2 = PageScope() wiz2.var1 = 'var1' wiz2.var2 = 'var2' def index2(): wiz = Scope('wizard_multi_page') wiz.var1 = 'var1' wiz.var2 = 'var2' wiz2 = PageScope() wiz2.var1 = 'var3' wiz2.var2 = 'var4' Session data structure will be something like: Session items +---- scopes +---- wizard_multi_page | +--- var1 = 'var1' | +--- var2 = 'var2' +---- /app/default/index | +---- var1 = 'var1' | +---- var2 = 'var2' +---- /app/default/index2 +---- var1 = 'var3' +---- var2 = 'var4' I think it is closed to what you want, at least the PageScope, isn't it? Note: I did a quick test, only with session in file, and seems to work. Ricardo -- --- 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/groups/opt_out.