1- # -*- coding: utf-8 -*-
2- """
3- flask_dropzone
4- ~~~~~~~~~~~~~~
5-
6- :author: Grey Li <[email protected] > 7- :copyright: (c) 2017 by Grey Li.
8- :license: MIT, see LICENSE for more details.
9- """
101import warnings
112
123from flask import Blueprint , current_app , render_template_string , url_for
2617
2718
2819class _Dropzone (object ):
29- @staticmethod
30- def load (js_url = "" , css_url = "" , version = "5.2.0" ):
31- """Load Dropzone resources with given version and init dropzone configuration.
32-
33- .. versionchanged:: 1.4.3
34- Added ``js_url`` and ``css_url`` parameters to pass custom resource URL.
35-
36- .. versionchanged:: 1.4.4
37- This method was deprecated due to inflexible. Now it's divided into three methods:
38- 1. Use ``load_css()`` to load css resources.
39- 2. Use ``load_js()`` to load js resources.
40- 3. Use ``config()`` to configure Dropzone.
41-
42- :param js_url: The JavaScript url for Dropzone.js.
43- :param css_url: The CSS url for Dropzone.js.
44- :param version: The version of Dropzone.js.
45- """
46- warnings .warn ("The method will be removed in 2.0, see docs for more details." )
47- js_filename = "dropzone.min.js"
48- css_filename = "dropzone.min.css"
49-
50- upload_multiple = current_app .config ["DROPZONE_UPLOAD_MULTIPLE" ]
51- parallel_uploads = current_app .config ["DROPZONE_PARALLEL_UPLOADS" ]
52-
53- if upload_multiple in [True , "true" , "True" , 1 ]:
54- upload_multiple = "true"
55- else :
56- upload_multiple = "false"
57-
58- serve_local = current_app .config ["DROPZONE_SERVE_LOCAL" ]
59- size = current_app .config ["DROPZONE_MAX_FILE_SIZE" ]
60- param = current_app .config ["DROPZONE_INPUT_NAME" ]
61- redirect_view = current_app .config ["DROPZONE_REDIRECT_VIEW" ]
62-
63- if redirect_view is not None :
64- redirect_js = """
65- this.on("queuecomplete", function(file) {
66- // Called when all files in the queue finish uploading.
67- window.location = "%s";
68- });""" % url_for (redirect_view )
69- else :
70- redirect_js = ""
71-
72- if not current_app .config ["DROPZONE_ALLOWED_FILE_CUSTOM" ]:
73- allowed_type = allowed_file_extensions [
74- current_app .config ["DROPZONE_ALLOWED_FILE_TYPE" ]
75- ]
76- else :
77- allowed_type = current_app .config ["DROPZONE_ALLOWED_FILE_TYPE" ]
78-
79- max_files = current_app .config ["DROPZONE_MAX_FILES" ]
80- default_message = current_app .config ["DROPZONE_DEFAULT_MESSAGE" ]
81- invalid_file_type = current_app .config ["DROPZONE_INVALID_FILE_TYPE" ]
82- file_too_big = current_app .config ["DROPZONE_FILE_TOO_BIG" ]
83- server_error = current_app .config ["DROPZONE_SERVER_ERROR" ]
84- browser_unsupported = current_app .config ["DROPZONE_BROWSER_UNSUPPORTED" ]
85- max_files_exceeded = current_app .config ["DROPZONE_MAX_FILE_EXCEED" ]
86- cancelUpload = current_app .config ["DROPZONE_CANCEL_UPLOAD" ]
87- removeFile = current_app .config ["DROPZONE_REMOVE_FILE" ]
88- cancelConfirmation = current_app .config ["DROPZONE_CANCEL_CONFIRMATION" ]
89- uploadCanceled = current_app .config ["DROPZONE_UPLOAD_CANCELED" ]
90-
91- timeout = current_app .config ["DROPZONE_TIMEOUT" ]
92- if timeout :
93- timeout_js = "timeout: %d," % timeout
94- else :
95- timeout_js = ""
96-
97- if serve_local :
98- js = '<script src="%s"></script>\n ' % url_for (
99- "dropzone.static" , filename = js_filename
100- )
101- css = '<link rel="stylesheet" href="%s" type="text/css">\n ' % url_for (
102- "dropzone.static" , filename = css_filename
103- )
104- else :
105- js = (
106- '<script src="https://cdn.jsdelivr.net/npm/dropzone@%s/dist/%s"></script>\n '
107- % (version , js_filename )
108- )
109- css = (
110- '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dropzone@%s/dist/min/%s"'
111- ' type="text/css">\n ' % (version , css_filename )
112- )
113-
114- if js_url :
115- js = '<script src="%s"></script>\n ' % js_url
116- if css_url :
117- css = '<link rel="stylesheet" href="%s" type="text/css">\n ' % css_url
118-
119- return Markup (
120- """
121- %s%s<script>
122- Dropzone.options.myDropzone = {
123- init: function() {%s},
124- uploadMultiple: %s,
125- parallelUploads: %d,
126- paramName: "%s", // The name that will be used to transfer the file
127- maxFilesize: %d, // MB
128- acceptedFiles: "%s",
129- maxFiles: %s,
130- dictDefaultMessage: "%s", // message display on drop area
131- dictFallbackMessage: "%s",
132- dictInvalidFileType: "%s",
133- dictFileTooBig: "%s",
134- dictResponseError: "%s",
135- dictMaxFilesExceeded: "%s",
136- dictCancelUpload: "%s",
137- dictRemoveFile: "%s",
138- dictCancelUploadConfirmation: "%s",
139- dictUploadCanceled: "%s",
140- %s // timeout
141- };
142- </script>
143- """
144- % (
145- css ,
146- js ,
147- redirect_js ,
148- upload_multiple ,
149- parallel_uploads ,
150- param ,
151- size ,
152- allowed_type ,
153- max_files ,
154- default_message ,
155- browser_unsupported ,
156- invalid_file_type ,
157- file_too_big ,
158- server_error ,
159- max_files_exceeded ,
160- cancelUpload ,
161- removeFile ,
162- cancelConfirmation ,
163- uploadCanceled ,
164- timeout_js ,
165- )
166- )
16720
16821 @staticmethod
169- def load_css (css_url = None , version = "5.9.2 " ):
22+ def load_css (css_url = None , version = "5.9.3 " ):
17023 """Load Dropzone's css resources with given version.
17124
17225 .. versionadded:: 1.4.4
@@ -192,7 +45,7 @@ def load_css(css_url=None, version="5.9.2"):
19245 return Markup (css )
19346
19447 @staticmethod
195- def load_js (js_url = None , version = "5.9.2 " ):
48+ def load_js (js_url = None , version = "5.9.3 " ):
19649 """Load Dropzone's js resources with given version.
19750
19851 .. versionadded:: 1.4.4
@@ -236,7 +89,8 @@ def config(
23689 :param redirect_url: The URL to redirect when upload complete.
23790 :param custom_init: Custom javascript code in ``init: function() {}``.
23891 :param custom_options: Custom javascript code in ``Dropzone.options.myDropzone = {}``.
239- :param nonce: Pass a nonce value that is newhen embedding the JavaScript code into a Content Security Policy protected web page.
92+ :param nonce: Pass a nonce value that is newhen embedding the JavaScript code into
93+ a Content Security Policy protected web page.
24094 :param id: The id of the dropzone element, it must matches the ``id`` argument passed to
24195 ``dropzone.create()`` if provided.
24296 :param **kwargs: Mirror configuration variable, lowercase and without prefix.
0 commit comments