diff browser_side/menu.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 603fa314880e
children c86d7a8d2c1e
line wrap: on
line diff
--- a/browser_side/menu.py	Wed Feb 26 14:13:15 2014 +0100
+++ b/browser_side/menu.py	Tue Feb 25 07:56:05 2014 +0100
@@ -17,25 +17,19 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import pyjd # this is dummy in pyjs
+import pyjd  # this is dummy in pyjs
 from pyjamas.ui.SimplePanel import SimplePanel
-from pyjamas.ui.VerticalPanel import VerticalPanel
-from pyjamas.ui.HorizontalPanel import HorizontalPanel
-from pyjamas.ui.DialogBox import DialogBox
-from pyjamas.ui.FormPanel import FormPanel
-from pyjamas.ui.FileUpload import FileUpload
 from pyjamas.ui.MenuBar import MenuBar
 from pyjamas.ui.MenuItem import MenuItem
 from pyjamas.ui.ListBox import ListBox
 from pyjamas.ui.Label import Label
 from pyjamas.ui.TextBox import TextBox
-from pyjamas.ui.Button import Button
 from pyjamas.ui.HTML import HTML
 from pyjamas.ui.Frame import Frame
 from pyjamas import Window
 from jid import JID
 from html_tools import html_sanitize
-from file_tools import FilterFileUpload
+from file_tools import FileUploadPanel
 from xmlui import XMLUI
 import panels
 import dialog
@@ -82,63 +76,14 @@
             self.popup.addStyleName('menuLastPopup')
 
 
-class AvatarUpload(FormPanel):
-
-    def __init__(self, close_cb=None):
-        FormPanel.__init__(self)
-        self.close_cb = close_cb
-        self.setEncoding(FormPanel.ENCODING_MULTIPART)
-        self.setMethod(FormPanel.METHOD_POST)
-        self.setAction("upload_avatar")
-        self.vPanel = VerticalPanel()
-        self.message = HTML('Please select an image to show as your avatar...<br>Your picture must be a square and will be resized to 64x64 pixels if necessary')
-        self.vPanel.add(self.message)
-
-        hPanel = HorizontalPanel()
-        hPanel.setSpacing(5)
-        self.file_upload = FilterFileUpload("avatar_path", 2)
-        self.vPanel.add(self.file_upload)
-
-        hPanel.add(Button("Cancel", getattr(self, "onCloseBtnClick")))
-        self.upload_btn = Button("Upload avatar", 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('<strong>Submitting, please wait...</strong>')
-        self.upload_btn.setEnabled(False)
-        self.submit()
-
-    def onSubmit(self, event):
-        pass
-
-    def onSubmitComplete(self, event):
-        result = event.getResults()
-        if result != "OK":
-            Window.alert("Can't open image... did you actually submit an image?")
-            self.message.setHTML('Please select another image file')
-            self.upload_btn.setEnabled(True)
-        else:
-            Window.alert("Your new profile picture has been set!")
-            self.close_cb()
+class AvatarUpload(FileUploadPanel):
+    def __init__(self):
+        texts = {'ok_button': 'Upload avatar',
+                 'body': 'Please select an image to show as your avatar...<br>Your picture must be a square and will be resized to 64x64 pixels if necessary.',
+                 'errback': "Can't open image... did you actually submit an image?",
+                 'body_errback': 'Please select another image file.',
+                 'callback': "Your new profile picture has been set!"}
+        FileUploadPanel.__init__(self, 'upload_avatar', 'avatar_path', 2, texts)
 
 
 class Menu(SimplePanel):
@@ -155,7 +100,7 @@
 
         def addMenu(menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd):
             """ add a menu to menu_dict """
-            print "addMenu:",menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd
+            print "addMenu:", menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd
             try:
                 menu_bar = menus_dict[menu_name]
             except KeyError:
@@ -191,7 +136,7 @@
             item_name_i18n = ' | '.join(path_i18n[1:])
             addMenu(menu_name, menu_name_i18n, item_name_i18n, 'plugins', PluginMenuCmd(self.host, action_id))
 
-        menus_order.append(None) # we add separator
+        menus_order.append(None)  # we add separator
 
         addMenu("Help", _("Help"), _("Social contract"), 'help', MenuCmd(self, "onSocialContract"))
         addMenu("Help", _("Help"), _("About"), 'help', MenuCmd(self, "onAbout"))
@@ -258,7 +203,7 @@
                 Window.alert('You must enter a valid contact JID (like "contact@%s")' % self.host._defaultDomain)
                 _dialog.show()
             else:
-                self.host.bridge.call('addContact', None, edit.getText(), '', _dialog.getSelectedGroups() )
+                self.host.bridge.call('addContact', None, edit.getText(), '', _dialog.getSelectedGroups())
 
         label = Label("New contact identifier (JID):")
         edit.setText('@%s' % self.host._defaultDomain)