Suggestions for running view code from a shell/cron?

53 views
Skip to first unread message

Seth

unread,
Feb 9, 2011, 6:11:16 PM2/9/11
to pylons...@googlegroups.com
This seems to me like it should be simple, so please excuse me if the answer is staring me in the face (I did some searching in the docs and elsewhere and couldn't find anything satisfactory):

I'm wondering what the recommended way is to run Pyramid view code from a shell/cron. It seems like it'd be pretty simple to load the pyramid.testing module and use DummyRequest to do things like create POSTs to view methods, but since testing.* probably wasn't intended for such purposes I'm hesitant to make that leap.

To be clear: I'm looking for a little more than a "from views import view_method; view_method()" approach. I'm hoping to be able to bootstrap the whole Pyramid app and actually simulate view *requests* (GET and POST) to these methods from a cron.

Suggestions?

Thanks,
Seth

Chris McDonough

unread,
Feb 9, 2011, 6:36:09 PM2/9/11
to pylons...@googlegroups.com

The most straightforward way to execute a view as it would be executed
if a real request were to come in is to use the technique described in
http://docs.pylonsproject.org/projects/pyramid/1.0/narr/testing.html#creating-functional-tests

If that seems unreasonable, probably you'll want to modify your
requirements. Your view code has the logic you want to execute right
now, but if you want to call it programmatically, you should likely
factor the bits of it out that need to be called outside of the context
of an actual request and just call the refactored function from within
your cron-executed script.

- C

Rob Miller

unread,
Feb 9, 2011, 7:15:53 PM2/9/11
to pylons...@googlegroups.com

Are you using Paster? If so, "paster request" and "paster post" are
probably what you're looking for.

-r

Seth

unread,
Feb 10, 2011, 2:27:42 AM2/10/11
to pylons...@googlegroups.com
Thanks for the suggestions. I actually can accomplish what I'd like to do using the pyramid.testing DummyRequest object without having to dive all the way into WebTest. As long as I'm not hearing any warnings against that here I think I'll move forward with that.

Seth

Chris McDonough

unread,
Feb 10, 2011, 2:36:36 AM2/10/11
to pylons...@googlegroups.com

No real reason to use DummyRequest there (it's a bit strange in
general). I'd probably just use pyramid.request.Request.

- C


Seth

unread,
Feb 10, 2011, 2:49:24 AM2/10/11
to pylons...@googlegroups.com
I went down that path a little at first but got frustrated with bumping up against the fact that I'm trying to POST data to a method that I've got an ACL security policy on. I finally went the testing route because testing.setUp, testing.DummyRequest, and testing.DummySecurity policy all worked well together.

Perhaps I will give it another go and be more patient this time.

Thanks again,
Seth
Reply all
Reply to author
Forward
0 new messages