Refactor the `load_views` API so it no longer sends multiple times the same fields description. e.g. When `load_views` is called to get the kanban, tree and form views, the list of fields of the model was sent 4 times: - Once for each view, with only the fields used in the view, in `['fields_views']['kanban']['fields']` for instance - Once globally, with all the fields of the model, in `['fields']` The goal of this revision is to change that so it sends the list of all fields only once. In addition, if a view contains x2many fields, the fields description of the comodel is also sent. It was sent in the `views` key of the view fields dict. e.g. When calling `load_views` of `res.partner` to get the kanban, tree and form views, the `res.partner` fields description was actually sent 6 times: - Once for each view - Once globally - Once for each view of the many2many field `child_ids` of the form view, in - `['fields_views']['form']['fields']['child_ids']['views']['kanban']['fields']` - `['fields_views']['form']['fields']['child_ids']['views']['form']['fields']` The change suggested in this revision is to: - Remove the fields description for each view in `['fields_views']`. As it no longer contains the fields, the key becomes `['views']` instead of `['fields_views']`. - Replace the dict key `['fields']` by `['models']`, which is a dict with as key the model name and as values the model fields description. It contains the fields description for all models implied in the view: the model of the main view and the model of all one2many and many2many fields. With this change, the fields description will only be sent once by model implied in the view. In addition, the web client was getting the information about the fields sometimes in the global fields description list (e.g. `['fields']`), sometimes in the fields description list of the view type (e.g. `['fields_views']['form']['fields']`), making it a pain to try to make changes / performance gain in these field description dictionaries, because you never knew in which dict the web client was getting its info. Now, as there is only one place to get the fields description from, it's clearer and cleaner. - one2many and many2many fields views are passed directly in the main view architecture rather than being put in the `views` key of the field description. This is actually easier to treat by the web client, and this will allow in a future work to cache an entire view in one block of text rather than having to combine multiple cached blocks of text to return one view. - one2many and many2many fields which do not have directly embedded views have their views directly injected in the architecture, so the web client doesn't have to do RPC calls to `load_views` for each one2many and many2many fields not having embedded views. For instance, this allow to reduce the number of RPC calls to `load_views` from 8 to 1 when loading the form of `product.product`. Currently, this behavior is limited to 1 level deep but we consider making it go all the way down in future works. We did not do it for the moment because in certain cases it rises the processing time and the size (bytes) too much. e.g. the sale.order view can be 5 levels deep, meaning you can reach 4 dialogs on top the main view. ``` sale.order form > order_line > sale.order.line form > invoice_lines > account.move.line form > asset_ids > account.asset form > depreciation_move_ids > account.move form. ``` This will also benefit in future works to cache an entire view in one block of text rather to having to combine multiple cached block of text to get one view. - `fields_view_get` becomes `get_view`. As it no longer returns the fields description, keeping the `fields` in the name `fields_view_get` no longer makes sense. Hence removing `fields` from the method name, it becomes `view_get`. As it gets renamed anyway, we take the opportunity to rename it `get_view`, which is more in line with the general getter/setter guidelines in the model object world. - `_fields_view_get` becomes `_get_view`. For the same reasons than above. - `load_views` becomes `get_views`. This is not mandatory, there is no technical reason to rename `load_views` as it practically sends the same info as before, the view architectures and their fields description. Just in another way. We just take the opportunity of this pull request to suggest a cleaner API: `_get_view`, `get_view` and `get_views`. - Arguments `toolbar=False, submenu=False` fo the methods `_fields_view_get` and `fields_view_get` are converted to a kwargs `**options` in `_get_view` and `get_view`. The rationale is that submenu was already no longer used (deprecated) and the mobile options is introduced. The mobile options is necessary to tell the server to send the mobile views for x2many fields (kanban instead of tree). Instead of adding a new argument each time we add a new option to `fields_view_get`, it seems wiser to have a kwargs `**options` to avoid to re-write all overrides each time a new option is introduced. - `_fields_view_get` returned a dict containing the arch in text and some of the view information. Now, `get_view` returns a tuple with the view architecture as an `etree` node, and the view as a browse record. The rationale is that all overrides of `_fields_view_get` were about modifying the arch only (e.g. changing the address format/re-organizing the address related field nodes of the partner according to the company country). To do so, all these overrides were doing `etree.fromstring` to parse the arch which was sent in text to convert it to an `etree`, then operations were done on the `etree`, and then `etree.tostring` was called to convert back the arch to string. With this change of signature to send the arch as an `etree`, all these back and forth `etree.fromstring` -> `etree.tostring` are avoided, allowing some performance gain and less code in the end. - A cleanup of the keys returned in the dict of `fields_view_get` has been performed in `get_view`: - `fields` is removed, as explained above, - `view_id` is renamed `id`, - `name` is removed, it was unused by the web client, - `type` is removed, it was unused by the web client, - `field_parent` is removed, it was unused by the web client, - `base_model` is removed, it was unused by the web client. - `filters` is moved from the global dict returned by `load_views` (now `get_views`) to the dict returned by `fields_view_get` (now `get_view`) as it applies only to the `search` view type. - Retro-compatible methods for the 3 methods `fields_view_get`, `_fields_view_get` and `load_views` are provided, with deprecation warnings in them. - The web client could cache the model fields description (as it already caches the views), so it doesn't need to fetch them again if it asks for another view of a model for which he already has the fields description. If we do so, `get_views` could return only the list of models used by the views, without the fields description as of now, and the web client would then call `fields_get` independently only for the models for which it doesn't have yet the fields description. This would avoid the server to return the fields description and to call `fields_get`, which is costly, for each `get_views`, therefore gaining performances. - Inject the views of the one2many and many2many fields all the way down, unlimited depth level, as explained above. - Cache with `ormcache` the architecture of back-end views. This is already done for qweb views, it's not done for back-end views. Therefore the postprocessing of the views is performed for each `get_views`, which is costly, while the view architecture doesn't change for users belonging to the same groups, according to the groups implied by the view. This pull request is co-authored by Aaron Bohy (aab) for the web client part and Denis Ledoux (dle) for the server part. Part-of: odoo/odoo#87522
483 lines
22 KiB
Python
483 lines
22 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
import logging
|
|
import os
|
|
import uuid
|
|
import werkzeug
|
|
|
|
from odoo import api, fields, models
|
|
from odoo import tools
|
|
from odoo.addons import website
|
|
from odoo.exceptions import AccessError
|
|
from odoo.osv import expression
|
|
from odoo.http import request
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class View(models.Model):
|
|
|
|
_name = "ir.ui.view"
|
|
_inherit = ["ir.ui.view", "website.seo.metadata"]
|
|
|
|
website_id = fields.Many2one('website', ondelete='cascade', string="Website")
|
|
page_ids = fields.One2many('website.page', 'view_id')
|
|
first_page_id = fields.Many2one('website.page', string='Website Page', help='First page linked to this view', compute='_compute_first_page_id')
|
|
track = fields.Boolean(string='Track', default=False, help="Allow to specify for one page of the website to be trackable or not")
|
|
visibility = fields.Selection([('', 'All'), ('connected', 'Signed In'), ('restricted_group', 'Restricted Group'), ('password', 'With Password')], default='')
|
|
visibility_password = fields.Char(groups='base.group_system', copy=False)
|
|
visibility_password_display = fields.Char(compute='_get_pwd', inverse='_set_pwd', groups='website.group_website_designer')
|
|
|
|
@api.depends('visibility_password')
|
|
def _get_pwd(self):
|
|
for r in self:
|
|
r.visibility_password_display = r.sudo().visibility_password and '********' or ''
|
|
|
|
def _set_pwd(self):
|
|
crypt_context = self.env.user._crypt_context()
|
|
for r in self:
|
|
if r.type == 'qweb':
|
|
r.sudo().visibility_password = r.visibility_password_display and crypt_context.encrypt(r.visibility_password_display) or ''
|
|
r.visibility = r.visibility # double check access
|
|
|
|
def _compute_first_page_id(self):
|
|
for view in self:
|
|
view.first_page_id = self.env['website.page'].search([('view_id', '=', view.id)], limit=1)
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
"""
|
|
SOC for ir.ui.view creation. If a view is created without a website_id,
|
|
it should get one if one is present in the context. Also check that
|
|
an explicit website_id in create values matches the one in the context.
|
|
"""
|
|
website_id = self.env.context.get('website_id', False)
|
|
if not website_id:
|
|
return super().create(vals_list)
|
|
|
|
for vals in vals_list:
|
|
if 'website_id' not in vals:
|
|
# Automatic addition of website ID during view creation if not
|
|
# specified but present in the context
|
|
vals['website_id'] = website_id
|
|
else:
|
|
# If website ID specified, automatic check that it is the same as
|
|
# the one in the context. Otherwise raise an error.
|
|
new_website_id = vals['website_id']
|
|
if not new_website_id:
|
|
raise ValueError(f"Trying to create a generic view from a website {website_id} environment")
|
|
elif new_website_id != website_id:
|
|
raise ValueError(f"Trying to create a view for website {new_website_id} from a website {website_id} environment")
|
|
return super().create(vals_list)
|
|
|
|
def name_get(self):
|
|
if not (self._context.get('display_key') or self._context.get('display_website')):
|
|
return super(View, self).name_get()
|
|
|
|
res = []
|
|
for view in self:
|
|
view_name = view.name
|
|
if self._context.get('display_key'):
|
|
view_name += ' <%s>' % view.key
|
|
if self._context.get('display_website') and view.website_id:
|
|
view_name += ' [%s]' % view.website_id.name
|
|
res.append((view.id, view_name))
|
|
return res
|
|
|
|
def write(self, vals):
|
|
'''COW for ir.ui.view. This way editing websites does not impact other
|
|
websites. Also this way newly created websites will only
|
|
contain the default views.
|
|
'''
|
|
current_website_id = self.env.context.get('website_id')
|
|
if not current_website_id or self.env.context.get('no_cow'):
|
|
return super(View, self).write(vals)
|
|
|
|
# We need to consider inactive views when handling multi-website cow
|
|
# feature (to copy inactive children views, to search for specific
|
|
# views, ...)
|
|
for view in self.with_context(active_test=False):
|
|
# Make sure views which are written in a website context receive
|
|
# a value for their 'key' field
|
|
if not view.key and not vals.get('key'):
|
|
view.with_context(no_cow=True).key = 'website.key_%s' % str(uuid.uuid4())[:6]
|
|
|
|
pages = view.page_ids
|
|
|
|
# Disable cache of page if we guess some dynamic content (form with csrf, ...)
|
|
if vals.get('arch'):
|
|
to_invalidate = pages.filtered(
|
|
lambda p: p.cache_time and not p._can_be_cached(vals['arch'])
|
|
)
|
|
to_invalidate and _logger.info('Disable cache for page %s', to_invalidate)
|
|
to_invalidate.cache_time = 0
|
|
|
|
# No need of COW if the view is already specific
|
|
if view.website_id:
|
|
super(View, view).write(vals)
|
|
continue
|
|
|
|
# Ensure the cache of the pages stay consistent when doing COW.
|
|
# This is necessary when writing view fields from a page record
|
|
# because the generic page will put the given values on its cache
|
|
# but in reality the values were only meant to go on the specific
|
|
# page. Invalidate all fields and not only those in vals because
|
|
# other fields could have been changed implicitly too.
|
|
pages.flush(records=pages)
|
|
pages.invalidate_cache(ids=pages.ids)
|
|
|
|
# If already a specific view for this generic view, write on it
|
|
website_specific_view = view.search([
|
|
('key', '=', view.key),
|
|
('website_id', '=', current_website_id)
|
|
], limit=1)
|
|
if website_specific_view:
|
|
super(View, website_specific_view).write(vals)
|
|
continue
|
|
|
|
# Set key to avoid copy() to generate an unique key as we want the
|
|
# specific view to have the same key
|
|
copy_vals = {'website_id': current_website_id, 'key': view.key}
|
|
# Copy with the 'inherit_id' field value that will be written to
|
|
# ensure the copied view's validation works
|
|
if vals.get('inherit_id'):
|
|
copy_vals['inherit_id'] = vals['inherit_id']
|
|
website_specific_view = view.copy(copy_vals)
|
|
|
|
view._create_website_specific_pages_for_view(website_specific_view,
|
|
view.env['website'].browse(current_website_id))
|
|
|
|
for inherit_child in view.inherit_children_ids.filter_duplicate().sorted(key=lambda v: (v.priority, v.id)):
|
|
if inherit_child.website_id.id == current_website_id:
|
|
# In the case the child was already specific to the current
|
|
# website, we cannot just reattach it to the new specific
|
|
# parent: we have to copy it there and remove it from the
|
|
# original tree. Indeed, the order of children 'id' fields
|
|
# must remain the same so that the inheritance is applied
|
|
# in the same order in the copied tree.
|
|
child = inherit_child.copy({'inherit_id': website_specific_view.id, 'key': inherit_child.key})
|
|
inherit_child.inherit_children_ids.write({'inherit_id': child.id})
|
|
inherit_child.unlink()
|
|
else:
|
|
# Trigger COW on inheriting views
|
|
inherit_child.write({'inherit_id': website_specific_view.id})
|
|
|
|
super(View, website_specific_view).write(vals)
|
|
|
|
return True
|
|
|
|
def _load_records_write_on_cow(self, cow_view, inherit_id, values):
|
|
inherit_id = self.search([
|
|
('key', '=', self.browse(inherit_id).key),
|
|
('website_id', 'in', (False, cow_view.website_id.id)),
|
|
], order='website_id', limit=1).id
|
|
values['inherit_id'] = inherit_id
|
|
cow_view.with_context(no_cow=True).write(values)
|
|
|
|
def _create_all_specific_views(self, processed_modules):
|
|
""" When creating a generic child view, we should
|
|
also create that view under specific view trees (COW'd).
|
|
Top level view (no inherit_id) do not need that behavior as they
|
|
will be shared between websites since there is no specific yet.
|
|
"""
|
|
# Only for the modules being processed
|
|
regex = '^(%s)[.]' % '|'.join(processed_modules)
|
|
# Retrieve the views through a SQl query to avoid ORM queries inside of for loop
|
|
# Retrieves all the views that are missing their specific counterpart with all the
|
|
# specific view parent id and their website id in one query
|
|
query = """
|
|
SELECT generic.id, ARRAY[array_agg(spec_parent.id), array_agg(spec_parent.website_id)]
|
|
FROM ir_ui_view generic
|
|
INNER JOIN ir_ui_view generic_parent ON generic_parent.id = generic.inherit_id
|
|
INNER JOIN ir_ui_view spec_parent ON spec_parent.key = generic_parent.key
|
|
LEFT JOIN ir_ui_view specific ON specific.key = generic.key AND specific.website_id = spec_parent.website_id
|
|
WHERE generic.type='qweb'
|
|
AND generic.website_id IS NULL
|
|
AND generic.key ~ %s
|
|
AND spec_parent.website_id IS NOT NULL
|
|
AND specific.id IS NULL
|
|
GROUP BY generic.id
|
|
"""
|
|
self.env.cr.execute(query, (regex, ))
|
|
result = dict(self.env.cr.fetchall())
|
|
|
|
for record in self.browse(result.keys()):
|
|
specific_parent_view_ids, website_ids = result[record.id]
|
|
for specific_parent_view_id, website_id in zip(specific_parent_view_ids, website_ids):
|
|
record.with_context(website_id=website_id).write({
|
|
'inherit_id': specific_parent_view_id,
|
|
})
|
|
super(View, self)._create_all_specific_views(processed_modules)
|
|
|
|
def unlink(self):
|
|
'''This implements COU (copy-on-unlink). When deleting a generic page
|
|
website-specific pages will be created so only the current
|
|
website is affected.
|
|
'''
|
|
current_website_id = self._context.get('website_id')
|
|
|
|
if current_website_id and not self._context.get('no_cow'):
|
|
for view in self.filtered(lambda view: not view.website_id):
|
|
for w in self.env['website'].search([('id', '!=', current_website_id)]):
|
|
# reuse the COW mechanism to create
|
|
# website-specific copies, it will take
|
|
# care of creating pages and menus.
|
|
view.with_context(website_id=w.id).write({'name': view.name})
|
|
|
|
specific_views = self.env['ir.ui.view']
|
|
if self and self.pool._init:
|
|
for view in self.filtered(lambda view: not view.website_id):
|
|
specific_views += view._get_specific_views()
|
|
|
|
result = super(View, self + specific_views).unlink()
|
|
self.clear_caches()
|
|
return result
|
|
|
|
def _create_website_specific_pages_for_view(self, new_view, website):
|
|
for page in self.page_ids:
|
|
# create new pages for this view
|
|
new_page = page.copy({
|
|
'view_id': new_view.id,
|
|
'is_published': page.is_published,
|
|
})
|
|
page.menu_ids.filtered(lambda m: m.website_id.id == website.id).page_id = new_page.id
|
|
|
|
def _get_top_level_view(self):
|
|
self.ensure_one()
|
|
return self.inherit_id._get_top_level_view() if self.inherit_id else self
|
|
|
|
@api.model
|
|
def get_related_views(self, key, bundles=False):
|
|
'''Make this only return most specific views for website.'''
|
|
# get_related_views can be called through website=False routes
|
|
# (e.g. /web_editor/get_assets_editor_resources), so website
|
|
# dispatch_parameters may not be added. Manually set
|
|
# website_id. (It will then always fallback on a website, this
|
|
# method should never be called in a generic context, even for
|
|
# tests)
|
|
self = self.with_context(website_id=self.env['website'].get_current_website().id)
|
|
return super(View, self).get_related_views(key, bundles=bundles)
|
|
|
|
def filter_duplicate(self):
|
|
""" Filter current recordset only keeping the most suitable view per distinct key.
|
|
Every non-accessible view will be removed from the set:
|
|
* In non website context, every view with a website will be removed
|
|
* In a website context, every view from another website
|
|
"""
|
|
current_website_id = self._context.get('website_id')
|
|
most_specific_views = self.env['ir.ui.view']
|
|
if not current_website_id:
|
|
return self.filtered(lambda view: not view.website_id)
|
|
|
|
for view in self:
|
|
# specific view: add it if it's for the current website and ignore
|
|
# it if it's for another website
|
|
if view.website_id and view.website_id.id == current_website_id:
|
|
most_specific_views |= view
|
|
# generic view: add it only if, for the current website, there is no
|
|
# specific view for this view (based on the same `key` attribute)
|
|
elif not view.website_id and not any(view.key == view2.key and view2.website_id and view2.website_id.id == current_website_id for view2 in self):
|
|
most_specific_views |= view
|
|
|
|
return most_specific_views
|
|
|
|
@api.model
|
|
def _view_get_inherited_children(self, view):
|
|
extensions = super(View, self)._view_get_inherited_children(view)
|
|
return extensions.filter_duplicate()
|
|
|
|
@api.model
|
|
def _view_obj(self, view_id):
|
|
''' Given an xml_id or a view_id, return the corresponding view record.
|
|
In case of website context, return the most specific one.
|
|
:param view_id: either a string xml_id or an integer view_id
|
|
:return: The view record or empty recordset
|
|
'''
|
|
if isinstance(view_id, str) or isinstance(view_id, int):
|
|
return self.env['website'].viewref(view_id)
|
|
else:
|
|
# It can already be a view object when called by '_views_get()' that is calling '_view_obj'
|
|
# for it's inherit_children_ids, passing them directly as object record. (Note that it might
|
|
# be a view_id from another website but it will be filtered in 'get_related_views()')
|
|
return view_id if view_id._name == 'ir.ui.view' else self.env['ir.ui.view']
|
|
|
|
@api.model
|
|
def _get_inheriting_views_domain(self):
|
|
domain = super(View, self)._get_inheriting_views_domain()
|
|
current_website = self.env['website'].browse(self._context.get('website_id'))
|
|
website_views_domain = current_website.website_domain()
|
|
# when rendering for the website we have to include inactive views
|
|
# we will prefer inactive website-specific views over active generic ones
|
|
if current_website:
|
|
domain = [leaf for leaf in domain if 'active' not in leaf]
|
|
return expression.AND([website_views_domain, domain])
|
|
|
|
@api.model
|
|
def _get_inheriting_views(self):
|
|
if not self._context.get('website_id'):
|
|
return super(View, self)._get_inheriting_views()
|
|
|
|
views = super(View, self.with_context(active_test=False))._get_inheriting_views()
|
|
# prefer inactive website-specific views over active generic ones
|
|
return views.filter_duplicate().filtered('active')
|
|
|
|
@api.model
|
|
def _get_filter_xmlid_query(self):
|
|
"""This method add some specific view that do not have XML ID
|
|
"""
|
|
if not self._context.get('website_id'):
|
|
return super()._get_filter_xmlid_query()
|
|
else:
|
|
return """SELECT res_id
|
|
FROM ir_model_data
|
|
WHERE res_id IN %(res_ids)s
|
|
AND model = 'ir.ui.view'
|
|
AND module IN %(modules)s
|
|
UNION
|
|
SELECT sview.id
|
|
FROM ir_ui_view sview
|
|
INNER JOIN ir_ui_view oview USING (key)
|
|
INNER JOIN ir_model_data d
|
|
ON oview.id = d.res_id
|
|
AND d.model = 'ir.ui.view'
|
|
AND d.module IN %(modules)s
|
|
WHERE sview.id IN %(res_ids)s
|
|
AND sview.website_id IS NOT NULL
|
|
AND oview.website_id IS NULL;
|
|
"""
|
|
|
|
@api.model
|
|
@tools.ormcache_context('self.env.uid', 'self.env.su', 'xml_id', keys=('website_id',))
|
|
def _get_view_id(self, xml_id):
|
|
"""If a website_id is in the context and the given xml_id is not an int
|
|
then try to get the id of the specific view for that website, but
|
|
fallback to the id of the generic view if there is no specific.
|
|
|
|
If no website_id is in the context, it might randomly return the generic
|
|
or the specific view, so it's probably not recommanded to use this
|
|
method. `viewref` is probably more suitable.
|
|
|
|
Archived views are ignored (unless the active_test context is set, but
|
|
then the ormcache_context will not work as expected).
|
|
"""
|
|
if 'website_id' in self._context and not isinstance(xml_id, int):
|
|
current_website = self.env['website'].browse(self._context.get('website_id'))
|
|
domain = ['&', ('key', '=', xml_id)] + current_website.website_domain()
|
|
|
|
view = self.sudo().search(domain, order='website_id', limit=1)
|
|
if not view:
|
|
_logger.warning("Could not find view object with xml_id '%s'", xml_id)
|
|
raise ValueError('View %r in website %r not found' % (xml_id, self._context['website_id']))
|
|
return view.id
|
|
return super(View, self.sudo())._get_view_id(xml_id)
|
|
|
|
def _handle_visibility(self, do_raise=True):
|
|
""" Check the visibility set on the main view and raise 403 if you should not have access.
|
|
Order is: Public, Connected, Has group, Password
|
|
|
|
It only check the visibility on the main content, others views called stay available in rpc.
|
|
"""
|
|
error = False
|
|
|
|
self = self.sudo()
|
|
|
|
if self.visibility and not request.env.user.has_group('website.group_website_designer'):
|
|
if (self.visibility == 'connected' and request.website.is_public_user()):
|
|
error = werkzeug.exceptions.Forbidden()
|
|
elif self.visibility == 'password' and \
|
|
(request.website.is_public_user() or self.id not in request.session.get('views_unlock', [])):
|
|
pwd = request.params.get('visibility_password')
|
|
if pwd and self.env.user._crypt_context().verify(
|
|
pwd, self.visibility_password):
|
|
request.session.setdefault('views_unlock', list()).append(self.id)
|
|
else:
|
|
error = werkzeug.exceptions.Forbidden('website_visibility_password_required')
|
|
|
|
if self.visibility not in ('password', 'connected'):
|
|
try:
|
|
self._check_view_access()
|
|
except AccessError:
|
|
error = werkzeug.exceptions.Forbidden()
|
|
|
|
if error:
|
|
if do_raise:
|
|
raise error
|
|
else:
|
|
return False
|
|
return True
|
|
|
|
def _render_template(self, template, values=None):
|
|
""" Render the template. If website is enabled on request, then extend rendering context with website values. """
|
|
view = self._get(template).sudo()
|
|
view._handle_visibility(do_raise=True)
|
|
if values is None:
|
|
values = {}
|
|
if 'main_object' not in values:
|
|
values['main_object'] = view
|
|
return super()._render_template(template, values=values)
|
|
|
|
@api.model
|
|
def get_default_lang_code(self):
|
|
website_id = self.env.context.get('website_id')
|
|
if website_id:
|
|
lang_code = self.env['website'].browse(website_id).default_lang_id.code
|
|
return lang_code
|
|
else:
|
|
return super(View, self).get_default_lang_code()
|
|
|
|
def redirect_to_page_manager(self):
|
|
return {
|
|
'type': 'ir.actions.act_url',
|
|
'url': '/website/pages',
|
|
'target': 'self',
|
|
}
|
|
|
|
def _read_template_keys(self):
|
|
return super(View, self)._read_template_keys() + ['website_id']
|
|
|
|
@api.model
|
|
def _save_oe_structure_hook(self):
|
|
res = super(View, self)._save_oe_structure_hook()
|
|
res['website_id'] = self.env['website'].get_current_website().id
|
|
return res
|
|
|
|
@api.model
|
|
def _set_noupdate(self):
|
|
'''If website is installed, any call to `save` from the frontend will
|
|
actually write on the specific view (or create it if not exist yet).
|
|
In that case, we don't want to flag the generic view as noupdate.
|
|
'''
|
|
if not self._context.get('website_id'):
|
|
super(View, self)._set_noupdate()
|
|
|
|
def save(self, value, xpath=None):
|
|
self.ensure_one()
|
|
current_website = self.env['website'].get_current_website()
|
|
# xpath condition is important to be sure we are editing a view and not
|
|
# a field as in that case `self` might not exist (check commit message)
|
|
if xpath and self.key and current_website:
|
|
# The first time a generic view is edited, if multiple editable parts
|
|
# were edited at the same time, multiple call to this method will be
|
|
# done but the first one may create a website specific view. So if there
|
|
# already is a website specific view, we need to divert the super to it.
|
|
website_specific_view = self.env['ir.ui.view'].search([
|
|
('key', '=', self.key),
|
|
('website_id', '=', current_website.id)
|
|
], limit=1)
|
|
if website_specific_view:
|
|
self = website_specific_view
|
|
super(View, self).save(value, xpath=xpath)
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Snippet saving
|
|
# --------------------------------------------------------------------------
|
|
|
|
@api.model
|
|
def _snippet_save_view_values_hook(self):
|
|
res = super()._snippet_save_view_values_hook()
|
|
website_id = self.env.context.get('website_id')
|
|
if website_id:
|
|
res['website_id'] = website_id
|
|
return res
|