Accessing a beaker session object from within Deform fileuploadWidget's tmpstore

65 views
Skip to first unread message

tonthon

unread,
Apr 6, 2012, 2:54:40 AM4/6/12
to pylons-discuss
Hi,

I'm building a FileUploadTempStore to handle file upload with deform and
I'm wondering how to access my beaker session object.

It works using :
session = pyramid.threadlocal.get_current_request().session

But since I ""shouldn't abuse thread locals"", I'm wondering how I
should do ?

Cheers

--
TJEBBES Gaston
Majerti
http://www.majerti.fr

Jeff Dairiki

unread,
Apr 7, 2012, 4:29:57 PM4/7/12
to pylons-...@googlegroups.com, tonthon
On Fri, Apr 06, 2012 at 08:54:40AM +0200, tonthon wrote:
> I'm building a FileUploadTempStore to handle file upload with deform and
> I'm wondering how to access my beaker session object.
>
> It works using :
> session = pyramid.threadlocal.get_current_request().session
>
> But since I ""shouldn't abuse thread locals"", I'm wondering how I
> should do ?

Use a deferred to construct the FileUploadWidget. See

http://docs.pylonsproject.org/projects/colander/en/latest/binding.html

Here's an (untested and incomplete) example:

@colander.deferred
def deferred_upload_widget(field, bindargs):
request = bindargs['request']
session = request.session
tmpstore = MyTmpStore(session)
return deform.widget.FileUploadWidget(tmpstore)

class MyUploadSchema(colander.Schema):
upload = colander.SchemaNode(deform.FileData(),
widget=deferred_upload_widget)

upload_schema = MyUploadSchema()

then, in your view logic (where you have access to the request object):

schema = upload_schema.bind(request=request)
form = Form(schema, ...


Jeff

tonthon

unread,
Apr 7, 2012, 5:49:28 PM4/7/12
to Jeff Dairiki, pylons-...@googlegroups.com
That's the way I had finally chosen.
Thanks for your answer.

Regards

2012/4/7 Jeff Dairiki <dai...@dairiki.org>
Reply all
Reply to author
Forward
0 new messages