[FIX] Accept http responses as server action response
[odoo/odoo.git] / addons / website / controllers / main.py
index d38b4b9..5025e5d 100644 (file)
 # -*- coding: utf-8 -*-
 import cStringIO
-import contextlib
-import hashlib
+import datetime
+from itertools import islice
 import json
 import logging
-import os
-import datetime
+import re
 
 from sys import maxint
 
-import psycopg2
-import werkzeug
-import werkzeug.exceptions
 import werkzeug.utils
 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.addons.web.http import request
-
-from ..utils import slugify
+from openerp.http import request, Response
 
 logger = logging.getLogger(__name__)
 
-NOPE = object()
 # 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):
-    @website.route('/', type='http', auth="public", multilang=True)
+    #------------------------------------------------------
+    # View
+    #------------------------------------------------------
+    @http.route('/', type='http', auth="public", website=True, multilang=True)
     def index(self, **kw):
-        # TODO: check if plain SQL is needed
-        menu = request.registry['website.menu']
-        root_domain = [('parent_id', '=', False)] # TODO: multiwebsite ('website_id', '=', request.website.id),
-        root_id = menu.search(request.cr, request.uid, root_domain, limit=1, context=request.context)[0]
-        first_menu = menu.search_read(
-            request.cr, request.uid, [('parent_id', '=', root_id)], ['url'],
-            limit=1, order='sequence', context=request.context)
-        if first_menu:
-            first_menu = first_menu[0]['url']
-        if first_menu and first_menu != '/':
-            return request.redirect(first_menu)
-        else:
-            return self.page("website.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:
+            pass
+        return self.page("website.homepage")
+
+    @http.route(website=True, auth="public", multilang=True)
+    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)
+    def page(self, page, **opt):
+        values = {
+            'path': page,
+        }
+        # allow shortcut for /page/<website_xml_id>
+        if '.' not in page:
+            page = 'website.%s' % page
 
-    @website.route('/pagenew/<path:path>', type='http', auth="user")
-    def pagenew(self, path, noredirect=NOPE):
-        web = request.registry['website']
         try:
-            path = web.new_page(request.cr, request.uid, path, request.context)
-        except psycopg2.IntegrityError:
-            logger.exception('Unable to create ir_model_data for page %s', path)
-            request.cr.execute('ROLLBACK TO SAVEPOINT pagenew')
-            return werkzeug.exceptions.InternalServerError()
-        url = "/page/" + path
-        if noredirect is not NOPE:
+            request.website.get_template(page)
+        except ValueError, e:
+            # page not found
+            if request.context['editable']:
+                page = 'website.page_404'
+            else:
+                return request.registry['ir.http']._handle_exception(e, 404)
+
+        return request.render(page, values)
+
+    @http.route(['/robots.txt'], type='http', auth="public")
+    def robots(self):
+        return request.render('website.robots', {'url_root': request.httprequest.url_root}, mimetype='text/plain')
+
+    @http.route('/sitemap.xml', type='http', auth="public", website=True)
+    def sitemap_xml_index(self):
+        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
+                loc_slice = islice(locs, start, start + LOC_PER_SITEMAP)
+                urls = iuv.render(cr, uid, 'website.sitemap_locs', dict(locs=loc_slice), 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)
+
+        return request.make_response(content, [('Content-Type', mimetype)])
+
+    #------------------------------------------------------
+    # Edit
+    #------------------------------------------------------
+    @http.route('/website/add/<path:path>', type='http', auth="user", website=True)
+    def pagenew(self, path, noredirect=False, add_menu=None):
+        xml_id = request.registry['website'].new_page(request.cr, request.uid, path, context=request.context)
+        if add_menu:
+            model, id  = request.registry["ir.model.data"].get_object_reference(request.cr, request.uid, 'website', 'main_menu')
+            request.registry['website.menu'].create(request.cr, request.uid, {
+                    'name': path,
+                    'url': "/page/" + xml_id,
+                    'parent_id': id,
+                }, context=request.context)
+        # Reverse action in order to allow shortcut for /page/<website_xml_id>
+        url = "/page/" + re.sub(r"^website\.", '', xml_id)
+
+        if noredirect:
             return werkzeug.wrappers.Response(url, mimetype='text/plain')
         return werkzeug.utils.redirect(url)
 
-    @website.route('/website/theme_change', type='http', auth="admin")
+    @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']
@@ -80,21 +169,29 @@ class Website(openerp.addons.web.controllers.main.Home):
             view.write(request.cr, request.uid, [view_id],
                        {'inherit_id': view_option_id}, context=request.context)
 
-        return request.website.render('website.themes', {'theme_changed': True})
+        return request.render('website.themes', {'theme_changed': True})
 
-    @website.route(['/website/snippets'], type='json', auth="public")
+    @http.route(['/website/snippets'], type='json', auth="public", website=True)
     def snippets(self):
         return request.website._render('website.snippets')
 
-    @website.route('/page/<page:page>', type='http', auth="public", multilang=True)
-    def page(self, page, **opt):
-        values = {
-            'path': page,
-        }
-
-        return request.website.render(page, values)
-
-    @website.route('/website/customize_template_toggle', type='json', auth='user')
+    @http.route('/website/reset_templates', type='http', auth='user', methods=['POST'], website=True)
+    def reset_template(self, templates, redirect='/'):
+        templates = request.httprequest.form.getlist('templates')
+        modules_to_update = []
+        for temp_id in templates:
+            view = request.registry['ir.ui.view'].browse(request.cr, request.uid, int(temp_id), context=request.context)
+            view.model_data_id.write({
+                'noupdate': False
+            })
+            if view.model_data_id.module not in modules_to_update:
+                modules_to_update.append(view.model_data_id.module)
+        module_obj = request.registry['ir.module.module']
+        module_ids = module_obj.search(request.cr, request.uid, [('name', 'in', modules_to_update)], context=request.context)
+        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),
@@ -108,22 +205,29 @@ class Website(openerp.addons.web.controllers.main.Home):
         }, context=request.context)
         return True
 
-    @website.route('/website/customize_template_get', type='json', auth='user')
+    @http.route('/website/customize_template_get', type='json', auth='user', website=True)
     def customize_template_get(self, xml_id, optional=True):
         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]
+
         view = request.registry.get("ir.ui.view")
-        views = view._views_get(request.cr, request.uid, xml_id, request.context)
+        views = view._views_get(request.cr, request.uid, xml_id, context=request.context)
         done = {}
         result = []
         for v in views:
+            if v.groups_id and [g for g in v.groups_id if g.id not in group_ids]:
+                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:
                     result.append({
                         'name': v.inherit_option_id.name,
                         'id': v.id,
+                        'xml_id': v.xml_id,
+                        'inherit_id': v.inherit_id.id,
                         'header': True,
                         'active': False
                     })
@@ -131,21 +235,23 @@ class Website(openerp.addons.web.controllers.main.Home):
                 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)
                 })
         return result
 
-    @website.route('/website/get_view_translations', type='json', auth='admin')
+    @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_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')
-        return irt.search_read(request.cr, request.uid, domain, ['id', 'res_id', 'value'], context=request.context)
+        return irt.search_read(request.cr, request.uid, domain, ['id', 'res_id', 'value','state','gengo_translation'], context=request.context)
 
-    @website.route('/website/set_translations', type='json', auth='admin')
+    @http.route('/website/set_translations', type='json', auth='public', website=True)
     def set_translations(self, data, lang):
         irt = request.registry.get('ir.translation')
         for view_id, trans in data.items():
@@ -177,40 +283,56 @@ class Website(openerp.addons.web.controllers.main.Home):
                         'source': initial_content,
                         'value': new_content,
                     }
+                    if t.get('gengo_translation'):
+                        new_trans['gengo_translation'] = t.get('gengo_translation')
+                        new_trans['gengo_comment'] = t.get('gengo_comment')
                     irt.create(request.cr, request.uid, new_trans)
         return True
 
-    @website.route('/website/attach', type='http', auth='user')
-    def attach(self, func, upload):
-        req = request.httprequest
-        if req.method != 'POST':
-            return werkzeug.exceptions.MethodNotAllowed(valid_methods=['POST'])
-
-        url = message = None
-        try:
-            attachment_id = request.registry['ir.attachment'].create(request.cr, request.uid, {
-                'name': upload.filename,
-                'datas': upload.read().encode('base64'),
-                'datas_fname': upload.filename,
+    @http.route('/website/attach', type='http', auth='user', methods=['POST'], website=True)
+    def attach(self, func, upload=None, url=None):
+        Attachments = request.registry['ir.attachment']
+
+        website_url = message = None
+        if not upload:
+            website_url = url
+            name = url.split("/").pop()
+            attachment_id = Attachments.create(request.cr, request.uid, {
+                'name':name,
+                'type': 'url',
+                'url': url,
                 'res_model': 'ir.ui.view',
             }, request.context)
-
-            url = website.urlplus('/website/image', {
-                'model': 'ir.attachment',
-                'id': attachment_id,
-                'field': 'datas',
-                'max_height': MAX_IMAGE_HEIGHT,
-                'max_width': MAX_IMAGE_WIDTH,
-            })
-        except Exception, e:
-            logger.exception("Failed to upload image to attachment")
-            message = str(e)
+        else:
+            try:
+                image_data = upload.read()
+                image = Image.open(cStringIO.StringIO(image_data))
+                w, h = image.size
+                if w*h > 42e6: # Nokia Lumia 1020 photo resolution
+                    raise ValueError(
+                        u"Image size excessive, uploaded images must be smaller "
+                        u"than 42 million pixel")
+
+                attachment_id = Attachments.create(request.cr, request.uid, {
+                    'name': upload.filename,
+                    'datas': image_data.encode('base64'),
+                    'datas_fname': upload.filename,
+                    'res_model': 'ir.ui.view',
+                }, request.context)
+
+                [attachment] = Attachments.read(
+                    request.cr, request.uid, [attachment_id], ['website_url'],
+                    context=request.context)
+                website_url = attachment['website_url']
+            except Exception, e:
+                logger.exception("Failed to upload image to attachment")
+                message = unicode(e)
 
         return """<script type='text/javascript'>
             window.parent['%s'](%s, %s);
-        </script>""" % (func, json.dumps(url), json.dumps(message))
+        </script>""" % (func, json.dumps(website_url), json.dumps(message))
 
-    @website.route(['/website/publish'], type='json', auth="public")
+    @http.route(['/website/publish'], type='json', auth="public", website=True)
     def publish(self, id, object):
         _id = int(id)
         _object = request.registry[object]
@@ -219,110 +341,82 @@ class Website(openerp.addons.web.controllers.main.Home):
         values = {}
         if 'website_published' in _object._all_columns:
             values['website_published'] = not obj.website_published
-        if 'website_published_datetime' in _object._all_columns and values.get('website_published'):
-            values['website_published_datetime'] = fields.datetime.now()
         _object.write(request.cr, request.uid, [_id],
                       values, context=request.context)
 
         obj = _object.browse(request.cr, request.uid, _id)
         return bool(obj.website_published)
 
-    @website.route(['/website/kanban/'], type='http', auth="public")
+    #------------------------------------------------------
+    # Helpers
+    #------------------------------------------------------
+    @http.route(['/website/kanban'], type='http', auth="public", methods=['POST'], website=True)
     def kanban(self, **post):
         return request.website.kanban_col(**post)
 
-    @website.route(['/robots.txt'], type='http', auth="public")
-    def robots(self):
-        response = request.website.render('website.robots', {'url_root': request.httprequest.url_root})
-        response.mimetype = 'text/plain'
-        return response
-
-    @website.route('/sitemap', type='http', auth='public', multilang=True)
-    def sitemap(self):
-        return request.website.render('website.sitemap', {'pages': request.website.enumerate_pages()})
-
-    @website.route('/sitemap.xml', type='http', auth="public")
-    def sitemap_xml(self):
-        response = request.website.render('website.sitemap_xml', {
-            'pages': request.website.enumerate_pages()
-        })
-        response.headers['Content-Type'] = 'application/xml;charset=utf-8'
-        return response
-
-class Images(http.Controller):
     def placeholder(self, response):
-        # file_open may return a StringIO. StringIO can be closed but are
-        # not context managers in Python 2 though that is fixed in 3
-        with contextlib.closing(openerp.tools.misc.file_open(
-                os.path.join('web', 'static', 'src', 'img', 'placeholder.png'),
-                mode='rb')) as f:
-            response.set_data(f.read())
-            return response.make_conditional(request.httprequest)
-
-    @website.route('/website/image', auth="public")
-    def image(self, model, id, field, max_width=maxint, max_height=maxint):
-        Model = request.registry[model]
-
+        return request.registry['website']._image_placeholder(response)
+
+    @http.route([
+        '/website/image',
+        '/website/image/<model>/<id>/<field>'
+        ], auth="public", website=True)
+    def website_image(self, model, id, field, max_width=maxint, max_height=maxint):
+        """ 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`.
+
+        Sets and checks conditional response parameters:
+        * :mailheader:`ETag` is always set (and checked)
+        * :mailheader:`Last-Modified is set iif the record has a concurrency
+          field (``__last_update``)
+
+        The requested field is assumed to be base64-encoded image data in
+        all cases.
+        """
         response = werkzeug.wrappers.Response()
-
-        id = int(id)
-
-        ids = Model.search(request.cr, request.uid,
-                           [('id', '=', id)], context=request.context) \
-            or Model.search(request.cr, openerp.SUPERUSER_ID,
-                            [('id', '=', id), ('website_published', '=', True)], context=request.context)
-
-        if not ids:
-            return self.placeholder(response)
-
-        concurrency = '__last_update'
-        [record] = Model.read(request.cr, openerp.SUPERUSER_ID, [id],
-                              [concurrency, field], context=request.context)
-
-        if concurrency in record:
-            server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
+        return request.registry['website']._image(
+                    request.cr, request.uid, model, id, field, response, max_width, max_height)
+
+
+    #------------------------------------------------------
+    # Server actions
+    #------------------------------------------------------
+    @http.route('/website/action/<path_or_xml_id_or_id>', type='http', auth="public", website=True)
+    def actions_server(self, path_or_xml_id_or_id, **post):
+        cr, uid, context = request.cr, request.uid, request.context
+        res, action_id, action = None, None, None
+        ServerActions = request.registry['ir.actions.server']
+
+        # find the action_id: either an xml_id, the path, or an ID
+        if isinstance(path_or_xml_id_or_id, basestring) and '.' in path_or_xml_id_or_id:
+            action_id = request.registry['ir.model.data'].xmlid_to_res_id(request.cr, request.uid, path_or_xml_id_or_id, raise_if_not_found=False)
+        if not action_id:
+            action_ids = ServerActions.search(cr, uid, [('website_path', '=', path_or_xml_id_or_id), ('website_published', '=', True)], context=context)
+            action_id = action_ids and action_ids[0] or None
+        if not action_id:
             try:
-                response.last_modified = datetime.datetime.strptime(
-                    record[concurrency], server_format + '.%f')
+                action_id = int(path_or_xml_id_or_id)
             except ValueError:
-                # just in case we have a timestamp without microseconds
-                response.last_modified = datetime.datetime.strptime(
-                    record[concurrency], server_format)
-
-        # Field does not exist on model or field set to False
-        if not record.get(field):
-            # FIXME: maybe a field which does not exist should be a 404?
-            return self.placeholder(response)
-
-        response.set_etag(hashlib.sha1(record[field]).hexdigest())
-        response.make_conditional(request.httprequest)
-
-        # conditional request match
-        if response.status_code == 304:
-            return response
-
-        data = record[field].decode('base64')
-        fit = int(max_width), int(max_height)
-
-        buf = cStringIO.StringIO(data)
-
-        image = Image.open(buf)
-        image.load()
-        response.mimetype = Image.MIME[image.format]
-
-        w, h = image.size
-        max_w, max_h = fit
-
-        if w < max_w and h < max_h:
-            response.set_data(data)
-        else:
-            image.thumbnail(fit, Image.ANTIALIAS)
-            image.save(response.stream, image.format)
-            # invalidate content-length computed by make_conditional as writing
-            # to response.stream does not do it (as of werkzeug 0.9.3)
-            del response.headers['Content-Length']
-
-        return response
-
+                pass
+
+        # check it effectively exists
+        if action_id:
+            action_ids = ServerActions.exists(cr, uid, [action_id], context=context)
+            action_id = action_ids and action_ids[0] or None
+        # run it, return only if we got a Response object
+        if action_id:
+            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, werkzeug.wrappers.Response):
+                    res = action_res
+        if res:
+            return res
+        return request.redirect('/')
 
-# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: