annotate browser_side/tools.py @ 362:019e1e706e74

browser_side: display the popup notifying the message's recipient when you edit the blog entry's bubble.
author souliane <souliane@mailoo.org>
date Wed, 19 Feb 2014 16:38:13 +0100
parents f488692c4903
children 187126b63170
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
3
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
4 # Libervia: a Salut à Toi frontend
340
ce5b33f499c5 dates update
Goffi <goffi@goffi.org>
parents: 339
diff changeset
5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
6
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
7 # This program is free software: you can redistribute it and/or modify
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
10 # (at your option) any later version.
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
11
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
12 # This program is distributed in the hope that it will be useful,
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
15 # GNU Affero General Public License for more details.
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
16
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
19
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
20 from pyjamas.ui.DragWidget import DragWidget
283
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
21 from pyjamas.ui.FileUpload import FileUpload
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
22 from pyjamas import Window
246
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
23 from nativedom import NativeDOM
332
6abd099c7007 browser side: sat_frontends.tools.xml is now called xmltools
Goffi <goffi@goffi.org>
parents: 306
diff changeset
24 from sat_frontends.tools import xmltools
347
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
25 import re
246
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
26
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
27 dom = NativeDOM()
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
28
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
29
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
30 def html_sanitize(html):
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
31 """Naive sanitization of HTML"""
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
32 return html.replace('<', '&lt;').replace('>', '&gt;')
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
33
217
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
34
347
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
35 def html_clean(html):
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
36 """
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
37 Remove HTML markup from the given string.
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
38 Copied from nltk.clean_html (http://www.nltk.org/)
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
39
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
40 @param html: the HTML string to be cleaned
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
41 @type html: C{string}
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
42 @rtype: C{string}
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
43 """
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
44
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
45 # First we remove inline JavaScript/CSS:
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
46 cleaned = re.sub(r"(?is)<(script|style).*?>.*?(</\1>)", "", html.strip())
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
47 # Then we remove html comments. This has to be done before removing regular
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
48 # tags since comments can contain '>' characters.
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
49 cleaned = re.sub(r"(?s)<!--(.*?)-->[\n]?", "", cleaned)
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
50 # Next we can remove the remaining tags:
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
51 cleaned = re.sub(r"(?s)<.*?>", " ", cleaned)
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
52 # Finally, we deal with whitespace
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
53 cleaned = re.sub(r"&nbsp;", " ", cleaned)
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
54 cleaned = re.sub(r" ", " ", cleaned)
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
55 cleaned = re.sub(r" ", " ", cleaned)
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
56 return cleaned.strip()
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
57
f1ba38043d78 browser_side: status panel is based on a new class LightTextEditor which uses HTML5 "editablecontent" property
souliane <souliane@mailoo.org>
parents: 340
diff changeset
58
349
f488692c4903 browser_side: LightTextEditor inheritates from BaseTextEditor + display URL in the status
souliane <souliane@mailoo.org>
parents: 347
diff changeset
59 def html_strip(html):
f488692c4903 browser_side: LightTextEditor inheritates from BaseTextEditor + display URL in the status
souliane <souliane@mailoo.org>
parents: 347
diff changeset
60 """Strip leading/trailing white spaces, HTML line breaks and &nbsp; sequences."""
f488692c4903 browser_side: LightTextEditor inheritates from BaseTextEditor + display URL in the status
souliane <souliane@mailoo.org>
parents: 347
diff changeset
61 cleaned = re.sub(r"^(<br/?>|&nbsp;|\s)+", "", html)
f488692c4903 browser_side: LightTextEditor inheritates from BaseTextEditor + display URL in the status
souliane <souliane@mailoo.org>
parents: 347
diff changeset
62 cleaned = re.sub(r"(<br/?>|&nbsp;|\s)+$", "", cleaned)
f488692c4903 browser_side: LightTextEditor inheritates from BaseTextEditor + display URL in the status
souliane <souliane@mailoo.org>
parents: 347
diff changeset
63 return cleaned
f488692c4903 browser_side: LightTextEditor inheritates from BaseTextEditor + display URL in the status
souliane <souliane@mailoo.org>
parents: 347
diff changeset
64
f488692c4903 browser_side: LightTextEditor inheritates from BaseTextEditor + display URL in the status
souliane <souliane@mailoo.org>
parents: 347
diff changeset
65
246
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
66 def inlineRoot(xhtml):
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
67 """ make root element inline """
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
68 doc = dom.parseString(xhtml)
332
6abd099c7007 browser side: sat_frontends.tools.xml is now called xmltools
Goffi <goffi@goffi.org>
parents: 306
diff changeset
69 return xmltools.inlineRoot(doc)
246
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
70
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
71
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
72 def setPresenceStyle(item, state, base_style="contact"):
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
73 """
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
74 @item: any UI element
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
75 @state: a value from ("", "chat", "away", "dnd", "xa")
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
76 """
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
77 if not hasattr(item, 'presence_style'):
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
78 item.presence_style = None
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
79 style = '%s-%s' % (base_style, state or 'connected')
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
80 if style == item.presence_style:
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
81 return
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
82 if item.presence_style is not None:
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
83 item.removeStyleName(item.presence_style)
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
84 item.addStyleName(style)
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
85 item.presence_style = style
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
86
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
87
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
88 class DragLabel(DragWidget):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
89
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
90 def __init__(self, text, _type):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
91 DragWidget.__init__(self)
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
92 self._text = text
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
93 self._type = _type
217
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
94
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
95 def onDragStart(self, event):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
96 dt = event.dataTransfer
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
97 dt.setData('text/plain', "%s\n%s" % (self._text, self._type))
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
98 dt.setDragImage(self.getElement(), 15, 15)
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
99
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
100
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
101 class LiberviaDragWidget(DragLabel):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
102 """ A DragLabel which keep the widget being dragged as class value """
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
103 current = None # widget currently dragged
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
104
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
105 def __init__(self, text, _type, widget):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
106 DragLabel.__init__(self, text, _type)
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
107 self.widget = widget
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
108
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
109 def onDragStart(self, event):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
110 LiberviaDragWidget.current = self.widget
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
111 DragLabel.onDragStart(self, event)
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
112
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
113 def onDragEnd(self, event):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
114 LiberviaDragWidget.current = None
283
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
115
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
116
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
117 class FilterFileUpload(FileUpload):
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
118
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
119 def __init__(self, name, max_size, types=None):
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
120 """
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
121 @param name: the input element name
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
122 @param max_size: maximum file size in MB
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
123 @param types: allowed types as a list of couples (x, y, z):
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
124 - x: MIME content type e.g. "audio/ogg"
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
125 - y: file extension e.g. "*.ogg"
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
126 - z: description for the user e.g. "Ogg Vorbis Audio"
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
127 If types is None, all file format are accepted
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
128 """
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
129 FileUpload.__init__(self)
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
130 self.setName(name)
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
131 from pyjamas import DOM
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
132 while DOM.getElementById(name):
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
133 name = "%s_" % name
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
134 self.setID(name)
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
135 self._id = name
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
136 self.max_size = max_size
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
137 self.types = types
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
138
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
139 def getFileInfo(self):
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
140 from __pyjamas__ import JS
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
141 JS("var file = top.document.getElementById(this._id).files[0]; return [file.size, file.type]")
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
142
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
143 def check(self):
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
144 if self.getFilename() == "":
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
145 return False
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
146 (size, filetype) = self.getFileInfo()
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
147 if self.types and filetype not in [x for (x, y, z) in self.types]:
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
148 types = ["- %s (%s)" % (z, y) for (x, y, z) in self.types]
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
149 Window.alert('This file type is not accepted.\nAccepted file types are:\n\n%s' % "\n".join(types))
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
150 return False
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
151 if size > self.max_size * pow(2, 20):
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
152 Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size)
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
153 return False
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
154 return True