From 63d026dc0f0875badba7cbd76dbaa34932b74e09 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 1 Apr 2016 17:07:28 +0200 Subject: [PATCH] do not depend on auquotidien to show fargo pick a file button (#10515) It also changes the behaviour from auquotidien so that the button is hidden if the user is not connected. Dropbox picking link is placed inside the hint of the upload widgeet, hint of the FileWithPreviewWidget is also moved there. qommon.fileupload.js and fargo.js are modified to hide the link when the field is filled with a file. --- wcs/file_validation.py | 54 ++++++++++++++++++++++++++++++- wcs/qommon/form.py | 22 ++++++++++++- wcs/qommon/static/css/qommon.css | 5 +++ wcs/qommon/static/js/fargo.js | 1 + wcs/qommon/static/js/qommon.fileupload.js | 3 ++ wcs/root.py | 4 ++- 6 files changed, 86 insertions(+), 3 deletions(-) diff --git a/wcs/file_validation.py b/wcs/file_validation.py index 5d9bb87..6a047b0 100644 --- a/wcs/file_validation.py +++ b/wcs/file_validation.py @@ -22,7 +22,10 @@ import base64 from qommon import get_logger from qommon.misc import http_get_page, json_loads, http_post_request, ConnectionError -from quixote import get_publisher, get_request, get_response +import qommon.form +from quixote import get_publisher, get_request, get_response, redirect, get_session +from quixote.directory import Directory +from quixote.html import TemplateIO, htmltext from wcs.api_utils import get_secret_and_orig, sign_url @@ -192,3 +195,52 @@ def push_document(user, filename, stream): get_response().add_after_job( N_('Sending file %s in portfolio of %s') % (filename, user.display_name), afterjob) + + +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('%spick?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/wcs/qommon/form.py b/wcs/qommon/form.py index e20b7f6..7696507 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -614,6 +614,23 @@ class FileWithPreviewWidget(CompositeWidget): self.max_file_size_bytes = FileSizeWidget.parse_file_size(self.max_file_size) self.add(HiddenWidget, 'token') if not self.readonly: + hint = '' + if self.allow_portfolio_picking: + root_url = get_publisher().get_root_url() + if (file_validation.has_file_validation() + and get_request().user + and not self.readonly): + get_response().add_javascript(['fargo.js']) + params = (root_url, + _('Pick a file from your dropbox'), + _('Use file from my dropbox')) + hint += htmltext('

%s

' % params) + hint += self.hint or '' attrs = {'data-url': get_publisher().get_root_url() + 'tmp-upload'} self.file_type = kwargs.pop('file_type', None) if self.file_type: @@ -621,7 +638,7 @@ class FileWithPreviewWidget(CompositeWidget): if self.max_file_size_bytes: # this could be used for client size validation of file size attrs['data-max-file-size'] = str(self.max_file_size_bytes) - self.add(FileWidget, 'file', render_br=False, attrs=attrs) + self.add(FileWidget, 'file', hint=hint, render_br=False, attrs=attrs) if self.document_type.get('metadata'): if self.readonly: self.add(HiddenWidget, 'validation_url') @@ -786,6 +803,9 @@ class FileWithPreviewWidget(CompositeWidget): if not valid_file_type: self.error = _('invalid file type') + def render_hint(self, hint): + return '' + class PicklableUpload(Upload): def __getstate__(self): diff --git a/wcs/qommon/static/css/qommon.css b/wcs/qommon/static/css/qommon.css index d66b32c..119eb28 100644 --- a/wcs/qommon/static/css/qommon.css +++ b/wcs/qommon/static/css/qommon.css @@ -97,6 +97,11 @@ div.FileWithPreviewWidget .validation { margin-right: 2em; } +p.use-file-from-fargo span { + border-bottom: 1px dotted #999; + cursor: pointer; +} + div.form .title, form.quixote .title { font-weight: bold; } diff --git a/wcs/qommon/static/js/fargo.js b/wcs/qommon/static/js/fargo.js index 75443de..42fcfb0 100644 --- a/wcs/qommon/static/js/fargo.js +++ b/wcs/qommon/static/js/fargo.js @@ -46,6 +46,7 @@ $(function() { $(base_widget).find('.fileinfo').show(); $(base_widget).find('input[type=hidden]').val(token); $(base_widget).find('input[type=file]').hide(); + $(base_widget).find('.use-file-from-fargo').hide(); } document.fargo_close_dialog(); } diff --git a/wcs/qommon/static/js/qommon.fileupload.js b/wcs/qommon/static/js/qommon.fileupload.js index 3e25d28..78b81af 100644 --- a/wcs/qommon/static/js/qommon.fileupload.js +++ b/wcs/qommon/static/js/qommon.fileupload.js @@ -3,6 +3,7 @@ $(function() { var base_widget = $(this); if ($(base_widget).find('input[type=hidden]').val()) { $(base_widget).find('input[type=file]').hide(); + $(base_widget).find('.use-file-from-fargo').hide(); } else { $(base_widget).find('.fileinfo').hide(); } @@ -36,6 +37,7 @@ $(function() { enable(); $(base_widget).parents('form').find('input[name=submit]').prop('disabled', false); $(this).hide(); + $(base_widget).find('.use-file-from-fargo').hide(); }, progress: function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); @@ -46,6 +48,7 @@ $(function() { $(base_widget).find('input[name$="$token"]').val(''); $(base_widget).find('.fileinfo').hide(); $(base_widget).find('input[type=file]').show(); + $(base_widget).find('.use-file-from-fargo').show(); return false; }); $(this).find('a.change').click(function() { diff --git a/wcs/root.py b/wcs/root.py index b4f164e..72d702e 100644 --- a/wcs/root.py +++ b/wcs/root.py @@ -51,6 +51,7 @@ from wcs.api import ApiDirectory from myspace import MyspaceDirectory from forms.preview import PreviewDirectory +from wcs import file_validation class CompatibilityDirectory(Directory): _q_exports = [''] @@ -197,12 +198,13 @@ class RootDirectory(Directory): _q_exports = ['admin', 'backoffice', 'forms', 'login', 'logout', 'saml', 'ident', 'register', 'afterjobs', 'themes', 'myspace', 'user', 'roles', 'pages', ('tmp-upload', 'tmp_upload'), 'api', '__version__', - 'tryauth', 'auth', 'preview', ('reload-top', 'reload_top')] + 'tryauth', 'auth', 'preview', ('reload-top', 'reload_top'), 'fargo'] api = ApiDirectory() themes = template.ThemesDirectory() myspace = MyspaceDirectory() pages = qommon.pages.PagesDirectory() + fargo = file_validation.FargoDirectory() def tryauth(self): return forms.root.tryauth(get_publisher().get_root_url()) -- 2.1.4