[ADD] Compress uploaded image by default
[odoo/odoo.git] / addons / website / controllers / main.py
index 7d4712a..4fc90e4 100644 (file)
@@ -8,8 +8,6 @@ import xml.etree.ElementTree as ET
 import logging
 import re
 
-from sys import maxint
-
 import werkzeug.utils
 import urllib2
 import werkzeug.wrappers
@@ -17,7 +15,8 @@ from PIL import Image
 
 import openerp
 from openerp.addons.web import http
-from openerp.http import request, Response
+from openerp.http import request, STATIC_CACHE
+from openerp.tools import image_save_for_web
 
 logger = logging.getLogger(__name__)
 
@@ -186,19 +185,18 @@ class Website(openerp.addons.web.controllers.main.Home):
             request.cr, request.uid, 'website', 'theme')
         views = Views.search(request.cr, request.uid, [
             ('inherit_id', '=', theme_template_id),
-            ('application', '=', 'enabled'),
         ], context=request.context)
         Views.write(request.cr, request.uid, views, {
-            'application': 'disabled',
-        }, context=request.context)
+            'active': False,
+        }, context=dict(request.context or {}, active_test=True))
 
         if theme_id:
             module, xml_id = theme_id.split('.')
             _, view_id = imd.get_object_reference(
                 request.cr, request.uid, module, xml_id)
             Views.write(request.cr, request.uid, [view_id], {
-                'application': 'enabled'
-            }, context=request.context)
+                'active': True
+            }, context=dict(request.context or {}, active_test=True))
 
         return request.render('website.themes', {'theme_changed': True})
 
@@ -242,13 +240,13 @@ class Website(openerp.addons.web.controllers.main.Home):
         user_groups = set(user.groups_id)
 
         views = request.registry["ir.ui.view"]\
-            ._views_get(request.cr, request.uid, xml_id, context=request.context)
+            ._views_get(request.cr, request.uid, xml_id, context=dict(request.context or {}, active_test=False))
         done = set()
         result = []
         for v in views:
             if not user_groups.issuperset(v.groups_id):
                 continue
-            if full or (v.application != 'always' and v.inherit_id.id != view_theme_id):
+            if full or (v.customize_show and v.inherit_id.id != view_theme_id):
                 if v.inherit_id not in done:
                     result.append({
                         'name': v.inherit_id.name,
@@ -265,7 +263,7 @@ class Website(openerp.addons.web.controllers.main.Home):
                     'xml_id': v.xml_id,
                     'inherit_id': v.inherit_id.id,
                     'header': False,
-                    'active': v.application in ('always', 'enabled'),
+                    'active': v.active,
                 })
         return result
 
@@ -317,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
@@ -340,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'),
@@ -398,7 +399,8 @@ class Website(openerp.addons.web.controllers.main.Home):
 
     @http.route([
         '/website/image',
-        '/website/image/<model>/<id>/<field>'
+        '/website/image/<model>-<id>-<field>',
+        '/website/image/<model>-<id>-<field>-<int:max_width>x<int:max_height>'
         ], auth="public", website=True)
     def website_image(self, model, id, field, max_width=None, max_height=None):
         """ Fetches the requested field and ensures it does not go above
@@ -416,9 +418,12 @@ class Website(openerp.addons.web.controllers.main.Home):
         all cases.
         """
         try:
+            idsha = id.split('_')
+            id = idsha[0]
             response = werkzeug.wrappers.Response()
             return request.registry['website']._image(
-                request.cr, request.uid, model, id, field, response, max_width, max_height)
+                request.cr, request.uid, model, id, field, response, max_width, max_height,
+                cache=STATIC_CACHE if len(idsha) > 1 else None)
         except Exception:
             logger.exception("Cannot render image field %r of record %s[%s] at size(%s,%s)",
                              field, model, id, max_width, max_height)