Ticket #3849: sagenb_3849.2.patch

File sagenb_3849.2.patch, 4.6 KB (added by mpatel, 12 years ago)

Version 2. Added backlink. Apply only this patch to the sagenb repo.

  • sagenb/notebook/twist.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1258677399 28800
    # Node ID f9e36ea1a14b1664846254e6207a0ce56e0f16fe
    # Parent  9be9f6374d872ddcf9e9625e6f2ab49ab19a8fcc
    trac 3849 -- get rid of internal server errors when uploading a worksheet.
    
    diff --git a/sagenb/notebook/twist.py b/sagenb/notebook/twist.py
    a b class UploadWorksheet(resource.PostableR 
    401401        self.username = username
    402402
    403403    def render(self, ctx):
     404        backlinks = """ Return to <a href="/upload" title="Upload a worksheet"><strong>Upload File</strong></a>."""
     405
    404406        url = ctx.args['urlField'][0].strip()
    405407        dir = ''  # we will delete the directory below if it is used
    406408        if url != '':
    class UploadWorksheet(resource.PostableR 
    410412            # uploading a file from the user's computer
    411413            dir = tmp_dir()
    412414            filename = ctx.files['fileField'][0][0]
     415            if filename == '':
     416                return HTMLResponse(stream=message("Please specify a worksheet to load.%s" % backlinks))
    413417            # Make tmp file in Sage temp directory
    414418            filename = os.path.join(dir, filename)
    415419            f = file(filename,'wb')
    class UploadWorksheet(resource.PostableR 
    419423            f.close()
    420424
    421425
    422         #We make a callback so that we can download a file remotely
    423         #while allowing the server to still serve requests.
     426        # We make a callback so that we can download a file remotely
     427        # while allowing the server to still serve requests.
    424428        def callback(result):
    425429
    426430            if ctx.args.has_key('nameField'):
    class UploadWorksheet(resource.PostableR 
    446450                    else:
    447451                        W = notebook.import_worksheet(filename, self.username)
    448452
    449                 except IOError, msg:
    450                     print msg
    451                     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!"
     453                except Exception, msg:
     454                    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 group</a> and post a link to your worksheet.  Alternatively, an sws file is just a bzip2 tarball; take a look inside!%s' % backlinks
     455                    return HTMLResponse(stream=message(s,'/'))
    452456                finally:
    453457                    # Clean up the temporarily uploaded filename.
    454458                    os.unlink(filename)
    class UploadWorksheet(resource.PostableR 
    457461                        shutil.rmtree(dir)
    458462
    459463            except ValueError, msg:
    460                 s = "Error uploading worksheet '%s'."%msg
     464                s = "Error uploading worksheet '%s'.%s" % (msg, backlinks)
    461465                return HTMLResponse(stream = message(s, '/'))
    462466
    463467            # If the user requested in the form a specific title for
    class UploadWorksheet(resource.PostableR 
    468472            return http.RedirectResponse('/home/'+W.filename())
    469473
    470474        if url != '':
    471             #We use the downloadPage function which returns a
    472             #deferred which we are allowed to return to the server.
    473             #The server waits until the download is finished and then runs
    474             #the callback function specified.
     475            # We use the downloadPage function which returns a
     476            # deferred which we are allowed to return to the server.
     477            # The server waits until the download is finished and then runs
     478            # the callback function specified.
    475479            from twisted.web.client import downloadPage
    476480            d = downloadPage(url, filename)
    477481            d.addCallback(callback)
     482            def errback(result):
     483                msg = "There was an error uploading '%s' (please recheck the URL).%s" % (url, backlinks)
     484                return HTMLResponse(stream=message(msg,'/'))
     485            d.addErrback(errback)
    478486            return d
    479487        else:
    480             #If we already have the file, then we
    481             #can just return the result of callback which will
    482             #give us the HTMLResponse.
     488            # If we already have the file, then we
     489            # can just return the result of callback which will
     490            # give us the HTMLResponse.
    483491            return callback(None)
    484492
    485493