Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
On 2009-12-7 22:21, Mike Orr wrote: On Mon, Dec 7, 2009 at 9:58 AM, Jonathan Vanascojonat...@findmeon.com wrote: mike- on the subject... does Pylons have a subrequest facility ? Some platforms , like mod_perl , offer it: http://perl.apache.org/docs/2.0/api/Apache2/SubRequest.html internal_redirect Redirect the current request to some other uri internally No. return self.othermethod() is the equivalent. I'm quite sure Paste has some API that allows you to do subrequests from Pylons. You could look at how middleware such as URLMapper or Deliverance handle that. Wichert. -- Wichert Akkerman wich...@wiggy.net It is simple to make things. http://www.wiggy.net/ It is hard to make things simple. -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
First, thanks all. Subrequests in Apache are weird. IIRC, they appear to be a new request - but something in mod_perl's request context object will note that it is a subrequest, and provide a facility to access the 'top level' context object information. this is in line with what Shailesh stated. Mike- you bring up return self.othermethod() as an alternative. do you think this would work : return OtherController().othermethod() Wichert- Thanks! -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
On Tue, Dec 8, 2009 at 9:04 AM, Jonathan Vanasco jonat...@findmeon.com wrote: First, thanks all. Subrequests in Apache are weird. IIRC, they appear to be a new request - but something in mod_perl's request context object will note that it is a subrequest, and provide a facility to access the 'top level' context object information. this is in line with what Shailesh stated. Mike- you bring up return self.othermethod() as an alternative. do you think this would work : return OtherController().othermethod() I'd think so because that's essentially what Pylons does. But I vaguely remember that somebody might have had problems with it a year or two ago. I'd say try it and see if it does what you expect. You'd have to supply any arguments the method expects, of course. Normally Pylons does this by introspecting the argument names and supplying them from the routing variables. Wichert Akkerman wrote: I'm quite sure Paste has some API that allows you to do subrequests from Pylons. Ben might know a way, but I don't. Although some ideas are below. You could look at how middleware such as URLMapper or Deliverance handle that. Middleware are in a different situation. They have the application (the next middleware) and call it in the WSGI manner. So they can call it on their own URLs instead of the original URL. But that brings up something that occurred to me this morning. If you have the PylonsApp instance, you can call it as a WSGI application too.But I don't know where you'd get the pylonsapp from because I don't see it in 'config' or 'request.environ'. You could reinstantiate it by calling make_app() with full_stack=False, and then call it. I'm not sure if the Pylons globals would be overwritten, but as long as you don't use them for anything else after calling the app, it wouldn't matter. So the action would look something like this: # Untested def subrequesting_action(self, environ, start_response): sub_environ = environ.copy() sub_environ[PATH_INFO] = /other_url sub_environ[REQUEST_METHOD] = GET sub_environ[QUERY_STRING] = sub_environ[wsgi.input] = StringIO(, r) return pylonsapp(sub_environ, start_response) Or using WebOb: # Untested def subrequesting_action(self): req = webob.Request.blank(/other_url) return req.get_response(pylonsapp) The latter would create a new 'environ' and 'start_response' rather than using the one supplied by Pylons. I think that's OK but I'm not 100% sure. There is a 'forward' function in pylons.controllers.util, but it seems more geared toward calling external WSGI applications, and you would have to modify request.environ to set the URL, so I'm not sure how practical it would be in this case. -- Mike Orr sluggos...@gmail.com -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Adding keys to request.params [was: Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?]
Shailesh Kochhar wrote: Mike Orr wrote: On Mon, Dec 7, 2009 at 9:58 AM, Jonathan Vanasco jonat...@findmeon.com wrote: mike- on the subject... does Pylons have a subrequest facility ? Some platforms , like mod_perl , offer it: http://perl.apache.org/docs/2.0/api/Apache2/SubRequest.html internal_redirect Redirect the current request to some other uri internally No. return self.othermethod() is the equivalent. Since the pylons Request and Response objects are thread locals, self.othermethod() isn't an exact equivalent of a sub-request. The application will still reference the existing Request object. If the request parameters need to be changed, you'll need to do that before calling self.othermethod(). I'm not sure how StackedObjectProxy works but perhaps a new Request could be pushed on-top of the current one? I tried updating request.params in one of my apps without much success. Turns out that request.params is dynamically generated from request.GET and request.POST. If you're trying to update request.params before redirecting to another method, you'll need to update one of the two. Best, - Shailesh -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
mike- on the subject... does Pylons have a subrequest facility ? Some platforms , like mod_perl , offer it: http://perl.apache.org/docs/2.0/api/Apache2/SubRequest.html internal_redirect Redirect the current request to some other uri internally $r-internal_redirect($new_uri); * obj: $r ( Apache2::RequestRec object ) The current request * arg1: $new_uri ( string ) The URI to replace the current request with * ret: no return value * since: 2.0.00 In case that you want some other request to be served as the top-level request instead of what the client requested directly, call this method from a handler, and then immediately return Apache2::Const::OK. The client will be unaware the a different request was served to her behind the scenes. -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
On Mon, Dec 7, 2009 at 9:58 AM, Jonathan Vanasco jonat...@findmeon.com wrote: mike- on the subject... does Pylons have a subrequest facility ? Some platforms , like mod_perl , offer it: http://perl.apache.org/docs/2.0/api/Apache2/SubRequest.html internal_redirect Redirect the current request to some other uri internally No. return self.othermethod() is the equivalent. However, the need for subrequests is diminished because of the flexible routing. Any route can have a match function that chooses the action based on some arbitrary aspect of the request. The error middleware does subrequests to produce the error page, but that's not the same thing. -- Mike Orr sluggos...@gmail.com -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
Mike Orr wrote: On Mon, Dec 7, 2009 at 9:58 AM, Jonathan Vanasco jonat...@findmeon.com wrote: mike- on the subject... does Pylons have a subrequest facility ? Some platforms , like mod_perl , offer it: http://perl.apache.org/docs/2.0/api/Apache2/SubRequest.html internal_redirect Redirect the current request to some other uri internally No. return self.othermethod() is the equivalent. Since the pylons Request and Response objects are thread locals, self.othermethod() isn't an exact equivalent of a sub-request. The application will still reference the existing Request object. If the request parameters need to be changed, you'll need to do that before calling self.othermethod(). I'm not sure how StackedObjectProxy works but perhaps a new Request could be pushed on-top of the current one? -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
the request cycle is not getting confused. Ha! O.k. I think it's pretty clear who is confused. :) Graham, your 'spurious_example()' bit really helped the lights go on. Of course it's obvious to me now (return self.foo() instead of call self.foo()) but sometimes you need to 'talk' it out to see the errors of your ways. Appreciate the discussion, almost feel like there could be a 'Friday Foibles' column (*ing yrs truly!). Hope people don't mind the blather but I really enjoy and learn and am indebted to 'the list'. Fwiw, this is a login process, but the more general pattern at play here, as I see it, is foo() having to get a foreign key for a primary table in bar(), and having to (o.k., I'll say it) redirect from bar() to foo() if the foreign key is not present. nxt up - my own web framework! (joke ;) -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 6 Dec 2009, at 18:40, jnowl wrote: Graham, your 'spurious_example()' bit really helped the lights go on. Heh, thought it might. I've been there before, got a whole wardrobe full of T-shirts :-) Glad I could help. Cheers, Graham http://www.linkedin.com/in/ghiggins -BEGIN PGP SIGNATURE- iEYEARECAAYFAksb/xsACgkQOsmLt1NhivwmsACeI8umYfCFEicQ/4oFghY+8+kO QUcAn3nCZysRixQ0tA20bTaLUsWIdwT4iQCVAgUBSxv/G1nrWVZ7aXD1AQLQYAQA kvh+yiIMUDWr7x7Bb++8Ah4nbHncp0RXdPoFlfRxjomBI8nRKgSW2FtVPdL7Hlv8 jGFRr73VBHRRkI+1nnhSp/KqoLxfodfo408/hTWSfMBkVKA9L5RA5bgeh7l6hod5 IBAams7kZahROkGJ7P49uhrvhI13CUVCV+1G/+GQYts= =QtrQ -END PGP SIGNATURE- -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
On Dec 6, 1:40 pm, jnowl john_now...@carleton.ca wrote: Ha! O.k. I think it's pretty clear who is confused. :) ha! just to reiterate on mike's point above, please don't take any of my curt responses as being insensitive, mean or condescending. i just meant for you to read up on that stuff yourself -- since that's really going to be the best/only way to learn. -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
This issue seems to hinge on what is a request, and when is a new response created. I hadn't realized these were in doubt. But in case it's helpful to anybody... A request comes in, and Pylons creates a global 'request' and 'response', and routes to the action. (These are global variables, but through the magic of StackedObjectProxy, they are local to the current request, thread, and application.) Normally the action method returns a string, and Pylons plugs it into the global response and sends it to the browser. 'render()' fills a template and returns a string, so this is still the normal case. The action can also change the response's status or headers; e.g., ``response.status = 401``. The action can also create its own Response object and return it, in which case it supercedes the global response object. When an action method calls another action method, it's still the same request, and the global 'request' and 'response' objects are the same. The second method can return a string, change the status or headers, or return a new Response object, as it wishes. The original action must pass the result of the second action through by returning it, or it will be lost. If you don't explicitly return a value, the default value is None as with all functions, and an empty page will be sent to the browser. Thus, there's no such thing in Pylons of transfering to another request -- or there is, depending on how you define a request. But in the code executed, what matters is the string returned by the original action and the final setting of the headers -- no matter which functions were called to produce this state. Calling 'redirect()' raises an HTTPRedirection that bypasses some of this processing, and the net result is that the 'response' status is changed and a Location: header added, so a redirect is sent to the browser. Alfredo Deza and I are updating the Pylons Execution Analysis. It's a bit inconsistent now because it's partway between Pylons 0.9.6 and Pylons 0.10, so don't trust it too closely, but the draft might give a bird's eye view of what's going on: http://bitbucket.org/sluggo/pylons-execution/src/tip/execution.rst -- Mike Orr sluggos...@gmail.com -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
On Fri, Dec 4, 2009 at 2:45 PM, Jonathan Vanasco jonat...@findmeon.com wrote: I'm not going to waste time describing why you should be doing that. everyone on the internet follows that paradigm for a reason. Ouch, that sounds like I'm better than you but I'm not going to tell you why. In other words, it may or may not be true, depending on whatever he might be basing it on. The problem with calling one action from another is that it breaks the one-to-one correspondence between URLs and action. Proxy caches and and search engines depend on this correspondence, otherwise they'd be returning random inappropriate results. However, it's not always bad. @validate calls the form action if the data doesn't validate, rather than redirecting to the form. It could redirect to the form of course, but then it would have to pass all the form data, and the other action would have to be modified to display it in that circumstance, etc In the case of doing login in place, the issue is that proxies and search engines can't log in, so they'd always see the login page and think it was the normal content. Whereas with a redirect, they know that URL is related to the other URL, and if they analyze the site they might think, Ah, every page on this site goes to a single page. Another factor is that proxies and search engines only cache GET requests with status 200. So if the request is not GET, or the other action changes the status to 401 (Unauthories, or not logged in), then it's safer. In the @validate case, the original request is not GET (usually). As for transfering to another function, that is exactly what return self.othermethod(args) does. Although technically it's a subroutine call, the net effect is the same. Shalisesh Kochhar wrote: http://jjinux.blogspot.com/2009/11/web-redirecting-user-back-after-some.html Yes, that's the normal way to save the original URL during login, and then redirect back to it afterward. Graham Higgins wrote: or, if it has to be a controller method, prefix it with an underscore which is Pylons convention for don't expose this action. Er, it's a convention with some precedence in Python. It's not necessarily a Pylons standard or something you have to do. I go back and forth on whether to underscore all my utility methods. However, if it's a utility method, it's likely useful for more than one controller, and thus would belong in the base controller. You could turn it into a standalone function too. However, I avoid doing that with routines that use the Pylons magic globals or other special features (unless they're passed in as arguments), because it makes it harder to refactor the code to another framework later if you need to. -- Mike Orr sluggos...@gmail.com -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
On Dec 5, 3:37 pm, Mike Orr sluggos...@gmail.com wrote: Ouch, that sounds like I'm better than you but I'm not going to tell you why. In other words, it may or may not be true, depending on whatever he might be basing it on. That's supposed to sound like Every website on the internet follows a certain paradigm. If you don't know why, you should be searching to learn -- because everyone will be wasting their time trying to teach you the right thing and you're not going to listen until you read it elsewhere as that's how people who ask for help always act . In terms of login actions ( as referenced above ) it is absolutely true. Getting back to the issue... as I said before , render just returns the value of the render. the reason why the code does not work, is because the OP is not calling return class AController(BaseController): def foo(self): #do stuff print 'in foo' return render(/foo.html) def bar(self): 'v' in request.params or self.foo() # foo should not return, it is calling render print 'what am i doing here?' v = request.params['v'] #attribute error if I visithttp://localhost/pylons/a/bar/. Why doesn't it render foo? #continue doing stuff However this code prints 'in foo' then 'what am I doing here?' and I think its because the request cycle is getting things confused (kind of jumping into another one before finishing first). While perhaps not a bug (but I'm not sure of that, it is indeterminate what will happen), it could be handled better and would be if I could throw out the response so far and restart with the new method (be more performant as well). self.foo() does not dispatch a new request, it merely calls the foo() method of self inline the request cycle is not getting confused. the dispatcher called self.bar() , and that is it. self is just a python object. if you want to return the output of foo, the code should be def bar(self): if 'v' not in request.params: return self.foo() print 'what am i doing here?' v= request.params['v'] #continue doing stuff Why doesn't it render foo? the original code did render foo. however it did nothing with the output. calling self.foo() doesn't create a new request / re-dispatch , it just runs foo() like any other python object woud. if you want to do that, you need to redirect or subrequest. in order to foo() and stop and return the output of foo() to the browser, you would call return self.foo() Look through my first example, and Graham's later example -- which both addressed this pattern. -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Calling a controllers method from another method? Is there a 'safe' way without redirecting?
Why can't I do the (simplified) following? class AuthController(BaseController): def login(self): #do stuff return render(/login.html) def bar(self): #'v' in request.params or self.login if 'v' not in request.params: self.login print 'what am i doing here?' v = request.params['v'] #attribute error if I visit http://localhost/pylons/auth/bar/. Why doesn't it render login? i.e. I'd like to transfer control to another method (self.login) from self.bar. I think I sort of understand whats happening (I get to 'what am I doing here?' when self.login returns, but shouldn't the render terminate things? Or is there a safe way to handle this? I seem to remember (don't laugh) ms asp had a request.transfer function or something that allowed you to terminate a request and transfer to another function. I find this a natural way to express things and would like to be able to do this without redirecting (i.e. no trip back to client). Thoughts? -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
Try this: change: self.login to:return self.login() Regards, Phil -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
On Dec 4, 3:43 pm, John_Nowlan john_now...@carleton.ca wrote: Why can't I do the (simplified) following? probably because you shouldn't be doing it. i.e. I'd like to transfer control to another method (self.login) from self.bar that's a really awkward, and bad, idea. its proper form to redirect someone to the login page. if 'logged_in' not in session or not session['logged_in']: redirect_to (/login) I'm not going to waste time describing why you should be doing that. everyone on the internet follows that paradigm for a reason. I think I sort of understand whats happening (I get to 'what am I doing here?' when self.login returns, but shouldn't the render terminate things? Or is there a safe way to handle this? I seem to remember (don't laugh) ms asp had a request.transfer function or something that allowed you to terminate a request and transfer to another function. I find this a natural way to express things and would like to be able to do this without redirecting (i.e. no trip back to client). render() is a function in the templating system. it simply accepts a template file location, and returns that template rendered with the C data and misc control logic. when you see in a controller: return render(/path) then you are returning ( to the browser ) the output of render. you could have this as a controller: class AccountLoginStatusController(BaseController): def logged_in(self): return You are logged in. Click here to continue: /my/home def logged_out(self): return You are not logged in. Click here to login: /my/ login def index(self): if 'logged_in' not in session or not session['logged_in']: return self.logged_out() return self.logged_in() As a technical answer, your line self.login should be: self.login() if the login() function is merely a setup/convenience function. calling it like this,however, will just run that function in place, and the controller will continue. if you want to return the output of login and stop processing, ie show the login form, the line would be: return self.login() However, as a user experience answer, your line should read redirect( /login) -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
As what mailzilla said: do: return self.login() At least from SEO perspective, it's not a good idea to have controller/action that can render content on other-controller/other-action. It's a duplication of contents and most search engines don't like that. - Didip - On Fri, Dec 4, 2009 at 1:32 PM, pyl...@mailzilla.com wrote: Try this: change: self.login to:return self.login() Regards, Phil -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.compylons-discuss%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en. -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
RE: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
Sorry. I butchered the code when I was trying to make it an example. It was self.login() . I'm saying this does not work, but I think it is a fairly common case, i.e. some logic within a method causes you to want to goto/call/transfer to a different method (usually based on user inputs). I understand it is not considered a good idea (esp. with foo.html being rendered under bar), but I'm not sure I buy the arguments. It is not supposed to be specifically a 'login' example. So trying to clean up my braindamaged pseudocode: class AController(BaseController): def foo(self): #do stuff print 'in foo' return render(/foo.html) def bar(self): 'v' in request.params or self.foo() # foo should not return, it is calling render print 'what am i doing here?' v = request.params['v'] #attribute error if I visit http://localhost/pylons/a/bar/. Why doesn't it render foo? #continue doing stuff However this code prints 'in foo' then 'what am I doing here?' and I think its because the request cycle is getting things confused (kind of jumping into another one before finishing first). While perhaps not a bug (but I'm not sure of that, it is indeterminate what will happen), it could be handled better and would be if I could throw out the response so far and restart with the new method (be more performant as well). But I'm no expert. And thanks for responses. Muchos appreciated but still wondering. -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
John_Nowlan wrote: Sorry. I butchered the code when I was trying to make it an example. It was self.login() . I'm saying this does not work, but I think it is a fairly common case, i.e. some logic within a method causes you to want to goto/call/transfer to a different method (usually based on user inputs). I understand it is not considered a good idea (esp. with foo.html being rendered under bar), but I'm not sure I buy the arguments. It is not supposed to be specifically a 'login' example. So trying to clean up my braindamaged pseudocode: class AController(BaseController): def foo(self): #do stuff print 'in foo' return render(/foo.html) def bar(self): 'v' in request.params or self.foo() # foo should not return, it is calling render print 'what am i doing here?' v = request.params['v'] #attribute error if I visit http://localhost/pylons/a/bar/. Why doesn't it render foo? #continue doing stuff However this code prints 'in foo' then 'what am I doing here?' and I think its because the request cycle is getting things confused (kind of jumping into another one before finishing first). While perhaps not a bug (but I'm not sure of that, it is indeterminate what will happen), it could be handled better and would be if I could throw out the response so far and restart with the new method (be more performant as well). But I'm no expert. And thanks for responses. Muchos appreciated but still wondering. I think what you're looking for is a workflow which allows the user to be redirected to perform an action before they can continue further. Here's a helpful blog post which shows how something like this could be done: http://jjinux.blogspot.com/2009/11/web-redirecting-user-back-after-some.html In essence you have to redirect and save some context which allows you to come back to the place from where you redirected. - Shailesh -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 5 Dec 2009, at 00:38, John_Nowlan wrote: return render(/foo.html) # foo should not return, it is calling render No, render doesn't work like that, it works like this: def spurious_example(): output_collector = render(header.html) output_collector += render(body.html) return output_collector # complete page of HTML In your example, foo() is defined to return the result of rendering the template which is then promptly discarded in bar() - but foo() is nevertheless executed and so is bar(), that's why you're seeing both printed telltales. If foo() is intended never to appear in a URL, then either move it out of the AController class if it is effectively a standalone support function ... def foo(): #do stuff log.debug('in foo') return render(/foo.html) # returns a string or, if it has to be a controller method, prefix it with an underscore which is Pylons convention for don't expose this action. class AController(BaseController): def _foo(): #do stuff log.debug('in _foo') return render(/foo.html) # returns a string def bar(self): if not 'v' in request.params: return _foo() # or save the result, whatever else: v = request.params['v'] log.debug('i was routed here explicitly') #continue doing stuff return stuff # whatever - but it must be # an iterable but actually, this would be the more usual approach ... class AController(BaseController): def _foo(): #do stuff log.debug('in _foo') return render(/foo.html) # returns a string def bar(self, v=None): if not v: return _foo() log.debug('hoorah, we have a v') #continue doing stuff return stuff # whatever - but bar /must/ return # something that can be iterated over With a URL of http://localhost/pylons/a/bar/12; and a routes map of map.connect('bar_it', '/pylons/a/bar/{v}', controller='a', action='bar) when bar() is called via that URL, Pylons will set the value of the keyword parameter v to 12, so you can use it directly in your action, no need to fish it out of the dict yourself. (Best to check first that it really is a positive integer / map reference / word, whatever.) HTH Cheers, Graham http://www.linkedin.com/in/ghiggins -BEGIN PGP SIGNATURE- iEYEARECAAYFAksZ2UAACgkQOsmLt1NhivyQOgCg6fksraHztOxj/BpLRrwbXLsc UhEAn0wtGuC9mhSa2bacHP9A/Lrkiw0OiQCVAgUBSxnZQFnrWVZ7aXD1AQLmfgP/ csnD4JpVx8P1mkpho0VqhPeJsYP+4xxXkd3WPQ/PmM+ttZ87tn6W7Q/w/vDpceJO wzcO+BU1GqsSHI0ZmZGf5pmLQgZYlaP4JAXstESDmRuSD4D6mFlomzZ/cy47qLBt XkFvB4lgq0T8TMzpgw4m3++saoMLAUgLRBXk/SjePSE= =gPka -END PGP SIGNATURE- -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
Re: Calling a controllers method from another method? Is there a 'safe' way without redirecting?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 5 Dec 2009, at 03:53, Graham Higgins wrote: class AController(BaseController): def _foo(): that'd be class AController(BaseController): def _foo(self): of course. Cheers, Graham http://www.linkedin.com/in/ghiggins -BEGIN PGP SIGNATURE- iEYEARECAAYFAksZ2kcACgkQOsmLt1NhivwFPACgo9Ohslr9D5jNdqlkHx6avXQX /hIAoJ+JI0vdNWOU2q7YfrHRZ1skpi+OiQCVAgUBSxnaR1nrWVZ7aXD1AQL8PwP/ SPwvxh84vqg1PUQSsF1OIEB87eCygVE5kSJ+VCEnD/uhZsIfSpNnvF9/BepqKtyp VSlfJImysbW5RQLOH5Zu/uW16KdwyiFR61eqpUuXQznvZvnD/529beSHEuW+U5/m xeI0R4csar9EC9PsVnK3xl3WsPcWlNz8OSV5hgjjjHo= =9vmM -END PGP SIGNATURE- -- You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-disc...@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.