Projet

Général

Profil

0001-do-not-depend-on-auquotidien-to-show-fargo-pick-a-fi.patch

Benjamin Dauvergne, 01 avril 2016 21:04

Télécharger (9,11 ko)

Voir les différences:

Subject: [PATCH] do not depend on auquotidien to show fargo pick a file button

It also changes the behaviour from auquotien so that the button is hidden if is
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                        | 18 ++++++++++-
 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, 82 insertions(+), 3 deletions(-)
wcs/file_validation.py
22 22

  
23 23
from qommon import get_logger
24 24
from qommon.misc import http_get_page, json_loads, http_post_request
25
from quixote import get_publisher, get_request, get_response
25
import qommon.form
26
from quixote import get_publisher, get_request, get_response, redirect, get_session
27
from quixote.directory import Directory
28
from quixote.html import TemplateIO, htmltext
26 29

  
27 30
from wcs.api_utils import get_secret_and_orig, sign_url
28 31

  
......
171 174
    get_response().add_after_job(
172 175
        N_('Sending file %s in portfolio of %s') % (filename, user.display_name),
173 176
        afterjob)
177

  
178

  
179
class FargoDirectory(Directory):
180
    _q_exports = ['pick']
181

  
182
    @property
183
    def fargo_url(self):
184
        return get_publisher().get_site_option('fargo_url')
185

  
186
    def pick(self):
187
        request = get_request()
188
        if 'cancel' in request.form:
189
            get_response().add_javascript(['jquery.js'])
190
            get_response().page_template_key = 'iframe'
191
            r = TemplateIO(html=True)
192
            r += htmltext('<html><body>')
193
            r += htmltext('<script>window.top.document.fargo_close_dialog();</script>')
194
            r += htmltext('</body></html>')
195
            return r.getvalue()
196
        elif 'url' in request.form:
197
            # Download file
198
            # FIXME: handle error cases
199
            url = request.form['url']
200
            document = urllib.urlopen(request.form['url']).read()
201
            scheme, netloc, path, qs, frag = urlparse.urlsplit(url)
202
            path = map(None, path.split('/'))
203
            name = urllib.unquote(path[-1])
204
            download = qommon.form.PicklableUpload(name, content_type='application/pdf')
205
            download.__setstate__({
206
                'data': document,
207
            })
208
            token = get_session().add_tempfile(download)
209
            return self.set_token(token, name)
210
        else:
211
            # Display file picker
212
            frontoffice_url = get_publisher().get_frontoffice_url()
213
            self_url = frontoffice_url
214
            self_url += '/fargo/pick'
215
            return redirect('%spick?pick=%s' % (self.fargo_url, urllib.quote(self_url)))
216

  
217
    def set_token(self, token, title):
218
        get_response().add_javascript(['jquery.js'])
219
        get_response().page_template_key = 'iframe'
220
        r = TemplateIO(html=True)
221
        r += htmltext('<html><body>')
222
        r += htmltext('<script>window.top.document.fargo_set_token(%s, %s);</script>' % (
223
            json.dumps(token), json.dumps(title)))
224
        r += htmltext('</body></html>')
225
        return r.getvalue()
wcs/qommon/form.py
612 612
            self.max_file_size_bytes = FileSizeWidget.parse_file_size(self.max_file_size)
613 613
        self.add(HiddenWidget, 'token')
614 614
        if not self.preview:
615
            hint = ''
616
            if self.allow_portfolio_picking:
617
                root_url = get_publisher().get_root_url()
618
                if file_validation.has_file_validation() and get_request().user and not self.preview:
619
                    get_response().add_javascript(['fargo.js'])
620
                    params = root_url, _('Pick a file from your dropbox'), _('Use file from my dropbox')
621
                    hint += htmltext('<p class="use-file-from-fargo"><span '
622
                                     'data-src="%sfargo/pick" '
623
                                     'data-width="500" '
624
                                     'data-height="400" '
625
                                     'data-title="%s" '
626
                                     'rel="popup">%s</span></p>' % params)
627
            hint += self.hint or ''
615 628
            attrs = {'data-url': get_publisher().get_root_url() + 'tmp-upload'}
616 629
            self.file_type = kwargs.pop('file_type', None)
617 630
            if self.file_type:
......
619 632
            if self.max_file_size_bytes:
620 633
                # this could be used for client size validation of file size
621 634
                attrs['data-max-file-size'] = str(self.max_file_size_bytes)
622
            self.add(FileWidget, 'file', render_br=False, attrs=attrs)
635
            self.add(FileWidget, 'file', hint=hint, render_br=False, attrs=attrs)
623 636
        if self.document_type.get('metadata'):
624 637
            if self.preview:
625 638
                self.add(HiddenWidget, 'validation_url')
......
772 785
            if not valid_file_type:
773 786
                self.error = _('invalid file type')
774 787

  
788
    def render_hint(self, hint):
789
        return ''
790

  
775 791

  
776 792
class PicklableUpload(Upload):
777 793
    def __getstate__(self):
wcs/qommon/static/css/qommon.css
89 89
	margin-right: 2em;
90 90
}
91 91

  
92
p.use-file-from-fargo span {
93
	border-bottom: 1px dotted #999;
94
	cursor: pointer;
95
}
96

  
92 97
div.form .title, form.quixote .title {
93 98
	font-weight: bold;
94 99
}
wcs/qommon/static/js/fargo.js
46 46
         $(base_widget).find('.fileinfo').show();
47 47
         $(base_widget).find('input[type=hidden]').val(token);
48 48
         $(base_widget).find('input[type=file]').hide();
49
         $(base_widget).find('.use-file-from-fargo').hide();
49 50
       }
50 51
       document.fargo_close_dialog();
51 52
    }
wcs/qommon/static/js/qommon.fileupload.js
3 3
        var base_widget = $(this);
4 4
        if ($(base_widget).find('input[type=hidden]').val()) {
5 5
            $(base_widget).find('input[type=file]').hide();
6
            $(base_widget).find('.use-file-from-fargo').hide();
6 7
        } else {
7 8
            $(base_widget).find('.fileinfo').hide();
8 9
        }
......
36 37
                enable();
37 38
                $(base_widget).parents('form').find('input[name=submit]').prop('disabled', false);
38 39
                $(this).hide();
40
                $(base_widget).find('.use-file-from-fargo').hide();
39 41
            },
40 42
            progress: function (e, data) {
41 43
                var progress = parseInt(data.loaded / data.total * 100, 10);
......
46 48
            $(base_widget).find('input[name$="$token"]').val('');
47 49
            $(base_widget).find('.fileinfo').hide();
48 50
            $(base_widget).find('input[type=file]').show();
51
            $(base_widget).find('.use-file-from-fargo').show();
49 52
            return false;
50 53
        });
51 54
        $(this).find('a.change').click(function() {
wcs/root.py
51 51
from myspace import MyspaceDirectory
52 52
from forms.preview import PreviewDirectory
53 53

  
54
from wcs import file_validation
54 55

  
55 56
class CompatibilityDirectory(Directory):
56 57
    _q_exports = ['']
......
197 198
    _q_exports = ['admin', 'backoffice', 'forms', 'login', 'logout', 'saml',
198 199
            'ident', 'register', 'afterjobs', 'themes', 'myspace', 'user', 'roles',
199 200
            'pages', ('tmp-upload', 'tmp_upload'), 'api', '__version__',
200
            'tryauth', 'auth', 'preview', ('reload-top', 'reload_top')]
201
            'tryauth', 'auth', 'preview', ('reload-top', 'reload_top'), 'fargo']
201 202

  
202 203
    api = ApiDirectory()
203 204
    themes = template.ThemesDirectory()
204 205
    myspace = MyspaceDirectory()
205 206
    pages = qommon.pages.PagesDirectory()
207
    fargo = file_validation.FargoDirectory()
206 208

  
207 209
    def tryauth(self):
208 210
        return forms.root.tryauth(get_publisher().get_root_url())
209
-