Django

Code

Ticket #4165: 5100_admin_progress_bar.diff

File 5100_admin_progress_bar.diff, 4.2 kB (added by Michael Axiak <axiak@mit.edu>, 1 year ago)

Pretty Admin Upload Survives!

  • django/contrib/admin/media/js/UploadProgress.js

    old new  
     1function getxy(){ 
     2    var x,y; 
     3    if (self.innerHeight) // all except Explorer 
     4        { 
     5        x = self.innerWidth; 
     6        y = self.innerHeight; 
     7        } 
     8    else if (document.documentElement && document.documentElement.clientHeight) 
     9        // Explorer 6 Strict Mode 
     10        { 
     11        x = document.documentElement.clientWidth; 
     12        y = document.documentElement.clientHeight; 
     13        } 
     14    else if (document.body) // other Explorers 
     15        { 
     16        x = document.body.clientWidth; 
     17        y = document.body.clientHeight; 
     18        } 
     19    return {'x':x,'y':y} 
     20    } 
     21 
     22var humanvalue = ['B','KB','MB','GB'] 
     23function humanize(bytes) { 
     24    curbytes = bytes 
     25    iterations = 0 
     26    while (curbytes>1024) { 
     27        iterations++ 
     28        curbytes=curbytes/1024 
     29        } 
     30    return curbytes.toFixed(1) + ' ' + humanvalue[iterations] 
     31    } 
     32 
     33interval = null; 
     34function fetch(uuid) { 
     35    req = xmlhttp 
     36    req.open("GET", "/upload_progress/", 1); 
     37    req.setRequestHeader("X-Progress-Id", uuid); 
     38    req.onreadystatechange = function () { 
     39    if (req.readyState == 4) { 
     40        if (req.status == 200) { 
     41 
     42            var upload = eval( '(' + req.responseText + ')' ); 
     43 
     44            if (upload.state == 'done' || upload.state == 'uploading') { 
     45                bar = document.getElementById('progress_bar'); 
     46                bar_txt = document.getElementById('progress_text') 
     47                bar_txt.innerHTML = ((upload.received / upload.size) * 100).toFixed(1) + '% - ' + 
     48                    humanize(upload.received) + ' of ' + humanize(upload.size)  
     49                w = 400 * upload.received / upload.size; 
     50                bar.style.width = w + 'px'; 
     51 
     52                } 
     53                if (upload.state == 'done') { 
     54                    window.clearTimeout(interval); 
     55                    } 
     56                } 
     57            } 
     58        } 
     59    req.send(null);  
     60 
     61    } 
     62 
     63function openprogress(e) { 
     64 
     65    uuid = ""; 
     66    for (i = 0; i < 32; i++) { 
     67        uuid += Math.floor(Math.random() * 16).toString(16); 
     68        } 
     69    frm = e.target||e.srcElement 
     70 
     71    frm.action=frm.action+"?" + uuid;  
     72 
     73    pos = getxy() 
     74    posx = parseInt((pos.x/2)-(420/2), 10) 
     75    posy = parseInt((pos.y/2)-(50/2), 10) 
     76 
     77    progress_wrap = quickElement('div', document.body, '', 'style',  
     78        'position: absolute; top: '+posy+'px; left: '+posx+'px; height: 50px; ' + 
     79        'padding: 10px; width: 420px; background: #ffffff; ' + 
     80        'border: solid 1px #dddddd;', 'id', 'progress_wrap') 
     81 
     82    progress_label = quickElement('h1', progress_wrap, 'Upload progress') 
     83 
     84    progress = quickElement('div', progress_wrap, '', 'style',  
     85        'top: 0; left: 0; width: 0px; ', 'id', 'progress_bar', 'class', 'submit-row') 
     86 
     87    progress_text = quickElement('div', progress_wrap, '0%', 'style', 
     88        'color: #000000; ', 'id', 'progress_text') 
     89  
     90    interval = window.setInterval( 
     91        function () { 
     92            fetch(uuid); 
     93            }, 
     94        1000 
     95        ); 
     96    } 
     97 
     98addEvent(window, 'load', function() { 
     99        frm = document.getElementsByTagName('form')[0] 
     100        addEvent(frm, 'submit',  openprogress)     
     101        } 
     102    ) 
  • django/middleware/upload.py

    old new  
     1"streaming upload middleware" 
     2from django.conf import settings 
     3from django.utils import simplejson 
     4from django.http import HttpResponse 
     5 
     6class UploadStateMiddleware(object): 
     7 
     8    def process_request(self, request): 
     9        progress_url = getattr(settings, 'PROGRESS_URL', '/upload_progress/') 
     10 
     11        if request.path == progress_url: 
     12            progress_id = request.META['UPLOAD_PROGRESS_ID'] 
     13 
     14            try: 
     15                content = simplejson.dumps(request.file_progress) 
     16            except: 
     17                content="{}" 
     18            if not content: 
     19                content="{}" 
     20 
     21 
     22            return HttpResponse(content=content, mimetype='text/plain')