annotate browser/sat_browser/file_tools.py @ 1178:92ca86e417e3

pages (events): moved get interests code in prepare_render + use image as thumb_url if it does not already exist
author Goffi <goffi@goffi.org>
date Sat, 04 May 2019 00:01:08 +0200
parents 2af117bfe6cc
children
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
1144
2af117bfe6cc dates update
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
5 # Copyright (C) 2011-2019 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
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 398
diff changeset
20 from sat.core.log import getLogger
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 398
diff changeset
21 log = getLogger(__name__)
481
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
22 from constants import Const as C
648
6d3142b782c3 browser_side: classes reorganisation:
Goffi <goffi@goffi.org>
parents: 494
diff changeset
23 from sat.core.i18n import _, D_
283
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
24 from pyjamas.ui.FileUpload import FileUpload
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
25 from pyjamas.ui.FormPanel import FormPanel
283
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
26 from pyjamas import Window
370
30d03d9f07e4 browser_side: refactorization of the file tools.py:
souliane <souliane@mailoo.org>
parents: 363
diff changeset
27 from pyjamas import DOM
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
28 from pyjamas.ui.VerticalPanel import VerticalPanel
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
29 from pyjamas.ui.HTML import HTML
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
30 from pyjamas.ui.HorizontalPanel import HorizontalPanel
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
31 from pyjamas.ui.Button import Button
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
32 from pyjamas.ui.Label import Label
283
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
33
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
34
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
35 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
36
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
37 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
38 """
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
39 @param name: the input element name and id
283
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
40 @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
41 @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
42 - 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
43 - 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
44 - 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
45 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
46 """
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 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
55
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
56 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
57 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
58 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
59
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
60 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
61 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
62 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
63 (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
64 if self.types and filetype not in [x for (x, y, z) in self.types]:
398
462fc3359ee3 browser_side: radiocol accepts mime type audio/mp3 in addition to audio/mpeg
souliane <souliane@mailoo.org>
parents: 392
diff changeset
65 types = []
462fc3359ee3 browser_side: radiocol accepts mime type audio/mp3 in addition to audio/mpeg
souliane <souliane@mailoo.org>
parents: 392
diff changeset
66 for type_ in ["- %s (%s)" % (z, y) for (x, y, z) in self.types]:
462fc3359ee3 browser_side: radiocol accepts mime type audio/mp3 in addition to audio/mpeg
souliane <souliane@mailoo.org>
parents: 392
diff changeset
67 if type_ not in types:
462fc3359ee3 browser_side: radiocol accepts mime type audio/mp3 in addition to audio/mpeg
souliane <souliane@mailoo.org>
parents: 392
diff changeset
68 types.append(type_)
283
0eba1c4f9c6f browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents: 279
diff changeset
69 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
70 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
71 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
72 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
73 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
74 return True
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
75
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
76
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
77 class FileUploadPanel(FormPanel):
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
78
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
79 def __init__(self, action_url, input_id, max_size, texts=None, close_cb=None):
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
80 """Build a form panel to upload a file.
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
81 @param action_url: the form action URL
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
82 @param input_id: the input element name and id
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
83 @param max_size: maximum file size in MB
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
84 @param texts: a dict to ovewrite the default textual values
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
85 @param close_cb: the close button callback method
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
86 """
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
87 FormPanel.__init__(self)
481
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
88 self.texts = {'ok_button': D_('Upload file'),
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
89 'cancel_button': D_('Cancel'),
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
90 'body': D_('Please select a file.'),
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
91 'submitting': D_('<strong>Submitting, please wait...</strong>'),
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
92 'errback': D_("Your file has been rejected..."),
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
93 'body_errback': D_('Please select another file.'),
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
94 'callback': D_("Your file has been accepted!")}
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
95 if isinstance(texts, dict):
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
96 self.texts.update(texts)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
97 self.close_cb = close_cb
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
98 self.setEncoding(FormPanel.ENCODING_MULTIPART)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
99 self.setMethod(FormPanel.METHOD_POST)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
100 self.setAction(action_url)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
101 self.vPanel = VerticalPanel()
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
102 self.message = HTML(self.texts['body'])
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
103 self.vPanel.add(self.message)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
104
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
105 hPanel = HorizontalPanel()
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
106 hPanel.setSpacing(5)
392
f539f6f8ee9c browser_side: standardize the dialogs size and buttons display:
souliane <souliane@mailoo.org>
parents: 387
diff changeset
107 hPanel.setStyleName('marginAuto')
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
108 self.file_upload = FilterFileUpload(input_id, max_size)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
109 self.vPanel.add(self.file_upload)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
110
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
111 self.upload_btn = Button(self.texts['ok_button'], getattr(self, "onSubmitBtnClick"))
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
112 hPanel.add(self.upload_btn)
392
f539f6f8ee9c browser_side: standardize the dialogs size and buttons display:
souliane <souliane@mailoo.org>
parents: 387
diff changeset
113 hPanel.add(Button(self.texts['cancel_button'], getattr(self, "onCloseBtnClick")))
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
114
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
115 self.status = Label()
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
116 hPanel.add(self.status)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
117
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
118 self.vPanel.add(hPanel)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
119
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
120 self.add(self.vPanel)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
121 self.addFormHandler(self)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
122
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
123 def setCloseCb(self, close_cb):
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
124 self.close_cb = close_cb
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
125
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
126 def onCloseBtnClick(self):
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
127 if self.close_cb:
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
128 self.close_cb()
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
129 else:
439
d52f529a6d42 browser side: use of new log system (first draft):
Goffi <goffi@goffi.org>
parents: 398
diff changeset
130 log.warning("no close method defined")
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
131
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
132 def onSubmitBtnClick(self):
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
133 if not self.file_upload.check():
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
134 return
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
135 self.message.setHTML(self.texts['submitting'])
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
136 self.upload_btn.setEnabled(False)
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
137 self.submit()
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
138
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
139 def onSubmit(self, event):
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
140 pass
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
141
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
142 def onSubmitComplete(self, event):
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
143 result = event.getResults()
481
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
144 if result == C.UPLOAD_KO:
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
145 Window.alert(self.texts['errback'])
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
146 self.message.setHTML(self.texts['body_errback'])
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
147 self.upload_btn.setEnabled(True)
481
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
148 elif result == C.UPLOAD_OK:
387
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
149 Window.alert(self.texts['callback'])
933bce4cb816 browser_side: factorize the code from AvatarUpload to a new class FileUploadPanel
souliane <souliane@mailoo.org>
parents: 370
diff changeset
150 self.close_cb()
481
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
151 else:
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
152 Window.alert(_('Submit error: %s' % result))
bbdc5357dc00 browser and server sides: refactor HTTP request result values + handle "NoReply" error
souliane <souliane@mailoo.org>
parents: 467
diff changeset
153 self.upload_btn.setEnabled(True)
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
154
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
155
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
156 class AvatarUpload(FileUploadPanel):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
157 def __init__(self):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
158 texts = {'ok_button': 'Upload avatar',
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
159 '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.',
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
160 'errback': "Can't open image... did you actually submit an image?",
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
161 'body_errback': 'Please select another image file.',
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
162 'callback': "Your new profile picture has been set!"}
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents: 481
diff changeset
163 FileUploadPanel.__init__(self, 'upload_avatar', 'avatar_path', 2, texts)