Mercurial > libervia-web
comparison 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 |
comparison
equal
deleted
inserted
replaced
386:87e1194e55d6 | 387:933bce4cb816 |
---|---|
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from pyjamas.ui.FileUpload import FileUpload | 20 from pyjamas.ui.FileUpload import FileUpload |
21 from pyjamas.ui.FormPanel import FormPanel | |
21 from pyjamas import Window | 22 from pyjamas import Window |
22 from pyjamas import DOM | 23 from pyjamas import DOM |
24 from pyjamas.ui.VerticalPanel import VerticalPanel | |
25 from pyjamas.ui.HTML import HTML | |
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel | |
27 from pyjamas.ui.Button import Button | |
28 from pyjamas.ui.Label import Label | |
23 | 29 |
24 | 30 |
25 class FilterFileUpload(FileUpload): | 31 class FilterFileUpload(FileUpload): |
26 | 32 |
27 def __init__(self, name, max_size, types=None): | 33 def __init__(self, name, max_size, types=None): |
28 """ | 34 """ |
29 @param name: the input element name | 35 @param name: the input element name and id |
30 @param max_size: maximum file size in MB | 36 @param max_size: maximum file size in MB |
31 @param types: allowed types as a list of couples (x, y, z): | 37 @param types: allowed types as a list of couples (x, y, z): |
32 - x: MIME content type e.g. "audio/ogg" | 38 - x: MIME content type e.g. "audio/ogg" |
33 - y: file extension e.g. "*.ogg" | 39 - y: file extension e.g. "*.ogg" |
34 - z: description for the user e.g. "Ogg Vorbis Audio" | 40 - z: description for the user e.g. "Ogg Vorbis Audio" |
57 return False | 63 return False |
58 if size > self.max_size * pow(2, 20): | 64 if size > self.max_size * pow(2, 20): |
59 Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size) | 65 Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size) |
60 return False | 66 return False |
61 return True | 67 return True |
68 | |
69 | |
70 class FileUploadPanel(FormPanel): | |
71 | |
72 def __init__(self, action_url, input_id, max_size, texts=None, close_cb=None): | |
73 """Build a form panel to upload a file. | |
74 @param action_url: the form action URL | |
75 @param input_id: the input element name and id | |
76 @param max_size: maximum file size in MB | |
77 @param texts: a dict to ovewrite the default textual values | |
78 @param close_cb: the close button callback method | |
79 """ | |
80 FormPanel.__init__(self) | |
81 self.texts = {'ok_button': 'Upload file', | |
82 'cancel_button': 'Cancel', | |
83 'body': 'Please select a file.', | |
84 'submitting': '<strong>Submitting, please wait...</strong>', | |
85 'errback': "Your file has been rejected...", | |
86 'body_errback': 'Please select another file.', | |
87 'callback': "Your file has been accepted!"} | |
88 if isinstance(texts, dict): | |
89 self.texts.update(texts) | |
90 self.close_cb = close_cb | |
91 self.setEncoding(FormPanel.ENCODING_MULTIPART) | |
92 self.setMethod(FormPanel.METHOD_POST) | |
93 self.setAction(action_url) | |
94 self.vPanel = VerticalPanel() | |
95 self.message = HTML(self.texts['body']) | |
96 self.vPanel.add(self.message) | |
97 | |
98 hPanel = HorizontalPanel() | |
99 hPanel.setSpacing(5) | |
100 self.file_upload = FilterFileUpload(input_id, max_size) | |
101 self.vPanel.add(self.file_upload) | |
102 | |
103 hPanel.add(Button(self.texts['cancel_button'], getattr(self, "onCloseBtnClick"))) | |
104 self.upload_btn = Button(self.texts['ok_button'], getattr(self, "onSubmitBtnClick")) | |
105 hPanel.add(self.upload_btn) | |
106 | |
107 self.status = Label() | |
108 hPanel.add(self.status) | |
109 | |
110 self.vPanel.add(hPanel) | |
111 | |
112 self.add(self.vPanel) | |
113 self.addFormHandler(self) | |
114 | |
115 def setCloseCb(self, close_cb): | |
116 self.close_cb = close_cb | |
117 | |
118 def onCloseBtnClick(self): | |
119 if self.close_cb: | |
120 self.close_cb() | |
121 else: | |
122 print ("WARNING: no close method defined") | |
123 | |
124 def onSubmitBtnClick(self): | |
125 if not self.file_upload.check(): | |
126 return | |
127 self.message.setHTML(self.texts['submitting']) | |
128 self.upload_btn.setEnabled(False) | |
129 self.submit() | |
130 | |
131 def onSubmit(self, event): | |
132 pass | |
133 | |
134 def onSubmitComplete(self, event): | |
135 result = event.getResults() | |
136 if result != "OK": | |
137 Window.alert(self.texts['errback']) | |
138 self.message.setHTML(self.texts['body_errback']) | |
139 self.upload_btn.setEnabled(True) | |
140 else: | |
141 Window.alert(self.texts['callback']) | |
142 self.close_cb() |