From 2331ca62ac3295649c99576ca9873bd38a607309 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 9 Mar 2015 12:42:05 +0100 Subject: [PATCH] add fargo dropbox support to file fields (#6651) --- extra/modules/fargo_ui.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ extra/modules/form.py | 13 ++++++++-- extra/modules/root.py | 4 +++- static/js/fargo.js | 42 +++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 extra/modules/fargo_ui.py create mode 100644 static/js/fargo.js diff --git a/extra/modules/fargo_ui.py b/extra/modules/fargo_ui.py new file mode 100644 index 0000000..f923fb8 --- /dev/null +++ b/extra/modules/fargo_ui.py @@ -0,0 +1,60 @@ +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('') + r += htmltext('') + r += htmltext('') + 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('') + r += htmltext('' % ( + json.dumps(token), json.dumps(title))) + r += htmltext('') + return r.getvalue() diff --git a/extra/modules/form.py b/extra/modules/form.py index 86f8791..dc158d9 100644 --- a/extra/modules/form.py +++ b/extra/modules/form.py @@ -1,25 +1,34 @@ from qommon.form import * class FileWithPreviewAndStrongboxWidget(FileWithPreviewWidget): def render_hint(self, hint): t = CompositeWidget.render_hint(self, hint) + root_url = get_publisher().get_root_url() if get_publisher().has_site_option('strongbox') and get_request().user and not self.preview: get_response().add_javascript(['../../aq/js/strongbox.js']) - root_url = get_publisher().get_root_url() t += htmltext('''

%s

''') % ( root_url, _('Use file from strongbox')) if get_publisher().get_site_option('msp') is not None and not self.preview: get_response().add_javascript(['../../aq/js/msp.js']) - root_url = get_publisher().get_root_url() t += htmltext('''

%s

''') % ( root_url, _('Pick a file on mon.Service-Public.fr'), _('Use file from mon.Service-Public.fr')) + if get_publisher().get_site_option('fargo_url') is not None and not self.preview: + get_response().add_javascript(['../../aq/js/fargo.js']) + t += htmltext('''

%s

''') % ( + root_url, _('Pick a file from your dropbox'), + _('Use file from my dropbox')) return t diff --git a/extra/modules/root.py b/extra/modules/root.py index 858a737..8ccefb6 100644 --- a/extra/modules/root.py +++ b/extra/modules/root.py @@ -49,16 +49,17 @@ from wcs.workflows import Workflow from saml2 import Saml2Directory OldRootDirectory = wcs.root.RootDirectory import qommon.ident.password import qommon.ident.idp import msp_ui +import fargo_ui def category_get_homepage_position(self): if hasattr(self, 'homepage_position') and self.homepage_position: return self.homepage_position if self.url_name == 'consultations': return '2nd' return '1st' Category.get_homepage_position = category_get_homepage_position @@ -753,29 +754,30 @@ class AlternateRootDirectory(OldRootDirectory): _q_exports = ['', 'admin', 'backoffice', 'forms', 'login', 'logout', 'token', 'saml', 'register', 'ident', 'afterjobs', ('informations-editeur', 'informations_editeur'), 'index2', ('announces', 'announces_dir'), 'accessibility', 'contact', 'help', 'myspace', 'services', 'agenda', 'categories', 'user', ('tmp-upload', 'tmp_upload'), 'json', '__version__', 'themes', 'pages', 'payment', 'invoices', 'accesscode', 'roles', - 'msp', 'api', 'code'] + 'msp', 'api', 'code', 'fargo'] admin = admin.AdminRootDirectory() announces_dir = AnnouncesDirectory() register = AlternateRegisterDirectory() login = AlternateLoginDirectory() ident = AlternateIdentDirectory() myspace = MyspaceDirectory() agenda = AgendaDirectory() saml = Saml2Directory() payment = PublicPaymentDirectory() invoices = InvoicesDirectory() msp = msp_ui.MSPDirectory() + fargo = fargo_ui.FargoDirectory() code = wcs.forms.root.TrackingCodesDirectory() def get_substitution_variables(self): d = {} def print_links(fd): fd.write(str(self.links())) d['links'] = print_links return d diff --git a/static/js/fargo.js b/static/js/fargo.js new file mode 100644 index 0000000..2b9cfe3 --- /dev/null +++ b/static/js/fargo.js @@ -0,0 +1,42 @@ + +$(function() { + var iframe = $(''); + var dialog = $("
").append(iframe).appendTo("body").dialog({ + autoOpen: false, + modal: true, + resizable: false, + width: "auto", + height: "auto", + close: function () { + iframe.attr("src", ""); + } + }); + $('p.use-file-from-fargo span').click(function(e) { + e.preventDefault(); + var base_widget = $(this).parents('.file-upload-widget'); + document.fargo_set_token = function (token, title) { + if (token) { + $(base_widget).find('.filename').text(title); + $(base_widget).find('.fileinfo').show(); + $(base_widget).find('input[type=hidden]').val(token); + $(base_widget).find('input[type=file]').hide(); + } + document.fargo_close_dialog(); + } + document.fargo_close_dialog = function () { + document.fargo_set_token = undefined; + dialog.dialog('close'); + } + var src = $(this).data('src'); + var title = $(this).data("title"); + var width = $(this).data("width"); + var height = $(this).data("height"); + iframe.attr({ + width: parseInt(width), + height: parseInt(height), + src: src + }); + dialog.dialog("option", "title", title); + dialog.dialog("open"); + }); +}); -- 1.9.1