📅  最后修改于: 2023-12-03 15:03:21.316000             🧑  作者: Mango
Odoo 是一种完全集成的开源商业管理软件,其中包括出色的CRM、销售、采购、库存、会计、人力资源、项目管理等功能。本文将演示如何在 Odoo 中添加图标。
Odoo 使用 Font Awesome 作为图标字体,因此需要安装 Font Awesome。我们可以通过下面的命令来安装:
$ npm install @fortawesome/fontawesome-free
接下来,我们需要在 Odoo 中加载 Font Awesome。为此,我们可以将 fa.min.css
文件复制到 addons/web/static/lib/fontawesome/css
目录下。
然后,在 addons/web/controllers/main.py
文件中添加以下行:
from odoo.addons.web.controllers.main import Web, Home
from odoo.addons.web.controllers.main import ensure_db
from odoo.addons.web_settings_dashboard.controllers.main import WebSettingsDashboard
from odoo.addons.web.controllers.main import DataSet
class MyWeb(Web):
@http.route('/web/binary/icons', type='binary', auth="none")
def load_icon(self, **kwargs):
return send_file(os.path.join(os.path.dirname(__file__), 'static', 'src', 'img', 'odoo_icon.png'), mimetype='image/png')
class MyHome(Home):
@http.route()
def index(self, **kw):
return self.load_page(**kw)
class MyWebSettingsDashboard(WebSettingsDashboard):
@http.route(['/web/action/load'], type='json', auth="user")
def web_action_load(self, action_id=None, **kw):
res = super(MyWebSettingsDashboard, self).web_action_load(action_id, **kw)
res['arch'] = res['arch'].replace("<i class='fa fa-dashboard o_web_settings_dashboard_icon'/>", "<img src='/web/binary/icons'/>")
return res
class MyDataSet(DataSet):
@http.route('/web/export/xlsx', type='http', auth='user')
def export_xlsx(self, model, fields, domain, sort, offset=0, limit=None, download=None, **kw):
return super(MyDataSet, self.with_context(icon_path="/web/binary/icons")).export_xlsx(model, fields, domain, sort, offset, limit, download, **kw)
最后,我们需要用我们自己的路由类覆盖现有的路由:
from odoo import http
from odoo.http import request
from odoo.addons.web.controllers.main import ensure_db
from odoo.addons.web.controllers.main import Home, Web
from odoo.addons.web_settings_dashboard.controllers.main import WebSettingsDashboard
from odoo.addons.web.controllers.main import DataSet
class MyWeb(Web):
@http.route('/web/binary/icons', type='binary', auth="none")
def load_icon(self, **kwargs):
return send_file(os.path.join(os.path.dirname(__file__), 'static', 'src', 'img', 'odoo_icon.png'), mimetype='image/png')
class MyHome(Home):
@http.route()
def index(self, **kw):
return self.load_page(**kw)
class MyWebSettingsDashboard(WebSettingsDashboard):
@http.route(['/web/action/load'], type='json', auth="user")
def web_action_load(self, action_id=None, **kw):
res = super(MyWebSettingsDashboard, self).web_action_load(action_id, **kw)
res['arch'] = res['arch'].replace("<i class='fa fa-dashboard o_web_settings_dashboard_icon'/>", "<img src='/web/binary/icons'/>")
return res
class MyDataSet(DataSet):
@http.route('/web/export/xlsx', type='http', auth='user')
def export_xlsx(self, model, fields, domain, sort, offset=0, limit=None, download=None, **kw):
return super(MyDataSet, self.with_context(icon_path="/web/binary/icons")).export_xlsx(model, fields, domain, sort, offset, limit, download, **kw)
class OdooIcons(http.Root):
def __init__(self):
super(OdooIcons, self).__init__()
self.main = MyWeb()
self.home = MyHome()
self.web_settings_dashboard = MyWebSettingsDashboard()
self.data_set = MyDataSet()
现在我们已经完成了所有准备工作,可以开始在 Odoo 中添加图标了。我们可以使用 icon
字段来添加图标。
例如,我们可以在模型中添加一个 company_logo
字段:
class ResCompany(models.Model):
_inherit = "res.company"
company_logo = fields.Binary("Logo")
icon = fields.Char("Icon", readonly=True, compute="_compute_icon")
def _compute_icon(self):
for record in self:
if record.company_logo:
record.icon = "<img src='/web/image/res.company/%s/company_logo' alt='Company logo' class='oe-company'></img>" % record.id
else:
record.icon = "<i class='fa fa-building'></i>"
在这个例子中,我们使用 _compute_icon
方法来根据 company_logo
字段的值计算 icon
字段的值。如果 company_logo
字段不为空,则用 <img>
标签添加图片,否则用 <i>
标签添加 Font Awesome 图标。
在本文中,我们演示了如何在 Odoo 中添加图标。我们需要安装 Font Awesome,并用我们自己的路由类覆盖现有的路由。然后,我们可以在模型中使用 icon
字段来添加图标。