diff browser_side/file_tools.py @ 387:933bce4cb816

browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
author souliane <souliane@mailoo.org>
date Tue, 25 Feb 2014 07:56:05 +0100
parents 30d03d9f07e4
children f539f6f8ee9c
line wrap: on
line diff
--- a/browser_side/file_tools.py	Wed Feb 26 14:13:15 2014 +0100
+++ b/browser_side/file_tools.py	Tue Feb 25 07:56:05 2014 +0100
@@ -18,15 +18,21 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from pyjamas.ui.FileUpload import FileUpload
+from pyjamas.ui.FormPanel import FormPanel
 from pyjamas import Window
 from pyjamas import DOM
+from pyjamas.ui.VerticalPanel import VerticalPanel
+from pyjamas.ui.HTML import HTML
+from pyjamas.ui.HorizontalPanel import HorizontalPanel
+from pyjamas.ui.Button import Button
+from pyjamas.ui.Label import Label
 
 
 class FilterFileUpload(FileUpload):
 
     def __init__(self, name, max_size, types=None):
         """
-        @param name: the input element name
+        @param name: the input element name and id
         @param max_size: maximum file size in MB
         @param types: allowed types as a list of couples (x, y, z):
         - x: MIME content type e.g. "audio/ogg"
@@ -59,3 +65,78 @@
             Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size)
             return False
         return True
+
+
+class FileUploadPanel(FormPanel):
+
+    def __init__(self, action_url, input_id, max_size, texts=None, close_cb=None):
+        """Build a form panel to upload a file.
+        @param action_url: the form action URL
+        @param input_id: the input element name and id
+        @param max_size: maximum file size in MB
+        @param texts: a dict to ovewrite the default textual values
+        @param close_cb: the close button callback method
+        """
+        FormPanel.__init__(self)
+        self.texts = {'ok_button': 'Upload file',
+                     'cancel_button': 'Cancel',
+                     'body': 'Please select a file.',
+                     'submitting': '<strong>Submitting, please wait...</strong>',
+                     'errback': "Your file has been rejected...",
+                     'body_errback': 'Please select another file.',
+                     'callback': "Your file has been accepted!"}
+        if isinstance(texts, dict):
+            self.texts.update(texts)
+        self.close_cb = close_cb
+        self.setEncoding(FormPanel.ENCODING_MULTIPART)
+        self.setMethod(FormPanel.METHOD_POST)
+        self.setAction(action_url)
+        self.vPanel = VerticalPanel()
+        self.message = HTML(self.texts['body'])
+        self.vPanel.add(self.message)
+
+        hPanel = HorizontalPanel()
+        hPanel.setSpacing(5)
+        self.file_upload = FilterFileUpload(input_id, max_size)
+        self.vPanel.add(self.file_upload)
+
+        hPanel.add(Button(self.texts['cancel_button'], getattr(self, "onCloseBtnClick")))
+        self.upload_btn = Button(self.texts['ok_button'], getattr(self, "onSubmitBtnClick"))
+        hPanel.add(self.upload_btn)
+
+        self.status = Label()
+        hPanel.add(self.status)
+
+        self.vPanel.add(hPanel)
+
+        self.add(self.vPanel)
+        self.addFormHandler(self)
+
+    def setCloseCb(self, close_cb):
+        self.close_cb = close_cb
+
+    def onCloseBtnClick(self):
+        if self.close_cb:
+            self.close_cb()
+        else:
+            print ("WARNING: no close method defined")
+
+    def onSubmitBtnClick(self):
+        if not self.file_upload.check():
+            return
+        self.message.setHTML(self.texts['submitting'])
+        self.upload_btn.setEnabled(False)
+        self.submit()
+
+    def onSubmit(self, event):
+        pass
+
+    def onSubmitComplete(self, event):
+        result = event.getResults()
+        if result != "OK":
+            Window.alert(self.texts['errback'])
+            self.message.setHTML(self.texts['body_errback'])
+            self.upload_btn.setEnabled(True)
+        else:
+            Window.alert(self.texts['callback'])
+            self.close_cb()