I'm trying to implement the viewScope variables for web2py controller 
methods. Idea behind is to mimic a behaviour of JAVA frameworks like JSF or 
Spring that use the concept of viewScope variables. Such variables live 
during the all posts to same controller action / to the same form. This is 
very handy when one is doing some heavy form actions or wizard actions etc. 
that we do all the time in corporate development. Using the viewScope 
variable one can avoid using a hidden fields (which using is rather a 
security issue as user can easily change the value in browser using dev 
tools.)  

Now my idea expressed in code snippet below is to generate UUID value 
during the get request (the first request to controller action) and store 
it into hidden form field. This hidden UUID is used as a key for cache.ram 
value that expresses the viewScope variable. I use Storage() object as a 
viewScope object as it is very easy to use. Now everything is working as 
expected and during the all posts to same action (all posts of  SQLFORM) 
the hidden key is available and I can access the the same cach.ram 
Storage() object, read and update values and everything is working fine. 
What is not fine is fact that cache expire_time is timeout counted from the 
first cache object creation time and expires in defined time. What I need 
 here is to have the expire time but prolonged each time I access the cache 
so the expire time reflects users inactivity. Is there a way how to 
accomplish this idea using the cache.ram?  Also is there anyone who tried 
to solve this already? I would appreciate your ideas / comments.

BTW: Despite the fact that HTTP is not statefull .. the statefull framework 
features are very important during corporate development (during forking 
with large form that requires many post submits until saved finaly). I 
would really appreciate having some statefull ideas implemented in web2py 
core. But its completely separate topic.

(Code below is just a proof of concept more than some final nice code. :-) 
 )

class ViewState:

    @staticmethod
    def getStateObject(state_id=None):
        if state_id==None:
            state_id=ViewState.getStateId()
        from gluon.storage import Storage
        return cache.ram('form_'+state_id, lambda:Storage(), time_expire=10)

    @staticmethod
    def getStateId(state_field='state_id'):
        import uuid
        return (request.post_vars.state_id or str(uuid.uuid4()))

def index():
    state_id=ViewState.getStateId()
    sc=ViewState.getStateObject(state_id)

    form=SQLFORM.factory(
        Field('jmeno','string',requires=IS_NOT_EMPTY()),
        Field('prijmeni','string'),
        hidden=dict(state_id=state_id)
    )
    ....





-- 

--- 
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.


Reply via email to