Ticket #7441: trac_7441_upload.patch
File trac_7441_upload.patch, 6.6 KB (added by , 12 years ago) |
---|
-
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 15 15 <input size="50" type="file" name="file" /> 16 16 </div> 17 17 <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> 19 21 <input size="50" type="text" name="url" /> 20 22 </div> 21 23 <div> -
sagenb/notebook/twist.py
diff --git a/sagenb/notebook/twist.py b/sagenb/notebook/twist.py
a b 443 443 def render(self, ctx): 444 444 backlinks = """ Return to <a href="/upload" title="Upload a worksheet"><strong>Upload File</strong></a>.""" 445 445 446 print 'ctx.args:', ctx.args 446 447 url = ctx.args['url'][0].strip() 447 448 dir = '' # we will delete the directory below if it is used 448 if url != '':449 if url.endswith('.sws'): 449 450 # downloading a file from the internet 450 451 filename = tmp_filename()+".sws" 452 elif url != '': 453 # regular web page with associated worksheet 454 filename = tmp_filename()+".html" 451 455 else: 452 456 # uploading a file from the user's computer 453 457 dir = tmp_dir() … … 466 470 # We make a callback so that we can download a file remotely 467 471 # while allowing the server to still serve requests. 468 472 def callback(result): 473 print '_dd_: in callback, filename:', filename 469 474 470 475 if ctx.args.has_key('name'): 471 476 new_name = ctx.args['name'][0].strip() … … 474 479 475 480 try: 476 481 try: 477 478 482 if filename.endswith('.zip'): 479 483 # Extract all the .sws files from a zip file. 480 484 zip_file = zipfile.ZipFile(filename) … … 487 491 W.set_name("%s - %s" % (new_name, W.name())) 488 492 return RedirectResponse('/') 489 493 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) 490 532 else: 533 print '_dd_: got a filename of', filename 491 534 W = notebook.import_worksheet(filename, self.username) 492 535 493 536 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) 495 539 return HTMLResponse(stream=message(s,'/')) 496 540 finally: 497 541 # Clean up the temporarily uploaded filename. 498 os.unlink(filename)542 # FIXME TODO uncomment this later os.unlink(filename) 499 543 # if a temp directory was created, we delete it now. 500 544 if dir: 501 545 shutil.rmtree(dir) … … 512 556 return RedirectResponse('/home/'+W.filename()) 513 557 514 558 if url != '': 559 print '_dd_ got a url of ', url 515 560 # We use the downloadPage function which returns a 516 561 # deferred which we are allowed to return to the server. 517 562 # The server waits until the download is finished and then runs … … 521 566 d.addCallback(callback) 522 567 def errback(result): 523 568 msg = "There was an error uploading '%s' (please recheck the URL).%s" % (url, backlinks) 569 msg += str(result) 524 570 return HTMLResponse(stream=message(msg,'/')) 525 571 d.addErrback(errback) 526 572 return d