[ADD] Compress uploaded image by default
authorFabien Meghazi <fme@openerp.com>
Mon, 8 Sep 2014 13:54:38 +0000 (15:54 +0200)
committerFabien Meghazi <fme@openerp.com>
Tue, 16 Sep 2014 17:55:00 +0000 (19:55 +0200)
Added an opt-out option available from the button's dropdown

addons/website/controllers/main.py
addons/website/static/src/js/website.editor.js
addons/website/static/src/xml/website.editor.xml

index 6891d76..4fc90e4 100644 (file)
@@ -16,6 +16,7 @@ from PIL import Image
 import openerp
 from openerp.addons.web import http
 from openerp.http import request, STATIC_CACHE
+from openerp.tools import image_save_for_web
 
 logger = logging.getLogger(__name__)
 
@@ -314,7 +315,7 @@ class Website(openerp.addons.web.controllers.main.Home):
         return True
 
     @http.route('/website/attach', type='http', auth='user', methods=['POST'], website=True)
-    def attach(self, func, upload=None, url=None):
+    def attach(self, func, upload=None, url=None, disable_optimization=None):
         Attachments = request.registry['ir.attachment']
 
         website_url = message = None
@@ -337,6 +338,9 @@ class Website(openerp.addons.web.controllers.main.Home):
                         u"Image size excessive, uploaded images must be smaller "
                         u"than 42 million pixel")
 
+                if not disable_optimization and image.format in ('PNG', 'JPEG'):
+                    image_data = image_save_for_web(image)
+
                 attachment_id = Attachments.create(request.cr, request.uid, {
                     'name': upload.filename,
                     'datas': image_data.encode('base64'),
index ab6abed..50c939a 100644 (file)
             'click button.filepicker': function () {
                 this.$('input[type=file]').click();
             },
+            'click .js_disable_optimization': function () {
+                this.$('input[name="disable_optimization"]').val('1');
+                this.$('button.filepicker').click();
+            },
             'change input[type=file]': 'file_selection',
             'submit form': 'form_submit',
             'change input.url': "change_input",
index 24d54ec..13b80b1 100644 (file)
                 <div class="well">
                     <div class="form-group pull-left">
                         <input type="file" name="upload" accept="image/*" style="position: absolute; opacity: 0; width: 1px; height: 1px;"/>
-                        <button type="button" class="btn btn-primary filepicker">Upload an image from your computer</button>
+                        <input type="hidden" name="disable_optimization" value=""/>
+                        <div class="btn-group">
+                            <button type="button" class="btn btn-primary filepicker">Upload an image from your computer</button>
+                            <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
+                                <span class="caret"></span>
+                                <span class="sr-only">Alternate Upload</span>
+                            </button>
+                            <ul class="dropdown-menu" role="menu">
+                                <li><a href="#" class="js_disable_optimization">Upload image without optimization</a></li>
+                            </ul>
+                        </div>
                         <button type="button" class="btn hidden wait" disabled="disabled">Uploading...</button>
                     </div>
                     <div>