Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
232 lines (186 sloc) 8.13 KB
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
from odoo import api, fields, models
from import xml_translate
from odoo.modules.module import get_resource_from_path
_logger = logging.getLogger(__name__)
class ThemeView(models.Model):
_name = ''
_description = 'Theme UI View'
def compute_arch_fs(self):
if 'install_filename' not in self._context:
return ''
path_info = get_resource_from_path(self._context['install_filename'])
if path_info:
return '/'.join(path_info[0:2])
name = fields.Char(required=True)
key = fields.Char()
type = fields.Char()
priority = fields.Integer(default=16, required=True)
mode = fields.Selection([('primary', "Base view"), ('extension', "Extension View")])
active = fields.Boolean(default=True)
arch = fields.Text(translate=xml_translate)
arch_fs = fields.Char(default=compute_arch_fs)
inherit_id = fields.Reference(selection=[('ir.ui.view', 'ir.ui.view'), ('', '')])
copy_ids = fields.One2many('ir.ui.view', 'theme_template_id', 'Views using a copy of me', copy=False, readonly=True)
# TODO master add missing field: customize_show
def _convert_to_base_model(self, website, **kwargs):
inherit = self.inherit_id
if self.inherit_id and self.inherit_id._name == '':
inherit = self.inherit_id.with_context(active_test=False).copy_ids.filtered(lambda x: x.website_id == website)
if not inherit:
# inherit_id not yet created, add to the queue
return False
if inherit and inherit.website_id != website:
website_specific_inherit = self.env['ir.ui.view'].with_context(active_test=False).search([
('key', '=', inherit.key),
('website_id', '=',
], limit=1)
if website_specific_inherit:
inherit = website_specific_inherit
new_view = {
'type': self.type or 'qweb',
'arch': self.arch,
'key': self.key,
'inherit_id': inherit and,
'arch_fs': self.arch_fs,
'priority': self.priority,
if self.mode: # if not provided, it will be computed automatically (if inherit_id or not)
new_view['mode'] = self.mode
return new_view
class ThemeAttachment(models.Model):
_name = ''
_description = 'Theme Attachments'
name = fields.Char(required=True)
key = fields.Char(required=True)
url = fields.Char()
copy_ids = fields.One2many('ir.attachment', 'theme_template_id', 'Attachment using a copy of me', copy=False, readonly=True)
def _convert_to_base_model(self, website, **kwargs):
new_attach = {
'key': self.key,
'public': True,
'res_model': 'ir.ui.view',
'type': 'url',
'url': self.url,
return new_attach
class ThemeMenu(models.Model):
_name = ''
_description = 'Website Theme Menu'
name = fields.Char(required=True, translate=True)
url = fields.Char(default='')
page_id = fields.Many2one('', ondelete='cascade')
new_window = fields.Boolean('New Window')
sequence = fields.Integer()
parent_id = fields.Many2one('', index=True, ondelete="cascade")
copy_ids = fields.One2many('', 'theme_template_id', 'Menu using a copy of me', copy=False, readonly=True)
def _convert_to_base_model(self, website, **kwargs):
page_id = self.page_id.copy_ids.filtered(lambda x: x.website_id == website)
parent_id = self.copy_ids.filtered(lambda x: x.website_id == website)
new_menu = {
'url': self.url,
'page_id': page_id and or False,
'new_window': self.new_window,
'sequence': self.sequence,
'parent_id': parent_id and or False,
return new_menu
class ThemePage(models.Model):
_name = ''
_description = 'Website Theme Page'
url = fields.Char()
view_id = fields.Many2one('', required=True, ondelete="cascade")
website_indexed = fields.Boolean('Page Indexed', default=True)
copy_ids = fields.One2many('', 'theme_template_id', 'Page using a copy of me', copy=False, readonly=True)
def _convert_to_base_model(self, website, **kwargs):
view_id = self.view_id.copy_ids.filtered(lambda x: x.website_id == website)
if not view_id:
# inherit_id not yet created, add to the queue
return False
new_page = {
'url': self.url,
'website_indexed': self.website_indexed,
return new_page
class Theme(models.AbstractModel):
_name = 'theme.utils'
_description = 'Theme Utils'
_auto = False
def _post_copy(self, mod, website=False):
# deprecated: to remove in master
if not website: # remove optional website in master
website = self.env['website'].get_current_website()
# Reinitialize font customizations
'font-number': 'null',
'headings-font-number': 'null',
'navbar-font-number': 'null',
'buttons-font-number': 'null',
# Call specific theme post copy
theme_post_copy = '_%s_post_copy' %
if hasattr(self, theme_post_copy):'Executing method %s' % theme_post_copy)
method = getattr(self.with_context(, theme_post_copy)
return method(mod)
return False
def _toggle_view(self, xml_id, active):
obj = self.env.ref(xml_id)
website = self.env['website'].get_current_website()
if obj._name == '':
obj = obj.with_context(active_test=False)
obj = obj.copy_ids.filtered(lambda x: x.website_id == website)
# If a theme post copy wants to enable/disable a view, this is to
# enable/disable a given functionality which is disabled/enabled
# by default. So if a post copy asks to enable/disable a view which
# is already enabled/disabled, we would not consider it otherwise it
# would COW the view for nothing.
View = self.env['ir.ui.view'].with_context(active_test=False)
has_specific = obj.key and View.search_count([
('key', '=', obj.key),
('website_id', '=',
]) >= 1
if not has_specific and active ==
obj.write({'active': active})
def enable_view(self, xml_id):
self._toggle_view(xml_id, True)
def disable_view(self, xml_id):
self._toggle_view(xml_id, False)
class IrUiView(models.Model):
_inherit = 'ir.ui.view'
theme_template_id = fields.Many2one('')
class IrAttachment(models.Model):
_inherit = 'ir.attachment'
key = fields.Char()
theme_template_id = fields.Many2one('')
class WebsiteMenu(models.Model):
_inherit = ''
theme_template_id = fields.Many2one('')
class WebsitePage(models.Model):
_inherit = ''
theme_template_id = fields.Many2one('')