Here is what I came up with:

class HelpFilter(cherrypy.filters.basefilter.BaseFilter):
    def before_main(self):
        if cherrypy.request.path.endswith('help'):
            output = context_sensitive_help()
            template = output.pop("tg_template")
            if template.startswith("."):
                template = 'ccleads' + template
            format = 'html'
            content_type = 'text/html'
            mapping = None
            body = controllers._process_output(output, template, format, content_type, mapping)
            cherrypy.response.status = "200 OK"
            cherrypy.response.body = body
            cherrypy.request.execute_main = False

def installHelpFilter():
    cherrypy.root._cp_filters.append(HelpFilter())

if installHelpFilter not in cherrypy.server.on_start_server_list:
    cherrypy.server.on_start_server_list.append(installHelpFilter)

You just have to paste it into your controller.py and it "just works". Of course there are some elements which are hard-coded to my application until I create a module.

I'm not very intimate with TG internals so I'm not sure if this is the right way to do it (I'm calling and underscore "_" function). Maybe somebody will have a better idea on how to do it.

Also, I had some problems with Identity while I was trying to install my filter using this notation:

class Root(controllers.RootController):
    _cp_filters = [UputeFilter()]

Problem was that, apparently, identity is also installed as a filter. I guess that you remove identity from filters by using _cp_filters = [...] approach. If identity filter is not in the filter list before help filter (or at all) I cannot access identity variables, such as current user, in my templates.

Also, in my initial approach (before posting first mail) I played with:

cherrypy.request.executeMain = False

as CP docs state that it should be done. It just didn't work so I gave up. Now, I discovered that you should use:

cherrypy.request.execute_main = False



On 4/11/06, ajones <[EMAIL PROTECTED]> wrote:

You should be able to extend the base controller and add a help
function to that. I just tested it using a mixin which seems to work
pretty well. Here is what I did:

class Helper:
    @turbogears.expose()
    def help(self):
        return dict(path=cherrypy.request.path)

class Root(controllers.RootController, Helper):
...

At that point root automatically gets a help method, and to add it to
anything other objects you would just mixin that same class. I had some
success adding it as an attribute of a method too, but I am not too
sure if that is a good way to handle it. Anyways, here is what I did
for that:

class Root(controllers.RootController, Helper):
...
    @turbogears.expose()
    def test(self)
        return dict(test="This is a test.")

    test.help = Helper().help

With testing it seems like this works well, even with the test function
accepting parameters. My guess is that calls to test/help are processed
by any attributes of test before they are turned into parameters of the
test method call.

Anyways, hope that helps. It does not entirely avoid having to make
calls to context_sensitive_help(), but it does limit the whole thing to
mixins and assigning attributes to methods. I have absolutely zero
experience with cherrypy filters, so I may be reinventing the wheel
there ... guess I have reading to do.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to