1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
25 from dateutil.relativedelta import relativedelta
30 from operator import itemgetter
35 from lxml import etree
38 from openerp import tools
39 from openerp.http import request
40 from openerp.osv import fields, osv, orm
41 from openerp.tools import graph, SKIPPED_ELEMENT_TYPES
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 _
48 _logger = logging.getLogger(__name__)
50 MOVABLE_BRANDING = ['data-oe-model', 'data-oe-id', 'data-oe-field', 'data-oe-xpath']
52 def keep_query(*keep_params, **additional_params):
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``.
57 Multiple values query string params will be merged into a single one with comma seperated
60 The ``keep_params`` arguments can use wildcards too, eg:
62 keep_query('search', 'shop_*', page=4)
64 if not keep_params and not additional_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)
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
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),
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)]
86 def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
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)
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\'')
99 cr.execute('CREATE INDEX ir_ui_view_custom_user_id_ref_id ON ir_ui_view_custom (user_id, ref_id)')
101 def _hasclass(context, *cls):
102 """ Checks if the context node has all the classes passed as arguments
104 node_classes = set(context.context_node.attrib.get('class', '').split())
106 return node_classes.issuperset(cls)
108 xpath_utils = etree.FunctionNamespace(None)
109 xpath_utils['hasclass'] = _hasclass
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))
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)
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([
134 ('calendar', 'Calendar'),
135 ('diagram','Diagram'),
137 ('kanban', 'Kanban'),
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='cascade', 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",
146 _name: (lambda s, c, u, i, ctx=None: i, None, 10),
147 'ir.model.data': (_views_from_model_data, ['model', 'res_id'], 10),
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),
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).
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
170 'application': fields.selection([
171 ('always', "Always applied"),
172 ('enabled', "Optional, enabled"),
173 ('disabled', "Optional, disabled"),
175 required=True, string="Application status",
176 help="""If this view is inherited,
177 * if always, the view always extends its parent
178 * if enabled, the view currently extends its parent but can be disabled
179 * if disabled, the view currently does not extend its parent but can be enabled
184 'application': 'always',
187 _order = "priority,name"
189 # Holds the RNG schema
190 _relaxng_validator = None
193 if not self._relaxng_validator:
194 frng = tools.file_open(os.path.join('base','rng','view.rng'))
196 relaxng_doc = etree.parse(frng)
197 self._relaxng_validator = etree.RelaxNG(relaxng_doc)
199 _logger.exception('Failed to load RelaxNG XML schema for views validation')
202 return self._relaxng_validator
204 def _check_xml(self, cr, uid, ids, context=None):
207 context = dict(context, check_view_ids=ids)
209 # Sanity checks: the view should not break anything upon rendering!
210 # Any exception raised below will cause a transaction rollback.
211 for view in self.browse(cr, uid, ids, context):
212 view_def = self.read_combined(cr, uid, view.id, None, context=context)
213 view_arch_utf8 = view_def['arch']
214 if view.type != 'qweb':
215 view_doc = etree.fromstring(view_arch_utf8)
216 # verify that all fields used are valid, etc.
217 self.postprocess_and_fields(cr, uid, view.model, view_doc, view.id, context=context)
218 # RNG-based validation is not possible anymore with 7.0 forms
219 view_docs = [view_doc]
220 if view_docs[0].tag == 'data':
221 # A <data> element is a wrapper for multiple root nodes
222 view_docs = view_docs[0]
223 validator = self._relaxng()
224 for view_arch in view_docs:
225 version = view_arch.get('version', '7.0')
226 if parse_version(version) < parse_version('7.0') and validator and not validator.validate(view_arch):
227 for error in validator.error_log:
228 _logger.error(tools.ustr(error))
230 if not valid_view(view_arch):
236 "CHECK (mode != 'extension' OR inherit_id IS NOT NULL)",
237 "Invalid inheritance mode: if the mode is 'extension', the view must"
238 " extend an other view"),
241 (_check_xml, 'Invalid view definition', ['arch']),
244 def _auto_init(self, cr, context=None):
245 super(view, self)._auto_init(cr, context)
246 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_ui_view_model_type_inherit_id\'')
247 if not cr.fetchone():
248 cr.execute('CREATE INDEX ir_ui_view_model_type_inherit_id ON ir_ui_view (model, inherit_id)')
250 def _compute_defaults(self, cr, uid, values, context=None):
251 if 'inherit_id' in values:
253 'mode', 'extension' if values['inherit_id'] else 'primary')
256 def create(self, cr, uid, values, context=None):
257 if 'type' not in values:
258 if values.get('inherit_id'):
259 values['type'] = self.browse(cr, uid, values['inherit_id'], context).type
261 values['type'] = etree.fromstring(values['arch']).tag
263 if not values.get('name'):
264 values['name'] = "%s %s" % (values.get('model'), values['type'])
266 self.read_template.clear_cache(self)
267 return super(view, self).create(
269 self._compute_defaults(cr, uid, values, context=context),
272 def write(self, cr, uid, ids, vals, context=None):
273 if not isinstance(ids, (list, tuple)):
278 # drop the corresponding view customizations (used for dashboards for example), otherwise
279 # not all users would see the updated views
280 custom_view_ids = self.pool.get('ir.ui.view.custom').search(cr, uid, [('ref_id', 'in', ids)])
282 self.pool.get('ir.ui.view.custom').unlink(cr, uid, custom_view_ids)
284 self.read_template.clear_cache(self)
285 ret = super(view, self).write(
287 self._compute_defaults(cr, uid, vals, context=context),
291 def toggle(self, cr, uid, ids, context=None):
292 """ Switches between enabled and disabled application statuses
294 for view in self.browse(cr, uid, ids, context=context):
295 if view.application == 'enabled':
296 view.write({'application': 'disabled'})
297 elif view.application == 'disabled':
298 view.write({'application': 'enabled'})
300 raise ValueError(_("Can't toggle view %d with application %r") % (
306 def copy(self, cr, uid, id, default=None, context=None):
312 return super(view, self).copy(cr, uid, id, default, context=context)
314 # default view selection
315 def default_view(self, cr, uid, model, view_type, context=None):
316 """ Fetches the default view for the provided (model, view_type) pair:
317 primary view with the lowest priority.
320 :param int view_type:
321 :return: id of the default view of False if none found
325 ['model', '=', model],
326 ['type', '=', view_type],
327 ['mode', '=', 'primary'],
329 ids = self.search(cr, uid, domain, limit=1, context=context)
334 #------------------------------------------------------
335 # Inheritance mecanism
336 #------------------------------------------------------
337 def get_inheriting_views_arch(self, cr, uid, view_id, model, context=None):
338 """Retrieves the architecture of views that inherit from the given view, from the sets of
339 views that should currently be used in the system. During the module upgrade phase it
340 may happen that a view is present in the database but the fields it relies on are not
341 fully loaded yet. This method only considers views that belong to modules whose code
342 is already loaded. Custom views defined directly in the database are loaded only
343 after the module initialization phase is completely finished.
345 :param int view_id: id of the view whose inheriting views should be retrieved
346 :param str model: model identifier of the inheriting views.
347 :rtype: list of tuples
348 :return: [(view_arch,view_id), ...]
351 user = self.pool['res.users'].browse(cr, 1, uid, context=context)
352 user_groups = frozenset(user.groups_id or ())
355 ['inherit_id', '=', view_id],
356 ['model', '=', model],
357 ['mode', '=', 'extension'],
358 ['application', 'in', ['always', 'enabled']],
361 # Module init currently in progress, only consider views from
362 # modules whose code is already loaded
365 ['model_ids.module', 'in', tuple(self.pool._init_modules)],
366 ['id', 'in', context and context.get('check_view_ids') or (0,)],
368 view_ids = self.search(cr, uid, conditions, context=context)
370 return [(view.arch, view.id)
371 for view in self.browse(cr, 1, view_ids, context)
372 if not (view.groups_id and user_groups.isdisjoint(view.groups_id))]
374 def raise_view_error(self, cr, uid, message, view_id, context=None):
375 view = self.browse(cr, uid, view_id, context)
377 message = ("%(msg)s\n\n" +
378 _("Error context:\nView `%(view_name)s`") +
379 "\n[view_id: %(viewid)s, xml_id: %(xmlid)s, "
380 "model: %(model)s, parent_id: %(parent)s]") % \
382 'view_name': view.name or not_avail,
383 'viewid': view_id or not_avail,
384 'xmlid': view.xml_id or not_avail,
385 'model': view.model or not_avail,
386 'parent': view.inherit_id.id or not_avail,
389 _logger.error(message)
390 raise AttributeError(message)
392 def locate_node(self, arch, spec):
393 """ Locate a node in a source (parent) architecture.
395 Given a complete source (parent) architecture (i.e. the field
396 `arch` in a view), and a 'spec' node (a node in an inheriting
397 view that specifies the location in the source view of what
398 should be changed), return (if it exists) the node in the
399 source view matching the specification.
401 :param arch: a parent architecture to modify
402 :param spec: a modifying node in an inheriting view
403 :return: a node in the source matching the spec
405 if spec.tag == 'xpath':
406 nodes = arch.xpath(spec.get('expr'))
407 return nodes[0] if nodes else None
408 elif spec.tag == 'field':
409 # Only compare the field name: a field can be only once in a given view
410 # at a given level (and for multilevel expressions, we should use xpath
411 # inheritance spec anyway).
412 for node in arch.iter('field'):
413 if node.get('name') == spec.get('name'):
417 for node in arch.iter(spec.tag):
418 if isinstance(node, SKIPPED_ELEMENT_TYPES):
420 if all(node.get(attr) == spec.get(attr) for attr in spec.attrib
421 if attr not in ('position','version')):
422 # Version spec should match parent's root element's version
423 if spec.get('version') and spec.get('version') != arch.get('version'):
428 def inherit_branding(self, specs_tree, view_id, root_id):
429 for node in specs_tree.iterchildren(tag=etree.Element):
430 xpath = node.getroottree().getpath(node)
431 if node.tag == 'data' or node.tag == 'xpath':
432 self.inherit_branding(node, view_id, root_id)
434 node.set('data-oe-id', str(view_id))
435 node.set('data-oe-source-id', str(root_id))
436 node.set('data-oe-xpath', xpath)
437 node.set('data-oe-model', 'ir.ui.view')
438 node.set('data-oe-field', 'arch')
442 def apply_inheritance_specs(self, cr, uid, source, specs_tree, inherit_id, context=None):
443 """ Apply an inheriting view (a descendant of the base view)
445 Apply to a source architecture all the spec nodes (i.e. nodes
446 describing where and what changes to apply to some parent
447 architecture) given by an inheriting view.
449 :param Element source: a parent architecture to modify
450 :param Elepect specs_tree: a modifying architecture in an inheriting view
451 :param inherit_id: the database id of specs_arch
452 :return: a modified source where the specs are applied
455 # Queue of specification nodes (i.e. nodes describing where and
456 # changes to apply to some parent architecture).
461 if isinstance(spec, SKIPPED_ELEMENT_TYPES):
463 if spec.tag == 'data':
464 specs += [c for c in spec]
466 node = self.locate_node(source, spec)
468 pos = spec.get('position', 'inside')
470 if node.getparent() is None:
471 source = copy.deepcopy(spec[0])
474 node.addprevious(child)
475 node.getparent().remove(node)
476 elif pos == 'attributes':
477 for child in spec.getiterator('attribute'):
478 attribute = (child.get('name'), child.text or None)
480 node.set(attribute[0], attribute[1])
481 elif attribute[0] in node.attrib:
482 del node.attrib[attribute[0]]
493 sib.addprevious(child)
494 elif pos == 'before':
495 node.addprevious(child)
497 self.raise_view_error(cr, uid, _("Invalid position attribute: '%s'") % pos, inherit_id, context=context)
500 ' %s="%s"' % (attr, spec.get(attr))
501 for attr in spec.attrib
502 if attr != 'position'
504 tag = "<%s%s>" % (spec.tag, attrs)
505 self.raise_view_error(cr, uid, _("Element '%s' cannot be located in parent view") % tag, inherit_id, context=context)
509 def apply_view_inheritance(self, cr, uid, source, source_id, model, root_id=None, context=None):
510 """ Apply all the (directly and indirectly) inheriting views.
512 :param source: a parent architecture to modify (with parent modifications already applied)
513 :param source_id: the database view_id of the parent view
514 :param model: the original model for which we create a view (not
515 necessarily the same as the source's model); only the inheriting
516 views with that specific model will be applied.
517 :return: a modified source where all the modifying architecture are applied
519 if context is None: context = {}
522 sql_inherit = self.pool['ir.ui.view'].get_inheriting_views_arch(cr, uid, source_id, model, context=context)
523 for (specs, view_id) in sql_inherit:
524 specs_tree = etree.fromstring(specs.encode('utf-8'))
525 if context.get('inherit_branding'):
526 self.inherit_branding(specs_tree, view_id, root_id)
527 source = self.apply_inheritance_specs(cr, uid, source, specs_tree, view_id, context=context)
528 source = self.apply_view_inheritance(cr, uid, source, view_id, model, root_id=root_id, context=context)
531 def read_combined(self, cr, uid, view_id, fields=None, context=None):
533 Utility function to get a view combined with its inherited views.
535 * Gets the top of the view tree if a sub-view is requested
536 * Applies all inherited archs on the root view
537 * Returns the view with all requested fields
538 .. note:: ``arch`` is always added to the fields list even if not
539 requested (similar to ``id``)
541 if context is None: context = {}
543 # if view_id is not a root view, climb back to the top.
544 base = v = self.browse(cr, uid, view_id, context=context)
545 while v.mode != 'primary':
549 # arch and model fields are always returned
551 fields = list(set(fields) | set(['arch', 'model']))
554 [view] = self.read(cr, uid, [root_id], fields=fields, context=context)
555 view_arch = etree.fromstring(view['arch'].encode('utf-8'))
557 arch_tree = view_arch
559 parent_view = self.read_combined(
560 cr, uid, v.inherit_id.id, fields=fields, context=context)
561 arch_tree = etree.fromstring(parent_view['arch'])
562 self.apply_inheritance_specs(
563 cr, uid, arch_tree, view_arch, parent_view['id'], context=context)
566 if context.get('inherit_branding'):
567 arch_tree.attrib.update({
568 'data-oe-model': 'ir.ui.view',
569 'data-oe-id': str(root_id),
570 'data-oe-field': 'arch',
573 # and apply inheritance
574 arch = self.apply_view_inheritance(
575 cr, uid, arch_tree, root_id, base.model, context=context)
577 return dict(view, arch=etree.tostring(arch, encoding='utf-8'))
579 #------------------------------------------------------
580 # Postprocessing: translation, groups and modifiers
581 #------------------------------------------------------
583 # - split postprocess so that it can be used instead of translate_qweb
584 # - remove group processing from ir_qweb
585 #------------------------------------------------------
586 def postprocess(self, cr, user, model, node, view_id, in_tree_view, model_fields, context=None):
587 """Return the description of the fields in the node.
589 In a normal call to this method, node is a complete view architecture
590 but it is actually possible to give some sub-node (this is used so
591 that the method can call itself recursively).
593 Originally, the field descriptions are drawn from the node itself.
594 But there is now some code calling fields_get() in order to merge some
595 of those information in the architecture.
605 Model = self.pool.get(model)
607 self.raise_view_error(cr, user, _('Model not found: %(model)s') % dict(model=model),
611 if isinstance(s, unicode):
612 return s.encode('utf8')
615 def check_group(node):
616 """Apply group restrictions, may be set at view level or model level::
617 * at view level this means the element should be made invisible to
618 people who are not members
619 * at model level (exclusively for fields, obviously), this means
620 the field should be completely removed from the view, as it is
621 completely unavailable for non-members
623 :return: True if field should be included in the result of fields_view_get
625 if node.tag == 'field' and node.get('name') in Model._all_columns:
626 column = Model._all_columns[node.get('name')].column
627 if column.groups and not self.user_has_groups(
628 cr, user, groups=column.groups, context=context):
629 node.getparent().remove(node)
630 fields.pop(node.get('name'), None)
631 # no point processing view-level ``groups`` anymore, return
633 if node.get('groups'):
634 can_see = self.user_has_groups(
635 cr, user, groups=node.get('groups'), context=context)
637 node.set('invisible', '1')
638 modifiers['invisible'] = True
639 if 'attrs' in node.attrib:
640 del(node.attrib['attrs']) #avoid making field visible later
641 del(node.attrib['groups'])
644 if node.tag in ('field', 'node', 'arrow'):
645 if node.get('object'):
651 xml += etree.tostring(f, encoding="utf-8")
653 new_xml = etree.fromstring(encode(xml))
655 ctx['base_model_name'] = model
656 xarch, xfields = self.postprocess_and_fields(cr, user, node.get('object'), new_xml, view_id, ctx)
661 attrs = {'views': views}
666 if node.get('name') in Model._columns:
667 column = Model._columns[node.get('name')]
669 column = Model._inherit_fields[node.get('name')][2]
677 if f.tag in ('form', 'tree', 'graph', 'kanban', 'calendar'):
680 ctx['base_model_name'] = model
681 xarch, xfields = self.postprocess_and_fields(cr, user, column._obj or None, f, view_id, ctx)
682 views[str(f.tag)] = {
686 attrs = {'views': views}
687 fields[node.get('name')] = attrs
689 field = model_fields.get(node.get('name'))
691 orm.transfer_field_to_modifiers(field, modifiers)
693 elif node.tag in ('form', 'tree'):
694 result = Model.view_header_get(cr, user, False, node.tag, context)
696 node.set('string', result)
697 in_tree_view = node.tag == 'tree'
699 elif node.tag == 'calendar':
700 for additional_field in ('date_start', 'date_delay', 'date_stop', 'color', 'all_day', 'attendee'):
701 if node.get(additional_field):
702 fields[node.get(additional_field)] = {}
704 if not check_group(node):
705 # node must be removed, no need to proceed further with its children
708 # The view architeture overrides the python model.
709 # Get the attrs before they are (possibly) deleted by check_group below
710 orm.transfer_node_to_modifiers(node, modifiers, context, in_tree_view)
712 # TODO remove attrs counterpart in modifiers when invisible is true ?
715 if 'lang' in context:
716 Translations = self.pool['ir.translation']
717 if node.text and node.text.strip():
718 trans = Translations._get_source(cr, user, model, 'view', context['lang'], node.text.strip())
720 node.text = node.text.replace(node.text.strip(), trans)
721 if node.tail and node.tail.strip():
722 trans = Translations._get_source(cr, user, model, 'view', context['lang'], node.tail.strip())
724 node.tail = node.tail.replace(node.tail.strip(), trans)
726 if node.get('string') and not result:
727 trans = Translations._get_source(cr, user, model, 'view', context['lang'], node.get('string'))
728 if trans == node.get('string') and ('base_model_name' in context):
729 # If translation is same as source, perhaps we'd have more luck with the alternative model name
730 # (in case we are in a mixed situation, such as an inherited view where parent_view.model != model
731 trans = Translations._get_source(cr, user, context['base_model_name'], 'view', context['lang'], node.get('string'))
733 node.set('string', trans)
735 for attr_name in ('confirm', 'sum', 'avg', 'help', 'placeholder'):
736 attr_value = node.get(attr_name)
738 trans = Translations._get_source(cr, user, model, 'view', context['lang'], attr_value)
740 node.set(attr_name, trans)
743 if children or (node.tag == 'field' and f.tag in ('filter','separator')):
744 fields.update(self.postprocess(cr, user, model, f, view_id, in_tree_view, model_fields, context))
746 orm.transfer_modifiers_to_node(modifiers, node)
749 def _disable_workflow_buttons(self, cr, user, model, node):
750 """ Set the buttons in node to readonly if the user can't activate them. """
751 if model is None or user == 1:
752 # admin user can always activate workflow buttons
755 # TODO handle the case of more than one workflow for a model or multiple
756 # transitions with different groups and same signal
757 usersobj = self.pool.get('res.users')
758 buttons = (n for n in node.getiterator('button') if n.get('type') != 'object')
759 for button in buttons:
760 user_groups = usersobj.read(cr, user, [user], ['groups_id'])[0]['groups_id']
761 cr.execute("""SELECT DISTINCT t.group_id
763 INNER JOIN wkf_activity a ON a.wkf_id = wkf.id
764 INNER JOIN wkf_transition t ON (t.act_to = a.id)
767 AND t.group_id is NOT NULL
768 """, (model, button.get('name')))
769 group_ids = [x[0] for x in cr.fetchall() if x[0]]
770 can_click = not group_ids or bool(set(user_groups).intersection(group_ids))
771 button.set('readonly', str(int(not can_click)))
774 def postprocess_and_fields(self, cr, user, model, node, view_id, context=None):
775 """ Return an architecture and a description of all the fields.
777 The field description combines the result of fields_get() and
780 :param node: the architecture as as an etree
781 :return: a tuple (arch, fields) where arch is the given node as a
782 string and fields is the description of all the fields.
786 Model = self.pool.get(model)
788 self.raise_view_error(cr, user, _('Model not found: %(model)s') % dict(model=model), view_id, context)
790 if node.tag == 'diagram':
791 if node.getchildren()[0].tag == 'node':
792 node_model = self.pool[node.getchildren()[0].get('object')]
793 node_fields = node_model.fields_get(cr, user, None, context)
794 fields.update(node_fields)
795 if not node.get("create") and not node_model.check_access_rights(cr, user, 'create', raise_exception=False):
796 node.set("create", 'false')
797 if node.getchildren()[1].tag == 'arrow':
798 arrow_fields = self.pool[node.getchildren()[1].get('object')].fields_get(cr, user, None, context)
799 fields.update(arrow_fields)
801 fields = Model.fields_get(cr, user, None, context)
803 fields_def = self.postprocess(cr, user, model, node, view_id, False, fields, context=context)
804 node = self._disable_workflow_buttons(cr, user, model, node)
805 if node.tag in ('kanban', 'tree', 'form', 'gantt'):
806 for action, operation in (('create', 'create'), ('delete', 'unlink'), ('edit', 'write')):
807 if not node.get(action) and not Model.check_access_rights(cr, user, operation, raise_exception=False):
808 node.set(action, 'false')
809 if node.tag in ('kanban'):
810 group_by_field = node.get('default_group_by')
811 if group_by_field and Model._all_columns.get(group_by_field):
812 group_by_column = Model._all_columns[group_by_field].column
813 if group_by_column._type == 'many2one':
814 group_by_model = Model.pool.get(group_by_column._obj)
815 for action, operation in (('group_create', 'create'), ('group_delete', 'unlink'), ('group_edit', 'write')):
816 if not node.get(action) and not group_by_model.check_access_rights(cr, user, operation, raise_exception=False):
817 node.set(action, 'false')
819 arch = etree.tostring(node, encoding="utf-8").replace('\t', '')
820 for k in fields.keys():
821 if k not in fields_def:
823 for field in fields_def:
825 # sometime, the view may contain the (invisible) field 'id' needed for a domain (when 2 objects have cross references)
826 fields['id'] = {'readonly': True, 'type': 'integer', 'string': 'ID'}
827 elif field in fields:
828 fields[field].update(fields_def[field])
830 message = _("Field `%(field_name)s` does not exist") % \
831 dict(field_name=field)
832 self.raise_view_error(cr, user, message, view_id, context)
835 #------------------------------------------------------
836 # QWeb template views
837 #------------------------------------------------------
838 @tools.ormcache_context(accepted_keys=('lang','inherit_branding', 'editable', 'translatable'))
839 def read_template(self, cr, uid, xml_id, context=None):
840 if isinstance(xml_id, (int, long)):
843 if '.' not in xml_id:
844 raise ValueError('Invalid template id: %r' % (xml_id,))
845 view_id = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, xml_id, raise_if_not_found=True)
847 arch = self.read_combined(cr, uid, view_id, fields=['arch'], context=context)['arch']
848 arch_tree = etree.fromstring(arch)
850 if 'lang' in context:
851 arch_tree = self.translate_qweb(cr, uid, view_id, arch_tree, context['lang'], context)
853 self.distribute_branding(arch_tree)
854 root = etree.Element('templates')
855 root.append(arch_tree)
856 arch = etree.tostring(root, encoding='utf-8', xml_declaration=True)
859 def clear_cache(self):
860 self.read_template.clear_cache(self)
862 def _contains_branded(self, node):
863 return node.tag == 't'\
864 or 't-raw' in node.attrib\
865 or any(self.is_node_branded(child) for child in node.iterdescendants())
867 def _pop_view_branding(self, element):
868 distributed_branding = dict(
869 (attribute, element.attrib.pop(attribute))
870 for attribute in MOVABLE_BRANDING
871 if element.get(attribute))
872 return distributed_branding
874 def distribute_branding(self, e, branding=None, parent_xpath='',
875 index_map=misc.ConstantMapping(1)):
876 if e.get('t-ignore') or e.tag == 'head':
877 # remove any view branding possibly injected by inheritance
878 attrs = set(MOVABLE_BRANDING)
879 for descendant in e.iterdescendants(tag=etree.Element):
880 if not attrs.intersection(descendant.attrib): continue
881 self._pop_view_branding(descendant)
882 # TODO: find a better name and check if we have a string to boolean helper
885 node_path = e.get('data-oe-xpath')
886 if node_path is None:
887 node_path = "%s/%s[%d]" % (parent_xpath, e.tag, index_map[e.tag])
888 if branding and not (e.get('data-oe-model') or e.get('t-field')):
889 e.attrib.update(branding)
890 e.set('data-oe-xpath', node_path)
891 if not e.get('data-oe-model'): return
893 if set(('t-esc', 't-escf', 't-raw', 't-rawf')).intersection(e.attrib):
894 # nodes which fully generate their content and have no reason to
895 # be branded because they can not sensibly be edited
896 self._pop_view_branding(e)
897 elif self._contains_branded(e):
898 # if a branded element contains branded elements distribute own
899 # branding to children unless it's t-raw, then just remove branding
901 distributed_branding = self._pop_view_branding(e)
903 if 't-raw' not in e.attrib:
904 # TODO: collections.Counter if remove p2.6 compat
905 # running index by tag type, for XPath query generation
906 indexes = collections.defaultdict(lambda: 0)
907 for child in e.iterchildren(tag=etree.Element):
908 if child.get('data-oe-xpath'):
909 # injected by view inheritance, skip otherwise
910 # generated xpath is incorrect
911 self.distribute_branding(child)
913 indexes[child.tag] += 1
914 self.distribute_branding(
915 child, distributed_branding,
916 parent_xpath=node_path, index_map=indexes)
918 def is_node_branded(self, node):
919 """ Finds out whether a node is branded or qweb-active (bears a
920 @data-oe-model or a @t-* *which is not t-field* as t-field does not
923 :param node: an etree-compatible element to test
924 :type node: etree._Element
928 (attr == 'data-oe-model' or (attr != 't-field' and attr.startswith('t-')))
929 for attr in node.attrib
932 def translate_qweb(self, cr, uid, id_, arch, lang, context=None):
933 # TODO: this should be moved in a place before inheritance is applied
934 # but process() is only called on fields_view_get()
935 Translations = self.pool['ir.translation']
936 h = HTMLParser.HTMLParser()
938 if not text or not text.strip():
940 text = h.unescape(text.strip())
941 if len(text) < 2 or (text.startswith('<!') and text.endswith('>')):
943 return Translations._get_source(cr, uid, 'website', 'view', lang, text, id_)
945 if arch.tag not in ['script']:
946 text = get_trans(arch.text)
948 arch.text = arch.text.replace(arch.text.strip(), text)
949 tail = get_trans(arch.tail)
951 arch.tail = arch.tail.replace(arch.tail.strip(), tail)
953 for attr_name in ('title', 'alt', 'placeholder'):
954 attr = get_trans(arch.get(attr_name))
956 arch.set(attr_name, attr)
957 for node in arch.iterchildren("*"):
958 self.translate_qweb(cr, uid, id_, node, lang, context)
961 @openerp.tools.ormcache()
962 def get_view_xmlid(self, cr, uid, id):
963 imd = self.pool['ir.model.data']
964 domain = [('model', '=', 'ir.ui.view'), ('res_id', '=', id)]
965 xmlid = imd.search_read(cr, uid, domain, ['module', 'name'])[0]
966 return '%s.%s' % (xmlid['module'], xmlid['name'])
968 def render(self, cr, uid, id_or_xml_id, values=None, engine='ir.qweb', context=None):
969 if isinstance(id_or_xml_id, list):
970 id_or_xml_id = id_or_xml_id[0]
978 keep_query=keep_query,
979 request=request, # might be unbound if we're not in an httprequest context
980 debug=request.debug if request else False,
982 quote_plus=werkzeug.url_quote_plus,
985 relativedelta=relativedelta,
987 qcontext.update(values)
989 # TODO: remove this as soon as the following branch is merged
990 # lp:~openerp-dev/openerp-web/trunk-module-closure-style-msh
991 from openerp.addons.web.controllers.main import module_boot
992 qcontext['modules'] = simplejson.dumps(module_boot()) if request else None
995 return self.read_template(cr, uid, name, context=context)
997 return self.pool[engine].render(cr, uid, id_or_xml_id, qcontext, loader=loader, context=context)
999 #------------------------------------------------------
1001 #------------------------------------------------------
1003 def graph_get(self, cr, uid, id, model, node_obj, conn_obj, src_node, des_node, label, scale, context=None):
1013 _Model_Obj = self.pool[model]
1014 _Node_Obj = self.pool[node_obj]
1015 _Arrow_Obj = self.pool[conn_obj]
1017 for model_key,model_value in _Model_Obj._columns.items():
1018 if model_value._type=='one2many':
1019 if model_value._obj==node_obj:
1020 _Node_Field=model_key
1021 _Model_Field=model_value._fields_id
1023 for node_key,node_value in _Node_Obj._columns.items():
1024 if node_value._type=='one2many':
1025 if node_value._obj==conn_obj:
1026 if src_node in _Arrow_Obj._columns and flag:
1027 _Source_Field=node_key
1028 if des_node in _Arrow_Obj._columns and not flag:
1029 _Destination_Field=node_key
1032 datas = _Model_Obj.read(cr, uid, id, [],context)
1033 for a in _Node_Obj.read(cr,uid,datas[_Node_Field],[]):
1034 if a[_Source_Field] or a[_Destination_Field]:
1035 nodes_name.append((a['id'],a['name']))
1036 nodes.append(a['id'])
1038 blank_nodes.append({'id': a['id'],'name':a['name']})
1040 if a.has_key('flow_start') and a['flow_start']:
1041 start.append(a['id'])
1043 if not a[_Source_Field]:
1044 no_ancester.append(a['id'])
1045 for t in _Arrow_Obj.read(cr,uid, a[_Destination_Field],[]):
1046 transitions.append((a['id'], t[des_node][0]))
1047 tres[str(t['id'])] = (a['id'],t[des_node][0])
1050 for lbl in eval(label):
1051 if t.has_key(tools.ustr(lbl)) and tools.ustr(t[lbl])=='False':
1054 label_string = label_string + " " + tools.ustr(t[lbl])
1055 labels[str(t['id'])] = (a['id'],label_string)
1056 g = graph(nodes, transitions, no_ancester)
1059 result = g.result_get()
1061 for node in nodes_name:
1062 results[str(node[0])] = result[node[0]]
1063 results[str(node[0])]['name'] = node[1]
1064 return {'nodes': results,
1065 'transitions': tres,
1067 'blank_nodes': blank_nodes,
1068 'node_parent_field': _Model_Field,}
1070 def _validate_custom_views(self, cr, uid, model):
1071 """Validate architecture of custom views (= without xml id) for a given model.
1072 This method is called at the end of registry update.
1074 cr.execute("""SELECT max(v.id)
1076 LEFT JOIN ir_model_data md ON (md.model = 'ir.ui.view' AND md.res_id = v.id)
1077 WHERE md.module IS NULL
1079 GROUP BY coalesce(v.inherit_id, v.id)
1082 ids = map(itemgetter(0), cr.fetchall())
1083 return self._check_xml(cr, uid, ids)
1085 def _validate_module_views(self, cr, uid, module):
1086 """Validate architecture of all the views of a given module"""
1087 assert not self.pool._init or module in self.pool._init_modules
1091 # only validate the views that are still existing...
1092 xmlid_filter = "AND md.name IN %s"
1093 names = tuple(name for (xmod, name), (model, res_id) in self.pool.model_data_reference_ids.items() if xmod == module and model == self._name)
1095 # no views for this module, nothing to validate
1098 cr.execute("""SELECT max(v.id)
1100 LEFT JOIN ir_model_data md ON (md.model = 'ir.ui.view' AND md.res_id = v.id)
1101 WHERE md.module = %s
1103 GROUP BY coalesce(v.inherit_id, v.id)
1104 """.format(xmlid_filter), params)
1106 for vid, in cr.fetchall():
1107 if not self._check_xml(cr, uid, [vid]):
1108 self.raise_view_error(cr, uid, "Can't validate view", vid)