I'm trying to port an existing mod_python app to Pylons, but had
some trouble with Routes. I'd like to keep using URLs the same
"classic" URL style:
http://localhost/location/edit?location_id=123
And then have Pylons receive it as arguments for the controller
method:
<code>
class LocationController(BaseController):
def edit(self, location_id):
return 'Editing %s' % location_id
</code>
But I'm getting this exception:
TypeError: edit() takes exactly 2 arguments (1 given)
Do you have any directions on what do I need to do to make it work?
--
Humberto Diógenes
Yes, pull location_id from the request GET parameters:
employment_id = request.GET.get('employment_id')
Alberto
Thanks, Alberto. But as I said, I'm trying to port an old application
-- that means I would have to add one request.GET line for every
parameter in every controller function (and then remove all parameters
from the function signature).
I could do that, but what I'd really like to know is this: does Pylons
support query string "parameter injection"? If not, how difficult
would it be to implement that?
--
Humberto Diógenes
http://humberto.digi.com.br
this would inject the variable names into your functions do you could
use them directly. I don't know what security risks this will bring up
however
Jose
> Thanks, Alberto. But as I said, I'm trying to port an old application
> -- that means I would have to add one request.GET line for every
> parameter in every controller function (and then remove all parameters
> from the function signature).
>
> I could do that, but what I'd really like to know is this: does Pylons
> support query string "parameter injection"? If not, how difficult
> would it be to implement that?
This past weekend, I participated in a sprint on Pylons where
we implemented two new controllers: DecoratedController and
ObjectDispatchController. Both of these controllers perform "parameter
injection." Here is an example of a DecoratedController:
from pylons.controllers import DecoratedController
from pylons.decorators import expose
class TestController(DecoratedController):
@expose()
def testing(self, name):
return 'Hello, %s' % name
Navigating your browser to '/test/testing?name=Humberto' will render the
string "Hello, Humberto." You can also render templates from Buffet
compatible engines by passing specific information into the expose
decorator, like so:
@expose('myproject.templates.testing')
def testing(self, name):
return dict(name=name)
The dictionary you return in the controller method will be used as the
namespace for your template. You can also render JSON by simply doing
@expose('json'). The expose decorator can be stacked:
@expose('json', content_type='application/json')
@expose('myproject.templates.testing', content_type='text/html')
def testing(self, name):
return dict(name=name)
You then send the `Accept:` header to perform content negotiation to
determine which output will be rendered.
This is all very new, so it probably has some bugs, but please do give
it a try, and we'll do our best to fix any bugs you find!
--
Jonathan LaCour
http://cleverdevil.org
Are 'c' and the other Pylons variables implicitly added?
--
Mike Orr <slugg...@gmail.com>
>> The dictionary you return in the controller method will be used as
>> the
>> namespace for your template.
>
> Are 'c' and the other Pylons variables implicitly added?
As of right now, they are not. You will have to return them as part of
the dictionary that is sent to your template.
You can't modify local variables via locals(). Local variables are
implemented as an array with the names converted to subscripts at
compile time. The only exception is if the function contains an
'eval', which is slow and insecure (if the arg is untrusted).
===x1,py===
def test():
locals()["k"] = "v"
print k
if __name__ == "__main__": test()
===
$ python /tmp/x.py
Traceback (most recent call last):
File "/tmp/x.py", line 5, in <module>
if __name__ == "__main__": test()
File "/tmp/x.py", line 3, in test
print k
NameError: global name 'k' is not defined
If you put "k = 2" before the locals() call, it will print 2.
> I don't know what security risks this will bring up
A user might override one of your variables like 'filename_to_delete'.
--
Mike Orr <slugg...@gmail.com>
On Nov 6, 5:56 pm, Jonathan LaCour <jonathan-li...@cleverdevil.org>
wrote:
>
> This past weekend, I participated in a sprint on Pylons where
> we implemented two new controllers: DecoratedController and
> ObjectDispatchController. Both of these controllers perform "parameter
> injection." Here is an example of a DecoratedController:
>
> from pylons.controllers import DecoratedController
> from pylons.decorators import expose
>
> class TestController(DecoratedController):
>
> @expose()
> def testing(self, name):
> return 'Hello, %s' % name
>
> This is all very new, so it probably has some bugs, but please do give
> it a try, and we'll do our best to fix any bugs you find!
>
That's great! :)
I pulled the latest version from the repository but couldn't use it.
### Existing project:
URL: http://localhost:5000/location/index
File '/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/
site-packages/Pylons-0.9.7dev_20071106-py2.4.egg/pylons/error.py',
line 245 in respond
app_iter = self.application(environ, detect_start_response)
File '/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/
site-packages/Pylons-0.9.7dev_20071106-py2.4.egg/pylons/wsgiapp.py',
line 95 in __call__
controller = self.resolve(environ, start_response)
File '/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/
site-packages/Pylons-0.9.7dev_20071106-py2.4.egg/pylons/wsgiapp.py',
line 173 in resolve
match = environ['wsgiorg.routing_args'][1]
KeyError: 'wsgiorg.routing_args'
### New project:
$ paster create -t pylons PylonsTest2
[...]
OSError: [Errno 2] No such file or directory: '/Library/Frameworks/
Python.framework/Versions/2.4/lib/python2.4/site-packages/
Pylons-0.9.7dev_20071106-py2.4.egg/pylons/templates/default_project'
$ ls -l /Library/Frameworks/Python.framework/Versions/2.4/lib/
python2.4/site-packages/Pylons-0.9.7dev_20071106-py2.4.egg/pylons/
templates/
-rwxr-xr-x 1 root admin 0 Nov 6 19:02 __init__.py
-rwxr-xr-x 1 root admin 221 Nov 6 19:02 __init__.pyc
I hope I'm not doing anything wrong. :P
This is going to be a recurring problem with Pylons-dev. When
upgrading an app from Pylons 0.9.6 to 0.9.7, you have to add three
lines to middleware.py:
# CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
# Routing/Session/Cache Middleware
app = RoutesMiddleware(app, config['routes.map'])
app = SessionMiddleware(app, config)
app = CacheMiddleware(app, config)
Downgrading, you have to remove these lines, because Pylons 0.9.6
activates them implicitly.
> ### New project:
>
> $ paster create -t pylons PylonsTest2
> [...]
> OSError: [Errno 2] No such file or directory: '/Library/Frameworks/
> Python.framework/Versions/2.4/lib/python2.4/site-packages/
> Pylons-0.9.7dev_20071106-py2.4.egg/pylons/templates/default_project'
>
> $ ls -l /Library/Frameworks/Python.framework/Versions/2.4/lib/
> python2.4/site-packages/Pylons-0.9.7dev_20071106-py2.4.egg/pylons/
> templates/
> -rwxr-xr-x 1 root admin 0 Nov 6 19:02 __init__.py
> -rwxr-xr-x 1 root admin 221 Nov 6 19:02 __init__.pyc
It works for me, both with my existing dev snapshot (d3c5b6893348) and
after "hg pull -u" (a7756badfe81). Maybe you got an incomplete
update?
$ ls -l ~/mercurial/Pylons/pylons/templates
total 20
-rw-r--r-- 1 mso mso 281 2007-10-22 12:36 controller.py_tmpl
drwxr-xr-x 5 mso mso 4096 2007-10-22 12:36 default_project
-rw-r--r-- 1 mso mso 0 2007-10-22 12:36 __init__.py
drwxr-xr-x 4 mso mso 4096 2007-10-22 12:36 minimal_project
-rw-r--r-- 1 mso mso 2085 2007-10-22 12:36 restcontroller.py_tmpl
-rw-r--r-- 1 mso mso 200 2007-10-22 12:36 test_controller.py_tmpl
--
Mike Orr <slugg...@gmail.com>
>
> On 11/6/07, Humberto Diógenes <hdio...@gmail.com> wrote:
>> ### Existing project:
>> URL: http://localhost:5000/location/index
>> [...]
>> KeyError: 'wsgiorg.routing_args'
>
> This is going to be a recurring problem with Pylons-dev. When
> upgrading an app from Pylons 0.9.6 to 0.9.7, you have to add three
> lines to middleware.py:
>
> [...]
>
> Downgrading, you have to remove these lines, because Pylons 0.9.6
> activates them implicitly.
>
Thanks! It worked. Here goes the code with the right imports:
# CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
# Routing/Session/Cache Middleware (0.9.7 only, remove for 0.9.6)
from routes.middleware import RoutesMiddleware
from beaker.middleware import SessionMiddleware, CacheMiddleware
app = RoutesMiddleware(app, config['routes.map'])
app = SessionMiddleware(app, config)
app = CacheMiddleware(app, config)
>> ### New project:
>>
>> $ paster create -t pylons PylonsTest2
>> [...]
>> OSError: [Errno 2] No such file or directory: '/Library/Frameworks/
>> Python.framework/Versions/2.4/lib/python2.4/site-packages/
>> Pylons-0.9.7dev_20071106-py2.4.egg/pylons/templates/default_project'
>>
>
> It works for me (...) Maybe you got an incomplete update?
No, the update was OK, the files are in the checkout. I copied them
manually to Python site-packages and it worked.
I think there's something wrong with the install scripts. When I ran
"easy_install ." again, the templates were gone from site-packages.
Anyway, the new expose() decorator worked! Thanks!!! (although it
took a little to notice that BaseController was changed to
DecoratedController :-)
is it possible to easy_install a development version in Mercurial?
I used "python setup.py develop"
--
Mike Orr <slugg...@gmail.com>
--
Mike Orr <slugg...@gmail.com>