Ticket #3849: sagenb_3849.patch

File sagenb_3849.patch, 3.7 KB (added by was, 12 years ago)
  • sagenb/notebook/twist.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1258677399 28800
    # Node ID db20f1bc4eab2f6741401fd44c382cfa5bedd199
    # Parent  3424c80b4c09ca3e75c753207cc10206aa8db83e
    trac 3849 -- get rid of internal server errors when uploading a worksheet.
    
    diff -r 3424c80b4c09 -r db20f1bc4eab sagenb/notebook/twist.py
    a b  
    411411            # uploading a file from the user's computer
    412412            dir = tmp_dir()
    413413            filename = ctx.files['fileField'][0][0]
     414            if filename == '':
     415                return HTMLResponse(stream=message("Please specify a worksheet to load."))
    414416            # Make tmp file in Sage temp directory
    415417            filename = os.path.join(dir, filename)
    416418            f = file(filename,'wb')
     
    420422            f.close()
    421423
    422424
    423         #We make a callback so that we can download a file remotely
    424         #while allowing the server to still serve requests.
     425        # We make a callback so that we can download a file remotely
     426        # while allowing the server to still serve requests.
    425427        def callback(result):
    426428
    427429            if ctx.args.has_key('nameField'):
     
    447449                    else:
    448450                        W = notebook.import_worksheet(filename, self.username)
    449451
    450                 except IOError, msg:
    451                     print msg
    452                     raise ValueError, "Unfortunately, there was an error uploading the worksheet.  It could be an old unsupported format or worse.  If you desperately need its contents contact the Google group sage-support and post a link to your worksheet.  Alternatively, an sws file is just a bzip2'd tarball; take a look inside!"
     452                except Exception, msg:
     453                    s = 'There was an error uploading the worksheet.  It could be an old unsupported format or worse.  If you desperately need its contents contact the <a href="http://groups.google.com/group/sage-support">sage-support</a> and post a link to your worksheet.  Alternatively, an sws file is just a bzip2 tarball; take a look inside!'
     454                    return HTMLResponse(stream=message(s,'/'))
    453455                finally:
    454456                    # Clean up the temporarily uploaded filename.
    455457                    os.unlink(filename)
     
    469471            return http.RedirectResponse('/home/'+W.filename())
    470472
    471473        if url != '':
    472             #We use the downloadPage function which returns a
    473             #deferred which we are allowed to return to the server.
    474             #The server waits until the download is finished and then runs
    475             #the callback function specified.
     474            # We use the downloadPage function which returns a
     475            # deferred which we are allowed to return to the server.
     476            # The server waits until the download is finished and then runs
     477            # the callback function specified.
    476478            from twisted.web.client import downloadPage
    477479            d = downloadPage(url, filename)
    478480            d.addCallback(callback)
     481            def errback(result):
     482                msg = "There was an error uploading '%s' (please recheck the URL)."%url
     483                return HTMLResponse(stream=message(msg,'/'))
     484            d.addErrback(errback)
    479485            return d
    480486        else:
    481             #If we already have the file, then we
    482             #can just return the result of callback which will
    483             #give us the HTMLResponse.
     487            # If we already have the file, then we
     488            # can just return the result of callback which will
     489            # give us the HTMLResponse.
    484490            return callback(None)
    485491
    486492