On Sun, 2011-01-09 at 23:12 -0500, Chris McDonough wrote:
> On Sun, 2011-01-09 at 16:25 -0800, lost_rat wrote:
> > Branching from within view_handler
> > 
> > from pyramid.response import Response
> > from pyramid.httpexceptions import HTTPRedirection
> > from pyramid.httpexceptions import HTTPFound
> > class MyViewHandler(object):
> >     __autoexpose__ = None
> >     def __init__(self, request):
> >         self.request = request
> >     @action(renderer='/index.mako')
> >     def index(self):
> >         if test():
> >             return {}
> >         elif test2():
> >             return Response('good stuff')
> >         elif test3():
> >             return HTTPRedirection(location='http://localhost/index3')
> >         else:
> >             return HTTPFound(location='http://localhost/index4')
> > 
> > 
> > In Pylons I could conditionally branch based on GET versus POST, where
> > the URL would be identical. I would transform the data from a POST
> > into a modified URL and redirect. I can't seem to accomplish the same
> > thing here within Pyramid.
> > 
> > The Response works, but not HTTPRedirection or HTTPFound.
> 
> You have a number of problems here.
> 
> First, HTTPRedirection is not a response object that will behave in any
> sensible way; it's a base class for both HTTPFound and HTTPSeeOther, or
> HTTPMovedPermanently which are the "right" classes to use in user code.
> 
> You say that HTTPFound "doesn't work", but I suspect it does, and your
> code is not reaching it somehow.
> 
> All that said, rather than branch within a single action method, you can
> use view predicates to break the branches into separate methods.  If
> test(), test2() and test3() are notational proxies for checking the
> request method above (say, test() means request_method=='GET', test2()
> is request_method=='POST' and test3() is request_method=='HEAD', the
> following code would be more Pyramidic:
> 
> from pyramid.response import Response
> from pyramid.httpexceptions import HTTPRedirection
> from pyramid.httpexceptions import HTTPFound
> 
> class MyViewHandler(object):
> 
>     __autoexpose__ = None
> 
>     def __init__(self, request):
>         self.request = request
> 
>     @action(renderer='/index.mako', request_method='GET')
>     def index_GET(self):
>         return {}
> 
>     @action(request_method='POST')
>     def index_POST(self):
>         return Response('good stuff')
> 
>     @action(request_method='HEAD')
>     def index_HEAD(self):
>         return HTTPRedirection(location='http://localhost/index3')
> 
>     @action()
>     def index_OTHER(self):
>         return HTTPFound(location='http://localhost/index4')
> 
> There are predicates other than "request_method" usable in view
> configuration decorators like view_config and action (see
> http://docs.pylonshq.com/pyramid/dev/narr/viewconfig.html#predicate-arguments).
>   You can even create your own predicates by using "custom_predicates".
> 
> Of course you can still do it the single-view-with-branching way, you
> just need to get your return values correct.
> 
> See also http://plope.com/weird_pyramid_urldispatch

Forgot to specify a "name" for each action so they'll all be accessible
as '/foo/index' when the pattern is '/foo/{action}':

from pyramid.response import Response
from pyramid.httpexceptions import HTTPRedirection
from pyramid.httpexceptions import HTTPFound

class MyViewHandler(object):

    __autoexpose__ = None

    def __init__(self, request):
        self.request = request

    @action(name='index', renderer='/index.mako', request_method='GET')
    def index_GET(self):
        return {}

    @action(name='index', request_method='POST')
    def index_POST(self):
        return Response('good stuff')

    @action(name='index', request_method='HEAD')
    def index_HEAD(self):
        return HTTPRedirection(location='http://localhost/index3')

    @action(name='index')
    def index_OTHER(self):
        return HTTPFound(location='http://localhost/index4')

> 
> - C
> 
> 
> 


-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss?hl=en.

Reply via email to