On Mar 4, 2011, at 4:21 PM, NiL wrote:
>> But you create a new class and instance on each request.
>>
>> This might lead to memory leaks.
>>
>
> yes, but ... the script parameter (that is where this widget should
> submit its data) is dynamic in my specific use case, and can't be
> determine before entering the controller.
> That's why I needed to implement this way.
No, you don't. You can pass parameters to widgets through the display/render
method.
I just tried to install & use tw.uploadify, and it's not really working.
First of all, you can't easy_install it because it misses a CHANGE.txt. Also,
you don't require tw.forms, which you IMHO should.
The example is also pretty bad, is it require jqgrid for no apparent reason,
but OTOH doesn't import Spacer (unless I'm missing something)
And then it's simply not a proper widget. See the attached test-script and the
fixed version to see how to pass parameters to widgets, and how to actually
implement widgets. The important thing is to remember that
*ALL*
dynamic state passed to a widget comes through update_params, and of course you
need to access these.
I guess somebody needs to release a new version ;)
Diez
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en.
from tw.api import Widget, JSLink, CSSLink, js_function, Link
from tw.jquery import jquery_js
from tw.forms import FileField
__all__ = ["Uploadify"]
uploadify_css = CSSLink(modname=__name__,
filename="static/css/uploadify.css")
uploadify = JSLink(modname=__name__,
filename='static/js/jquery.uploadify.v2.1.4.min.js',
css = [uploadify_css,],
javascript = [jquery_js,],)
swfobject = JSLink(modname=__name__,
filename='static/js/swfobject.js',
)
flash_uploadify = Link(modname=__name__,
filename='static/swf/uploadify.swf')
flash_install = Link(modname=__name__,
filename='static/swf/expressInstall.swf')
cancel_img = Link(modname=__name__,
filename='static/images/cancel.png')
class Uploadify(FileField):
javascript = [uploadify, swfobject]
css = [uploadify_css,]
# params = ["sortable", "searchable"]
# sortable = True
# searchable = True
# all available options: http://www.uploadify.com/documentation/
params = {
"auto" : "Automatically upload files as they are added to the queue.",
"buttonText" : "The text that appears on the default button.",
"cancelImg" : "The path to an image you would like to use as the cancel button.",
"expressInstall" : "The path to the expressInstall.swf file.",
"fileDataName" : "The name of your files array in the back-end script.",
"fileDesc" : "The text that will appear in the file type drop down at the bottom of the browse dialog box.",
"fileExt" : "A list of file extensions that are allowed for upload.",
"multi": "The multi option allows the user to add multiple files to the queue for upload.",
"removeCompleted" : "Enable automatic removal of the queue item for completed uploads.",
"script": "Tells us where to get the data.",
"scriptAccess": "The scriptAccess option sets the scriptAccess property for the flash button file.",
"scriptData" : "An object containing name/value pairs with additional information that should be sent to the back-end script when processing a file upload.",
"uploader" : "The path to the uploadify.swf file.",
}
#defaults
auto = True
buttonText = 'SELECT FILES'
cancelImg = cancel_img.link
expressInstall = flash_install.link
fileDataName = "Filedata"
fileDesc = None # "Document Files (.DOC, .ODT, .PDF)"
fileExt = None # "*.doc;*.odt;*.pdf"
multi = False
removeCompleted = True
scriptAccess = "sameDomain" # or always
scriptData = {}
uploader = flash_uploadify.link
script = None
def update_params(self, d):
"""This method is called every time the widget is displayed. It's task
is to prepare all variables that are sent to the template. Those
variables can accessed as attributes of d."""
super(Uploadify, self).update_params(d)
upload_params = dict(
auto=d.auto,
buttonText=d.buttonText,
cancelImg=d.cancelImg,
expressInstall=d.expressInstall,
fileDataName=d.fileDataName,
fileDesc=d.fileDesc,
fileExt=d.fileExt,
multi=d.multi,
removeCompleted=d.removeCompleted,
script=d.script,
scriptAccess=d.scriptAccess,
scriptData=d.scriptData,
uploader=d.uploader,
)
call = js_function('$("#%s").uploadify' % d.id)(upload_params)
self.add_call(call)
from tw.forms import TableFieldSet
from tw.uploadify import Uploadify
from tw.core.middleware import make_middleware
import webtest
class UploadFieldset(TableFieldSet):
label_text = "upload files"
fields = [
Uploadify('upload_file', label_text='upload File',
script='upload',
fileDataName='upload_file',
scriptData={'_method': 'PUT'},
removeCompleted=False,
),
]
upload_fieldset = UploadFieldset("upload_fieldset")
def test_app(environ, start_response):
start_response("200 Ok",
[("Content-type", "text/html")])
widget_output = upload_fieldset.render(child_args=dict(
upload_file=dict(
script="foobarbaz"
)
))
return ("<html><head></head><body>%s</body></html>" % widget_output).encode("utf-8")
app = make_middleware(test_app,
{
'toscawidgets.middleware.inject_resources': True,
},
stack_registry=True,
)
test_app = webtest.TestApp(app)
res = test_app.get("/")
assert "foobarbaz" in res, res.body