Merge lp:~fabien-morin/unifield-web/fm-us-1965 into lp:unifield-web

Proposed by jftempo
Status: Merged
Merge reported by: jftempo
Merged at revision: not available
Proposed branch: lp:~fabien-morin/unifield-web/fm-us-1965
Merge into: lp:unifield-web
Diff against target: 348 lines (+123/-24)
10 files modified
addons/openerp/controllers/attachment.py (+11/-6)
addons/openerp/controllers/form.py (+3/-0)
addons/openerp/po/javascript/fr.po (+4/-0)
addons/openerp/static/javascript/binary.js (+9/-6)
addons/openerp/static/javascript/form.js (+66/-1)
addons/openerp/utils/tools.py (+3/-0)
addons/openerp/widgets/form/_binary.py (+4/-2)
addons/openerp/widgets/form/templates/binary.mako (+17/-7)
addons/openerp/widgets/sidebar.py (+4/-1)
addons/openerp/widgets/templates/sidebar.mako (+2/-1)
To merge this branch: bzr merge lp:~fabien-morin/unifield-web/fm-us-1965
Reviewer Review Type Date Requested Status
UniField Dev Team Pending
Review via email: mp+315894@code.launchpad.net
To post a comment you must log in.
4835. By Fabien MORIN

US-1965 [FIX] check no save are done if a attachment is bigger than the max
allowed size. Before this fix, it was possible to select a bigger file than the
max allowed, get the warning message that this file is too big, and then click
on save.
This actions were resulting on sending the datas to the server that was refusing
it because they were too big. Now if the size of the file is too big, the
document is not saved, data not sent to the server.

4836. By Fabien MORIN

US-1965 [MERGE] with latest trunk

4837. By Fabien MORIN

US-1965 [FIX] check a attachment is set before to check the size of it

Revision history for this message
jftempo (jfb-tempo-consulting) :
4838. By Fabien MORIN

US-1965 [IMP] remove unused variables added by mistake

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'addons/openerp/controllers/attachment.py'
2--- addons/openerp/controllers/attachment.py 2017-01-02 14:59:00 +0000
3+++ addons/openerp/controllers/attachment.py 2017-02-09 07:41:48 +0000
4@@ -39,6 +39,7 @@
5
6 if id:
7 ctx = dict(rpc.session.context)
8+ ctx['from_web_interface'] = True
9
10 action = dict(
11 rpc.RPCProxy('ir.attachment').action_get(ctx),
12@@ -75,13 +76,17 @@
13 ctx = dict(rpc.session.context,
14 default_res_model=params.model, default_res_id=params.id,
15 active_id=False, active_ids=[])
16+ ctx['from_web_interface'] = True
17
18- attachment_id = rpc.RPCProxy('ir.attachment').create({
19- 'name': datas.filename,
20- 'datas_fname': datas.filename,
21- 'datas': base64.encodestring(datas.file.read()),
22- }, ctx)
23- return {'id': attachment_id, 'name': datas.filename}
24+ try:
25+ attachment_id = rpc.RPCProxy('ir.attachment').create({
26+ 'name': datas.filename,
27+ 'datas_fname': datas.filename,
28+ 'datas': base64.encodestring(datas.file.read()),
29+ }, ctx)
30+ return {'id': attachment_id, 'name': datas.filename}
31+ except Exception, e:
32+ return {'error': ustr(e)}
33
34 @expose('json', methods=('POST',))
35 def remove(self, id=False, **kw):
36
37=== modified file 'addons/openerp/controllers/form.py'
38--- addons/openerp/controllers/form.py 2017-01-02 14:59:00 +0000
39+++ addons/openerp/controllers/form.py 2017-02-09 07:41:48 +0000
40@@ -491,6 +491,7 @@
41 params.count += 1
42 else:
43 ctx = utils.context_with_concurrency_info(params.context, params.concurrency_info)
44+ ctx['from_web_interface'] = True
45 if params.button and params.button.name:
46 ctx.update({'button': params.button.name})
47
48@@ -784,6 +785,7 @@
49
50 proxy = rpc.RPCProxy(params.model)
51 ctx = utils.context_with_concurrency_info(params.context, params.concurrency_info)
52+ ctx['from_web_interface'] = True
53
54 if params.fname:
55 proxy.write([params.id], {params.field: False, params.fname: False}, ctx)
56@@ -855,6 +857,7 @@
57 params, data = TinyDict.split(kw)
58 if params.get('_terp_save_current_id'):
59 ctx = dict((params.context or {}), **rpc.session.context)
60+ ctx['from_web_interface'] = True
61 if params.id:
62 rpc.RPCProxy(params.model).write([params.id], data, ctx)
63 else:
64
65=== modified file 'addons/openerp/po/javascript/fr.po'
66--- addons/openerp/po/javascript/fr.po 2017-01-02 14:59:00 +0000
67+++ addons/openerp/po/javascript/fr.po 2017-02-09 07:41:48 +0000
68@@ -110,3 +110,7 @@
69 #: static/javascript/openerp/openerp.ui.tips.js:52
70 msgid "Field"
71 msgstr "Champ"
72+
73+#: static/javascript/form.js
74+msgid "You cannot upload files bigger than %(max_size)sMB, current size is %(size)sMB"
75+msgstr "Vous ne pouvez pas envoyer des fichiers plus gros que %(max_size)sMB, la taille actuelle est de %(size)sMB"
76
77=== modified file 'addons/openerp/static/javascript/binary.js'
78--- addons/openerp/static/javascript/binary.js 2017-01-02 14:59:00 +0000
79+++ addons/openerp/static/javascript/binary.js 2017-02-09 07:41:48 +0000
80@@ -19,6 +19,7 @@
81 //
82 ////////////////////////////////////////////////////////////////////////////////
83
84+
85 function save_binary_data(src, filename) {
86
87 var name = openobject.dom.get(src) ? openobject.dom.get(src).name : src;
88@@ -71,24 +72,26 @@
89 }
90
91 function set_binary_filename(src, filename) {
92-
93 var $src = jQuery(src);
94-
95+
96+ // Check attachment size is not bigger than max attachment size
97+ // refuse it if bigger.
98+ if (check_attachment_size(src) !== true) {
99+ return false
100+ }
101+
102 var name = $src.attr('name');
103-
104 var prefix = name.split('/'); prefix.pop();
105 var prefix = prefix.join('/'); prefix = prefix ? prefix + '/' : '';
106-
107 var target = getElement(prefix + filename);
108 var fname = $src.val() || '';
109-
110+
111 if (/Windows NT/.test(window.navigator.userAgent)) {
112 fname = fname.split('\\'); fname = fname.pop();
113 }
114 else {
115 fname = fname.split('/'); fname = fname.pop();
116 }
117-
118 if (target) {
119 target.value = fname;
120 }
121
122=== modified file 'addons/openerp/static/javascript/form.js'
123--- addons/openerp/static/javascript/form.js 2017-01-02 14:59:00 +0000
124+++ addons/openerp/static/javascript/form.js 2017-02-09 07:41:48 +0000
125@@ -230,6 +230,34 @@
126 return result;
127 }
128
129+function validate_binary_size(form){
130+
131+ if (typeof form == 'string') {
132+ form = jQuery('#' + form).get(0);
133+ }
134+
135+ if (!form) {
136+ return true;
137+ }
138+
139+ var elements = MochiKit.Base.filter(function(el){
140+ return !el.disabled && el.id && el.name && el.id.indexOf('_terp_listfields/') == -1 && hasElementClass(el, 'binary');
141+ }, form.elements);
142+
143+ var result = true;
144+
145+ for (var i = 0; i < elements.length; i++) {
146+ var elem = elements[i];
147+ var kind = jQuery(elem).attr('kind');
148+
149+ if (kind == 'binary') {
150+ return check_attachment_size(elem);
151+ }
152+ }
153+ return result;
154+}
155+
156+
157 function error_display(msg) {
158 var error = jQuery("<table>",{'width': '100%', 'height': '100%'}
159 ).append(
160@@ -298,6 +326,11 @@
161 return;
162 }
163
164+ // check there is no binary data exceding the maximum size
165+ if (/\/save(\?|\/)?/.test(action) && !validate_binary_size($form[0])) {
166+ return;
167+ }
168+
169 // Cant use $form.attr due to http://dev.jquery.com/ticket/3113 as there is a form with a field called
170 // action when creating an activity
171 $form[0].setAttribute('action', action);
172@@ -1295,6 +1328,26 @@
173 return false;
174 }
175
176+function check_attachment_size(obj) {
177+ var $datas = jQuery(obj);
178+ var $max_size = $datas.attr('max-size');
179+ if (typeof $max_size !== "undefined" && obj.files && obj.files[0]) {
180+ var $file_size = obj.files[0].size;
181+ if ($file_size > $max_size) {
182+ var $mb_size = $file_size/1024/1024;
183+ $mb_size = parseFloat($mb_size).toFixed( 2 );
184+ var $mb_max_size = $max_size/1024/1024;
185+ $mb_max_size = parseFloat($mb_max_size).toFixed( 2 );
186+ var msg = _('You cannot upload files bigger than %(max_size)sMB, current size is %(size)sMB');
187+ msg = msg.replace('%(size)s', $mb_size);
188+ msg = msg.replace('%(max_size)s', $mb_max_size);
189+ return error_display(msg);
190+ };
191+ };
192+ return true;
193+}
194+
195+
196 /**
197 * @event form submission
198 *
199@@ -1303,6 +1356,13 @@
200 * Creates a new line in #attachments if the creation succeeds.
201 */
202 function createAttachment(){
203+
204+ // Check attachment size is not bigger than max attachment size
205+ // refuse it if bigger.
206+ if (check_attachment_size(this.children.datas) !== true) {
207+ return false
208+ }
209+
210 var $form = jQuery(this);
211 if(!jQuery(idSelector('_terp_id')).val() || jQuery(idSelector('_terp_id')).val() == 'False') {
212 return error_display(_('No record selected ! You can only attach to existing record.'));
213@@ -1315,7 +1375,12 @@
214 $form.ajaxSubmit({
215 dataType: 'json',
216 data: {'requested_with': 'XMLHttpRequest'},
217- success: function(data){
218+ type: 'POST',
219+ success: function(data) {
220+ if ('error' in data) {
221+ // display error message from server
222+ return error_display(data['error']);
223+ }
224 var $attachment_line = jQuery('<li>', {
225 'id': 'attachment_item_' + data['id'],
226 'data-id': data['id']
227
228=== modified file 'addons/openerp/utils/tools.py'
229--- addons/openerp/utils/tools.py 2016-08-31 07:37:14 +0000
230+++ addons/openerp/utils/tools.py 2017-02-09 07:41:48 +0000
231@@ -230,6 +230,9 @@
232 concurrency_info = [concurrency_info]
233 return dict(ctx, __last_update=dict(concurrency_info))
234
235+def get_max_attachment_size():
236+ attachment = rpc.RPCProxy('ir.attachment')
237+ return attachment.get_attachment_max_size()
238
239 class TempFileName(str):
240 '''A string representing a temporary file name that will be deleted when object is deleted'''
241
242=== modified file 'addons/openerp/widgets/form/_binary.py'
243--- addons/openerp/widgets/form/_binary.py 2017-01-02 14:59:00 +0000
244+++ addons/openerp/widgets/form/_binary.py 2017-02-09 07:41:48 +0000
245@@ -7,7 +7,7 @@
246 # Developed by OpenERP (http://openerp.com) and Axelor (http://axelor.com).
247 #
248 # The OpenERP web client is distributed under the "OpenERP Public License".
249-# It's based on Mozilla Public License Version (MPL) 1.1 with following
250+# It's based on Mozilla Public License Version (MPL) 1.1 with following
251 # restrictions:
252 #
253 # - All names, links and logos of OpenERP must be kept as in original
254@@ -40,7 +40,8 @@
255
256 class Binary(TinyInputWidget):
257 template = "/openerp/widgets/form/templates/binary.mako"
258- params = ["name", "text", "readonly", "filename", "bin_data", 'value_bin_size', 'ctx', 'accept']
259+ params = ["name", "text", "readonly", "filename", "bin_data",
260+ 'value_bin_size', 'ctx', 'accept', 'max_attachment_size']
261
262 text = None
263 file_upload = True
264@@ -53,6 +54,7 @@
265 # if bin_size was in context when reading the binary field, then the field's value is actually the binary
266 # field's content size
267 self.value_bin_size = getattr(self, 'context', {}).get('bin_size', False)
268+ self.max_attachment_size = utils.get_max_attachment_size()
269 self.ctx = getattr(self, 'context', {}).get('from', False)
270 self.accept = attrs.get('accept')
271
272
273=== modified file 'addons/openerp/widgets/form/templates/binary.mako'
274--- addons/openerp/widgets/form/templates/binary.mako 2017-01-02 14:59:00 +0000
275+++ addons/openerp/widgets/form/templates/binary.mako 2017-02-09 07:41:48 +0000
276@@ -9,13 +9,23 @@
277 value="${bin_data}"/>
278 % endif
279 <div id="${name}_binary_add" style="display: none;">
280- % if editable and not readonly:
281- <input ${py.attrs(attrs)}
282- accept="${accept}"
283- type="file"
284- class="${css_class}"
285- kind="${kind}"
286- disabled="disabled"
287+ % if editable and not readonly and model != 'ir.attachment':
288+ <input ${py.attrs(attrs)}
289+ accept="${accept}"
290+ type="file"
291+ class="${css_class}"
292+ kind="${kind}"
293+ disabled="disabled"
294+ id="${name}"
295+ name="${name}"/>
296+ % elif editable and not readonly and model == 'ir.attachment':
297+ <input ${py.attrs(attrs)}
298+ accept="${accept}"
299+ type="file"
300+ class="${css_class}"
301+ kind="${kind}"
302+ disabled="disabled"
303+ max-size="${max_attachment_size}"
304 id="${name}"
305 name="${name}"/>
306 % endif
307
308=== modified file 'addons/openerp/widgets/sidebar.py'
309--- addons/openerp/widgets/sidebar.py 2014-04-17 14:01:17 +0000
310+++ addons/openerp/widgets/sidebar.py 2017-02-09 07:41:48 +0000
311@@ -20,12 +20,14 @@
312 ###############################################################################
313 from openerp.utils import rpc
314 from openerp.widgets import TinyWidget
315+from openerp import utils
316
317
318 class Sidebar(TinyWidget):
319
320 template = "/openerp/widgets/templates/sidebar.mako"
321- params = ['reports', 'actions', 'relates', 'attachments', 'sub_menu', 'view_type', 'model', 'id', 'ctx']
322+ params = ['reports', 'actions', 'relates', 'attachments', 'sub_menu',
323+ 'view_type', 'model', 'id', 'ctx', 'max_attachment_size']
324
325 def add_remote_action_values(self, action_type, current_actions):
326 actions = rpc.RPCProxy('ir.values').get(
327@@ -47,6 +49,7 @@
328 self.reports = toolbar.get('print', [])
329 self.actions = toolbar.get('action', [])
330 self.relates = toolbar.get('relate', [])
331+ self.max_attachment_size = utils.get_max_attachment_size()
332 self.attachments = []
333 self.sub_menu = None
334
335
336=== modified file 'addons/openerp/widgets/templates/sidebar.mako'
337--- addons/openerp/widgets/templates/sidebar.mako 2013-01-09 10:29:26 +0000
338+++ addons/openerp/widgets/templates/sidebar.mako 2017-02-09 07:41:48 +0000
339@@ -73,7 +73,8 @@
340 enctype="multipart/form-data">
341 <label for="sidebar_attachments_datas">${_("File")}:</label>
342 <input type="file" id="sidebar_attachments_datas" class="binary"
343- name="datas" kind="binary" size="5"/>
344+ name="datas" kind="binary" size="5"
345+ max-size="${max_attachment_size}"/>
346 </form>
347 </div>
348 % endif

Subscribers

People subscribed via source and target branches