Using a Request Factory in Pyramid to write a little less code
ratchet.io
ratchet.io
The caveat, of course, is that if you are using anything other than a vanilla CGI-type gateway, you need to make sure to clear out the threadlocal variable at the end of the request. Don't want it getting reused by somebody else. :)
pyramid.threadlocal.get_current_request()First, I didn't want dependencies on Pyramid imports in certain sections of my code.
Second, this comment in the Pyramid docs: This function should be used extremely sparingly, usually only in unit testing code. it’s almost always usually a mistake to use get_current_request outside a testing context because its usage makes it possible to write code that can be neither easily tested nor scripted.
That made me think that they might not have done the due diligence in making sure that my code was safe (which is completely reasonable since they made it explicitly clear they felt that way). Since I knew exactly how I was deploying, I could make it safe.
Before: > def flash_success(request, body, title=''): > request.session.flash({'body': body, 'title': title'})
After: > def flash_success(self, body, title=''): > self.session.flash({'body': body, 'title': title'})