61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
import urllib
|
|
import urlparse
|
|
import json
|
|
|
|
from quixote import get_publisher, get_request, redirect, get_response, get_session
|
|
from quixote.directory import Directory
|
|
from quixote.html import TemplateIO, htmltext
|
|
|
|
import qommon.form
|
|
|
|
|
|
class FargoDirectory(Directory):
|
|
_q_exports = ['pick']
|
|
|
|
@property
|
|
def fargo_url(self):
|
|
return get_publisher().get_site_option('fargo_url')
|
|
|
|
def pick (self):
|
|
request = get_request()
|
|
if 'cancel' in request.form:
|
|
get_response().add_javascript(['jquery.js'])
|
|
get_response().page_template_key = 'iframe'
|
|
r = TemplateIO(html=True)
|
|
r += htmltext('<html><body>')
|
|
r += htmltext('<script>window.top.document.fargo_close_dialog();</script>')
|
|
r += htmltext('</body></html>')
|
|
return r.getvalue()
|
|
elif 'url' in request.form:
|
|
# Download file
|
|
# FIXME: handle error cases
|
|
url = request.form['url']
|
|
document = urllib.urlopen(request.form['url']).read()
|
|
scheme, netloc, path, qs, frag = urlparse.urlsplit(url)
|
|
path = map(None, path.split('/'))
|
|
name = urllib.unquote(path[-1])
|
|
download = qommon.form.PicklableUpload(name,
|
|
content_type='application/pdf')
|
|
download.__setstate__({
|
|
'data': document,
|
|
})
|
|
token = get_session().add_tempfile(download)
|
|
return self.set_token(token, name)
|
|
else:
|
|
# Display file picker
|
|
frontoffice_url = get_publisher().get_frontoffice_url()
|
|
self_url = frontoffice_url
|
|
self_url += '/fargo/pick'
|
|
return redirect('%s?pick=%s' % (self.fargo_url,
|
|
urllib.quote(self_url)))
|
|
|
|
def set_token(self, token, title):
|
|
get_response().add_javascript(['jquery.js'])
|
|
get_response().page_template_key = 'iframe'
|
|
r = TemplateIO(html=True)
|
|
r += htmltext('<html><body>')
|
|
r += htmltext('<script>window.top.document.fargo_set_token(%s, %s);</script>' % (
|
|
json.dumps(token), json.dumps(title)))
|
|
r += htmltext('</body></html>')
|
|
return r.getvalue()
|