import cgi def stream_progress( uploading_file, filename, fraction_reported ): """ Stream a progress meter as a file uploads. """ progress_bytes = 0 progress_width_em = 20 tick_increment = 0.01 progress_bar = u'' % \ ( progress_width_em * tick_increment ) yield \ u""" """ FILENAME_TRUNCATION_WIDTH = 40 base_filename = filename.split( u"/" )[ -1 ].split( u"\\" )[ -1 ] if len( base_filename ) > FILENAME_TRUNCATION_WIDTH: base_filename = base_filename[ : FILENAME_TRUNCATION_WIDTH ] + u"..." yield \ u"""
uploading %s:
%s
0%%
""" % ( cgi.escape( base_filename ), progress_bar, progress_width_em ) if uploading_file: received_bytes = 0 while received_bytes < uploading_file.content_length: received_bytes = uploading_file.wait_for_total_received_bytes() fraction_done = float( received_bytes ) / float( uploading_file.content_length ) if fraction_done > 1.0: fraction_done = 1.0 if fraction_done == 1.0 or fraction_done > fraction_reported + tick_increment: fraction_reported = fraction_done yield '' % fraction_reported uploading_file.wait_for_complete() if fraction_reported < 1.0: yield "An error occurred when uploading the file." return yield \ u""" """ general_error_script = \ """ withDocument( window.parent.document, function () { var frame = getElement( 'upload_frame' ); if ( frame && frame.pulldown ) frame.pulldown.cancel_due_to_error( "%s" ); } ); """ quota_error_script = \ """ withDocument( window.parent.document, function () { var frame = getElement( 'upload_frame' ); if ( frame && frame.pulldown ) frame.pulldown.cancel_due_to_quota(); } ); """ def stream_quota_error(): yield \ u""" """ % quota_error_script