diff --git a/controller/Files.py b/controller/Files.py index 83b8d50..25a7f7c 100644 --- a/controller/Files.py +++ b/controller/Files.py @@ -13,6 +13,7 @@ from model.File import File from view.Upload_page import Upload_page from view.Blank_page import Blank_page from view.Json import Json +from view.Progress_bar import stream_progress class Access_error( Exception ): @@ -392,81 +393,7 @@ class Files( object ): fraction_reported = 1.0 break - # TODO: maybe move this to the view/ directory - def report( uploading_file, fraction_reported ): - """ - Stream a progress meter as it uploads. - """ - progress_bytes = 0 - progress_width_em = 20 - tick_increment = 0.01 - progress_bar = u'' % \ - ( progress_width_em * tick_increment ) - - yield \ - u""" - - - - - - - - """ - - base_filename = filename.split( u"/" )[ -1 ].split( u"\\" )[ -1 ] - yield \ - u""" -
uploading %s:
- - - - - - -
- %s -
- - """ % ( 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 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""" - - - - """ - - return report( uploading_file, fraction_reported ) + return stream_progress( uploading_file, filename, fraction_reported ) @expose( view = Json ) @strongly_expire diff --git a/view/Progress_bar.py b/view/Progress_bar.py new file mode 100644 index 0000000..51330ad --- /dev/null +++ b/view/Progress_bar.py @@ -0,0 +1,75 @@ +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""" + + + + + + + + """ + + base_filename = filename.split( u"/" )[ -1 ].split( u"\\" )[ -1 ] + yield \ + u""" +
uploading %s:
+ + + + + + +
+ %s +
+ + """ % ( 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 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""" + + + + """