1
|
import urllib
|
2
|
import urlparse
|
3
|
import json
|
4
|
|
5
|
from quixote import get_publisher, get_request, redirect, get_response, get_session
|
6
|
from quixote.directory import Directory
|
7
|
from quixote.html import TemplateIO, htmltext
|
8
|
|
9
|
import qommon.form
|
10
|
|
11
|
|
12
|
class FargoDirectory(Directory):
|
13
|
_q_exports = ['pick']
|
14
|
|
15
|
@property
|
16
|
def fargo_url(self):
|
17
|
return get_publisher().get_site_option('fargo_url')
|
18
|
|
19
|
def pick (self):
|
20
|
request = get_request()
|
21
|
if 'cancel' in request.form:
|
22
|
get_response().add_javascript(['jquery.js'])
|
23
|
get_response().page_template_key = 'iframe'
|
24
|
r = TemplateIO(html=True)
|
25
|
r += htmltext('<html><body>')
|
26
|
r += htmltext('<script>window.top.document.fargo_close_dialog();</script>')
|
27
|
r += htmltext('</body></html>')
|
28
|
return r.getvalue()
|
29
|
elif 'url' in request.form:
|
30
|
# Download file
|
31
|
# FIXME: handle error cases
|
32
|
url = request.form['url']
|
33
|
document = urllib.urlopen(request.form['url']).read()
|
34
|
scheme, netloc, path, qs, frag = urlparse.urlsplit(url)
|
35
|
path = map(None, path.split('/'))
|
36
|
name = urllib.unquote(path[-1])
|
37
|
download = qommon.form.PicklableUpload(name,
|
38
|
content_type='application/pdf')
|
39
|
download.__setstate__({
|
40
|
'data': document,
|
41
|
})
|
42
|
token = get_session().add_tempfile(download)
|
43
|
return self.set_token(token, name)
|
44
|
else:
|
45
|
# Display file picker
|
46
|
frontoffice_url = get_publisher().get_frontoffice_url()
|
47
|
self_url = frontoffice_url
|
48
|
self_url += '/fargo/pick'
|
49
|
return redirect('%spick?pick=%s' % (self.fargo_url,
|
50
|
urllib.quote(self_url)))
|
51
|
|
52
|
def set_token(self, token, title):
|
53
|
get_response().add_javascript(['jquery.js'])
|
54
|
get_response().page_template_key = 'iframe'
|
55
|
r = TemplateIO(html=True)
|
56
|
r += htmltext('<html><body>')
|
57
|
r += htmltext('<script>window.top.document.fargo_set_token(%s, %s);</script>' % (
|
58
|
json.dumps(token), json.dumps(title)))
|
59
|
r += htmltext('</body></html>')
|
60
|
return r.getvalue()
|