Thanks your relying.
Both of your solution works. I would like to share what I was learned.

I got a little try. That would be very useful for hierarchical web
design. Without that I have to write exposed function like this:

@expose()
def foo(self, site, ...)
    # get site... do some check..

@expose()
def bar(self, site, ...)
    # get site... do some check..

@expose()
def add_category(self, site, section, category, ...)
    # get site... do some check..
    # get section ... do some check

@expose()
def del_category(self, site, section, category...)
    # get site... do some check..
    # get section ... do some check

As you see, everything as a parent level should be parameters. And you
have to write housekeeping code, such as passing parent parameters.
When your function goes into a deep level of your site, that would be
a nightmare. Now I found a better solution.

    @tg.expose()
    def default(self, site_name, action_name, *args, **kwargs):
        site = model.Site.get_by(site_name=site_name)
        assert site is not None, _(u'Site %s does not exist') %
site_name
        cherrypy.request.site = site

        action = getattr(self, action_name, None)
        if action is not None:
            assert getattr(action, 'exposed', False) == True, _
(u'Access deny')
            return action(*args, **kwargs)
        raise cherrypy.HTTPError(404, _(u'No such action %s') %
action_name)

Now you can access the site like this:

http://examplesite.com/foo/book_list/TurboGears

The default method will do the housekeeping work for you. It get the
site and assign to cherrypy.request, so you can access it in your
method later. But however, that might be dangerous to allow user call
any method of controller. For example, you have a method like this

    def _do_something_evil(self, *args, **kwargs):
        return 'you will go to hell'

And then you can do some evil thing by visit the url

http://examplesite.com/foo/_do_something_evil/

So that's better to limit functions default can called. My example
code is just a simple implement. It may not works if there is
something needed to be called in sub-controller. But I think it should
be not too difficult to solve. Hope this would be helpful. :P

Finally...although it works, I am wondering are there any potential
issues?

On 2月13日, 上午1時02分, Remi Jolin - SysGroup <[email protected]>
wrote:
> le 12.02.2009 15:30 Victor a écrit:
>
> > Hi,
>
> >     @tg.expose()
> >     def default(self, name, action, *args, **kwargs):
> >         return "hello %s %s %s %s" % (name, action, args, kwargs)
>
> > It works. But here comes the problem is. How can I redirect the
> > request without change of url to my controller? I know that I can
> > redirect with redirect function like this:
>
> >     @tg.expose()
> >     def default(self, name, action, *args, **kwargs):
> >         redirect(tg.url('/site', dict(name=name, action=action,
> > **kwargs)))
>
> That's a good start...
>
> @tg.expose()
> def default(self, name, action, *args, **kwargs):
> # check if action is a method of your controller
> action_method = getattr(self, action, None)
> if action_method:
> return action_method(name, *args, **kwargs)
> ...
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to