[FIX] fields: inherited fields get their attribute 'state' from their base field
[odoo/odoo.git] / openerp / addons / base / ir / ir_ui_view.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21 import collections
22 import copy
23 import datetime
24 import dateutil
25 from dateutil.relativedelta import relativedelta
26 import fnmatch
27 import logging
28 import os
29 import time
30 from operator import itemgetter
31
32 import simplejson
33 import werkzeug
34 import HTMLParser
35 from lxml import etree
36
37 import openerp
38 from openerp import tools, api
39 from openerp.http import request
40 from openerp.osv import fields, osv, orm
41 from openerp.tools import graph, SKIPPED_ELEMENT_TYPES, SKIPPED_ELEMENTS
42 from openerp.tools.parse_version import parse_version
43 from openerp.tools.safe_eval import safe_eval as eval
44 from openerp.tools.view_validation import valid_view
45 from openerp.tools import misc
46 from openerp.tools.translate import _
47
48 _logger = logging.getLogger(__name__)
49
50 MOVABLE_BRANDING = ['data-oe-model', 'data-oe-id', 'data-oe-field', 'data-oe-xpath', 'data-oe-source-id']
51
52 def keep_query(*keep_params, **additional_params):
53     """
54     Generate a query string keeping the current request querystring's parameters specified
55     in ``keep_params`` and also adds the parameters specified in ``additional_params``.
56
57     Multiple values query string params will be merged into a single one with comma seperated
58     values.
59
60     The ``keep_params`` arguments can use wildcards too, eg:
61
62         keep_query('search', 'shop_*', page=4)
63     """
64     if not keep_params and not additional_params:
65         keep_params = ('*',)
66     params = additional_params.copy()
67     qs_keys = request.httprequest.args.keys()
68     for keep_param in keep_params:
69         for param in fnmatch.filter(qs_keys, keep_param):
70             if param not in additional_params and param in qs_keys:
71                 params[param] = ','.join(request.httprequest.args.getlist(param))
72     return werkzeug.urls.url_encode(params)
73
74 class view_custom(osv.osv):
75     _name = 'ir.ui.view.custom'
76     _order = 'create_date desc'  # search(limit=1) should return the last customization
77     _columns = {
78         'ref_id': fields.many2one('ir.ui.view', 'Original View', select=True, required=True, ondelete='cascade'),
79         'user_id': fields.many2one('res.users', 'User', select=True, required=True, ondelete='cascade'),
80         'arch': fields.text('View Architecture', required=True),
81     }
82
83     def name_get(self, cr, uid, ids, context=None):
84         return [(rec.id, rec.user_id.name) for rec in self.browse(cr, uid, ids, context=context)]
85
86     def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
87         if args is None:
88             args = []
89         if name:
90             ids = self.search(cr, user, [('user_id', operator, name)] + args, limit=limit)
91             return self.name_get(cr, user, ids, context=context)
92         return super(view_custom, self).name_search(cr, user, name, args=args, operator=operator, context=context, limit=limit)
93
94
95     def _auto_init(self, cr, context=None):
96         super(view_custom, self)._auto_init(cr, context)
97         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_ui_view_custom_user_id_ref_id\'')
98         if not cr.fetchone():
99             cr.execute('CREATE INDEX ir_ui_view_custom_user_id_ref_id ON ir_ui_view_custom (user_id, ref_id)')
100
101 def _hasclass(context, *cls):
102     """ Checks if the context node has all the classes passed as arguments
103     """
104     node_classes = set(context.context_node.attrib.get('class', '').split())
105
106     return node_classes.issuperset(cls)
107
108 xpath_utils = etree.FunctionNamespace(None)
109 xpath_utils['hasclass'] = _hasclass
110
111 class view(osv.osv):
112     _name = 'ir.ui.view'
113
114     def _get_model_data(self, cr, uid, ids, fname, args, context=None):
115         result = dict.fromkeys(ids, False)
116         IMD = self.pool['ir.model.data']
117         data_ids = IMD.search_read(cr, uid, [('res_id', 'in', ids), ('model', '=', 'ir.ui.view')], ['res_id'], context=context)
118         result.update(map(itemgetter('res_id', 'id'), data_ids))
119         return result
120
121     def _views_from_model_data(self, cr, uid, ids, context=None):
122         IMD = self.pool['ir.model.data']
123         data_ids = IMD.search_read(cr, uid, [('id', 'in', ids), ('model', '=', 'ir.ui.view')], ['res_id'], context=context)
124         return map(itemgetter('res_id'), data_ids)
125
126     _columns = {
127         'name': fields.char('View Name', required=True),
128         'model': fields.char('Object', select=True),
129         'priority': fields.integer('Sequence', required=True),
130         'type': fields.selection([
131             ('tree','Tree'),
132             ('form','Form'),
133             ('graph', 'Graph'),
134             ('calendar', 'Calendar'),
135             ('diagram','Diagram'),
136             ('gantt', 'Gantt'),
137             ('kanban', 'Kanban'),
138             ('search','Search'),
139             ('qweb', 'QWeb')], string='View Type'),
140         'arch': fields.text('View Architecture', required=True),
141         'inherit_id': fields.many2one('ir.ui.view', 'Inherited View', ondelete='restrict', select=True),
142         'inherit_children_ids': fields.one2many('ir.ui.view','inherit_id', 'Inherit Views'),
143         'field_parent': fields.char('Child Field'),
144         'model_data_id': fields.function(_get_model_data, type='many2one', relation='ir.model.data', string="Model Data",
145                                          store={
146                                              _name: (lambda s, c, u, i, ctx=None: i, None, 10),
147                                              'ir.model.data': (_views_from_model_data, ['model', 'res_id'], 10),
148                                          }),
149         'xml_id': fields.function(osv.osv.get_xml_id, type='char', size=128, string="External ID",
150                                   help="ID of the view defined in xml file"),
151         'groups_id': fields.many2many('res.groups', 'ir_ui_view_group_rel', 'view_id', 'group_id',
152             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."),
153         'model_ids': fields.one2many('ir.model.data', 'res_id', domain=[('model','=','ir.ui.view')], auto_join=True),
154         'create_date': fields.datetime('Create Date', readonly=True),
155         'write_date': fields.datetime('Last Modification Date', readonly=True),
156
157         'mode': fields.selection(
158             [('primary', "Base view"), ('extension', "Extension View")],
159             string="View inheritance mode", required=True,
160             help="""Only applies if this view inherits from an other one (inherit_id is not False/Null).
161
162 * if extension (default), if this view is requested the closest primary view
163   is looked up (via inherit_id), then all views inheriting from it with this
164   view's model are applied
165 * if primary, the closest primary view is fully resolved (even if it uses a
166   different model than this one), then this view's inheritance specs
167   (<xpath/>) are applied, and the result is used as if it were this view's
168   actual arch.
169 """),
170         'active': fields.boolean("Active",
171             help="""If this view is inherited,
172 * if True, the view always extends its parent
173 * if False, the view currently does not extend its parent but can be enabled
174              """),
175     }
176     _defaults = {
177         'mode': 'primary',
178         'active': True,
179         'priority': 16,
180     }
181     _order = "priority,name"
182
183     # Holds the RNG schema
184     _relaxng_validator = None
185
186     def _relaxng(self):
187         if not self._relaxng_validator:
188             frng = tools.file_open(os.path.join('base','rng','view.rng'))
189             try:
190                 relaxng_doc = etree.parse(frng)
191                 self._relaxng_validator = etree.RelaxNG(relaxng_doc)
192             except Exception:
193                 _logger.exception('Failed to load RelaxNG XML schema for views validation')
194             finally:
195                 frng.close()
196         return self._relaxng_validator
197
198     def _check_xml(self, cr, uid, ids, context=None):
199         if context is None:
200             context = {}
201         context = dict(context, check_view_ids=ids)
202
203         # Sanity checks: the view should not break anything upon rendering!
204         # Any exception raised below will cause a transaction rollback.
205         for view in self.browse(cr, uid, ids, context):
206             view_def = self.read_combined(cr, uid, view.id, None, context=context)
207             view_arch_utf8 = view_def['arch']
208             if view.type != 'qweb':
209                 view_doc = etree.fromstring(view_arch_utf8)
210                 # verify that all fields used are valid, etc.
211                 self.postprocess_and_fields(cr, uid, view.model, view_doc, view.id, context=context)
212                 # RNG-based validation is not possible anymore with 7.0 forms
213                 view_docs = [view_doc]
214                 if view_docs[0].tag == 'data':
215                     # A <data> element is a wrapper for multiple root nodes
216                     view_docs = view_docs[0]
217                 validator = self._relaxng()
218                 for view_arch in view_docs:
219                     version = view_arch.get('version', '7.0')
220                     if parse_version(version) < parse_version('7.0') and validator and not validator.validate(view_arch):
221                         for error in validator.error_log:
222                             _logger.error(tools.ustr(error))
223                         return False
224                     if not valid_view(view_arch):
225                         return False
226         return True
227
228     _sql_constraints = [
229         ('inheritance_mode',
230          "CHECK (mode != 'extension' OR inherit_id IS NOT NULL)",
231          "Invalid inheritance mode: if the mode is 'extension', the view must"
232          " extend an other view"),
233     ]
234     _constraints = [
235         (_check_xml, 'Invalid view definition', ['arch']),
236     ]
237
238     def _auto_init(self, cr, context=None):
239         super(view, self)._auto_init(cr, context)
240         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_ui_view_model_type_inherit_id\'')
241         if not cr.fetchone():
242             cr.execute('CREATE INDEX ir_ui_view_model_type_inherit_id ON ir_ui_view (model, inherit_id)')
243
244     def _compute_defaults(self, cr, uid, values, context=None):
245         if 'inherit_id' in values:
246             values.setdefault(
247                 'mode', 'extension' if values['inherit_id'] else 'primary')
248         return values
249
250     def create(self, cr, uid, values, context=None):
251         if not values.get('type'):
252             if values.get('inherit_id'):
253                 values['type'] = self.browse(cr, uid, values['inherit_id'], context).type
254             else:
255                 values['type'] = etree.fromstring(values['arch']).tag
256
257         if not values.get('name'):
258             values['name'] = "%s %s" % (values.get('model'), values['type'])
259
260         self.clear_cache()
261         return super(view, self).create(
262             cr, uid,
263             self._compute_defaults(cr, uid, values, context=context),
264             context=context)
265
266     def write(self, cr, uid, ids, vals, context=None):
267         if not isinstance(ids, (list, tuple)):
268             ids = [ids]
269         if context is None:
270             context = {}
271
272         # drop the corresponding view customizations (used for dashboards for example), otherwise
273         # not all users would see the updated views
274         custom_view_ids = self.pool.get('ir.ui.view.custom').search(cr, uid, [('ref_id', 'in', ids)])
275         if custom_view_ids:
276             self.pool.get('ir.ui.view.custom').unlink(cr, uid, custom_view_ids)
277
278         self.clear_cache()
279         ret = super(view, self).write(
280             cr, uid, ids,
281             self._compute_defaults(cr, uid, vals, context=context),
282             context)
283         return ret
284
285     def toggle(self, cr, uid, ids, context=None):
286         """ Switches between enabled and disabled statuses
287         """
288         for view in self.browse(cr, uid, ids, context=dict(context or {}, active_test=False)):
289             view.write({'active': not view.active})
290
291     # default view selection
292     def default_view(self, cr, uid, model, view_type, context=None):
293         """ Fetches the default view for the provided (model, view_type) pair:
294          primary view with the lowest priority.
295
296         :param str model:
297         :param int view_type:
298         :return: id of the default view of False if none found
299         :rtype: int
300         """
301         domain = [
302             ['model', '=', model],
303             ['type', '=', view_type],
304             ['mode', '=', 'primary'],
305         ]
306         ids = self.search(cr, uid, domain, limit=1, context=context)
307         if not ids:
308             return False
309         return ids[0]
310
311     #------------------------------------------------------
312     # Inheritance mecanism
313     #------------------------------------------------------
314     def get_inheriting_views_arch(self, cr, uid, view_id, model, context=None):
315         """Retrieves the architecture of views that inherit from the given view, from the sets of
316            views that should currently be used in the system. During the module upgrade phase it
317            may happen that a view is present in the database but the fields it relies on are not
318            fully loaded yet. This method only considers views that belong to modules whose code
319            is already loaded. Custom views defined directly in the database are loaded only
320            after the module initialization phase is completely finished.
321
322            :param int view_id: id of the view whose inheriting views should be retrieved
323            :param str model: model identifier of the inheriting views.
324            :rtype: list of tuples
325            :return: [(view_arch,view_id), ...]
326         """
327
328         user = self.pool['res.users'].browse(cr, 1, uid, context=context)
329         user_groups = frozenset(user.groups_id or ())
330
331         conditions = [
332             ['inherit_id', '=', view_id],
333             ['model', '=', model],
334             ['mode', '=', 'extension'],
335             ['active', '=', True],
336         ]
337         if self.pool._init:
338             # Module init currently in progress, only consider views from
339             # modules whose code is already loaded
340             conditions.extend([
341                 '|',
342                 ['model_ids.module', 'in', tuple(self.pool._init_modules)],
343                 ['id', 'in', context and context.get('check_view_ids') or (0,)],
344             ])
345         view_ids = self.search(cr, uid, conditions, context=context)
346
347         return [(view.arch, view.id)
348                 for view in self.browse(cr, 1, view_ids, context)
349                 if not (view.groups_id and user_groups.isdisjoint(view.groups_id))]
350
351     def raise_view_error(self, cr, uid, message, view_id, context=None):
352         view = self.browse(cr, uid, view_id, context)
353         not_avail = _('n/a')
354         message = ("%(msg)s\n\n" +
355                    _("Error context:\nView `%(view_name)s`") + 
356                    "\n[view_id: %(viewid)s, xml_id: %(xmlid)s, "
357                    "model: %(model)s, parent_id: %(parent)s]") % \
358                         {
359                           'view_name': view.name or not_avail, 
360                           'viewid': view_id or not_avail,
361                           'xmlid': view.xml_id or not_avail,
362                           'model': view.model or not_avail,
363                           'parent': view.inherit_id.id or not_avail,
364                           'msg': message,
365                         }
366         _logger.error(message)
367         raise AttributeError(message)
368
369     def locate_node(self, arch, spec):
370         """ Locate a node in a source (parent) architecture.
371
372         Given a complete source (parent) architecture (i.e. the field
373         `arch` in a view), and a 'spec' node (a node in an inheriting
374         view that specifies the location in the source view of what
375         should be changed), return (if it exists) the node in the
376         source view matching the specification.
377
378         :param arch: a parent architecture to modify
379         :param spec: a modifying node in an inheriting view
380         :return: a node in the source matching the spec
381         """
382         if spec.tag == 'xpath':
383             nodes = arch.xpath(spec.get('expr'))
384             return nodes[0] if nodes else None
385         elif spec.tag == 'field':
386             # Only compare the field name: a field can be only once in a given view
387             # at a given level (and for multilevel expressions, we should use xpath
388             # inheritance spec anyway).
389             for node in arch.iter('field'):
390                 if node.get('name') == spec.get('name'):
391                     return node
392             return None
393
394         for node in arch.iter(spec.tag):
395             if isinstance(node, SKIPPED_ELEMENT_TYPES):
396                 continue
397             if all(node.get(attr) == spec.get(attr) for attr in spec.attrib
398                    if attr not in ('position','version')):
399                 # Version spec should match parent's root element's version
400                 if spec.get('version') and spec.get('version') != arch.get('version'):
401                     return None
402                 return node
403         return None
404
405     def inherit_branding(self, specs_tree, view_id, root_id):
406         for node in specs_tree.iterchildren(tag=etree.Element):
407             xpath = node.getroottree().getpath(node)
408             if node.tag == 'data' or node.tag == 'xpath':
409                 self.inherit_branding(node, view_id, root_id)
410             else:
411                 node.set('data-oe-id', str(view_id))
412                 node.set('data-oe-source-id', str(root_id))
413                 node.set('data-oe-xpath', xpath)
414                 node.set('data-oe-model', 'ir.ui.view')
415                 node.set('data-oe-field', 'arch')
416
417         return specs_tree
418
419     def apply_inheritance_specs(self, cr, uid, source, specs_tree, inherit_id, context=None):
420         """ Apply an inheriting view (a descendant of the base view)
421
422         Apply to a source architecture all the spec nodes (i.e. nodes
423         describing where and what changes to apply to some parent
424         architecture) given by an inheriting view.
425
426         :param Element source: a parent architecture to modify
427         :param Elepect specs_tree: a modifying architecture in an inheriting view
428         :param inherit_id: the database id of specs_arch
429         :return: a modified source where the specs are applied
430         :rtype: Element
431         """
432         # Queue of specification nodes (i.e. nodes describing where and
433         # changes to apply to some parent architecture).
434         specs = [specs_tree]
435
436         while len(specs):
437             spec = specs.pop(0)
438             if isinstance(spec, SKIPPED_ELEMENT_TYPES):
439                 continue
440             if spec.tag == 'data':
441                 specs += [c for c in spec]
442                 continue
443             node = self.locate_node(source, spec)
444             if node is not None:
445                 pos = spec.get('position', 'inside')
446                 if pos == 'replace':
447                     if node.getparent() is None:
448                         source = copy.deepcopy(spec[0])
449                     else:
450                         for child in spec:
451                             node.addprevious(child)
452                         node.getparent().remove(node)
453                 elif pos == 'attributes':
454                     for child in spec.getiterator('attribute'):
455                         attribute = (child.get('name'), child.text or None)
456                         if attribute[1]:
457                             node.set(attribute[0], attribute[1])
458                         elif attribute[0] in node.attrib:
459                             del node.attrib[attribute[0]]
460                 else:
461                     sib = node.getnext()
462                     for child in spec:
463                         if pos == 'inside':
464                             node.append(child)
465                         elif pos == 'after':
466                             if sib is None:
467                                 node.addnext(child)
468                                 node = child
469                             else:
470                                 sib.addprevious(child)
471                         elif pos == 'before':
472                             node.addprevious(child)
473                         else:
474                             self.raise_view_error(cr, uid, _("Invalid position attribute: '%s'") % pos, inherit_id, context=context)
475             else:
476                 attrs = ''.join([
477                     ' %s="%s"' % (attr, spec.get(attr))
478                     for attr in spec.attrib
479                     if attr != 'position'
480                 ])
481                 tag = "<%s%s>" % (spec.tag, attrs)
482                 self.raise_view_error(cr, uid, _("Element '%s' cannot be located in parent view") % tag, inherit_id, context=context)
483
484         return source
485
486     def apply_view_inheritance(self, cr, uid, source, source_id, model, root_id=None, context=None):
487         """ Apply all the (directly and indirectly) inheriting views.
488
489         :param source: a parent architecture to modify (with parent modifications already applied)
490         :param source_id: the database view_id of the parent view
491         :param model: the original model for which we create a view (not
492             necessarily the same as the source's model); only the inheriting
493             views with that specific model will be applied.
494         :return: a modified source where all the modifying architecture are applied
495         """
496         if context is None: context = {}
497         if root_id is None:
498             root_id = source_id
499         sql_inherit = self.get_inheriting_views_arch(cr, uid, source_id, model, context=context)
500         for (specs, view_id) in sql_inherit:
501             specs_tree = etree.fromstring(specs.encode('utf-8'))
502             if context.get('inherit_branding'):
503                 self.inherit_branding(specs_tree, view_id, root_id)
504             source = self.apply_inheritance_specs(cr, uid, source, specs_tree, view_id, context=context)
505             source = self.apply_view_inheritance(cr, uid, source, view_id, model, root_id=root_id, context=context)
506         return source
507
508     def read_combined(self, cr, uid, view_id, fields=None, context=None):
509         """
510         Utility function to get a view combined with its inherited views.
511
512         * Gets the top of the view tree if a sub-view is requested
513         * Applies all inherited archs on the root view
514         * Returns the view with all requested fields
515           .. note:: ``arch`` is always added to the fields list even if not
516                     requested (similar to ``id``)
517         """
518         if context is None: context = {}
519
520         # if view_id is not a root view, climb back to the top.
521         base = v = self.browse(cr, uid, view_id, context=context)
522         while v.mode != 'primary':
523             v = v.inherit_id
524         root_id = v.id
525
526         # arch and model fields are always returned
527         if fields:
528             fields = list(set(fields) | set(['arch', 'model']))
529
530         # read the view arch
531         [view] = self.read(cr, uid, [root_id], fields=fields, context=context)
532         view_arch = etree.fromstring(view['arch'].encode('utf-8'))
533         if not v.inherit_id:
534             arch_tree = view_arch
535         else:
536             parent_view = self.read_combined(
537                 cr, uid, v.inherit_id.id, fields=fields, context=context)
538             arch_tree = etree.fromstring(parent_view['arch'])
539             self.apply_inheritance_specs(
540                 cr, uid, arch_tree, view_arch, parent_view['id'], context=context)
541
542
543         if context.get('inherit_branding'):
544             arch_tree.attrib.update({
545                 'data-oe-model': 'ir.ui.view',
546                 'data-oe-id': str(root_id),
547                 'data-oe-field': 'arch',
548             })
549
550         # and apply inheritance
551         arch = self.apply_view_inheritance(
552             cr, uid, arch_tree, root_id, base.model, context=context)
553
554         return dict(view, arch=etree.tostring(arch, encoding='utf-8'))
555
556     #------------------------------------------------------
557     # Postprocessing: translation, groups and modifiers
558     #------------------------------------------------------
559     # TODO: 
560     # - split postprocess so that it can be used instead of translate_qweb
561     # - remove group processing from ir_qweb
562     #------------------------------------------------------
563     def postprocess(self, cr, user, model, node, view_id, in_tree_view, model_fields, context=None):
564         """Return the description of the fields in the node.
565
566         In a normal call to this method, node is a complete view architecture
567         but it is actually possible to give some sub-node (this is used so
568         that the method can call itself recursively).
569
570         Originally, the field descriptions are drawn from the node itself.
571         But there is now some code calling fields_get() in order to merge some
572         of those information in the architecture.
573
574         """
575         if context is None:
576             context = {}
577         result = False
578         fields = {}
579         children = True
580
581         modifiers = {}
582         Model = self.pool.get(model)
583         if Model is None:
584             self.raise_view_error(cr, user, _('Model not found: %(model)s') % dict(model=model),
585                                   view_id, context)
586
587         def encode(s):
588             if isinstance(s, unicode):
589                 return s.encode('utf8')
590             return s
591
592         def check_group(node):
593             """Apply group restrictions,  may be set at view level or model level::
594                * at view level this means the element should be made invisible to
595                  people who are not members
596                * at model level (exclusively for fields, obviously), this means
597                  the field should be completely removed from the view, as it is
598                  completely unavailable for non-members
599
600                :return: True if field should be included in the result of fields_view_get
601             """
602             if node.tag == 'field' and node.get('name') in Model._fields:
603                 field = Model._fields[node.get('name')]
604                 if field.groups and not self.user_has_groups(
605                         cr, user, groups=field.groups, context=context):
606                     node.getparent().remove(node)
607                     fields.pop(node.get('name'), None)
608                     # no point processing view-level ``groups`` anymore, return
609                     return False
610             if node.get('groups'):
611                 can_see = self.user_has_groups(
612                     cr, user, groups=node.get('groups'), context=context)
613                 if not can_see:
614                     node.set('invisible', '1')
615                     modifiers['invisible'] = True
616                     if 'attrs' in node.attrib:
617                         del(node.attrib['attrs']) #avoid making field visible later
618                 del(node.attrib['groups'])
619             return True
620
621         if node.tag in ('field', 'node', 'arrow'):
622             if node.get('object'):
623                 attrs = {}
624                 views = {}
625                 xml = "<form>"
626                 for f in node:
627                     if f.tag == 'field':
628                         xml += etree.tostring(f, encoding="utf-8")
629                 xml += "</form>"
630                 new_xml = etree.fromstring(encode(xml))
631                 ctx = context.copy()
632                 ctx['base_model_name'] = model
633                 xarch, xfields = self.postprocess_and_fields(cr, user, node.get('object'), new_xml, view_id, ctx)
634                 views['form'] = {
635                     'arch': xarch,
636                     'fields': xfields
637                 }
638                 attrs = {'views': views}
639                 fields = xfields
640             if node.get('name'):
641                 attrs = {}
642                 field = Model._fields.get(node.get('name'))
643                 if field:
644                     children = False
645                     views = {}
646                     for f in node:
647                         if f.tag in ('form', 'tree', 'graph', 'kanban', 'calendar'):
648                             node.remove(f)
649                             ctx = context.copy()
650                             ctx['base_model_name'] = model
651                             xarch, xfields = self.postprocess_and_fields(cr, user, field.comodel_name, f, view_id, ctx)
652                             views[str(f.tag)] = {
653                                 'arch': xarch,
654                                 'fields': xfields
655                             }
656                     attrs = {'views': views}
657                 fields[node.get('name')] = attrs
658
659                 field = model_fields.get(node.get('name'))
660                 if field:
661                     orm.transfer_field_to_modifiers(field, modifiers)
662
663         elif node.tag in ('form', 'tree'):
664             result = Model.view_header_get(cr, user, False, node.tag, context)
665             if result:
666                 node.set('string', result)
667             in_tree_view = node.tag == 'tree'
668
669         elif node.tag == 'calendar':
670             for additional_field in ('date_start', 'date_delay', 'date_stop', 'color', 'all_day', 'attendee'):
671                 if node.get(additional_field):
672                     fields[node.get(additional_field)] = {}
673
674         if not check_group(node):
675             # node must be removed, no need to proceed further with its children
676             return fields
677
678         # The view architeture overrides the python model.
679         # Get the attrs before they are (possibly) deleted by check_group below
680         orm.transfer_node_to_modifiers(node, modifiers, context, in_tree_view)
681
682         # TODO remove attrs counterpart in modifiers when invisible is true ?
683
684         # translate view
685         if 'lang' in context:
686             Translations = self.pool['ir.translation']
687             if node.text and node.text.strip():
688                 term = node.text.strip()
689                 trans = Translations._get_source(cr, user, model, 'view', context['lang'], term)
690                 if trans:
691                     node.text = node.text.replace(term, trans)
692             if node.tail and node.tail.strip():
693                 term = node.tail.strip()
694                 trans = Translations._get_source(cr, user, model, 'view', context['lang'], term)
695                 if trans:
696                     node.tail =  node.tail.replace(term, trans)
697
698             if node.get('string') and node.get('string').strip() and not result:
699                 term = node.get('string').strip()
700                 trans = Translations._get_source(cr, user, model, 'view', context['lang'], term)
701                 if trans == term and ('base_model_name' in context):
702                     # If translation is same as source, perhaps we'd have more luck with the alternative model name
703                     # (in case we are in a mixed situation, such as an inherited view where parent_view.model != model
704                     trans = Translations._get_source(cr, user, context['base_model_name'], 'view', context['lang'], term)
705                 if trans:
706                     node.set('string', trans)
707
708             for attr_name in ('confirm', 'sum', 'avg', 'help', 'placeholder'):
709                 attr_value = node.get(attr_name)
710                 if attr_value and attr_value.strip():
711                     trans = Translations._get_source(cr, user, model, 'view', context['lang'], attr_value.strip())
712                     if trans:
713                         node.set(attr_name, trans)
714
715         for f in node:
716             if children or (node.tag == 'field' and f.tag in ('filter','separator')):
717                 fields.update(self.postprocess(cr, user, model, f, view_id, in_tree_view, model_fields, context))
718
719         orm.transfer_modifiers_to_node(modifiers, node)
720         return fields
721
722     def add_on_change(self, cr, user, model_name, arch):
723         """ Add attribute on_change="1" on fields that are dependencies of
724             computed fields on the same view.
725         """
726         # map each field object to its corresponding nodes in arch
727         field_nodes = collections.defaultdict(list)
728
729         def collect(node, model):
730             if node.tag == 'field':
731                 field = model._fields.get(node.get('name'))
732                 if field:
733                     field_nodes[field].append(node)
734                     if field.relational:
735                         model = self.pool.get(field.comodel_name)
736             for child in node:
737                 collect(child, model)
738
739         collect(arch, self.pool[model_name])
740
741         for field, nodes in field_nodes.iteritems():
742             # if field should trigger an onchange, add on_change="1" on the
743             # nodes referring to field
744             model = self.pool[field.model_name]
745             if model._has_onchange(field, field_nodes):
746                 for node in nodes:
747                     if not node.get('on_change'):
748                         node.set('on_change', '1')
749
750         return arch
751
752     def _disable_workflow_buttons(self, cr, user, model, node):
753         """ Set the buttons in node to readonly if the user can't activate them. """
754         if model is None or user == 1:
755             # admin user can always activate workflow buttons
756             return node
757
758         # TODO handle the case of more than one workflow for a model or multiple
759         # transitions with different groups and same signal
760         usersobj = self.pool.get('res.users')
761         buttons = (n for n in node.getiterator('button') if n.get('type') != 'object')
762         for button in buttons:
763             user_groups = usersobj.read(cr, user, [user], ['groups_id'])[0]['groups_id']
764             cr.execute("""SELECT DISTINCT t.group_id
765                         FROM wkf
766                   INNER JOIN wkf_activity a ON a.wkf_id = wkf.id
767                   INNER JOIN wkf_transition t ON (t.act_to = a.id)
768                        WHERE wkf.osv = %s
769                          AND t.signal = %s
770                          AND t.group_id is NOT NULL
771                    """, (model, button.get('name')))
772             group_ids = [x[0] for x in cr.fetchall() if x[0]]
773             can_click = not group_ids or bool(set(user_groups).intersection(group_ids))
774             button.set('readonly', str(int(not can_click)))
775         return node
776
777     def postprocess_and_fields(self, cr, user, model, node, view_id, context=None):
778         """ Return an architecture and a description of all the fields.
779
780         The field description combines the result of fields_get() and
781         postprocess().
782
783         :param node: the architecture as as an etree
784         :return: a tuple (arch, fields) where arch is the given node as a
785             string and fields is the description of all the fields.
786
787         """
788         fields = {}
789         Model = self.pool.get(model)
790         if Model is None:
791             self.raise_view_error(cr, user, _('Model not found: %(model)s') % dict(model=model), view_id, context)
792
793         if node.tag == 'diagram':
794             if node.getchildren()[0].tag == 'node':
795                 node_model = self.pool[node.getchildren()[0].get('object')]
796                 node_fields = node_model.fields_get(cr, user, None, context)
797                 fields.update(node_fields)
798                 if not node.get("create") and not node_model.check_access_rights(cr, user, 'create', raise_exception=False):
799                     node.set("create", 'false')
800             if node.getchildren()[1].tag == 'arrow':
801                 arrow_fields = self.pool[node.getchildren()[1].get('object')].fields_get(cr, user, None, context)
802                 fields.update(arrow_fields)
803         else:
804             fields = Model.fields_get(cr, user, None, context)
805
806         node = self.add_on_change(cr, user, model, node)
807         fields_def = self.postprocess(cr, user, model, node, view_id, False, fields, context=context)
808         node = self._disable_workflow_buttons(cr, user, model, node)
809         if node.tag in ('kanban', 'tree', 'form', 'gantt'):
810             for action, operation in (('create', 'create'), ('delete', 'unlink'), ('edit', 'write')):
811                 if not node.get(action) and not Model.check_access_rights(cr, user, operation, raise_exception=False):
812                     node.set(action, 'false')
813         if node.tag in ('kanban'):
814             group_by_name = node.get('default_group_by')
815             if group_by_name in Model._fields:
816                 group_by_field = Model._fields[group_by_name]
817                 if group_by_field.type == 'many2one':
818                     group_by_model = Model.pool[group_by_field.comodel_name]
819                     for action, operation in (('group_create', 'create'), ('group_delete', 'unlink'), ('group_edit', 'write')):
820                         if not node.get(action) and not group_by_model.check_access_rights(cr, user, operation, raise_exception=False):
821                             node.set(action, 'false')
822
823         arch = etree.tostring(node, encoding="utf-8").replace('\t', '')
824         for k in fields.keys():
825             if k not in fields_def:
826                 del fields[k]
827         for field in fields_def:
828             if field == 'id':
829                 # sometime, the view may contain the (invisible) field 'id' needed for a domain (when 2 objects have cross references)
830                 fields['id'] = {'readonly': True, 'type': 'integer', 'string': 'ID'}
831             elif field in fields:
832                 fields[field].update(fields_def[field])
833             else:
834                 message = _("Field `%(field_name)s` does not exist") % \
835                                 dict(field_name=field)
836                 self.raise_view_error(cr, user, message, view_id, context)
837         return arch, fields
838
839     #------------------------------------------------------
840     # QWeb template views
841     #------------------------------------------------------
842     @tools.ormcache_context(accepted_keys=('lang','inherit_branding', 'editable', 'translatable'))
843     def read_template(self, cr, uid, xml_id, context=None):
844         if isinstance(xml_id, (int, long)):
845             view_id = xml_id
846         else:
847             if '.' not in xml_id:
848                 raise ValueError('Invalid template id: %r' % (xml_id,))
849             view_id = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, xml_id, raise_if_not_found=True)
850
851         arch = self.read_combined(cr, uid, view_id, fields=['arch'], context=context)['arch']
852         arch_tree = etree.fromstring(arch)
853
854         if 'lang' in context:
855             arch_tree = self.translate_qweb(cr, uid, view_id, arch_tree, context['lang'], context)
856
857         self.distribute_branding(arch_tree)
858         root = etree.Element('templates')
859         root.append(arch_tree)
860         arch = etree.tostring(root, encoding='utf-8', xml_declaration=True)
861         return arch
862
863     def clear_cache(self):
864         self.read_template.clear_cache(self)
865
866     def _contains_branded(self, node):
867         return node.tag == 't'\
868             or 't-raw' in node.attrib\
869             or any(self.is_node_branded(child) for child in node.iterdescendants())
870
871     def _pop_view_branding(self, element):
872         distributed_branding = dict(
873             (attribute, element.attrib.pop(attribute))
874             for attribute in MOVABLE_BRANDING
875             if element.get(attribute))
876         return distributed_branding
877
878     def distribute_branding(self, e, branding=None, parent_xpath='',
879                             index_map=misc.ConstantMapping(1)):
880         if e.get('t-ignore') or e.tag == 'head':
881             # remove any view branding possibly injected by inheritance
882             attrs = set(MOVABLE_BRANDING)
883             for descendant in e.iterdescendants(tag=etree.Element):
884                 if not attrs.intersection(descendant.attrib): continue
885                 self._pop_view_branding(descendant)
886             # TODO: find a better name and check if we have a string to boolean helper
887             return
888
889         node_path = e.get('data-oe-xpath')
890         if node_path is None:
891             node_path = "%s/%s[%d]" % (parent_xpath, e.tag, index_map[e.tag])
892         if branding and not (e.get('data-oe-model') or e.get('t-field')):
893             e.attrib.update(branding)
894             e.set('data-oe-xpath', node_path)
895         if not e.get('data-oe-model'): return
896
897         if {'t-esc', 't-raw'}.intersection(e.attrib):
898             # nodes which fully generate their content and have no reason to
899             # be branded because they can not sensibly be edited
900             self._pop_view_branding(e)
901         elif self._contains_branded(e):
902             # if a branded element contains branded elements distribute own
903             # branding to children unless it's t-raw, then just remove branding
904             # on current element
905             distributed_branding = self._pop_view_branding(e)
906
907             if 't-raw' not in e.attrib:
908                 # TODO: collections.Counter if remove p2.6 compat
909                 # running index by tag type, for XPath query generation
910                 indexes = collections.defaultdict(lambda: 0)
911                 for child in e.iterchildren(tag=etree.Element):
912                     if child.get('data-oe-xpath'):
913                         # injected by view inheritance, skip otherwise
914                         # generated xpath is incorrect
915                         self.distribute_branding(child)
916                     else:
917                         indexes[child.tag] += 1
918                         self.distribute_branding(
919                             child, distributed_branding,
920                             parent_xpath=node_path, index_map=indexes)
921
922     def is_node_branded(self, node):
923         """ Finds out whether a node is branded or qweb-active (bears a
924         @data-oe-model or a @t-* *which is not t-field* as t-field does not
925         section out views)
926
927         :param node: an etree-compatible element to test
928         :type node: etree._Element
929         :rtype: boolean
930         """
931         return any(
932             (attr in ('data-oe-model', 'group') or (attr != 't-field' and attr.startswith('t-')))
933             for attr in node.attrib
934         )
935
936     def translate_qweb(self, cr, uid, id_, arch, lang, context=None):
937         # TODO: this should be moved in a place before inheritance is applied
938         #       but process() is only called on fields_view_get()
939         Translations = self.pool['ir.translation']
940         h = HTMLParser.HTMLParser()
941         def get_trans(text):
942             if not text or not text.strip():
943                 return None
944             text = h.unescape(text.strip())
945             if len(text) < 2 or (text.startswith('<!') and text.endswith('>')):
946                 return None
947             return Translations._get_source(cr, uid, 'website', 'view', lang, text, id_)
948
949         if type(arch) not in SKIPPED_ELEMENT_TYPES and arch.tag not in SKIPPED_ELEMENTS:
950             text = get_trans(arch.text)
951             if text:
952                 arch.text = arch.text.replace(arch.text.strip(), text)
953             tail = get_trans(arch.tail)
954             if tail:
955                 arch.tail = arch.tail.replace(arch.tail.strip(), tail)
956
957             for attr_name in ('title', 'alt', 'label', 'placeholder'):
958                 attr = get_trans(arch.get(attr_name))
959                 if attr:
960                     arch.set(attr_name, attr)
961             for node in arch.iterchildren("*"):
962                 self.translate_qweb(cr, uid, id_, node, lang, context)
963         return arch
964
965     @openerp.tools.ormcache()
966     def get_view_xmlid(self, cr, uid, id):
967         imd = self.pool['ir.model.data']
968         domain = [('model', '=', 'ir.ui.view'), ('res_id', '=', id)]
969         xmlid = imd.search_read(cr, uid, domain, ['module', 'name'])[0]
970         return '%s.%s' % (xmlid['module'], xmlid['name'])
971
972     @api.cr_uid_ids_context
973     def render(self, cr, uid, id_or_xml_id, values=None, engine='ir.qweb', context=None):
974         if isinstance(id_or_xml_id, list):
975             id_or_xml_id = id_or_xml_id[0]
976
977         if not context:
978             context = {}
979
980         if values is None:
981             values = dict()
982         qcontext = dict(
983             keep_query=keep_query,
984             request=request, # might be unbound if we're not in an httprequest context
985             debug=request.debug if request else False,
986             json=simplejson,
987             quote_plus=werkzeug.url_quote_plus,
988             time=time,
989             datetime=datetime,
990             relativedelta=relativedelta,
991         )
992         qcontext.update(values)
993
994         # TODO: This helper can be used by any template that wants to embedd the backend.
995         #       It is currently necessary because the ir.ui.view bundle inheritance does not
996         #       match the module dependency graph.
997         def get_modules_order():
998             if request:
999                 from openerp.addons.web.controllers.main import module_boot
1000                 return simplejson.dumps(module_boot())
1001             return '[]'
1002         qcontext['get_modules_order'] = get_modules_order
1003
1004         def loader(name):
1005             return self.read_template(cr, uid, name, context=context)
1006
1007         return self.pool[engine].render(cr, uid, id_or_xml_id, qcontext, loader=loader, context=context)
1008
1009     #------------------------------------------------------
1010     # Misc
1011     #------------------------------------------------------
1012
1013     def graph_get(self, cr, uid, id, model, node_obj, conn_obj, src_node, des_node, label, scale, context=None):
1014         nodes=[]
1015         nodes_name=[]
1016         transitions=[]
1017         start=[]
1018         tres={}
1019         labels={}
1020         no_ancester=[]
1021         blank_nodes = []
1022
1023         _Model_Obj = self.pool[model]
1024         _Node_Obj = self.pool[node_obj]
1025         _Arrow_Obj = self.pool[conn_obj]
1026
1027         for model_key,model_value in _Model_Obj._columns.items():
1028                 if model_value._type=='one2many':
1029                     if model_value._obj==node_obj:
1030                         _Node_Field=model_key
1031                         _Model_Field=model_value._fields_id
1032                     flag=False
1033                     for node_key,node_value in _Node_Obj._columns.items():
1034                         if node_value._type=='one2many':
1035                              if node_value._obj==conn_obj:
1036                                  if src_node in _Arrow_Obj._columns and flag:
1037                                     _Source_Field=node_key
1038                                  if des_node in _Arrow_Obj._columns and not flag:
1039                                     _Destination_Field=node_key
1040                                     flag = True
1041
1042         datas = _Model_Obj.read(cr, uid, id, [],context)
1043         for a in _Node_Obj.read(cr,uid,datas[_Node_Field],[]):
1044             if a[_Source_Field] or a[_Destination_Field]:
1045                 nodes_name.append((a['id'],a['name']))
1046                 nodes.append(a['id'])
1047             else:
1048                 blank_nodes.append({'id': a['id'],'name':a['name']})
1049
1050             if a.has_key('flow_start') and a['flow_start']:
1051                 start.append(a['id'])
1052             else:
1053                 if not a[_Source_Field]:
1054                     no_ancester.append(a['id'])
1055             for t in _Arrow_Obj.read(cr,uid, a[_Destination_Field],[]):
1056                 transitions.append((a['id'], t[des_node][0]))
1057                 tres[str(t['id'])] = (a['id'],t[des_node][0])
1058                 label_string = ""
1059                 if label:
1060                     for lbl in eval(label):
1061                         if t.has_key(tools.ustr(lbl)) and tools.ustr(t[lbl])=='False':
1062                             label_string += ' '
1063                         else:
1064                             label_string = label_string + " " + tools.ustr(t[lbl])
1065                 labels[str(t['id'])] = (a['id'],label_string)
1066         g  = graph(nodes, transitions, no_ancester)
1067         g.process(start)
1068         g.scale(*scale)
1069         result = g.result_get()
1070         results = {}
1071         for node in nodes_name:
1072             results[str(node[0])] = result[node[0]]
1073             results[str(node[0])]['name'] = node[1]
1074         return {'nodes': results,
1075                 'transitions': tres,
1076                 'label' : labels,
1077                 'blank_nodes': blank_nodes,
1078                 'node_parent_field': _Model_Field,}
1079
1080     def _validate_custom_views(self, cr, uid, model):
1081         """Validate architecture of custom views (= without xml id) for a given model.
1082             This method is called at the end of registry update.
1083         """
1084         cr.execute("""SELECT max(v.id)
1085                         FROM ir_ui_view v
1086                    LEFT JOIN ir_model_data md ON (md.model = 'ir.ui.view' AND md.res_id = v.id)
1087                        WHERE md.module IS NULL
1088                          AND v.model = %s
1089                     GROUP BY coalesce(v.inherit_id, v.id)
1090                    """, (model,))
1091
1092         ids = map(itemgetter(0), cr.fetchall())
1093         return self._check_xml(cr, uid, ids)
1094
1095     def _validate_module_views(self, cr, uid, module):
1096         """Validate architecture of all the views of a given module"""
1097         assert not self.pool._init or module in self.pool._init_modules
1098         xmlid_filter = ''
1099         params = (module,)
1100         if self.pool._init:
1101             # only validate the views that are still existing...
1102             xmlid_filter = "AND md.name IN %s"
1103             names = tuple(name for (xmod, name), (model, res_id) in self.pool.model_data_reference_ids.items() if xmod == module and model == self._name)
1104             if not names:
1105                 # no views for this module, nothing to validate
1106                 return
1107             params += (names,)
1108         cr.execute("""SELECT max(v.id)
1109                         FROM ir_ui_view v
1110                    LEFT JOIN ir_model_data md ON (md.model = 'ir.ui.view' AND md.res_id = v.id)
1111                        WHERE md.module = %s
1112                          {0}
1113                     GROUP BY coalesce(v.inherit_id, v.id)
1114                    """.format(xmlid_filter), params)
1115
1116         for vid, in cr.fetchall():
1117             if not self._check_xml(cr, uid, [vid]):
1118                 self.raise_view_error(cr, uid, "Can't validate view", vid)
1119
1120 # vim:et: