[MERGE] Perform auto-join and mailboxes+needaction speed udpate. Server-side branch...
[odoo/odoo.git] / openerp / tools / view_validation.py
1 """ View validation code (using assertions, not the RNG schema). """
2
3 import logging
4
5 _logger = logging.getLogger(__name__)
6
7
8 def valid_page_in_book(arch):
9     """A `page` node must be below a `book` node."""
10     return not arch.xpath('//page[not(ancestor::notebook)]')
11
12
13 def valid_field_in_graph(arch):
14     """A `graph` must have `string` attribute and an immediate node of `graph` view must be `field`."""
15     if arch.xpath('//graph[not (@string)]'):
16         return False
17     for child in arch.xpath('/graph/child::*'):
18         if child.tag != 'field':
19             return False
20     return True
21
22
23 def valid_field_in_tree(arch):
24     """A `tree` must have `string` attribute and an immediate node of `tree` view must be `field` or `button`."""
25     if arch.xpath('//tree[not (@string)]'):
26         return False
27     for child in arch.xpath('/tree/child::*'):
28         if child.tag not in ('field', 'button'):
29             return False
30     return True
31
32
33 def valid_att_in_field(arch):
34     """A `name` attribute must be in a `field` node."""
35     return not arch.xpath('//field[not (@name)]')
36
37
38 def valid_att_in_label(arch):
39     """A `for` and `string` attribute must be on a `label` node."""
40     return not arch.xpath('//label[not ((@for) or (@string))]')
41
42
43 def valid_att_in_form(arch):
44     """A `string` attribute must be on a `form` node."""
45     return not arch.xpath('//form[not (@string)]')
46
47
48 def valid_type_in_colspan(arch):
49     """A `colspan` attribute must be an `integer` type."""
50     for attrib in arch.xpath('//*/@colspan'):
51         try:
52             int(attrib)
53         except:
54             return False
55     return True
56
57
58 def valid_type_in_col(arch):
59     """A `col` attribute must be an `integer` type."""
60     for attrib in arch.xpath('//*/@col'):
61         try:
62             int(attrib)
63         except:
64             return False
65     return True
66
67
68 def valid_view(arch):
69     if arch.tag == 'form':
70         for pred in [valid_page_in_book, valid_att_in_form, valid_type_in_colspan,\
71                       valid_type_in_col, valid_att_in_field, valid_att_in_label]:
72             if not pred(arch):
73                 _logger.error('Invalid XML: %s', pred.__doc__)
74                 return False
75     elif arch.tag == 'graph':
76         for pred in [valid_field_in_graph, valid_att_in_field]:
77             if not pred(arch):
78                 _logger.error('Invalid XML: %s', pred.__doc__)
79                 return False
80     elif arch.tag == 'tree':
81         for pred in [valid_field_in_tree, valid_att_in_field]:
82             if not pred(arch):
83                 _logger.error('Invalid XML: %s', pred.__doc__)
84                 return False
85     return True