2009-10-20 10:52:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-12-07 13:41:40 +00:00
|
|
|
##############################################################################
|
2010-02-02 11:54:22 +00:00
|
|
|
#
|
2008-11-10 10:15:05 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2009-10-14 12:32:15 +00:00
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 12:32:15 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 12:32:15 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2009-10-14 12:32:15 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2010-02-02 11:54:22 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
2013-09-19 13:08:47 +00:00
|
|
|
import collections
|
2013-04-23 10:58:56 +00:00
|
|
|
import copy
|
2014-04-23 16:28:27 +00:00
|
|
|
import datetime
|
|
|
|
import dateutil
|
2014-04-24 09:28:28 +00:00
|
|
|
from dateutil.relativedelta import relativedelta
|
2014-02-06 16:12:04 +00:00
|
|
|
import fnmatch
|
2013-06-27 09:13:29 +00:00
|
|
|
import logging
|
2008-07-01 21:31:46 +00:00
|
|
|
import os
|
2014-04-23 16:28:27 +00:00
|
|
|
import time
|
|
|
|
from operator import itemgetter
|
|
|
|
|
2014-02-06 16:12:04 +00:00
|
|
|
import simplejson
|
|
|
|
import werkzeug
|
2013-09-23 11:46:49 +00:00
|
|
|
import HTMLParser
|
2014-04-23 16:28:27 +00:00
|
|
|
from lxml import etree
|
2013-06-27 09:13:29 +00:00
|
|
|
|
2013-10-08 09:58:05 +00:00
|
|
|
import openerp
|
2014-07-06 14:44:26 +00:00
|
|
|
from openerp import tools, api
|
2014-02-06 16:12:04 +00:00
|
|
|
from openerp.http import request
|
2013-04-24 13:58:25 +00:00
|
|
|
from openerp.osv import fields, osv, orm
|
2014-08-13 09:08:02 +00:00
|
|
|
from openerp.tools import graph, SKIPPED_ELEMENT_TYPES, SKIPPED_ELEMENTS
|
2014-06-11 12:03:20 +00:00
|
|
|
from openerp.tools.parse_version import parse_version
|
2012-12-10 15:27:23 +00:00
|
|
|
from openerp.tools.safe_eval import safe_eval as eval
|
|
|
|
from openerp.tools.view_validation import valid_view
|
2013-10-04 11:41:55 +00:00
|
|
|
from openerp.tools import misc
|
2014-01-30 10:07:16 +00:00
|
|
|
from openerp.tools.translate import _
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2012-01-24 11:47:30 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2014-08-20 15:16:29 +00:00
|
|
|
MOVABLE_BRANDING = ['data-oe-model', 'data-oe-id', 'data-oe-field', 'data-oe-xpath', 'data-oe-source-id']
|
2014-01-19 17:44:37 +00:00
|
|
|
|
2014-06-17 09:35:29 +00:00
|
|
|
def keep_query(*keep_params, **additional_params):
|
|
|
|
"""
|
|
|
|
Generate a query string keeping the current request querystring's parameters specified
|
|
|
|
in ``keep_params`` and also adds the parameters specified in ``additional_params``.
|
|
|
|
|
|
|
|
Multiple values query string params will be merged into a single one with comma seperated
|
|
|
|
values.
|
|
|
|
|
|
|
|
The ``keep_params`` arguments can use wildcards too, eg:
|
|
|
|
|
|
|
|
keep_query('search', 'shop_*', page=4)
|
|
|
|
"""
|
|
|
|
if not keep_params and not additional_params:
|
|
|
|
keep_params = ('*',)
|
|
|
|
params = additional_params.copy()
|
|
|
|
qs_keys = request.httprequest.args.keys()
|
|
|
|
for keep_param in keep_params:
|
|
|
|
for param in fnmatch.filter(qs_keys, keep_param):
|
|
|
|
if param not in additional_params and param in qs_keys:
|
|
|
|
params[param] = ','.join(request.httprequest.args.getlist(param))
|
2014-02-06 16:12:04 +00:00
|
|
|
return werkzeug.urls.url_encode(params)
|
|
|
|
|
2008-08-07 10:57:47 +00:00
|
|
|
class view_custom(osv.osv):
|
2008-08-06 09:01:54 +00:00
|
|
|
_name = 'ir.ui.view.custom'
|
2011-06-01 10:52:09 +00:00
|
|
|
_order = 'create_date desc' # search(limit=1) should return the last customization
|
2008-07-22 14:24:36 +00:00
|
|
|
_columns = {
|
2011-07-27 09:54:14 +00:00
|
|
|
'ref_id': fields.many2one('ir.ui.view', 'Original View', select=True, required=True, ondelete='cascade'),
|
|
|
|
'user_id': fields.many2one('res.users', 'User', select=True, required=True, ondelete='cascade'),
|
2008-08-07 10:57:47 +00:00
|
|
|
'arch': fields.text('View Architecture', required=True),
|
2008-07-22 14:24:36 +00:00
|
|
|
}
|
2010-12-10 12:47:16 +00:00
|
|
|
|
2014-04-03 11:14:45 +00:00
|
|
|
def name_get(self, cr, uid, ids, context=None):
|
|
|
|
return [(rec.id, rec.user_id.name) for rec in self.browse(cr, uid, ids, context=context)]
|
|
|
|
|
2014-05-06 10:23:33 +00:00
|
|
|
def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
|
2014-05-09 09:49:20 +00:00
|
|
|
if args is None:
|
2014-05-06 10:23:33 +00:00
|
|
|
args = []
|
|
|
|
if name:
|
|
|
|
ids = self.search(cr, user, [('user_id', operator, name)] + args, limit=limit)
|
2014-05-09 09:49:20 +00:00
|
|
|
return self.name_get(cr, user, ids, context=context)
|
|
|
|
return super(view_custom, self).name_search(cr, user, name, args=args, operator=operator, context=context, limit=limit)
|
|
|
|
|
2014-05-06 10:23:33 +00:00
|
|
|
|
2010-12-10 12:47:16 +00:00
|
|
|
def _auto_init(self, cr, context=None):
|
|
|
|
super(view_custom, self)._auto_init(cr, context)
|
|
|
|
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_ui_view_custom_user_id_ref_id\'')
|
|
|
|
if not cr.fetchone():
|
|
|
|
cr.execute('CREATE INDEX ir_ui_view_custom_user_id_ref_id ON ir_ui_view_custom (user_id, ref_id)')
|
2008-08-06 09:01:54 +00:00
|
|
|
|
[ADD] hasclass() xpath function
Server-side, view extension is done via xpath. This includes "template" views
full of HTML.
HTML elements often have a bunch of classes, sometimes even semantic
(!). XPath is generally great, but specifically lousy at dealing with
space-separated values: in standard XPath 1.0 to know if an element has a
class 'foo' the predicate is:
contains(concat(' ', normalize-space(@class), ' '), ' foo ')
and this has to be fully duplicated if there's a second class involved.
Things are slightly better with EXSLT/XPath 2.0 and tokenize, but still not
great:
tokenize(@class, '\s+') = 'foo'
and the equality check is very weird when unaware of XPath's evaluation rules.
``hasclass`` makes this much simpler to deal with: to get any ``foo`` node
with the class ``bar`` is as simple as:
//foo[hasclass('bar')
and it can take multiple class, as with e.g. jquery it will return elements
with all specified classes.
Beware though, the predicate function will be called once for each element to
check, since it's implemented in pure python and not profiled elements should
be filtered as much as possible before this point.
2014-04-15 14:56:59 +00:00
|
|
|
def _hasclass(context, *cls):
|
|
|
|
""" Checks if the context node has all the classes passed as arguments
|
|
|
|
"""
|
|
|
|
node_classes = set(context.context_node.attrib.get('class', '').split())
|
|
|
|
|
|
|
|
return node_classes.issuperset(cls)
|
|
|
|
|
|
|
|
xpath_utils = etree.FunctionNamespace(None)
|
|
|
|
xpath_utils['hasclass'] = _hasclass
|
|
|
|
|
2008-08-06 09:01:54 +00:00
|
|
|
class view(osv.osv):
|
|
|
|
_name = 'ir.ui.view'
|
2012-06-19 10:14:25 +00:00
|
|
|
|
2014-03-19 14:41:38 +00:00
|
|
|
def _get_model_data(self, cr, uid, ids, fname, args, context=None):
|
|
|
|
result = dict.fromkeys(ids, False)
|
|
|
|
IMD = self.pool['ir.model.data']
|
|
|
|
data_ids = IMD.search_read(cr, uid, [('res_id', 'in', ids), ('model', '=', 'ir.ui.view')], ['res_id'], context=context)
|
|
|
|
result.update(map(itemgetter('res_id', 'id'), data_ids))
|
2013-12-17 13:14:39 +00:00
|
|
|
return result
|
|
|
|
|
2014-03-19 14:41:38 +00:00
|
|
|
def _views_from_model_data(self, cr, uid, ids, context=None):
|
|
|
|
IMD = self.pool['ir.model.data']
|
|
|
|
data_ids = IMD.search_read(cr, uid, [('id', 'in', ids), ('model', '=', 'ir.ui.view')], ['res_id'], context=context)
|
|
|
|
return map(itemgetter('res_id'), data_ids)
|
|
|
|
|
2008-08-06 09:01:54 +00:00
|
|
|
_columns = {
|
2012-10-13 12:09:24 +00:00
|
|
|
'name': fields.char('View Name', required=True),
|
2014-01-29 05:03:41 +00:00
|
|
|
'model': fields.char('Object', select=True),
|
2010-09-27 07:12:29 +00:00
|
|
|
'priority': fields.integer('Sequence', required=True),
|
2013-06-27 13:16:29 +00:00
|
|
|
'type': fields.selection([
|
2008-08-06 09:01:54 +00:00
|
|
|
('tree','Tree'),
|
|
|
|
('form','Form'),
|
|
|
|
('graph', 'Graph'),
|
2008-10-10 12:30:23 +00:00
|
|
|
('calendar', 'Calendar'),
|
2010-02-01 09:53:55 +00:00
|
|
|
('diagram','Diagram'),
|
2009-09-17 07:27:12 +00:00
|
|
|
('gantt', 'Gantt'),
|
2011-08-02 09:36:59 +00:00
|
|
|
('kanban', 'Kanban'),
|
2013-06-27 13:16:29 +00:00
|
|
|
('search','Search'),
|
|
|
|
('qweb', 'QWeb')], string='View Type'),
|
2013-07-18 17:03:14 +00:00
|
|
|
'arch': fields.text('View Architecture', required=True),
|
2010-12-10 12:47:16 +00:00
|
|
|
'inherit_id': fields.many2one('ir.ui.view', 'Inherited View', ondelete='cascade', select=True),
|
2013-08-11 10:28:31 +00:00
|
|
|
'inherit_children_ids': fields.one2many('ir.ui.view','inherit_id', 'Inherit Views'),
|
2014-01-29 05:03:41 +00:00
|
|
|
'field_parent': fields.char('Child Field'),
|
2014-03-19 14:41:38 +00:00
|
|
|
'model_data_id': fields.function(_get_model_data, type='many2one', relation='ir.model.data', string="Model Data",
|
|
|
|
store={
|
|
|
|
_name: (lambda s, c, u, i, ctx=None: i, None, 10),
|
|
|
|
'ir.model.data': (_views_from_model_data, ['model', 'res_id'], 10),
|
|
|
|
}),
|
2011-09-06 07:57:11 +00:00
|
|
|
'xml_id': fields.function(osv.osv.get_xml_id, type='char', size=128, string="External ID",
|
2012-01-04 13:30:27 +00:00
|
|
|
help="ID of the view defined in xml file"),
|
2012-08-29 12:35:39 +00:00
|
|
|
'groups_id': fields.many2many('res.groups', 'ir_ui_view_group_rel', 'view_id', 'group_id',
|
|
|
|
string='Groups', help="If this field is empty, the view applies to all users. Otherwise, the view applies to the users of those groups only."),
|
2014-01-30 12:01:09 +00:00
|
|
|
'model_ids': fields.one2many('ir.model.data', 'res_id', domain=[('model','=','ir.ui.view')], auto_join=True),
|
2014-05-11 07:47:55 +00:00
|
|
|
'create_date': fields.datetime('Create Date', readonly=True),
|
|
|
|
'write_date': fields.datetime('Last Modification Date', readonly=True),
|
2014-05-27 09:43:08 +00:00
|
|
|
|
|
|
|
'mode': fields.selection(
|
|
|
|
[('primary', "Base view"), ('extension', "Extension View")],
|
2014-05-27 10:20:04 +00:00
|
|
|
string="View inheritance mode", required=True,
|
|
|
|
help="""Only applies if this view inherits from an other one (inherit_id is not False/Null).
|
|
|
|
|
|
|
|
* if extension (default), if this view is requested the closest primary view
|
|
|
|
is looked up (via inherit_id), then all views inheriting from it with this
|
|
|
|
view's model are applied
|
|
|
|
* if primary, the closest primary view is fully resolved (even if it uses a
|
|
|
|
different model than this one), then this view's inheritance specs
|
|
|
|
(<xpath/>) are applied, and the result is used as if it were this view's
|
|
|
|
actual arch.
|
|
|
|
"""),
|
2014-09-17 11:54:11 +00:00
|
|
|
'active': fields.boolean("Active",
|
2014-05-27 10:26:38 +00:00
|
|
|
help="""If this view is inherited,
|
2014-08-31 14:56:44 +00:00
|
|
|
* if True, the view always extends its parent
|
|
|
|
* if False, the view currently does not extend its parent but can be enabled
|
2014-05-27 10:26:38 +00:00
|
|
|
"""),
|
2008-08-06 09:01:54 +00:00
|
|
|
}
|
|
|
|
_defaults = {
|
2014-05-27 09:46:58 +00:00
|
|
|
'mode': 'primary',
|
2014-08-31 14:56:44 +00:00
|
|
|
'active': True,
|
2013-03-27 16:02:58 +00:00
|
|
|
'priority': 16,
|
2008-08-06 09:01:54 +00:00
|
|
|
}
|
2010-12-10 22:42:58 +00:00
|
|
|
_order = "priority,name"
|
2011-11-13 19:27:42 +00:00
|
|
|
|
[IMP] modules, ir.ui.view: improve view validation + avoid validation errors during updates
As of 7.0, RNG validation is not possible for form views
that have a version attribute equal to "7.0", due to the
allowed usage of HTML syntax mixed with the regular OpenERP
view syntax. RNG validation is still enabled for regular
form views (@version missing or less than "7.0"), and for
all other views types.
Validation of 7.0 form views should be improved with the
addition of an assertion-based schema, still to be done.
The above is also complemented with an explicit call to fields_view_get()
during view installation, in order to immediately verify
that the updated view hierarchy does not cause any
issue when loaded along with its related views (i.e
parent and siblings, for inheriting views).
In addition to that, fields_view_get() will now only
consider loading views that belong to modules that have
already been loaded. This avoids a lot of validation errors
during a module update operation, which runs on top of
an existing database with all previous views visible,
even those whose module is not loaded yet.
bzr revid: odo@openerp.com-20120611122758-qcw9xdhupl24busq
2012-06-11 12:27:58 +00:00
|
|
|
# Holds the RNG schema
|
2012-10-13 12:09:24 +00:00
|
|
|
_relaxng_validator = None
|
2012-06-04 16:01:24 +00:00
|
|
|
|
[IMP] modules, ir.ui.view: improve view validation + avoid validation errors during updates
As of 7.0, RNG validation is not possible for form views
that have a version attribute equal to "7.0", due to the
allowed usage of HTML syntax mixed with the regular OpenERP
view syntax. RNG validation is still enabled for regular
form views (@version missing or less than "7.0"), and for
all other views types.
Validation of 7.0 form views should be improved with the
addition of an assertion-based schema, still to be done.
The above is also complemented with an explicit call to fields_view_get()
during view installation, in order to immediately verify
that the updated view hierarchy does not cause any
issue when loaded along with its related views (i.e
parent and siblings, for inheriting views).
In addition to that, fields_view_get() will now only
consider loading views that belong to modules that have
already been loaded. This avoids a lot of validation errors
during a module update operation, which runs on top of
an existing database with all previous views visible,
even those whose module is not loaded yet.
bzr revid: odo@openerp.com-20120611122758-qcw9xdhupl24busq
2012-06-11 12:27:58 +00:00
|
|
|
def _relaxng(self):
|
|
|
|
if not self._relaxng_validator:
|
2011-11-13 19:27:42 +00:00
|
|
|
frng = tools.file_open(os.path.join('base','rng','view.rng'))
|
|
|
|
try:
|
|
|
|
relaxng_doc = etree.parse(frng)
|
[IMP] modules, ir.ui.view: improve view validation + avoid validation errors during updates
As of 7.0, RNG validation is not possible for form views
that have a version attribute equal to "7.0", due to the
allowed usage of HTML syntax mixed with the regular OpenERP
view syntax. RNG validation is still enabled for regular
form views (@version missing or less than "7.0"), and for
all other views types.
Validation of 7.0 form views should be improved with the
addition of an assertion-based schema, still to be done.
The above is also complemented with an explicit call to fields_view_get()
during view installation, in order to immediately verify
that the updated view hierarchy does not cause any
issue when loaded along with its related views (i.e
parent and siblings, for inheriting views).
In addition to that, fields_view_get() will now only
consider loading views that belong to modules that have
already been loaded. This avoids a lot of validation errors
during a module update operation, which runs on top of
an existing database with all previous views visible,
even those whose module is not loaded yet.
bzr revid: odo@openerp.com-20120611122758-qcw9xdhupl24busq
2012-06-11 12:27:58 +00:00
|
|
|
self._relaxng_validator = etree.RelaxNG(relaxng_doc)
|
|
|
|
except Exception:
|
|
|
|
_logger.exception('Failed to load RelaxNG XML schema for views validation')
|
2011-11-13 19:27:42 +00:00
|
|
|
finally:
|
|
|
|
frng.close()
|
[IMP] modules, ir.ui.view: improve view validation + avoid validation errors during updates
As of 7.0, RNG validation is not possible for form views
that have a version attribute equal to "7.0", due to the
allowed usage of HTML syntax mixed with the regular OpenERP
view syntax. RNG validation is still enabled for regular
form views (@version missing or less than "7.0"), and for
all other views types.
Validation of 7.0 form views should be improved with the
addition of an assertion-based schema, still to be done.
The above is also complemented with an explicit call to fields_view_get()
during view installation, in order to immediately verify
that the updated view hierarchy does not cause any
issue when loaded along with its related views (i.e
parent and siblings, for inheriting views).
In addition to that, fields_view_get() will now only
consider loading views that belong to modules that have
already been loaded. This avoids a lot of validation errors
during a module update operation, which runs on top of
an existing database with all previous views visible,
even those whose module is not loaded yet.
bzr revid: odo@openerp.com-20120611122758-qcw9xdhupl24busq
2012-06-11 12:27:58 +00:00
|
|
|
return self._relaxng_validator
|
2012-10-13 12:09:24 +00:00
|
|
|
|
[IMP] modules, ir.ui.view: improve view validation + avoid validation errors during updates
As of 7.0, RNG validation is not possible for form views
that have a version attribute equal to "7.0", due to the
allowed usage of HTML syntax mixed with the regular OpenERP
view syntax. RNG validation is still enabled for regular
form views (@version missing or less than "7.0"), and for
all other views types.
Validation of 7.0 form views should be improved with the
addition of an assertion-based schema, still to be done.
The above is also complemented with an explicit call to fields_view_get()
during view installation, in order to immediately verify
that the updated view hierarchy does not cause any
issue when loaded along with its related views (i.e
parent and siblings, for inheriting views).
In addition to that, fields_view_get() will now only
consider loading views that belong to modules that have
already been loaded. This avoids a lot of validation errors
during a module update operation, which runs on top of
an existing database with all previous views visible,
even those whose module is not loaded yet.
bzr revid: odo@openerp.com-20120611122758-qcw9xdhupl24busq
2012-06-11 12:27:58 +00:00
|
|
|
def _check_xml(self, cr, uid, ids, context=None):
|
2013-09-12 14:10:18 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2014-01-30 10:07:16 +00:00
|
|
|
context = dict(context, check_view_ids=ids)
|
2013-09-12 14:10:18 +00:00
|
|
|
|
2014-01-30 10:07:16 +00:00
|
|
|
# Sanity checks: the view should not break anything upon rendering!
|
|
|
|
# Any exception raised below will cause a transaction rollback.
|
[IMP] modules, ir.ui.view: improve view validation + avoid validation errors during updates
As of 7.0, RNG validation is not possible for form views
that have a version attribute equal to "7.0", due to the
allowed usage of HTML syntax mixed with the regular OpenERP
view syntax. RNG validation is still enabled for regular
form views (@version missing or less than "7.0"), and for
all other views types.
Validation of 7.0 form views should be improved with the
addition of an assertion-based schema, still to be done.
The above is also complemented with an explicit call to fields_view_get()
during view installation, in order to immediately verify
that the updated view hierarchy does not cause any
issue when loaded along with its related views (i.e
parent and siblings, for inheriting views).
In addition to that, fields_view_get() will now only
consider loading views that belong to modules that have
already been loaded. This avoids a lot of validation errors
during a module update operation, which runs on top of
an existing database with all previous views visible,
even those whose module is not loaded yet.
bzr revid: odo@openerp.com-20120611122758-qcw9xdhupl24busq
2012-06-11 12:27:58 +00:00
|
|
|
for view in self.browse(cr, uid, ids, context):
|
2014-01-30 10:07:16 +00:00
|
|
|
view_def = self.read_combined(cr, uid, view.id, None, context=context)
|
|
|
|
view_arch_utf8 = view_def['arch']
|
2013-06-30 21:35:15 +00:00
|
|
|
if view.type != 'qweb':
|
2014-01-30 10:07:16 +00:00
|
|
|
view_doc = etree.fromstring(view_arch_utf8)
|
|
|
|
# verify that all fields used are valid, etc.
|
|
|
|
self.postprocess_and_fields(cr, uid, view.model, view_doc, view.id, context=context)
|
2013-06-30 21:35:15 +00:00
|
|
|
# RNG-based validation is not possible anymore with 7.0 forms
|
2014-01-30 10:07:16 +00:00
|
|
|
view_docs = [view_doc]
|
2013-06-30 21:35:15 +00:00
|
|
|
if view_docs[0].tag == 'data':
|
|
|
|
# A <data> element is a wrapper for multiple root nodes
|
|
|
|
view_docs = view_docs[0]
|
|
|
|
validator = self._relaxng()
|
|
|
|
for view_arch in view_docs:
|
2014-06-11 12:03:20 +00:00
|
|
|
version = view_arch.get('version', '7.0')
|
|
|
|
if parse_version(version) < parse_version('7.0') and validator and not validator.validate(view_arch):
|
2013-06-30 21:35:15 +00:00
|
|
|
for error in validator.error_log:
|
|
|
|
_logger.error(tools.ustr(error))
|
|
|
|
return False
|
|
|
|
if not valid_view(view_arch):
|
|
|
|
return False
|
2011-11-13 19:27:42 +00:00
|
|
|
return True
|
|
|
|
|
2014-05-27 10:21:52 +00:00
|
|
|
_sql_constraints = [
|
|
|
|
('inheritance_mode',
|
|
|
|
"CHECK (mode != 'extension' OR inherit_id IS NOT NULL)",
|
|
|
|
"Invalid inheritance mode: if the mode is 'extension', the view must"
|
|
|
|
" extend an other view"),
|
|
|
|
]
|
2008-08-06 09:01:54 +00:00
|
|
|
_constraints = [
|
2014-05-27 09:43:08 +00:00
|
|
|
(_check_xml, 'Invalid view definition', ['arch']),
|
2008-08-06 09:01:54 +00:00
|
|
|
]
|
2008-09-23 05:36:21 +00:00
|
|
|
|
2010-12-10 12:47:16 +00:00
|
|
|
def _auto_init(self, cr, context=None):
|
|
|
|
super(view, self)._auto_init(cr, context)
|
|
|
|
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_ui_view_model_type_inherit_id\'')
|
|
|
|
if not cr.fetchone():
|
2012-06-19 10:14:25 +00:00
|
|
|
cr.execute('CREATE INDEX ir_ui_view_model_type_inherit_id ON ir_ui_view (model, inherit_id)')
|
2010-12-10 12:47:16 +00:00
|
|
|
|
2014-05-27 09:43:08 +00:00
|
|
|
def _compute_defaults(self, cr, uid, values, context=None):
|
2014-05-27 09:46:58 +00:00
|
|
|
if 'inherit_id' in values:
|
|
|
|
values.setdefault(
|
|
|
|
'mode', 'extension' if values['inherit_id'] else 'primary')
|
2014-05-27 09:43:08 +00:00
|
|
|
return values
|
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
def create(self, cr, uid, values, context=None):
|
2014-10-07 11:11:20 +00:00
|
|
|
if not values.get('type'):
|
2013-06-29 21:35:16 +00:00
|
|
|
if values.get('inherit_id'):
|
|
|
|
values['type'] = self.browse(cr, uid, values['inherit_id'], context).type
|
|
|
|
else:
|
|
|
|
values['type'] = etree.fromstring(values['arch']).tag
|
2013-04-24 09:51:14 +00:00
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
if not values.get('name'):
|
2014-05-27 09:43:08 +00:00
|
|
|
values['name'] = "%s %s" % (values.get('model'), values['type'])
|
2013-11-30 15:47:23 +00:00
|
|
|
|
2014-09-17 13:52:47 +00:00
|
|
|
self.clear_cache()
|
2014-05-27 09:43:08 +00:00
|
|
|
return super(view, self).create(
|
|
|
|
cr, uid,
|
|
|
|
self._compute_defaults(cr, uid, values, context=context),
|
|
|
|
context=context)
|
2013-04-24 09:51:14 +00:00
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
|
|
|
if not isinstance(ids, (list, tuple)):
|
|
|
|
ids = [ids]
|
2013-12-18 10:55:11 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2013-04-24 09:51:14 +00:00
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
# drop the corresponding view customizations (used for dashboards for example), otherwise
|
|
|
|
# not all users would see the updated views
|
2013-12-18 10:55:11 +00:00
|
|
|
custom_view_ids = self.pool.get('ir.ui.view.custom').search(cr, uid, [('ref_id', 'in', ids)])
|
2013-06-29 21:35:16 +00:00
|
|
|
if custom_view_ids:
|
|
|
|
self.pool.get('ir.ui.view.custom').unlink(cr, uid, custom_view_ids)
|
|
|
|
|
2014-09-17 13:52:47 +00:00
|
|
|
self.clear_cache()
|
2014-05-27 09:43:08 +00:00
|
|
|
ret = super(view, self).write(
|
|
|
|
cr, uid, ids,
|
|
|
|
self._compute_defaults(cr, uid, vals, context=context),
|
|
|
|
context)
|
2013-12-17 13:14:39 +00:00
|
|
|
return ret
|
2013-04-23 15:08:44 +00:00
|
|
|
|
2014-05-27 09:54:01 +00:00
|
|
|
def toggle(self, cr, uid, ids, context=None):
|
2014-08-31 14:56:44 +00:00
|
|
|
""" Switches between enabled and disabled statuses
|
2014-05-27 09:54:01 +00:00
|
|
|
"""
|
2014-08-31 14:56:44 +00:00
|
|
|
for view in self.browse(cr, uid, ids, context=dict(context or {}, active_test=False)):
|
|
|
|
view.write({'active': not view.active})
|
2014-05-27 09:54:01 +00:00
|
|
|
|
2014-01-19 17:44:37 +00:00
|
|
|
# default view selection
|
2013-06-29 21:35:16 +00:00
|
|
|
def default_view(self, cr, uid, model, view_type, context=None):
|
|
|
|
""" Fetches the default view for the provided (model, view_type) pair:
|
2014-05-27 09:44:55 +00:00
|
|
|
primary view with the lowest priority.
|
2013-06-29 21:35:16 +00:00
|
|
|
|
|
|
|
:param str model:
|
|
|
|
:param int view_type:
|
|
|
|
:return: id of the default view of False if none found
|
|
|
|
:rtype: int
|
2013-04-24 13:09:07 +00:00
|
|
|
"""
|
2013-06-30 21:35:15 +00:00
|
|
|
domain = [
|
2013-06-29 21:35:16 +00:00
|
|
|
['model', '=', model],
|
|
|
|
['type', '=', view_type],
|
2014-05-27 09:44:55 +00:00
|
|
|
['mode', '=', 'primary'],
|
2013-06-30 21:35:15 +00:00
|
|
|
]
|
2014-05-12 12:56:30 +00:00
|
|
|
ids = self.search(cr, uid, domain, limit=1, context=context)
|
2013-06-29 21:35:16 +00:00
|
|
|
if not ids:
|
|
|
|
return False
|
2013-06-30 21:35:15 +00:00
|
|
|
return ids[0]
|
2013-06-27 17:10:27 +00:00
|
|
|
|
2014-01-19 17:44:37 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Inheritance mecanism
|
|
|
|
#------------------------------------------------------
|
2013-07-01 14:53:34 +00:00
|
|
|
def get_inheriting_views_arch(self, cr, uid, view_id, model, context=None):
|
2013-06-29 21:35:16 +00:00
|
|
|
"""Retrieves the architecture of views that inherit from the given view, from the sets of
|
|
|
|
views that should currently be used in the system. During the module upgrade phase it
|
|
|
|
may happen that a view is present in the database but the fields it relies on are not
|
|
|
|
fully loaded yet. This method only considers views that belong to modules whose code
|
|
|
|
is already loaded. Custom views defined directly in the database are loaded only
|
|
|
|
after the module initialization phase is completely finished.
|
2013-06-27 17:10:27 +00:00
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
:param int view_id: id of the view whose inheriting views should be retrieved
|
2013-07-01 14:53:34 +00:00
|
|
|
:param str model: model identifier of the inheriting views.
|
2013-06-29 21:35:16 +00:00
|
|
|
:rtype: list of tuples
|
|
|
|
:return: [(view_arch,view_id), ...]
|
|
|
|
"""
|
2013-09-12 14:10:18 +00:00
|
|
|
|
2014-02-07 10:03:02 +00:00
|
|
|
user = self.pool['res.users'].browse(cr, 1, uid, context=context)
|
|
|
|
user_groups = frozenset(user.groups_id or ())
|
2013-06-27 17:10:27 +00:00
|
|
|
|
2014-05-27 10:23:02 +00:00
|
|
|
conditions = [
|
|
|
|
['inherit_id', '=', view_id],
|
|
|
|
['model', '=', model],
|
|
|
|
['mode', '=', 'extension'],
|
2014-08-31 14:56:44 +00:00
|
|
|
['active', '=', True],
|
2014-05-27 10:23:02 +00:00
|
|
|
]
|
2013-06-29 21:35:16 +00:00
|
|
|
if self.pool._init:
|
|
|
|
# Module init currently in progress, only consider views from
|
|
|
|
# modules whose code is already loaded
|
|
|
|
conditions.extend([
|
2013-10-02 13:38:20 +00:00
|
|
|
'|',
|
2013-06-29 21:35:16 +00:00
|
|
|
['model_ids.module', 'in', tuple(self.pool._init_modules)],
|
2014-05-27 09:46:58 +00:00
|
|
|
['id', 'in', context and context.get('check_view_ids') or (0,)],
|
2013-06-29 21:35:16 +00:00
|
|
|
])
|
|
|
|
view_ids = self.search(cr, uid, conditions, context=context)
|
2013-06-27 17:10:27 +00:00
|
|
|
|
2014-06-26 16:18:12 +00:00
|
|
|
return [(view.arch, view.id)
|
|
|
|
for view in self.browse(cr, 1, view_ids, context)
|
|
|
|
if not (view.groups_id and user_groups.isdisjoint(view.groups_id))]
|
2013-06-29 21:35:16 +00:00
|
|
|
|
2013-07-01 00:24:24 +00:00
|
|
|
def raise_view_error(self, cr, uid, message, view_id, context=None):
|
2014-01-30 15:43:49 +00:00
|
|
|
view = self.browse(cr, uid, view_id, context)
|
2014-01-30 10:07:16 +00:00
|
|
|
not_avail = _('n/a')
|
|
|
|
message = ("%(msg)s\n\n" +
|
|
|
|
_("Error context:\nView `%(view_name)s`") +
|
|
|
|
"\n[view_id: %(viewid)s, xml_id: %(xmlid)s, "
|
|
|
|
"model: %(model)s, parent_id: %(parent)s]") % \
|
|
|
|
{
|
|
|
|
'view_name': view.name or not_avail,
|
|
|
|
'viewid': view_id or not_avail,
|
|
|
|
'xmlid': view.xml_id or not_avail,
|
|
|
|
'model': view.model or not_avail,
|
|
|
|
'parent': view.inherit_id.id or not_avail,
|
|
|
|
'msg': message,
|
|
|
|
}
|
|
|
|
_logger.error(message)
|
2013-06-29 21:35:16 +00:00
|
|
|
raise AttributeError(message)
|
2013-04-23 15:08:44 +00:00
|
|
|
|
2013-04-19 14:14:09 +00:00
|
|
|
def locate_node(self, arch, spec):
|
|
|
|
""" Locate a node in a source (parent) architecture.
|
|
|
|
|
|
|
|
Given a complete source (parent) architecture (i.e. the field
|
|
|
|
`arch` in a view), and a 'spec' node (a node in an inheriting
|
|
|
|
view that specifies the location in the source view of what
|
|
|
|
should be changed), return (if it exists) the node in the
|
|
|
|
source view matching the specification.
|
|
|
|
|
|
|
|
:param arch: a parent architecture to modify
|
|
|
|
:param spec: a modifying node in an inheriting view
|
|
|
|
:return: a node in the source matching the spec
|
|
|
|
"""
|
|
|
|
if spec.tag == 'xpath':
|
|
|
|
nodes = arch.xpath(spec.get('expr'))
|
|
|
|
return nodes[0] if nodes else None
|
|
|
|
elif spec.tag == 'field':
|
|
|
|
# Only compare the field name: a field can be only once in a given view
|
|
|
|
# at a given level (and for multilevel expressions, we should use xpath
|
|
|
|
# inheritance spec anyway).
|
2013-04-22 09:19:58 +00:00
|
|
|
for node in arch.iter('field'):
|
2013-04-19 14:14:09 +00:00
|
|
|
if node.get('name') == spec.get('name'):
|
|
|
|
return node
|
|
|
|
return None
|
|
|
|
|
2013-04-22 09:19:58 +00:00
|
|
|
for node in arch.iter(spec.tag):
|
2013-04-19 14:14:09 +00:00
|
|
|
if isinstance(node, SKIPPED_ELEMENT_TYPES):
|
|
|
|
continue
|
2013-04-22 09:10:14 +00:00
|
|
|
if all(node.get(attr) == spec.get(attr) for attr in spec.attrib
|
|
|
|
if attr not in ('position','version')):
|
2013-04-19 14:14:09 +00:00
|
|
|
# Version spec should match parent's root element's version
|
|
|
|
if spec.get('version') and spec.get('version') != arch.get('version'):
|
|
|
|
return None
|
|
|
|
return node
|
|
|
|
return None
|
|
|
|
|
2014-02-24 10:43:01 +00:00
|
|
|
def inherit_branding(self, specs_tree, view_id, root_id):
|
2013-09-23 14:33:49 +00:00
|
|
|
for node in specs_tree.iterchildren(tag=etree.Element):
|
2013-08-12 07:45:09 +00:00
|
|
|
xpath = node.getroottree().getpath(node)
|
|
|
|
if node.tag == 'data' or node.tag == 'xpath':
|
2014-02-24 10:43:01 +00:00
|
|
|
self.inherit_branding(node, view_id, root_id)
|
2013-08-12 07:45:09 +00:00
|
|
|
else:
|
|
|
|
node.set('data-oe-id', str(view_id))
|
2014-02-24 10:43:01 +00:00
|
|
|
node.set('data-oe-source-id', str(root_id))
|
2013-08-12 07:45:09 +00:00
|
|
|
node.set('data-oe-xpath', xpath)
|
2013-09-19 13:08:47 +00:00
|
|
|
node.set('data-oe-model', 'ir.ui.view')
|
|
|
|
node.set('data-oe-field', 'arch')
|
2013-07-01 00:24:24 +00:00
|
|
|
|
2013-06-29 23:41:58 +00:00
|
|
|
return specs_tree
|
|
|
|
|
|
|
|
def apply_inheritance_specs(self, cr, uid, source, specs_tree, inherit_id, context=None):
|
2013-04-23 10:58:56 +00:00
|
|
|
""" Apply an inheriting view (a descendant of the base view)
|
|
|
|
|
|
|
|
Apply to a source architecture all the spec nodes (i.e. nodes
|
|
|
|
describing where and what changes to apply to some parent
|
|
|
|
architecture) given by an inheriting view.
|
|
|
|
|
2013-04-24 09:51:14 +00:00
|
|
|
:param Element source: a parent architecture to modify
|
2013-06-29 23:41:58 +00:00
|
|
|
:param Elepect specs_tree: a modifying architecture in an inheriting view
|
|
|
|
:param inherit_id: the database id of specs_arch
|
2013-04-23 10:58:56 +00:00
|
|
|
:return: a modified source where the specs are applied
|
2013-04-24 09:51:14 +00:00
|
|
|
:rtype: Element
|
2013-04-23 10:58:56 +00:00
|
|
|
"""
|
|
|
|
# Queue of specification nodes (i.e. nodes describing where and
|
|
|
|
# changes to apply to some parent architecture).
|
|
|
|
specs = [specs_tree]
|
|
|
|
|
|
|
|
while len(specs):
|
|
|
|
spec = specs.pop(0)
|
|
|
|
if isinstance(spec, SKIPPED_ELEMENT_TYPES):
|
|
|
|
continue
|
|
|
|
if spec.tag == 'data':
|
2014-02-06 13:03:49 +00:00
|
|
|
specs += [c for c in spec]
|
2013-04-23 10:58:56 +00:00
|
|
|
continue
|
|
|
|
node = self.locate_node(source, spec)
|
|
|
|
if node is not None:
|
|
|
|
pos = spec.get('position', 'inside')
|
|
|
|
if pos == 'replace':
|
|
|
|
if node.getparent() is None:
|
|
|
|
source = copy.deepcopy(spec[0])
|
|
|
|
else:
|
|
|
|
for child in spec:
|
|
|
|
node.addprevious(child)
|
|
|
|
node.getparent().remove(node)
|
|
|
|
elif pos == 'attributes':
|
|
|
|
for child in spec.getiterator('attribute'):
|
2014-06-24 10:27:09 +00:00
|
|
|
attribute = (child.get('name'), child.text or None)
|
2013-04-23 10:58:56 +00:00
|
|
|
if attribute[1]:
|
|
|
|
node.set(attribute[0], attribute[1])
|
2013-12-02 19:14:11 +00:00
|
|
|
elif attribute[0] in node.attrib:
|
|
|
|
del node.attrib[attribute[0]]
|
2013-04-23 10:58:56 +00:00
|
|
|
else:
|
|
|
|
sib = node.getnext()
|
|
|
|
for child in spec:
|
|
|
|
if pos == 'inside':
|
|
|
|
node.append(child)
|
|
|
|
elif pos == 'after':
|
|
|
|
if sib is None:
|
|
|
|
node.addnext(child)
|
|
|
|
node = child
|
|
|
|
else:
|
|
|
|
sib.addprevious(child)
|
|
|
|
elif pos == 'before':
|
|
|
|
node.addprevious(child)
|
|
|
|
else:
|
2014-01-30 10:07:16 +00:00
|
|
|
self.raise_view_error(cr, uid, _("Invalid position attribute: '%s'") % pos, inherit_id, context=context)
|
2013-04-23 10:58:56 +00:00
|
|
|
else:
|
|
|
|
attrs = ''.join([
|
|
|
|
' %s="%s"' % (attr, spec.get(attr))
|
|
|
|
for attr in spec.attrib
|
|
|
|
if attr != 'position'
|
|
|
|
])
|
|
|
|
tag = "<%s%s>" % (spec.tag, attrs)
|
2014-01-30 10:07:16 +00:00
|
|
|
self.raise_view_error(cr, uid, _("Element '%s' cannot be located in parent view") % tag, inherit_id, context=context)
|
2013-04-23 10:58:56 +00:00
|
|
|
|
|
|
|
return source
|
|
|
|
|
2014-02-24 10:43:01 +00:00
|
|
|
def apply_view_inheritance(self, cr, uid, source, source_id, model, root_id=None, context=None):
|
2013-06-29 21:35:16 +00:00
|
|
|
""" Apply all the (directly and indirectly) inheriting views.
|
2008-08-07 10:57:47 +00:00
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
:param source: a parent architecture to modify (with parent modifications already applied)
|
2013-06-29 23:41:58 +00:00
|
|
|
:param source_id: the database view_id of the parent view
|
2013-07-01 14:53:34 +00:00
|
|
|
:param model: the original model for which we create a view (not
|
|
|
|
necessarily the same as the source's model); only the inheriting
|
|
|
|
views with that specific model will be applied.
|
2013-06-29 21:35:16 +00:00
|
|
|
:return: a modified source where all the modifying architecture are applied
|
|
|
|
"""
|
2013-06-29 23:41:58 +00:00
|
|
|
if context is None: context = {}
|
2014-02-24 10:43:01 +00:00
|
|
|
if root_id is None:
|
|
|
|
root_id = source_id
|
2014-06-24 16:52:38 +00:00
|
|
|
sql_inherit = self.get_inheriting_views_arch(cr, uid, source_id, model, context=context)
|
2013-06-29 23:41:58 +00:00
|
|
|
for (specs, view_id) in sql_inherit:
|
|
|
|
specs_tree = etree.fromstring(specs.encode('utf-8'))
|
|
|
|
if context.get('inherit_branding'):
|
2014-02-24 10:43:01 +00:00
|
|
|
self.inherit_branding(specs_tree, view_id, root_id)
|
2013-06-29 23:41:58 +00:00
|
|
|
source = self.apply_inheritance_specs(cr, uid, source, specs_tree, view_id, context=context)
|
2014-02-24 10:43:01 +00:00
|
|
|
source = self.apply_view_inheritance(cr, uid, source, view_id, model, root_id=root_id, context=context)
|
2013-06-29 21:35:16 +00:00
|
|
|
return source
|
2010-02-02 11:54:22 +00:00
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
def read_combined(self, cr, uid, view_id, fields=None, context=None):
|
|
|
|
"""
|
|
|
|
Utility function to get a view combined with its inherited views.
|
2010-02-22 10:18:23 +00:00
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
* Gets the top of the view tree if a sub-view is requested
|
|
|
|
* Applies all inherited archs on the root view
|
|
|
|
* Returns the view with all requested fields
|
|
|
|
.. note:: ``arch`` is always added to the fields list even if not
|
|
|
|
requested (similar to ``id``)
|
|
|
|
"""
|
|
|
|
if context is None: context = {}
|
2010-02-05 12:58:52 +00:00
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
# if view_id is not a root view, climb back to the top.
|
2013-12-12 13:44:22 +00:00
|
|
|
base = v = self.browse(cr, uid, view_id, context=context)
|
2014-05-27 09:46:58 +00:00
|
|
|
while v.mode != 'primary':
|
2013-06-29 21:35:16 +00:00
|
|
|
v = v.inherit_id
|
|
|
|
root_id = v.id
|
2010-02-05 12:58:52 +00:00
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
# arch and model fields are always returned
|
|
|
|
if fields:
|
|
|
|
fields = list(set(fields) | set(['arch', 'model']))
|
2010-02-26 09:53:21 +00:00
|
|
|
|
2013-07-01 11:23:09 +00:00
|
|
|
# read the view arch
|
2013-06-29 21:35:16 +00:00
|
|
|
[view] = self.read(cr, uid, [root_id], fields=fields, context=context)
|
2014-05-27 09:46:58 +00:00
|
|
|
view_arch = etree.fromstring(view['arch'].encode('utf-8'))
|
|
|
|
if not v.inherit_id:
|
|
|
|
arch_tree = view_arch
|
|
|
|
else:
|
|
|
|
parent_view = self.read_combined(
|
|
|
|
cr, uid, v.inherit_id.id, fields=fields, context=context)
|
|
|
|
arch_tree = etree.fromstring(parent_view['arch'])
|
|
|
|
self.apply_inheritance_specs(
|
|
|
|
cr, uid, arch_tree, view_arch, parent_view['id'], context=context)
|
|
|
|
|
2013-06-29 23:41:58 +00:00
|
|
|
|
|
|
|
if context.get('inherit_branding'):
|
|
|
|
arch_tree.attrib.update({
|
|
|
|
'data-oe-model': 'ir.ui.view',
|
2013-06-30 01:09:41 +00:00
|
|
|
'data-oe-id': str(root_id),
|
2013-06-29 23:41:58 +00:00
|
|
|
'data-oe-field': 'arch',
|
|
|
|
})
|
2013-06-29 21:35:16 +00:00
|
|
|
|
|
|
|
# and apply inheritance
|
2013-12-12 13:44:22 +00:00
|
|
|
arch = self.apply_view_inheritance(
|
|
|
|
cr, uid, arch_tree, root_id, base.model, context=context)
|
2013-06-29 21:35:16 +00:00
|
|
|
|
|
|
|
return dict(view, arch=etree.tostring(arch, encoding='utf-8'))
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2014-01-19 17:44:37 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Postprocessing: translation, groups and modifiers
|
|
|
|
#------------------------------------------------------
|
|
|
|
# TODO:
|
|
|
|
# - split postprocess so that it can be used instead of translate_qweb
|
|
|
|
# - remove group processing from ir_qweb
|
|
|
|
#------------------------------------------------------
|
2013-06-29 21:35:16 +00:00
|
|
|
def postprocess(self, cr, user, model, node, view_id, in_tree_view, model_fields, context=None):
|
2013-04-24 13:58:25 +00:00
|
|
|
"""Return the description of the fields in the node.
|
|
|
|
|
|
|
|
In a normal call to this method, node is a complete view architecture
|
|
|
|
but it is actually possible to give some sub-node (this is used so
|
|
|
|
that the method can call itself recursively).
|
|
|
|
|
|
|
|
Originally, the field descriptions are drawn from the node itself.
|
|
|
|
But there is now some code calling fields_get() in order to merge some
|
|
|
|
of those information in the architecture.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
result = False
|
|
|
|
fields = {}
|
|
|
|
children = True
|
|
|
|
|
|
|
|
modifiers = {}
|
2013-04-25 09:37:39 +00:00
|
|
|
Model = self.pool.get(model)
|
2014-07-06 14:44:26 +00:00
|
|
|
if Model is None:
|
2014-01-30 15:43:49 +00:00
|
|
|
self.raise_view_error(cr, user, _('Model not found: %(model)s') % dict(model=model),
|
|
|
|
view_id, context)
|
2013-04-24 13:58:25 +00:00
|
|
|
|
|
|
|
def encode(s):
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
return s.encode('utf8')
|
|
|
|
return s
|
|
|
|
|
|
|
|
def check_group(node):
|
|
|
|
"""Apply group restrictions, may be set at view level or model level::
|
|
|
|
* at view level this means the element should be made invisible to
|
|
|
|
people who are not members
|
|
|
|
* at model level (exclusively for fields, obviously), this means
|
|
|
|
the field should be completely removed from the view, as it is
|
|
|
|
completely unavailable for non-members
|
|
|
|
|
|
|
|
:return: True if field should be included in the result of fields_view_get
|
|
|
|
"""
|
2014-07-06 14:44:26 +00:00
|
|
|
if node.tag == 'field' and node.get('name') in Model._fields:
|
|
|
|
field = Model._fields[node.get('name')]
|
|
|
|
if field.groups and not self.user_has_groups(
|
|
|
|
cr, user, groups=field.groups, context=context):
|
2013-04-24 13:58:25 +00:00
|
|
|
node.getparent().remove(node)
|
|
|
|
fields.pop(node.get('name'), None)
|
|
|
|
# no point processing view-level ``groups`` anymore, return
|
|
|
|
return False
|
|
|
|
if node.get('groups'):
|
2013-04-25 09:37:39 +00:00
|
|
|
can_see = self.user_has_groups(
|
|
|
|
cr, user, groups=node.get('groups'), context=context)
|
2013-04-24 13:58:25 +00:00
|
|
|
if not can_see:
|
|
|
|
node.set('invisible', '1')
|
|
|
|
modifiers['invisible'] = True
|
|
|
|
if 'attrs' in node.attrib:
|
|
|
|
del(node.attrib['attrs']) #avoid making field visible later
|
|
|
|
del(node.attrib['groups'])
|
|
|
|
return True
|
|
|
|
|
|
|
|
if node.tag in ('field', 'node', 'arrow'):
|
|
|
|
if node.get('object'):
|
|
|
|
attrs = {}
|
|
|
|
views = {}
|
|
|
|
xml = "<form>"
|
|
|
|
for f in node:
|
|
|
|
if f.tag == 'field':
|
|
|
|
xml += etree.tostring(f, encoding="utf-8")
|
|
|
|
xml += "</form>"
|
|
|
|
new_xml = etree.fromstring(encode(xml))
|
|
|
|
ctx = context.copy()
|
|
|
|
ctx['base_model_name'] = model
|
2013-06-29 21:35:16 +00:00
|
|
|
xarch, xfields = self.postprocess_and_fields(cr, user, node.get('object'), new_xml, view_id, ctx)
|
2013-04-24 13:58:25 +00:00
|
|
|
views['form'] = {
|
|
|
|
'arch': xarch,
|
|
|
|
'fields': xfields
|
|
|
|
}
|
|
|
|
attrs = {'views': views}
|
|
|
|
fields = xfields
|
|
|
|
if node.get('name'):
|
|
|
|
attrs = {}
|
2014-07-06 14:44:26 +00:00
|
|
|
field = Model._fields.get(node.get('name'))
|
|
|
|
if field:
|
2013-04-24 13:58:25 +00:00
|
|
|
children = False
|
|
|
|
views = {}
|
|
|
|
for f in node:
|
2014-01-16 09:17:16 +00:00
|
|
|
if f.tag in ('form', 'tree', 'graph', 'kanban', 'calendar'):
|
2013-04-24 13:58:25 +00:00
|
|
|
node.remove(f)
|
|
|
|
ctx = context.copy()
|
2014-02-11 16:27:36 +00:00
|
|
|
ctx['base_model_name'] = model
|
2014-07-06 14:44:26 +00:00
|
|
|
xarch, xfields = self.postprocess_and_fields(cr, user, field.comodel_name, f, view_id, ctx)
|
2013-04-24 13:58:25 +00:00
|
|
|
views[str(f.tag)] = {
|
|
|
|
'arch': xarch,
|
|
|
|
'fields': xfields
|
|
|
|
}
|
|
|
|
attrs = {'views': views}
|
|
|
|
fields[node.get('name')] = attrs
|
|
|
|
|
|
|
|
field = model_fields.get(node.get('name'))
|
|
|
|
if field:
|
|
|
|
orm.transfer_field_to_modifiers(field, modifiers)
|
|
|
|
|
|
|
|
elif node.tag in ('form', 'tree'):
|
|
|
|
result = Model.view_header_get(cr, user, False, node.tag, context)
|
|
|
|
if result:
|
|
|
|
node.set('string', result)
|
|
|
|
in_tree_view = node.tag == 'tree'
|
|
|
|
|
|
|
|
elif node.tag == 'calendar':
|
2014-01-16 09:17:16 +00:00
|
|
|
for additional_field in ('date_start', 'date_delay', 'date_stop', 'color', 'all_day', 'attendee'):
|
2013-04-24 13:58:25 +00:00
|
|
|
if node.get(additional_field):
|
|
|
|
fields[node.get(additional_field)] = {}
|
|
|
|
|
|
|
|
if not check_group(node):
|
|
|
|
# node must be removed, no need to proceed further with its children
|
|
|
|
return fields
|
|
|
|
|
|
|
|
# The view architeture overrides the python model.
|
|
|
|
# Get the attrs before they are (possibly) deleted by check_group below
|
|
|
|
orm.transfer_node_to_modifiers(node, modifiers, context, in_tree_view)
|
|
|
|
|
2014-01-30 10:07:16 +00:00
|
|
|
# TODO remove attrs counterpart in modifiers when invisible is true ?
|
2013-04-24 13:58:25 +00:00
|
|
|
|
|
|
|
# translate view
|
|
|
|
if 'lang' in context:
|
|
|
|
Translations = self.pool['ir.translation']
|
|
|
|
if node.text and node.text.strip():
|
2014-09-16 15:02:05 +00:00
|
|
|
term = node.text.strip()
|
|
|
|
trans = Translations._get_source(cr, user, model, 'view', context['lang'], term)
|
2013-04-24 13:58:25 +00:00
|
|
|
if trans:
|
2014-09-16 15:02:05 +00:00
|
|
|
node.text = node.text.replace(term, trans)
|
2013-04-24 13:58:25 +00:00
|
|
|
if node.tail and node.tail.strip():
|
2014-09-16 15:02:05 +00:00
|
|
|
term = node.tail.strip()
|
|
|
|
trans = Translations._get_source(cr, user, model, 'view', context['lang'], term)
|
2013-04-24 13:58:25 +00:00
|
|
|
if trans:
|
2014-09-16 15:02:05 +00:00
|
|
|
node.tail = node.tail.replace(term, trans)
|
2013-04-24 13:58:25 +00:00
|
|
|
|
2014-09-16 15:02:05 +00:00
|
|
|
if node.get('string') and node.get('string').strip() and not result:
|
|
|
|
term = node.get('string').strip()
|
|
|
|
trans = Translations._get_source(cr, user, model, 'view', context['lang'], term)
|
|
|
|
if trans == term and ('base_model_name' in context):
|
2013-04-24 13:58:25 +00:00
|
|
|
# If translation is same as source, perhaps we'd have more luck with the alternative model name
|
|
|
|
# (in case we are in a mixed situation, such as an inherited view where parent_view.model != model
|
2014-09-16 15:02:05 +00:00
|
|
|
trans = Translations._get_source(cr, user, context['base_model_name'], 'view', context['lang'], term)
|
2013-04-24 13:58:25 +00:00
|
|
|
if trans:
|
|
|
|
node.set('string', trans)
|
|
|
|
|
|
|
|
for attr_name in ('confirm', 'sum', 'avg', 'help', 'placeholder'):
|
|
|
|
attr_value = node.get(attr_name)
|
2014-09-16 15:02:05 +00:00
|
|
|
if attr_value and attr_value.strip():
|
|
|
|
trans = Translations._get_source(cr, user, model, 'view', context['lang'], attr_value.strip())
|
2013-04-24 13:58:25 +00:00
|
|
|
if trans:
|
|
|
|
node.set(attr_name, trans)
|
|
|
|
|
|
|
|
for f in node:
|
|
|
|
if children or (node.tag == 'field' and f.tag in ('filter','separator')):
|
2013-06-29 21:35:16 +00:00
|
|
|
fields.update(self.postprocess(cr, user, model, f, view_id, in_tree_view, model_fields, context))
|
2013-04-24 13:58:25 +00:00
|
|
|
|
|
|
|
orm.transfer_modifiers_to_node(modifiers, node)
|
|
|
|
return fields
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
def add_on_change(self, cr, user, model_name, arch):
|
|
|
|
""" Add attribute on_change="1" on fields that are dependencies of
|
|
|
|
computed fields on the same view.
|
|
|
|
"""
|
|
|
|
# map each field object to its corresponding nodes in arch
|
|
|
|
field_nodes = collections.defaultdict(list)
|
|
|
|
|
|
|
|
def collect(node, model):
|
|
|
|
if node.tag == 'field':
|
|
|
|
field = model._fields.get(node.get('name'))
|
|
|
|
if field:
|
|
|
|
field_nodes[field].append(node)
|
|
|
|
if field.relational:
|
|
|
|
model = self.pool.get(field.comodel_name)
|
|
|
|
for child in node:
|
|
|
|
collect(child, model)
|
|
|
|
|
|
|
|
collect(arch, self.pool[model_name])
|
|
|
|
|
|
|
|
for field, nodes in field_nodes.iteritems():
|
|
|
|
# if field should trigger an onchange, add on_change="1" on the
|
|
|
|
# nodes referring to field
|
|
|
|
model = self.pool[field.model_name]
|
|
|
|
if model._has_onchange(field, field_nodes):
|
|
|
|
for node in nodes:
|
|
|
|
if not node.get('on_change'):
|
|
|
|
node.set('on_change', '1')
|
|
|
|
|
|
|
|
return arch
|
|
|
|
|
2013-04-24 13:58:25 +00:00
|
|
|
def _disable_workflow_buttons(self, cr, user, model, node):
|
|
|
|
""" Set the buttons in node to readonly if the user can't activate them. """
|
2013-04-25 09:37:39 +00:00
|
|
|
if model is None or user == 1:
|
2013-04-24 13:58:25 +00:00
|
|
|
# admin user can always activate workflow buttons
|
|
|
|
return node
|
|
|
|
|
|
|
|
# TODO handle the case of more than one workflow for a model or multiple
|
|
|
|
# transitions with different groups and same signal
|
|
|
|
usersobj = self.pool.get('res.users')
|
|
|
|
buttons = (n for n in node.getiterator('button') if n.get('type') != 'object')
|
|
|
|
for button in buttons:
|
|
|
|
user_groups = usersobj.read(cr, user, [user], ['groups_id'])[0]['groups_id']
|
|
|
|
cr.execute("""SELECT DISTINCT t.group_id
|
|
|
|
FROM wkf
|
|
|
|
INNER JOIN wkf_activity a ON a.wkf_id = wkf.id
|
|
|
|
INNER JOIN wkf_transition t ON (t.act_to = a.id)
|
|
|
|
WHERE wkf.osv = %s
|
|
|
|
AND t.signal = %s
|
|
|
|
AND t.group_id is NOT NULL
|
|
|
|
""", (model, button.get('name')))
|
|
|
|
group_ids = [x[0] for x in cr.fetchall() if x[0]]
|
|
|
|
can_click = not group_ids or bool(set(user_groups).intersection(group_ids))
|
|
|
|
button.set('readonly', str(int(not can_click)))
|
|
|
|
return node
|
|
|
|
|
2013-06-29 21:35:16 +00:00
|
|
|
def postprocess_and_fields(self, cr, user, model, node, view_id, context=None):
|
2013-04-24 13:58:25 +00:00
|
|
|
""" Return an architecture and a description of all the fields.
|
|
|
|
|
|
|
|
The field description combines the result of fields_get() and
|
2013-06-29 21:35:16 +00:00
|
|
|
postprocess().
|
2013-04-24 13:58:25 +00:00
|
|
|
|
|
|
|
:param node: the architecture as as an etree
|
|
|
|
:return: a tuple (arch, fields) where arch is the given node as a
|
|
|
|
string and fields is the description of all the fields.
|
|
|
|
|
|
|
|
"""
|
|
|
|
fields = {}
|
2013-04-25 09:37:39 +00:00
|
|
|
Model = self.pool.get(model)
|
2014-07-06 14:44:26 +00:00
|
|
|
if Model is None:
|
2014-01-30 15:43:49 +00:00
|
|
|
self.raise_view_error(cr, user, _('Model not found: %(model)s') % dict(model=model), view_id, context)
|
2013-04-25 09:37:39 +00:00
|
|
|
|
2013-04-24 13:58:25 +00:00
|
|
|
if node.tag == 'diagram':
|
|
|
|
if node.getchildren()[0].tag == 'node':
|
|
|
|
node_model = self.pool[node.getchildren()[0].get('object')]
|
|
|
|
node_fields = node_model.fields_get(cr, user, None, context)
|
|
|
|
fields.update(node_fields)
|
|
|
|
if not node.get("create") and not node_model.check_access_rights(cr, user, 'create', raise_exception=False):
|
|
|
|
node.set("create", 'false')
|
|
|
|
if node.getchildren()[1].tag == 'arrow':
|
|
|
|
arrow_fields = self.pool[node.getchildren()[1].get('object')].fields_get(cr, user, None, context)
|
|
|
|
fields.update(arrow_fields)
|
2014-01-30 15:43:49 +00:00
|
|
|
else:
|
2013-04-24 13:58:25 +00:00
|
|
|
fields = Model.fields_get(cr, user, None, context)
|
2013-04-25 09:37:39 +00:00
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
node = self.add_on_change(cr, user, model, node)
|
2013-06-29 21:35:16 +00:00
|
|
|
fields_def = self.postprocess(cr, user, model, node, view_id, False, fields, context=context)
|
2013-04-24 13:58:25 +00:00
|
|
|
node = self._disable_workflow_buttons(cr, user, model, node)
|
|
|
|
if node.tag in ('kanban', 'tree', 'form', 'gantt'):
|
|
|
|
for action, operation in (('create', 'create'), ('delete', 'unlink'), ('edit', 'write')):
|
|
|
|
if not node.get(action) and not Model.check_access_rights(cr, user, operation, raise_exception=False):
|
|
|
|
node.set(action, 'false')
|
2014-04-17 11:40:33 +00:00
|
|
|
if node.tag in ('kanban'):
|
|
|
|
group_by_field = node.get('default_group_by')
|
|
|
|
if group_by_field and Model._all_columns.get(group_by_field):
|
|
|
|
group_by_column = Model._all_columns[group_by_field].column
|
|
|
|
if group_by_column._type == 'many2one':
|
|
|
|
group_by_model = Model.pool.get(group_by_column._obj)
|
|
|
|
for action, operation in (('group_create', 'create'), ('group_delete', 'unlink'), ('group_edit', 'write')):
|
|
|
|
if not node.get(action) and not group_by_model.check_access_rights(cr, user, operation, raise_exception=False):
|
|
|
|
node.set(action, 'false')
|
2014-03-04 12:21:48 +00:00
|
|
|
|
2013-04-24 13:58:25 +00:00
|
|
|
arch = etree.tostring(node, encoding="utf-8").replace('\t', '')
|
|
|
|
for k in fields.keys():
|
|
|
|
if k not in fields_def:
|
|
|
|
del fields[k]
|
|
|
|
for field in fields_def:
|
|
|
|
if field == 'id':
|
|
|
|
# sometime, the view may contain the (invisible) field 'id' needed for a domain (when 2 objects have cross references)
|
|
|
|
fields['id'] = {'readonly': True, 'type': 'integer', 'string': 'ID'}
|
|
|
|
elif field in fields:
|
|
|
|
fields[field].update(fields_def[field])
|
|
|
|
else:
|
2014-01-30 10:07:16 +00:00
|
|
|
message = _("Field `%(field_name)s` does not exist") % \
|
|
|
|
dict(field_name=field)
|
|
|
|
self.raise_view_error(cr, user, message, view_id, context)
|
2013-04-24 13:58:25 +00:00
|
|
|
return arch, fields
|
|
|
|
|
2014-01-19 17:44:37 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# QWeb template views
|
|
|
|
#------------------------------------------------------
|
2013-12-01 11:51:42 +00:00
|
|
|
@tools.ormcache_context(accepted_keys=('lang','inherit_branding', 'editable', 'translatable'))
|
2014-01-19 17:44:37 +00:00
|
|
|
def read_template(self, cr, uid, xml_id, context=None):
|
2014-04-09 12:46:26 +00:00
|
|
|
if isinstance(xml_id, (int, long)):
|
|
|
|
view_id = xml_id
|
|
|
|
else:
|
|
|
|
if '.' not in xml_id:
|
|
|
|
raise ValueError('Invalid template id: %r' % (xml_id,))
|
|
|
|
view_id = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, xml_id, raise_if_not_found=True)
|
2014-01-19 17:44:37 +00:00
|
|
|
|
|
|
|
arch = self.read_combined(cr, uid, view_id, fields=['arch'], context=context)['arch']
|
2013-09-23 11:46:49 +00:00
|
|
|
arch_tree = etree.fromstring(arch)
|
2013-11-04 12:57:35 +00:00
|
|
|
|
2013-09-23 11:46:49 +00:00
|
|
|
if 'lang' in context:
|
2014-01-19 17:44:37 +00:00
|
|
|
arch_tree = self.translate_qweb(cr, uid, view_id, arch_tree, context['lang'], context)
|
|
|
|
|
2013-09-23 11:46:49 +00:00
|
|
|
self.distribute_branding(arch_tree)
|
2014-01-19 17:44:37 +00:00
|
|
|
root = etree.Element('templates')
|
2013-09-25 08:53:05 +00:00
|
|
|
root.append(arch_tree)
|
|
|
|
arch = etree.tostring(root, encoding='utf-8', xml_declaration=True)
|
2013-09-23 11:46:49 +00:00
|
|
|
return arch
|
2013-06-27 14:25:06 +00:00
|
|
|
|
2014-01-17 14:38:53 +00:00
|
|
|
def clear_cache(self):
|
|
|
|
self.read_template.clear_cache(self)
|
|
|
|
|
2014-02-25 11:00:46 +00:00
|
|
|
def _contains_branded(self, node):
|
|
|
|
return node.tag == 't'\
|
|
|
|
or 't-raw' in node.attrib\
|
|
|
|
or any(self.is_node_branded(child) for child in node.iterdescendants())
|
|
|
|
|
|
|
|
def _pop_view_branding(self, element):
|
|
|
|
distributed_branding = dict(
|
|
|
|
(attribute, element.attrib.pop(attribute))
|
|
|
|
for attribute in MOVABLE_BRANDING
|
|
|
|
if element.get(attribute))
|
|
|
|
return distributed_branding
|
|
|
|
|
2013-09-19 13:08:47 +00:00
|
|
|
def distribute_branding(self, e, branding=None, parent_xpath='',
|
|
|
|
index_map=misc.ConstantMapping(1)):
|
2013-09-17 15:45:05 +00:00
|
|
|
if e.get('t-ignore') or e.tag == 'head':
|
2014-02-25 11:47:06 +00:00
|
|
|
# remove any view branding possibly injected by inheritance
|
|
|
|
attrs = set(MOVABLE_BRANDING)
|
|
|
|
for descendant in e.iterdescendants(tag=etree.Element):
|
|
|
|
if not attrs.intersection(descendant.attrib): continue
|
|
|
|
self._pop_view_branding(descendant)
|
2013-07-31 12:36:03 +00:00
|
|
|
# TODO: find a better name and check if we have a string to boolean helper
|
|
|
|
return
|
2013-09-19 13:08:47 +00:00
|
|
|
|
|
|
|
node_path = e.get('data-oe-xpath')
|
|
|
|
if node_path is None:
|
|
|
|
node_path = "%s/%s[%d]" % (parent_xpath, e.tag, index_map[e.tag])
|
2013-09-17 15:45:05 +00:00
|
|
|
if branding and not (e.get('data-oe-model') or e.get('t-field')):
|
2013-06-30 01:09:41 +00:00
|
|
|
e.attrib.update(branding)
|
2013-09-19 13:08:47 +00:00
|
|
|
e.set('data-oe-xpath', node_path)
|
2013-09-17 15:45:05 +00:00
|
|
|
if not e.get('data-oe-model'): return
|
2013-08-02 10:42:08 +00:00
|
|
|
|
2014-09-09 07:17:32 +00:00
|
|
|
if {'t-esc', 't-raw'}.intersection(e.attrib):
|
2014-02-25 11:00:46 +00:00
|
|
|
# nodes which fully generate their content and have no reason to
|
|
|
|
# be branded because they can not sensibly be edited
|
|
|
|
self._pop_view_branding(e)
|
|
|
|
elif self._contains_branded(e):
|
|
|
|
# if a branded element contains branded elements distribute own
|
|
|
|
# branding to children unless it's t-raw, then just remove branding
|
|
|
|
# on current element
|
|
|
|
distributed_branding = self._pop_view_branding(e)
|
2013-08-02 10:42:08 +00:00
|
|
|
|
|
|
|
if 't-raw' not in e.attrib:
|
2013-09-19 13:08:47 +00:00
|
|
|
# TODO: collections.Counter if remove p2.6 compat
|
|
|
|
# running index by tag type, for XPath query generation
|
|
|
|
indexes = collections.defaultdict(lambda: 0)
|
2013-09-17 15:44:14 +00:00
|
|
|
for child in e.iterchildren(tag=etree.Element):
|
2014-02-13 16:45:46 +00:00
|
|
|
if child.get('data-oe-xpath'):
|
|
|
|
# injected by view inheritance, skip otherwise
|
|
|
|
# generated xpath is incorrect
|
2014-02-24 17:03:01 +00:00
|
|
|
self.distribute_branding(child)
|
|
|
|
else:
|
|
|
|
indexes[child.tag] += 1
|
|
|
|
self.distribute_branding(
|
|
|
|
child, distributed_branding,
|
|
|
|
parent_xpath=node_path, index_map=indexes)
|
2013-06-30 01:09:41 +00:00
|
|
|
|
2013-09-17 16:37:08 +00:00
|
|
|
def is_node_branded(self, node):
|
|
|
|
""" Finds out whether a node is branded or qweb-active (bears a
|
|
|
|
@data-oe-model or a @t-* *which is not t-field* as t-field does not
|
|
|
|
section out views)
|
|
|
|
|
|
|
|
:param node: an etree-compatible element to test
|
|
|
|
:type node: etree._Element
|
|
|
|
:rtype: boolean
|
|
|
|
"""
|
|
|
|
return any(
|
2014-09-08 09:14:44 +00:00
|
|
|
(attr in ('data-oe-model', 'group') or (attr != 't-field' and attr.startswith('t-')))
|
2013-09-17 16:37:08 +00:00
|
|
|
for attr in node.attrib
|
|
|
|
)
|
2013-06-30 01:09:41 +00:00
|
|
|
|
2013-09-23 11:46:49 +00:00
|
|
|
def translate_qweb(self, cr, uid, id_, arch, lang, context=None):
|
|
|
|
# TODO: this should be moved in a place before inheritance is applied
|
|
|
|
# but process() is only called on fields_view_get()
|
|
|
|
Translations = self.pool['ir.translation']
|
|
|
|
h = HTMLParser.HTMLParser()
|
|
|
|
def get_trans(text):
|
|
|
|
if not text or not text.strip():
|
|
|
|
return None
|
|
|
|
text = h.unescape(text.strip())
|
|
|
|
if len(text) < 2 or (text.startswith('<!') and text.endswith('>')):
|
|
|
|
return None
|
|
|
|
return Translations._get_source(cr, uid, 'website', 'view', lang, text, id_)
|
|
|
|
|
2014-08-13 09:08:02 +00:00
|
|
|
if type(arch) not in SKIPPED_ELEMENT_TYPES and arch.tag not in SKIPPED_ELEMENTS:
|
2013-09-23 11:46:49 +00:00
|
|
|
text = get_trans(arch.text)
|
|
|
|
if text:
|
|
|
|
arch.text = arch.text.replace(arch.text.strip(), text)
|
|
|
|
tail = get_trans(arch.tail)
|
|
|
|
if tail:
|
|
|
|
arch.tail = arch.tail.replace(arch.tail.strip(), tail)
|
|
|
|
|
2014-08-13 09:08:02 +00:00
|
|
|
for attr_name in ('title', 'alt', 'label', 'placeholder'):
|
2013-09-23 11:46:49 +00:00
|
|
|
attr = get_trans(arch.get(attr_name))
|
|
|
|
if attr:
|
|
|
|
arch.set(attr_name, attr)
|
|
|
|
for node in arch.iterchildren("*"):
|
|
|
|
self.translate_qweb(cr, uid, id_, node, lang, context)
|
|
|
|
return arch
|
|
|
|
|
2014-01-23 10:19:39 +00:00
|
|
|
@openerp.tools.ormcache()
|
|
|
|
def get_view_xmlid(self, cr, uid, id):
|
|
|
|
imd = self.pool['ir.model.data']
|
|
|
|
domain = [('model', '=', 'ir.ui.view'), ('res_id', '=', id)]
|
|
|
|
xmlid = imd.search_read(cr, uid, domain, ['module', 'name'])[0]
|
|
|
|
return '%s.%s' % (xmlid['module'], xmlid['name'])
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
@api.cr_uid_ids_context
|
2014-01-23 10:19:39 +00:00
|
|
|
def render(self, cr, uid, id_or_xml_id, values=None, engine='ir.qweb', context=None):
|
|
|
|
if isinstance(id_or_xml_id, list):
|
|
|
|
id_or_xml_id = id_or_xml_id[0]
|
|
|
|
|
2013-09-23 11:46:49 +00:00
|
|
|
if not context:
|
|
|
|
context = {}
|
2013-10-08 09:58:05 +00:00
|
|
|
|
2014-02-06 16:12:04 +00:00
|
|
|
if values is None:
|
|
|
|
values = dict()
|
|
|
|
qcontext = dict(
|
|
|
|
keep_query=keep_query,
|
2014-04-09 16:17:58 +00:00
|
|
|
request=request, # might be unbound if we're not in an httprequest context
|
|
|
|
debug=request.debug if request else False,
|
2014-02-06 16:12:04 +00:00
|
|
|
json=simplejson,
|
|
|
|
quote_plus=werkzeug.url_quote_plus,
|
2014-04-23 16:28:27 +00:00
|
|
|
time=time,
|
|
|
|
datetime=datetime,
|
2014-04-24 09:28:28 +00:00
|
|
|
relativedelta=relativedelta,
|
2014-02-06 16:12:04 +00:00
|
|
|
)
|
|
|
|
qcontext.update(values)
|
2014-05-06 16:32:04 +00:00
|
|
|
|
2014-07-01 10:14:55 +00:00
|
|
|
# TODO: This helper can be used by any template that wants to embedd the backend.
|
|
|
|
# It is currently necessary because the ir.ui.view bundle inheritance does not
|
|
|
|
# match the module dependency graph.
|
|
|
|
def get_modules_order():
|
|
|
|
if request:
|
|
|
|
from openerp.addons.web.controllers.main import module_boot
|
|
|
|
return simplejson.dumps(module_boot())
|
|
|
|
return '[]'
|
|
|
|
qcontext['get_modules_order'] = get_modules_order
|
2014-02-06 16:12:04 +00:00
|
|
|
|
2013-06-27 09:13:29 +00:00
|
|
|
def loader(name):
|
2013-11-28 09:48:09 +00:00
|
|
|
return self.read_template(cr, uid, name, context=context)
|
2013-07-18 17:03:14 +00:00
|
|
|
|
2014-04-09 12:46:26 +00:00
|
|
|
return self.pool[engine].render(cr, uid, id_or_xml_id, qcontext, loader=loader, context=context)
|
2013-06-27 09:13:29 +00:00
|
|
|
|
2014-01-19 17:44:37 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Misc
|
|
|
|
#------------------------------------------------------
|
2013-06-29 21:35:16 +00:00
|
|
|
|
|
|
|
def graph_get(self, cr, uid, id, model, node_obj, conn_obj, src_node, des_node, label, scale, context=None):
|
|
|
|
nodes=[]
|
|
|
|
nodes_name=[]
|
|
|
|
transitions=[]
|
|
|
|
start=[]
|
|
|
|
tres={}
|
|
|
|
labels={}
|
|
|
|
no_ancester=[]
|
|
|
|
blank_nodes = []
|
|
|
|
|
|
|
|
_Model_Obj = self.pool[model]
|
|
|
|
_Node_Obj = self.pool[node_obj]
|
|
|
|
_Arrow_Obj = self.pool[conn_obj]
|
|
|
|
|
|
|
|
for model_key,model_value in _Model_Obj._columns.items():
|
|
|
|
if model_value._type=='one2many':
|
|
|
|
if model_value._obj==node_obj:
|
|
|
|
_Node_Field=model_key
|
|
|
|
_Model_Field=model_value._fields_id
|
|
|
|
flag=False
|
|
|
|
for node_key,node_value in _Node_Obj._columns.items():
|
|
|
|
if node_value._type=='one2many':
|
|
|
|
if node_value._obj==conn_obj:
|
|
|
|
if src_node in _Arrow_Obj._columns and flag:
|
|
|
|
_Source_Field=node_key
|
|
|
|
if des_node in _Arrow_Obj._columns and not flag:
|
|
|
|
_Destination_Field=node_key
|
|
|
|
flag = True
|
|
|
|
|
|
|
|
datas = _Model_Obj.read(cr, uid, id, [],context)
|
|
|
|
for a in _Node_Obj.read(cr,uid,datas[_Node_Field],[]):
|
|
|
|
if a[_Source_Field] or a[_Destination_Field]:
|
|
|
|
nodes_name.append((a['id'],a['name']))
|
|
|
|
nodes.append(a['id'])
|
|
|
|
else:
|
|
|
|
blank_nodes.append({'id': a['id'],'name':a['name']})
|
|
|
|
|
|
|
|
if a.has_key('flow_start') and a['flow_start']:
|
|
|
|
start.append(a['id'])
|
|
|
|
else:
|
|
|
|
if not a[_Source_Field]:
|
|
|
|
no_ancester.append(a['id'])
|
|
|
|
for t in _Arrow_Obj.read(cr,uid, a[_Destination_Field],[]):
|
|
|
|
transitions.append((a['id'], t[des_node][0]))
|
|
|
|
tres[str(t['id'])] = (a['id'],t[des_node][0])
|
|
|
|
label_string = ""
|
|
|
|
if label:
|
|
|
|
for lbl in eval(label):
|
|
|
|
if t.has_key(tools.ustr(lbl)) and tools.ustr(t[lbl])=='False':
|
|
|
|
label_string += ' '
|
|
|
|
else:
|
|
|
|
label_string = label_string + " " + tools.ustr(t[lbl])
|
|
|
|
labels[str(t['id'])] = (a['id'],label_string)
|
|
|
|
g = graph(nodes, transitions, no_ancester)
|
|
|
|
g.process(start)
|
|
|
|
g.scale(*scale)
|
|
|
|
result = g.result_get()
|
|
|
|
results = {}
|
|
|
|
for node in nodes_name:
|
|
|
|
results[str(node[0])] = result[node[0]]
|
|
|
|
results[str(node[0])]['name'] = node[1]
|
|
|
|
return {'nodes': results,
|
|
|
|
'transitions': tres,
|
|
|
|
'label' : labels,
|
|
|
|
'blank_nodes': blank_nodes,
|
|
|
|
'node_parent_field': _Model_Field,}
|
|
|
|
|
2013-09-12 18:29:26 +00:00
|
|
|
def _validate_custom_views(self, cr, uid, model):
|
|
|
|
"""Validate architecture of custom views (= without xml id) for a given model.
|
|
|
|
This method is called at the end of registry update.
|
|
|
|
"""
|
|
|
|
cr.execute("""SELECT max(v.id)
|
|
|
|
FROM ir_ui_view v
|
|
|
|
LEFT JOIN ir_model_data md ON (md.model = 'ir.ui.view' AND md.res_id = v.id)
|
|
|
|
WHERE md.module IS NULL
|
|
|
|
AND v.model = %s
|
|
|
|
GROUP BY coalesce(v.inherit_id, v.id)
|
|
|
|
""", (model,))
|
|
|
|
|
|
|
|
ids = map(itemgetter(0), cr.fetchall())
|
|
|
|
return self._check_xml(cr, uid, ids)
|
2013-10-07 09:02:06 +00:00
|
|
|
|
2014-04-18 14:15:50 +00:00
|
|
|
def _validate_module_views(self, cr, uid, module):
|
|
|
|
"""Validate architecture of all the views of a given module"""
|
|
|
|
assert not self.pool._init or module in self.pool._init_modules
|
2014-04-23 14:56:39 +00:00
|
|
|
xmlid_filter = ''
|
|
|
|
params = (module,)
|
|
|
|
if self.pool._init:
|
|
|
|
# only validate the views that are still existing...
|
|
|
|
xmlid_filter = "AND md.name IN %s"
|
|
|
|
names = tuple(name for (xmod, name), (model, res_id) in self.pool.model_data_reference_ids.items() if xmod == module and model == self._name)
|
|
|
|
if not names:
|
|
|
|
# no views for this module, nothing to validate
|
|
|
|
return
|
|
|
|
params += (names,)
|
2014-04-18 14:15:50 +00:00
|
|
|
cr.execute("""SELECT max(v.id)
|
|
|
|
FROM ir_ui_view v
|
|
|
|
LEFT JOIN ir_model_data md ON (md.model = 'ir.ui.view' AND md.res_id = v.id)
|
|
|
|
WHERE md.module = %s
|
2014-04-23 14:56:39 +00:00
|
|
|
{0}
|
2014-04-18 14:15:50 +00:00
|
|
|
GROUP BY coalesce(v.inherit_id, v.id)
|
2014-04-23 14:56:39 +00:00
|
|
|
""".format(xmlid_filter), params)
|
2014-04-18 14:15:50 +00:00
|
|
|
|
|
|
|
for vid, in cr.fetchall():
|
|
|
|
if not self._check_xml(cr, uid, [vid]):
|
|
|
|
self.raise_view_error(cr, uid, "Can't validate view", vid)
|
|
|
|
|
2014-01-19 17:44:37 +00:00
|
|
|
# vim:et:
|