Mercurial > libervia-web
comparison src/browser/sat_browser/file_tools.py @ 467:97c72fe4a5f2
browser_side: import fixes:
- moved browser modules in a sat_browser packages, to avoid import conflicts with std lib (e.g. logging), and let pyjsbuild work normaly
- refactored bad import practices: classes are most of time not imported directly, module is imported instead.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 09 Jun 2014 22:15:26 +0200 |
parents | src/browser/file_tools.py@981ed669d3b3 |
children | bbdc5357dc00 |
comparison
equal
deleted
inserted
replaced
466:01880aa8ea2d | 467:97c72fe4a5f2 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Libervia: a Salut à Toi frontend | |
5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org> | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
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/>. | |
19 | |
20 from sat.core.log import getLogger | |
21 log = getLogger(__name__) | |
22 from pyjamas.ui.FileUpload import FileUpload | |
23 from pyjamas.ui.FormPanel import FormPanel | |
24 from pyjamas import Window | |
25 from pyjamas import DOM | |
26 from pyjamas.ui.VerticalPanel import VerticalPanel | |
27 from pyjamas.ui.HTML import HTML | |
28 from pyjamas.ui.HorizontalPanel import HorizontalPanel | |
29 from pyjamas.ui.Button import Button | |
30 from pyjamas.ui.Label import Label | |
31 | |
32 | |
33 class FilterFileUpload(FileUpload): | |
34 | |
35 def __init__(self, name, max_size, types=None): | |
36 """ | |
37 @param name: the input element name and id | |
38 @param max_size: maximum file size in MB | |
39 @param types: allowed types as a list of couples (x, y, z): | |
40 - x: MIME content type e.g. "audio/ogg" | |
41 - y: file extension e.g. "*.ogg" | |
42 - z: description for the user e.g. "Ogg Vorbis Audio" | |
43 If types is None, all file format are accepted | |
44 """ | |
45 FileUpload.__init__(self) | |
46 self.setName(name) | |
47 while DOM.getElementById(name): | |
48 name = "%s_" % name | |
49 self.setID(name) | |
50 self._id = name | |
51 self.max_size = max_size | |
52 self.types = types | |
53 | |
54 def getFileInfo(self): | |
55 from __pyjamas__ import JS | |
56 JS("var file = top.document.getElementById(this._id).files[0]; return [file.size, file.type]") | |
57 | |
58 def check(self): | |
59 if self.getFilename() == "": | |
60 return False | |
61 (size, filetype) = self.getFileInfo() | |
62 if self.types and filetype not in [x for (x, y, z) in self.types]: | |
63 types = [] | |
64 for type_ in ["- %s (%s)" % (z, y) for (x, y, z) in self.types]: | |
65 if type_ not in types: | |
66 types.append(type_) | |
67 Window.alert('This file type is not accepted.\nAccepted file types are:\n\n%s' % "\n".join(types)) | |
68 return False | |
69 if size > self.max_size * pow(2, 20): | |
70 Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size) | |
71 return False | |
72 return True | |
73 | |
74 | |
75 class FileUploadPanel(FormPanel): | |
76 | |
77 def __init__(self, action_url, input_id, max_size, texts=None, close_cb=None): | |
78 """Build a form panel to upload a file. | |
79 @param action_url: the form action URL | |
80 @param input_id: the input element name and id | |
81 @param max_size: maximum file size in MB | |
82 @param texts: a dict to ovewrite the default textual values | |
83 @param close_cb: the close button callback method | |
84 """ | |
85 FormPanel.__init__(self) | |
86 self.texts = {'ok_button': 'Upload file', | |
87 'cancel_button': 'Cancel', | |
88 'body': 'Please select a file.', | |
89 'submitting': '<strong>Submitting, please wait...</strong>', | |
90 'errback': "Your file has been rejected...", | |
91 'body_errback': 'Please select another file.', | |
92 'callback': "Your file has been accepted!"} | |
93 if isinstance(texts, dict): | |
94 self.texts.update(texts) | |
95 self.close_cb = close_cb | |
96 self.setEncoding(FormPanel.ENCODING_MULTIPART) | |
97 self.setMethod(FormPanel.METHOD_POST) | |
98 self.setAction(action_url) | |
99 self.vPanel = VerticalPanel() | |
100 self.message = HTML(self.texts['body']) | |
101 self.vPanel.add(self.message) | |
102 | |
103 hPanel = HorizontalPanel() | |
104 hPanel.setSpacing(5) | |
105 hPanel.setStyleName('marginAuto') | |
106 self.file_upload = FilterFileUpload(input_id, max_size) | |
107 self.vPanel.add(self.file_upload) | |
108 | |
109 self.upload_btn = Button(self.texts['ok_button'], getattr(self, "onSubmitBtnClick")) | |
110 hPanel.add(self.upload_btn) | |
111 hPanel.add(Button(self.texts['cancel_button'], getattr(self, "onCloseBtnClick"))) | |
112 | |
113 self.status = Label() | |
114 hPanel.add(self.status) | |
115 | |
116 self.vPanel.add(hPanel) | |
117 | |
118 self.add(self.vPanel) | |
119 self.addFormHandler(self) | |
120 | |
121 def setCloseCb(self, close_cb): | |
122 self.close_cb = close_cb | |
123 | |
124 def onCloseBtnClick(self): | |
125 if self.close_cb: | |
126 self.close_cb() | |
127 else: | |
128 log.warning("no close method defined") | |
129 | |
130 def onSubmitBtnClick(self): | |
131 if not self.file_upload.check(): | |
132 return | |
133 self.message.setHTML(self.texts['submitting']) | |
134 self.upload_btn.setEnabled(False) | |
135 self.submit() | |
136 | |
137 def onSubmit(self, event): | |
138 pass | |
139 | |
140 def onSubmitComplete(self, event): | |
141 result = event.getResults() | |
142 if result != "OK": | |
143 Window.alert(self.texts['errback']) | |
144 self.message.setHTML(self.texts['body_errback']) | |
145 self.upload_btn.setEnabled(True) | |
146 else: | |
147 Window.alert(self.texts['callback']) | |
148 self.close_cb() |