Ticket #8473: trac_8473-sagenb.patch

File trac_8473-sagenb.patch, 7.0 KB (added by iandrus, 10 years ago)
  • sagenb/notebook/run_notebook.py

    # HG changeset patch
    # User Ivan Andrus <darthandrus@gmail.com>
    # Date 1294151712 -3600
    # Node ID 88b97a3b2b344b40d7b0aee9892abcdb0950cf08
    # Parent  94af219318b5c3204efdd78e92f447e56590c584
    #8473/sagenb: Add support for upload from file urls and --notebook upload=
    
    diff --git a/sagenb/notebook/run_notebook.py b/sagenb/notebook/run_notebook.py
    a b  
    159159
    160160             timeout       = 0,
    161161
     162             upload        = None,
    162163             open_viewer   = True,
    163164
    164165             sagetex_path  = "",
     
    258259                if str(e).startswith('Another twistd server is running,'):
    259260                    print 'Another Sage Notebook server is running, PID %d.' % pid
    260261                    old_interface, old_port, old_secure = get_old_settings(conf)
    261                     if open_viewer and old_port:
     262                    if old_port and (open_viewer or upload):
    262263                        old_interface = old_interface or 'localhost'
    263264
    264                         print 'Opening web browser at http%s://%s:%s/ ...' % (
    265                             's' if old_secure else '', old_interface, old_port)
     265                        startpath = ''
     266                        if upload:
     267                            startpath = '/upload_worksheet?url=file:///%s' % (upload)
     268
     269                        print 'Opening web browser at http%s://%s:%s/%s ...' % (
     270                            's' if old_secure else '', old_interface, old_port, startpath)
    266271
    267272                        from sagenb.misc.misc import open_page as browse_to
    268                         browse_to(old_interface, old_port, old_secure, '/')
     273                        browse_to(old_interface, old_port, old_secure, startpath)
    269274                        return
    270275                    print '\nPlease either stop the old server or run the new server in a different directory.'
    271276                    return
     
    288293        notebook_opts = '"%s",interface="%s",port=%s,secure=%s' % (
    289294            os.path.abspath(directory), interface, port, secure)
    290295
    291         if open_viewer:
     296        if open_viewer or upload:
    292297            if require_login:
    293298                start_path = "'/?startup_token=%s' % startup_token"
     299            elif upload:
     300                start_path = "'/upload_worksheet?url=file:///%s'" % upload
    294301            else:
    295302                start_path = "'/'"
    296303            if interface:
    297304                hostname = interface
    298305            else:
    299306                hostname = 'localhost'
    300             open_page = "from sagenb.misc.misc import open_page; open_page('%s', %s, %s, %s)" % (hostname, port, secure, start_path)
     307            open_page = "from sagenb.misc.misc import open_page; open_page('%s', %s, %s, %s);" % (hostname, port, secure, start_path)
     308
     309            # If we have to login and upload a file, then we do them
     310            # in that order and hope that the login is fast enough.
     311            if require_login and upload:
     312                import urllib
     313                open_page += "open_page('%s', %s, %s, '/upload_worksheet?url=file:///%s');" % (hostname, port, secure, urllib.quote(upload))
     314
    301315        else:
    302316            open_page = ''
    303317
  • sagenb/notebook/twist.py

    diff --git a/sagenb/notebook/twist.py b/sagenb/notebook/twist.py
    a b  
    189189        headers.setHeader('location', location)
    190190        headers.setRawHeaders('cache-control', ['no-cache', 'no-store'])
    191191        super(RedirectResponse, self).__init__(303, headers)
    192    
     192
    193193############################
    194194# Create a Sage worksheet from a latex2html'd file
    195195############################
     
    445445
    446446        url = ctx.args['url'][0].strip()
    447447        dir = ''  # we will delete the directory below if it is used
    448         if url != '':
     448        if url != '' and url[0:7] != 'file://':
    449449            # downloading a file from the internet
    450450            filename = tmp_filename()+".sws"
     451
     452        elif url != '' and notebook.interface == 'localhost':
     453            # uploading a file from the user's computer (as a file url)
     454            dir = tmp_dir()
     455            fin = url[7:]
     456            if fin.startswith('localhost'):
     457                fin = fin[9:]
     458            filename = fin.split('/')[-1]
     459            if filename == '':
     460                return HTMLResponse(stream=message("Please specify a worksheet to load.%s" % backlinks))
     461            # Make tmp file in Sage temp directory
     462            filename = os.path.join(dir, filename)
     463            f = file(filename,'wb')
     464            # Then download to that file.
     465            f.write(open(fin).read())
     466            # TODO: Server blocking issues (?!)
     467            f.close()
     468
    451469        else:
    452470            # uploading a file from the user's computer
    453471            dir = tmp_dir()
     
    511529
    512530            return RedirectResponse('/home/'+W.filename())
    513531
    514         if url != '':
     532        if url != '' and url[0:7] != 'file://':
    515533            # We use the downloadPage function which returns a
    516534            # deferred which we are allowed to return to the server.
    517535            # The server waits until the download is finished and then runs
     
    637655
    638656        response = RedirectResponse(worksheet_url + '/datafile?name=%s' % name)
    639657
    640         if url != '':
     658        if url != '' and url[0:7] != 'file://':
    641659            # Here we use twisted's downloadPage function which
    642660            # returns a deferred object.  We return the deferred to
    643661            # the server, and it will wait until the download has
     
    657675            d.addCallback(callback)
    658676            d.addErrback(errback)
    659677            return d
     678
     679        elif url != '' and notebook.interface == 'localhost':
     680            f = file(dest, 'wb')
     681            fin = url[7:]
     682            if fin.startswith('localhost'):
     683                fin = fin[9:]
     684            f.write(open(fin).read())
     685            f.close()
     686            return response
     687
    660688        elif newfield:
    661689            open(dest, 'w').close()
    662690            return response
     
    14861514        def callback(success):
    14871515            """Called when interrupt is successful"""
    14881516            return HTMLResponse(stream = 'success' if success else 'failed')
    1489        
     1517
    14901518        deferred = self.worksheet.interrupt(callback)
    14911519        return deferred
    14921520
     
    16691697
    16701698    def http_GET(self, request):
    16711699        return http.StatusResponse(403, 'This url can only be accessed through a POST request.')
    1672        
     1700
    16731701    def render(self, ctx):
    16741702        """
    16751703        Rendering this resource (1) empties the trash, and (2) returns a
     
    24472475                                               (username, password), '/adduser',
    24482476                                               title=u'New User'))
    24492477        else:
    2450            
     2478
    24512479            return HTMLResponse(stream=template(os.path.join('html', 'settings', 'admin_add_user.html'), **template_dict))
    24522480
    24532481class InvalidPage(resource.Resource):
     
    26692697
    26702698class AdminToplevel(UserToplevel):
    26712699    addSlash = True
    2672    
     2700
    26732701    userchild_home = WorksheetsAdmin
    26742702    userchild_users = ListOfUsers
    26752703    userchild_adduser = AdminAddUser