percious schrieb:
> Cool idea, one that is implemented in TW2.
>
> I'd love to see a code example if you've got one.
See the following (not fully finished, but you get the idea) upload-form:
# -*- coding: utf-8 -*-
"""Main Controller"""
from __future__ import with_statement
import time
import uuid
from simplejson import dumps
from tg import expose, flash, require, url, request, redirect
from pylons.i18n import ugettext as _, lazy_ugettext as l_
from tg import tmpl_context as c
from uploadtest.lib.base import BaseController
from uploadtest.model import DBSession, metadata
from uploadtest.controllers.error import ErrorController
__all__ = ['RootController']
from tw.api import (
Widget,
WidgetsList,
ServerSideCallableMixin,
serverside_callback,
always_allow,)
from tw.jquery import jquery_js
from tw.jquery.ui import ui_dialog_js
import tw.forms as twf
import webob
class UploadSession(object):
ratio = .0
def __init__(self, path):
self._start = time.time()
self.path = path
@property
def age(self):
return time.time() - self._start
class UploadForm(twf.TableForm, ServerSideCallableMixin):
javascript = [jquery_js, ui_dialog_js]
UPLOAD_SESSIONS = {}
def __init__(self, *args, **kwargs):
super(UploadForm, self).__init__(*args, **kwargs)
class fields(WidgetsList):
image_file = twf.FileField()
def update_params(self, d):
super(UploadForm, self).update_params(d)
session = uuid.uuid4()
d.action = self.url_for_callback(self.upload_action) +
"?session=%s" % session
progress_url = self.url_for_callback(self.progress) +
"?session=%s" % session
self.add_call("""
$(document).ready(function() {
var id = "%(id)s";
$("#" + id).append("<iframe name='%(id)s_iframe'/>");
$("#" + id).submit(function() {
$(this).append("<div
id='dialog'>Progress:</div>");
$("#dialog").dialog();
function update_progress() {
$.get("%(progress_url)s", {},
function(data, textStatus) {
$("#dialog").text(data.ratio);
if(data.status == "ok") {
window.setTimeout(update_progress, 200);
}
else if(data.status == "finished") {
$("#dialog").dialog("close");
}
else if(data.status = "unknown
session") {
window.setTimeout(update_progress, 200);
}
}, "json");
};
window.setTimeout(update_progress, 200);
return true;
});
});
""" % dict(id=self.id, progress_url=progress_url)) #"
@serverside_callback
def upload_action(self, request):
inf = request.environ["wsgi.input"]
length = float(request.content_length)
read = 0.0
ratio = .0
session_id = request.GET["session"]
session_path = "/tmp/%s.body" % session_id
session = UploadSession(session_path)
self.UPLOAD_SESSIONS[session_id] = session
with open(session_path, "w") as outf:
while True:
block = inf.read(4096)
if not block:
break
outf.write(block)
read += len(block)
session.ratio = read / length
response = webob.Response()
response.body = "OK"
response.content_type = "text/plain"
return response
@serverside_callback
def progress(self, request):
session_id = request.params["session"]
res = dict(status="unknown session", ratio=0.0)
if session_id in self.UPLOAD_SESSIONS:
session = self.UPLOAD_SESSIONS[session_id]
res["ratio"] = session.ratio
res["status"] = "ok" if session.ratio < 1.0 else "finished"
response = webob.Response()
response.content_type = "application/javascript"
response.body = dumps(res)
return response
upload_form = UploadForm("upload_form",
attrs=dict(target="upload_form_iframe"),
callback_authorization=always_allow)
class RootController(BaseController):
@expose('uploadtest.templates.index')
def index(self):
"""Handle the front-page."""
c.upload_form = upload_form
return dict(page='index')
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Trunk" 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-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---