Merge remote-tracking branch 'odoo/master' into master-less-support-in-bundles-fme
[odoo/odoo.git] / addons / website / controllers / main.py
index a002abc..624daaf 100644 (file)
@@ -1,5 +1,7 @@
 # -*- coding: utf-8 -*-
 import cStringIO
+import datetime
+from itertools import islice
 import json
 import logging
 import re
@@ -11,8 +13,6 @@ import werkzeug.wrappers
 from PIL import Image
 
 import openerp
-from openerp.osv import fields
-from openerp.addons.website.models import website
 from openerp.addons.web import http
 from openerp.http import request, Response
 
@@ -21,29 +21,34 @@ logger = logging.getLogger(__name__)
 # Completely arbitrary limits
 MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT = IMAGE_LIMITS = (1024, 768)
 LOC_PER_SITEMAP = 45000
+SITEMAP_CACHE_TIME = datetime.timedelta(hours=12)
 
 class Website(openerp.addons.web.controllers.main.Home):
     #------------------------------------------------------
     # View
     #------------------------------------------------------
-    @http.route('/', type='http', auth="public", website=True, multilang=True)
+    @http.route('/', type='http', auth="public", website=True)
     def index(self, **kw):
+        page = 'homepage'
         try:
             main_menu = request.registry['ir.model.data'].get_object(request.cr, request.uid, 'website', 'main_menu')
-            first_menu = main_menu.child_id and main_menu.child_id[0]
-            # Dont 302 loop on /
-            if first_menu and not ((first_menu.url == '/') or first_menu.url.startswith('/#') or first_menu.url.startswith('/?')):
-                return request.redirect(first_menu.url)
-        except:
+        except Exception:
             pass
-        return self.page("website.homepage")
-
-    @http.route(website=True, auth="public", multilang=True)
+        else:
+            first_menu = main_menu.child_id and main_menu.child_id[0]
+            if first_menu:
+                if not (first_menu.url.startswith(('/page/', '/?', '/#')) or (first_menu.url=='/')):
+                    return request.redirect(first_menu.url)
+                if first_menu.url.startswith('/page/'):
+                    return request.registry['ir.http'].reroute(first_menu.url)
+        return self.page(page)
+
+    @http.route(website=True, auth="public")
     def web_login(self, *args, **kw):
         # TODO: can't we just put auth=public, ... in web client ?
         return super(Website, self).web_login(*args, **kw)
 
-    @http.route('/page/<path:page>', type='http', auth="public", website=True, multilang=True)
+    @http.route('/page/<page:page>', type='http', auth="public", website=True)
     def page(self, page, **opt):
         values = {
             'path': page,
@@ -56,7 +61,7 @@ class Website(openerp.addons.web.controllers.main.Home):
             request.website.get_template(page)
         except ValueError, e:
             # page not found
-            if request.context['editable']:
+            if request.website.is_publisher():
                 page = 'website.page_404'
             else:
                 return request.registry['ir.http']._handle_exception(e, 404)
@@ -69,33 +74,67 @@ class Website(openerp.addons.web.controllers.main.Home):
 
     @http.route('/sitemap.xml', type='http', auth="public", website=True)
     def sitemap_xml_index(self):
-        pages = list(request.website.enumerate_pages())
-        if len(pages)<=LOC_PER_SITEMAP:
-            return self.__sitemap_xml(pages, 0)
-        # Sitemaps must be split in several smaller files with a sitemap index
-        values = {
-            'pages': range(len(pages)/LOC_PER_SITEMAP+1),
-            'url_root': request.httprequest.url_root
-        }
-        headers = {
-            'Content-Type': 'application/xml;charset=utf-8',
-        }
-        return request.render('website.sitemap_index_xml', values, headers=headers)
-
-    @http.route('/sitemap-<int:page>.xml', type='http', auth="public", website=True)
-    def sitemap_xml(self, page):
-        pages = list(request.website.enumerate_pages())
-        return self.__sitemap_xml(pages, page)
+        cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context
+        ira = request.registry['ir.attachment']
+        iuv = request.registry['ir.ui.view']
+        mimetype ='application/xml;charset=utf-8'
+        content = None
+
+        def create_sitemap(url, content):
+            ira.create(cr, uid, dict(
+                datas=content.encode('base64'),
+                mimetype=mimetype,
+                type='binary',
+                name=url,
+                url=url,
+            ), context=context)
+
+        sitemap = ira.search_read(cr, uid, [('url', '=' , '/sitemap.xml'), ('type', '=', 'binary')], ('datas', 'create_date'), context=context)
+        if sitemap:
+            # Check if stored version is still valid
+            server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
+            create_date = datetime.datetime.strptime(sitemap[0]['create_date'], server_format)
+            delta = datetime.datetime.now() - create_date
+            if delta < SITEMAP_CACHE_TIME:
+                content = sitemap[0]['datas'].decode('base64')
+
+        if not content:
+            # Remove all sitemaps in ir.attachments as we're going to regenerated them
+            sitemap_ids = ira.search(cr, uid, [('url', '=like' , '/sitemap%.xml'), ('type', '=', 'binary')], context=context)
+            if sitemap_ids:
+                ira.unlink(cr, uid, sitemap_ids, context=context)
+
+            pages = 0
+            first_page = None
+            locs = request.website.enumerate_pages()
+            while True:
+                start = pages * LOC_PER_SITEMAP
+                values = {
+                    'locs': islice(locs, start, start + LOC_PER_SITEMAP),
+                    'url_root': request.httprequest.url_root[:-1],
+                }
+                urls = iuv.render(cr, uid, 'website.sitemap_locs', values, context=context)
+                if urls.strip():
+                    page = iuv.render(cr, uid, 'website.sitemap_xml', dict(content=urls), context=context)
+                    if not first_page:
+                        first_page = page
+                    pages += 1
+                    create_sitemap('/sitemap-%d.xml' % pages, page)
+                else:
+                    break
+            if not pages:
+                return request.not_found()
+            elif pages == 1:
+                content = first_page
+            else:
+                # Sitemaps must be split in several smaller files with a sitemap index
+                content = iuv.render(cr, uid, 'website.sitemap_index_xml', dict(
+                    pages=range(1, pages + 1),
+                    url_root=request.httprequest.url_root,
+                ), context=context)
+            create_sitemap('/sitemap.xml', content)
 
-    def __sitemap_xml(self, pages, index=0):
-        values = {
-            'pages': pages[index*LOC_PER_SITEMAP:(index+1)*LOC_PER_SITEMAP],
-            'url_root': request.httprequest.url_root.rstrip('/')
-        }
-        headers = {
-            'Content-Type': 'application/xml;charset=utf-8',
-        }
-        return request.render('website.sitemap_xml', values, headers=headers)
+        return request.make_response(content, [('Content-Type', mimetype)])
 
     #------------------------------------------------------
     # Edit
@@ -120,22 +159,25 @@ class Website(openerp.addons.web.controllers.main.Home):
     @http.route('/website/theme_change', type='http', auth="user", website=True)
     def theme_change(self, theme_id=False, **kwargs):
         imd = request.registry['ir.model.data']
-        view = request.registry['ir.ui.view']
+        Views = request.registry['ir.ui.view']
 
-        view_model, view_option_id = imd.get_object_reference(
+        _, theme_template_id = imd.get_object_reference(
             request.cr, request.uid, 'website', 'theme')
-        views = view.search(
-            request.cr, request.uid, [('inherit_id', '=', view_option_id)],
-            context=request.context)
-        view.write(request.cr, request.uid, views, {'inherit_id': False},
-                   context=request.context)
+        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)
 
         if theme_id:
             module, xml_id = theme_id.split('.')
-            view_model, view_id = imd.get_object_reference(
+            _, view_id = imd.get_object_reference(
                 request.cr, request.uid, module, xml_id)
-            view.write(request.cr, request.uid, [view_id],
-                       {'inherit_id': view_option_id}, context=request.context)
+            Views.write(request.cr, request.uid, [view_id], {
+                'application': 'enabled'
+            }, context=request.context)
 
         return request.render('website.themes', {'theme_changed': True})
 
@@ -159,61 +201,53 @@ class Website(openerp.addons.web.controllers.main.Home):
         module_obj.button_immediate_upgrade(request.cr, request.uid, module_ids, context=request.context)
         return request.redirect(redirect)
 
-    @http.route('/website/customize_template_toggle', type='json', auth='user', website=True)
-    def customize_template_set(self, view_id):
-        view_obj = request.registry.get("ir.ui.view")
-        view = view_obj.browse(request.cr, request.uid, int(view_id),
-                               context=request.context)
-        if view.inherit_id:
-            value = False
-        else:
-            value = view.inherit_option_id and view.inherit_option_id.id or False
-        view_obj.write(request.cr, request.uid, [view_id], {
-            'inherit_id': value
-        }, context=request.context)
-        return True
-
     @http.route('/website/customize_template_get', type='json', auth='user', website=True)
-    def customize_template_get(self, xml_id, optional=True):
+    def customize_template_get(self, xml_id, full=False, bundles=False):
+        """ Lists the templates customizing ``xml_id``. By default, only
+        returns optional templates (which can be toggled on and off), if
+        ``full=True`` returns all templates customizing ``xml_id``
+        ``bundles=True`` returns also the asset bundles
+        """
         imd = request.registry['ir.model.data']
         view_model, view_theme_id = imd.get_object_reference(
             request.cr, request.uid, 'website', 'theme')
 
-        user = request.registry['res.users'].browse(request.cr, request.uid, request.uid, request.context)
-        group_ids = [g.id for g in user.groups_id]
+        user = request.registry['res.users']\
+            .browse(request.cr, request.uid, request.uid, request.context)
+        user_groups = set(user.groups_id)
 
-        view = request.registry.get("ir.ui.view")
-        views = view._views_get(request.cr, request.uid, xml_id, context=request.context)
-        done = {}
+        views = request.registry["ir.ui.view"]\
+            ._views_get(request.cr, request.uid, xml_id, bundles=bundles, context=request.context)
+        done = set()
         result = []
         for v in views:
-            if v.groups_id and [g for g in v.groups_id if g.id not in group_ids]:
+            if not user_groups.issuperset(v.groups_id):
                 continue
-            if v.inherit_option_id and v.inherit_option_id.id != view_theme_id or not optional:
-                if v.inherit_option_id.id not in done:
+            if full or (v.application != 'always' and v.inherit_id.id != view_theme_id):
+                if v.inherit_id not in done:
                     result.append({
-                        'name': v.inherit_option_id.name,
+                        'name': v.inherit_id.name,
                         'id': v.id,
                         'xml_id': v.xml_id,
                         'inherit_id': v.inherit_id.id,
                         'header': True,
                         'active': False
                     })
-                    done[v.inherit_option_id.id] = True
+                    done.add(v.inherit_id)
                 result.append({
                     'name': v.name,
                     'id': v.id,
                     'xml_id': v.xml_id,
                     'inherit_id': v.inherit_id.id,
                     'header': False,
-                    'active': (v.inherit_id.id == v.inherit_option_id.id) or (not optional and v.inherit_id.id)
+                    'active': v.application in ('always', 'enabled'),
                 })
         return result
 
     @http.route('/website/get_view_translations', type='json', auth='public', website=True)
     def get_view_translations(self, xml_id, lang=None):
         lang = lang or request.context.get('lang')
-        views = self.customize_template_get(xml_id, optional=False)
+        views = self.customize_template_get(xml_id, full=True)
         views_ids = [view.get('id') for view in views if view.get('active')]
         domain = [('type', '=', 'view'), ('res_id', 'in', views_ids), ('lang', '=', lang)]
         irt = request.registry.get('ir.translation')
@@ -329,13 +363,10 @@ class Website(openerp.addons.web.controllers.main.Home):
         '/website/image',
         '/website/image/<model>/<id>/<field>'
         ], auth="public", website=True)
-    def website_image(self, model, id, field, max_width=maxint, max_height=maxint):
+    def website_image(self, model, id, field, max_width=None, max_height=None):
         """ Fetches the requested field and ensures it does not go above
         (max_width, max_height), resizing it if necessary.
 
-        Resizing is bypassed if the object provides a $field_big, which will
-        be interpreted as a pre-resized version of the base field.
-
         If the record is not found or does not have the requested field,
         returns a placeholder image via :meth:`~.placeholder`.
 
@@ -349,7 +380,7 @@ class Website(openerp.addons.web.controllers.main.Home):
         """
         response = werkzeug.wrappers.Response()
         return request.registry['website']._image(
-                    request.cr, request.uid, model, id, field, response)
+                    request.cr, request.uid, model, id, field, response, max_width, max_height)
 
 
     #------------------------------------------------------
@@ -382,7 +413,7 @@ class Website(openerp.addons.web.controllers.main.Home):
             action = ServerActions.browse(cr, uid, action_id, context=context)
             if action.state == 'code' and action.website_published:
                 action_res = ServerActions.run(cr, uid, [action_id], context=context)
-                if isinstance(action_res, Response):
+                if isinstance(action_res, werkzeug.wrappers.Response):
                     res = action_res
         if res:
             return res