web2py defines

response._caller = lambda f: f()

this function is in charge of calling the action. You can redefine it
in the controller or models

def mycaller(f):
      # before calling action
      r=f()
      # after calling action but before calling view
      if isinstance(r,dict):
          r=response.render(r)
      # after calling view before returning response
      try:
          return r
      finally:
          # after everything
          pass

response._caller=mycaller
On May 24, 12:58 pm, Yarko Tymciurak <[email protected]>
wrote:
> On May 24, 8:22 am, Tex <[email protected]> wrote:
>
>
>
> > Hi,
>
> > sorry for my poor english...
>
> > I'm wondering how can I put controller code filters like rails
> > (before_filter / after_filter / around_filter).
>
> > Searching into user group posts I see many responses on activerecord
> > callbacks (before_save / after_save) wrongly referred to question on
> > before/after/around filter (controller filter).
>
> > Now I know that in web2py I can put code into model /db.py) and that
> > code is execute before globals and before controller code, my
> > questions are:
>
> > 1. in rails I write before_filter only for given controllers and given
> > actions, seems that in web2py if I write the code in db.py it is
> > executed for all controllers and actions, is there a way to limit that
> > code execution to given controllers and actions ? (I think that put
> > too much code into db.py may slow the excution of requests, it'isnt ?)
>
> > 2. how can I write code execution that emulates after_filter or
> > around_filter ala Rails ? (example: I want to track remote ip for all
> > my controller/actions into database...),
>
> I think this may be more a python question than a web2py question:
> With python, you want to read up on decorators.    For example:
>
> @auth.requires_login()
> def  mycontroller:
> .....
>
> can be either  before, around, or after (although you can certainly do
> before / after in other, simpler ways).
>
> For example:
>
> @before_decorator()
> @around_decorator()
> def myfunction:
> ....
>
> For background, you could start 
> with:http://docs.python.org/reference/compound_stmts.html#functionhttp://www.artima.com/weblogs/viewpost.jsp?thread=240808http://www.artima.com/weblogs/viewpost.jsp?thread=240845
>
>
>
> > Many thanks in advance.
>
> Regards,
> - Yarko
>
>
>
> > Tex

Reply via email to