Ticket #7441: trac_7441_upload.patch

File trac_7441_upload.patch, 6.6 KB (added by ddrake, 12 years ago)

add support to "upload a worksheet" page for parsing <link rel="alternate"... links

  • sagenb/data/sage/html/upload.html

    # HG changeset patch
    # User Dan Drake <drake@kaist.edu>
    # Date 1274322804 -32400
    # Node ID 245b0c6c1d0dc988d478e13c1634a13aa1ee7b2c
    # Parent  f1215a3c86a946a2ab847ff08ba6a90674f41d61
    [mq]: trac_7441_upload.patch
    
    diff --git a/sagenb/data/sage/html/upload.html b/sagenb/data/sage/html/upload.html
    a b  
    1515            <input size="50" type="file" name="file" />
    1616        </div>
    1717        <div>
    18             <label for="url">Or enter the URL of a worksheet file on the web:</label>
     18            <label for="url">Or enter the URL of a worksheet file on the
     19              web. This can be a .sws file, a published worksheet, or
     20              any webpage with an associated worksheet:</label>
    1921            <input size="50" type="text" name="url" />
    2022        </div>
    2123        <div>
  • sagenb/notebook/twist.py

    diff --git a/sagenb/notebook/twist.py b/sagenb/notebook/twist.py
    a b  
    443443    def render(self, ctx):
    444444        backlinks = """ Return to <a href="/upload" title="Upload a worksheet"><strong>Upload File</strong></a>."""
    445445
     446        print 'ctx.args:', ctx.args
    446447        url = ctx.args['url'][0].strip()
    447448        dir = ''  # we will delete the directory below if it is used
    448         if url != '':
     449        if url.endswith('.sws'):
    449450            # downloading a file from the internet
    450451            filename = tmp_filename()+".sws"
     452        elif url != '':
     453            # regular web page with associated worksheet
     454            filename = tmp_filename()+".html"
    451455        else:
    452456            # uploading a file from the user's computer
    453457            dir = tmp_dir()
     
    466470        # We make a callback so that we can download a file remotely
    467471        # while allowing the server to still serve requests.
    468472        def callback(result):
     473            print '_dd_: in callback, filename:', filename
    469474
    470475            if ctx.args.has_key('name'):
    471476                new_name = ctx.args['name'][0].strip()
     
    474479
    475480            try:
    476481                try:
    477 
    478482                    if filename.endswith('.zip'):
    479483                        # Extract all the .sws files from a zip file.
    480484                        zip_file = zipfile.ZipFile(filename)
     
    487491                                    W.set_name("%s - %s" % (new_name, W.name()))
    488492                        return RedirectResponse('/')
    489493
     494                    elif filename.endswith('html'):
     495                        # look through source for appropriate link_rel
     496
     497                        from HTMLParser import HTMLParser
     498                        class GetLinkRelWorksheets(HTMLParser):
     499                            def __init__(self):
     500                                HTMLParser.__init__(self)
     501                                self.worksheets = []
     502
     503                            def handle_starttag(self, tag, attrs):
     504                                if (tag == 'link' and
     505                                    ('rel', 'alternate') in attrs and
     506                                    ('type', 'application/sage') in attrs):
     507                                    self.worksheets.append({'title': [_ for _ in attrs if _[0] == 'title'][0][1],
     508                                                              'url': [_ for _ in attrs if _[0] == 'href'][0][1]})
     509
     510                        parser = GetLinkRelWorksheets()
     511
     512                        with open(filename) as f:
     513                            parser.feed(f.read(50000))
     514
     515                        # did we find any worksheets? If so, we
     516                        # (arbitrarily) only download the first one. I
     517                        # want to just call self.render() with an
     518                        # appropriate context, but I don't know how to
     519                        # do that. :(
     520                        # TODO: fix this up, so relative and absolute
     521                        # urls are okay
     522                        # TODO: url encode this and do joining of
     523                        # relative URls properly
     524                        import urllib
     525                        print 'url:', url
     526                        print 'from link rel:', parser.worksheets[0]['url']
     527                        u = url + urllib.quote(urllib.unquote(parser.worksheets[0]['url']))
     528                        print 'u:', u
     529                        ctx.args = {'url': [u], 'name': ['']}
     530                        print 'about to recurse...'
     531                        self.render(ctx)
    490532                    else:
     533                        print '_dd_: got a filename of', filename
    491534                        W = notebook.import_worksheet(filename, self.username)
    492535
    493536                except Exception, msg:
    494                     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
     537                    # TODO: put in <p> tags below
     538                    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!<br />The error was:<br />%s<br /><br />%s' % (msg, backlinks)
    495539                    return HTMLResponse(stream=message(s,'/'))
    496540                finally:
    497541                    # Clean up the temporarily uploaded filename.
    498                     os.unlink(filename)
     542                    # FIXME TODO uncomment this later           os.unlink(filename)
    499543                    # if a temp directory was created, we delete it now.
    500544                    if dir:
    501545                        shutil.rmtree(dir)
     
    512556            return RedirectResponse('/home/'+W.filename())
    513557
    514558        if url != '':
     559            print '_dd_ got a url of ', url
    515560            # We use the downloadPage function which returns a
    516561            # deferred which we are allowed to return to the server.
    517562            # The server waits until the download is finished and then runs
     
    521566            d.addCallback(callback)
    522567            def errback(result):
    523568                msg = "There was an error uploading '%s' (please recheck the URL).%s" % (url, backlinks)
     569                msg += str(result)
    524570                return HTMLResponse(stream=message(msg,'/'))
    525571            d.addErrback(errback)
    526572            return d