[IMP] ir_ui_view: pep8 + log catched exception when view_id cannot be found.
[odoo/odoo.git] / addons / website / models / ir_ui_view.py
1 # -*- coding: utf-8 -*-
2 import copy
3 import logging
4
5 from lxml import etree, html
6
7 from openerp import SUPERUSER_ID, tools
8 from openerp.addons.website.models import website
9 from openerp.http import request
10 from openerp.osv import osv, fields
11
12 _logger = logging.getLogger(__name__)
13
14
15 class view(osv.osv):
16     _inherit = "ir.ui.view"
17     _columns = {
18         'page': fields.boolean("Whether this view is a web page template (complete)"),
19         'website_meta_title': fields.char("Website meta title", size=70, translate=True),
20         'website_meta_description': fields.text("Website meta description", size=160, translate=True),
21         'website_meta_keywords': fields.char("Website meta keywords", translate=True),
22         'customize_show': fields.boolean("Show As Optional Inherit"),
23         'website_id': fields.many2one('website', ondelete='cascade', string="Website"),
24     }
25
26     _sql_constraints = [
27         ('key_website_id_uniq', 'unique(key, website_id)',
28             'Key must be unique per website.'),
29     ]
30
31     _defaults = {
32         'page': False,
33         'customize_show': False,
34     }
35
36     def _view_obj(self, cr, uid, view_id, context=None):
37         if isinstance(view_id, basestring):
38             return self.pool['ir.model.data'].xmlid_to_object(
39                 cr, uid, view_id, raise_if_not_found=True, context=context
40             )
41         elif isinstance(view_id, (int, long)):
42             return self.browse(cr, uid, view_id, context=context)
43
44         # assume it's already a view object (WTF?)
45         return view_id
46
47     # Returns all views (called and inherited) related to a view
48     # Used by translation mechanism, SEO and optional templates
49     def _views_get(self, cr, uid, view_id, options=True, bundles=False, context=None, root=True):
50         """ For a given view ``view_id``, should return:
51
52         * the view itself
53         * all views inheriting from it, enabled or not
54           - but not the optional children of a non-enabled child
55         * all views called from it (via t-call)
56         """
57
58         try:
59             view = self._view_obj(cr, uid, view_id, context=context)
60         except ValueError:
61             _logger.warning("Could not find view object with view_id '%s'" % (view_id))
62             # Shall we log that ? Yes, you should !
63             return []
64
65         while root and view.inherit_id:
66             view = view.inherit_id
67
68         result = [view]
69
70         node = etree.fromstring(view.arch)
71         xpath = "//t[@t-call]"
72         if bundles:
73             xpath += "| //t[@t-call-assets]"
74         for child in node.xpath(xpath):
75             try:
76                 called_view = self._view_obj(cr, uid, child.get('t-call', child.get('t-call-assets')), context=context)
77             except ValueError:
78                 continue
79             if called_view not in result:
80                 result += self._views_get(cr, uid, called_view, options=options, bundles=bundles, context=context)
81
82         extensions = view.inherit_children_ids
83         if not options:
84             # only active children
85             extensions = (v for v in view.inherit_children_ids if v.active)
86
87         # Keep options in a deterministic order regardless of their applicability
88         for extension in sorted(extensions, key=lambda v: v.id):
89             for r in self._views_get(
90                     cr, uid, extension,
91                     # only return optional grandchildren if this child is enabled
92                     options=extension.active,
93                     context=context, root=False):
94                 if r not in result:
95                     result.append(r)
96         return result
97
98     def extract_embedded_fields(self, cr, uid, arch, context=None):
99         return arch.xpath('//*[@data-oe-model != "ir.ui.view"]')
100
101     def save_embedded_field(self, cr, uid, el, context=None):
102         Model = self.pool[el.get('data-oe-model')]
103         field = el.get('data-oe-field')
104
105         converter = self.pool['website.qweb'].get_converter_for(el.get('data-oe-type'))
106         value = converter.from_html(cr, uid, Model, Model._fields[field], el)
107
108         if value is not None:
109             # TODO: batch writes?
110             Model.write(cr, uid, [int(el.get('data-oe-id'))], {
111                 field: value
112             }, context=context)
113
114     def to_field_ref(self, cr, uid, el, context=None):
115         # filter out meta-information inserted in the document
116         attributes = dict((k, v) for k, v in el.items()
117                           if not k.startswith('data-oe-'))
118         attributes['t-field'] = el.get('data-oe-expression')
119
120         out = html.html_parser.makeelement(el.tag, attrib=attributes)
121         out.tail = el.tail
122         return out
123
124     def replace_arch_section(self, cr, uid, view_id, section_xpath, replacement, context=None):
125         # the root of the arch section shouldn't actually be replaced as it's
126         # not really editable itself, only the content truly is editable.
127
128         [view] = self.browse(cr, uid, [view_id], context=context)
129         arch = etree.fromstring(view.arch.encode('utf-8'))
130         # => get the replacement root
131         if not section_xpath:
132             root = arch
133         else:
134             # ensure there's only one match
135             [root] = arch.xpath(section_xpath)
136
137         root.text = replacement.text
138         root.tail = replacement.tail
139         # replace all children
140         del root[:]
141         for child in replacement:
142             root.append(copy.deepcopy(child))
143
144         return arch
145
146     @tools.ormcache_context(accepted_keys=('website_id',))
147     def get_view_id(self, cr, uid, xml_id, context=None):
148         if context and 'website_id' in context and not isinstance(xml_id, (int, long)):
149             domain = [('key', '=', xml_id), '|', ('website_id', '=', context['website_id']), ('website_id', '=', False)]
150             [xml_id] = self.search(cr, uid, domain, order='website_id', limit=1, context=context)
151         else:
152             xml_id = super(view, self).get_view_id(cr, uid, xml_id, context=context)
153         return xml_id
154
155     def render(self, cr, uid, id_or_xml_id, values=None, engine='ir.qweb', context=None):
156         if request and getattr(request, 'website_enabled', False):
157             engine = 'website.qweb'
158
159             if isinstance(id_or_xml_id, list):
160                 id_or_xml_id = id_or_xml_id[0]
161
162             if not context:
163                 context = {}
164
165             company = self.pool['res.company'].browse(cr, SUPERUSER_ID, request.website.company_id.id, context=context)
166
167             qcontext = dict(
168                 context.copy(),
169                 website=request.website,
170                 url_for=website.url_for,
171                 slug=website.slug,
172                 res_company=company,
173                 user_id=self.pool.get("res.users").browse(cr, uid, uid),
174                 translatable=context.get('lang') != request.website.default_lang_code,
175                 editable=request.website.is_publisher(),
176                 menu_data=self.pool['ir.ui.menu'].load_menus_root(cr, uid, context=context) if request.website.is_user() else None,
177             )
178
179             # add some values
180             if values:
181                 qcontext.update(values)
182
183             # in edit mode ir.ui.view will tag nodes
184             if qcontext.get('editable'):
185                 context = dict(context, inherit_branding=True)
186             elif request.registry['res.users'].has_group(cr, uid, 'base.group_website_publisher'):
187                 context = dict(context, inherit_branding_auto=True)
188
189             view_obj = request.website.get_template(id_or_xml_id)
190             if 'main_object' not in qcontext:
191                 qcontext['main_object'] = view_obj
192
193             values = qcontext
194
195         return super(view, self).render(cr, uid, id_or_xml_id, values=values, engine=engine, context=context)
196
197     def _pretty_arch(self, arch):
198         # remove_blank_string does not seem to work on HTMLParser, and
199         # pretty-printing with lxml more or less requires stripping
200         # whitespace: http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output
201         # so serialize to XML, parse as XML (remove whitespace) then serialize
202         # as XML (pretty print)
203         arch_no_whitespace = etree.fromstring(
204             etree.tostring(arch, encoding='utf-8'),
205             parser=etree.XMLParser(encoding='utf-8', remove_blank_text=True))
206         return etree.tostring(
207             arch_no_whitespace, encoding='unicode', pretty_print=True)
208
209     def save(self, cr, uid, res_id, value, xpath=None, context=None):
210         """ Update a view section. The view section may embed fields to write
211
212         :param str model:
213         :param int res_id:
214         :param str xpath: valid xpath to the tag to replace
215         """
216         res_id = int(res_id)
217
218         arch_section = html.fromstring(
219             value, parser=html.HTMLParser(encoding='utf-8'))
220
221         if xpath is None:
222             # value is an embedded field on its own, not a view section
223             self.save_embedded_field(cr, uid, arch_section, context=context)
224             return
225
226         for el in self.extract_embedded_fields(cr, uid, arch_section, context=context):
227             self.save_embedded_field(cr, uid, el, context=context)
228
229             # transform embedded field back to t-field
230             el.getparent().replace(el, self.to_field_ref(cr, uid, el, context=context))
231
232         arch = self.replace_arch_section(cr, uid, res_id, xpath, arch_section, context=context)
233         self.write(cr, uid, res_id, {
234             'arch': self._pretty_arch(arch)
235         }, context=context)
236
237         view = self.browse(cr, SUPERUSER_ID, res_id, context=context)
238         if view.model_data_id:
239             view.model_data_id.write({'noupdate': True})