Odoo - 为特定用户隐藏按钮
Odoo - Hide button for specific user
我正在使用 odoo 10 企业版。我想向特定用户显示按钮而不是组,因为组中会有很多用户,我只想在下面显示具有 reject/approve 对象优先权的按钮。
这是按钮
<xpath expr="//sheet" position="before">
<header>
<button name="update_approve" attrs="{'invisible':[('first_approve', '=', uid)]}" string="Approve" type="object" class="oe_highlight"/>
<button name="update_reject" attrs="{'invisible':[('second_approve', '=', uid)]}" string="Reject" type="object" class="btn-danger"/>
</header>
</xpath>
我尝试使用 uid
执行此操作,但 uid 在 xml
中不可用
first_approve
和 second_approve
是我的模型中的字段,我想根据这些字段仅向在 first_approve/second_approve
中分配的用户显示按钮
我知道在 attrs 中使用字段的一件事是必须在表单中提及该字段。
我不知道如何在表单中获取用户 ID 的值。但如果没有短路
像 uid
或 user
这样你可以解决这个问题,只需创建一个 m2o 字段到 res.users
使用 store = False 使该字段计算字段。
# by default store = False this means the value of this field
# is always computed.
current_user = fields.Many2one('res.users', compute='_get_current_user')
@api.depends()
def _get_current_user(self):
for rec in self:
rec.current_user = self.env.user
您可以在表单中使用此字段。
<xpath expr="//sheet" position="before">
<header>
<!-- fin a good place for the field if i make the header look ugly -->
<!-- make invisible -->
<field name="current_user" invisible="1"/>
<!-- hope it work like this -->
<button name="update_approve" attrs="{'invisible':[('first_approve', '=', current_user)]}" string="Approve" type="object" class="oe_highlight"/>
<button name="update_reject" attrs="{'invisible':[('second_approve', '=', current_user)]}" string="Reject" type="object" class="btn-danger"/>
</header>
</xpath>
对不起我的英语。
我了解到您有一个可以批准的固定用户列表和另一个可以拒绝的固定用户列表。尽管有几个用户,我会创建两个组并在你的按钮上使用 groups
属性,但即使这样你也不想为他们创建几个组,你可以这样做:
from openerp import models, api
import json
from lxml import etree
FIRST_APPROVE = [] # Fill this list with the IDs of the users who can update approve
SECOND_APPROVE = [] # Fill this list with the IDs of the users who can update reject
class YourClass(models.Model):
_inherit = 'your.class'
def update_json_data(self, json_data=False, update_data={}):
''' It updates JSON data. It gets JSON data, converts it to a Python
dictionary, updates this, and converts the dictionary to JSON data
again. '''
dict_data = json.loads(json_data) if json_data else {}
dict_data.update(update_data)
return json.dumps(dict_data, ensure_ascii=False)
def set_modifiers(self, element=False, modifiers_upd={}):
''' It updates the JSON modifiers with the specified data to indicate
if a XML tag is readonly or invisible or not. '''
if element is not False: # Do not write only if element:
modifiers = element.get('modifiers') or {}
modifiers_json = self.update_json_data(
modifiers, modifiers_upd)
element.set('modifiers', modifiers_json)
@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
submenu=False):
res = super(YourClass, self).fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)
doc = etree.XML(res['arch'])
if view_type == 'form':
if self.env.uid in FIRST_APPROVE:
upd_approve_btn_search = doc.xpath("//button[@name='update_approve']")
upd_approve_btn = upd_approve_btn_search[0] \
if upd_approve_btn_search else False
if upd_approve_btn:
self.set_modifiers(upd_approve_btn, {'invisible': False, })
if self.env.uid in SECOND_APPROVE:
upd_reject_btn_search = doc.xpath("//button[@name='update_reject']")
upd_reject_btn = upd_reject_btn_search[0] \
if upd_reject_btn_search else False
if upd_reject_btn:
self.set_modifiers(upd_reject_btn, {'invisible': False, })
res['arch'] = etree.tostring(doc)
return res
FIRST APPROVE
和SECOND_APPROVE
将是const,其中必须引入可以执行相应操作的用户的固定ID(例如:FIRST APPROVE = [2, 7, 9]
)。
YourClass
必须是您声明按钮方法的 class(您声明 update_approve
和 update_reject
的方法)。
重要提示:使用此代码,您的按钮必须始终不可见(在 XML 视图中写入 invisible="1"
),因为在加载 XML 代码后, fields_view_get
将覆盖 invisible
值以设置为 0。
这是一种管理目的的不常见方法,但不幸的是,如果您不想创建组,我认为这是最简单的方法。希望对您和其他用户有所帮助!
我正在使用 odoo 10 企业版。我想向特定用户显示按钮而不是组,因为组中会有很多用户,我只想在下面显示具有 reject/approve 对象优先权的按钮。 这是按钮
<xpath expr="//sheet" position="before">
<header>
<button name="update_approve" attrs="{'invisible':[('first_approve', '=', uid)]}" string="Approve" type="object" class="oe_highlight"/>
<button name="update_reject" attrs="{'invisible':[('second_approve', '=', uid)]}" string="Reject" type="object" class="btn-danger"/>
</header>
</xpath>
我尝试使用 uid
执行此操作,但 uid 在 xml
first_approve
和 second_approve
是我的模型中的字段,我想根据这些字段仅向在 first_approve/second_approve
我知道在 attrs 中使用字段的一件事是必须在表单中提及该字段。
我不知道如何在表单中获取用户 ID 的值。但如果没有短路
像 uid
或 user
这样你可以解决这个问题,只需创建一个 m2o 字段到 res.users
使用 store = False 使该字段计算字段。
# by default store = False this means the value of this field
# is always computed.
current_user = fields.Many2one('res.users', compute='_get_current_user')
@api.depends()
def _get_current_user(self):
for rec in self:
rec.current_user = self.env.user
您可以在表单中使用此字段。
<xpath expr="//sheet" position="before">
<header>
<!-- fin a good place for the field if i make the header look ugly -->
<!-- make invisible -->
<field name="current_user" invisible="1"/>
<!-- hope it work like this -->
<button name="update_approve" attrs="{'invisible':[('first_approve', '=', current_user)]}" string="Approve" type="object" class="oe_highlight"/>
<button name="update_reject" attrs="{'invisible':[('second_approve', '=', current_user)]}" string="Reject" type="object" class="btn-danger"/>
</header>
</xpath>
对不起我的英语。
我了解到您有一个可以批准的固定用户列表和另一个可以拒绝的固定用户列表。尽管有几个用户,我会创建两个组并在你的按钮上使用 groups
属性,但即使这样你也不想为他们创建几个组,你可以这样做:
from openerp import models, api
import json
from lxml import etree
FIRST_APPROVE = [] # Fill this list with the IDs of the users who can update approve
SECOND_APPROVE = [] # Fill this list with the IDs of the users who can update reject
class YourClass(models.Model):
_inherit = 'your.class'
def update_json_data(self, json_data=False, update_data={}):
''' It updates JSON data. It gets JSON data, converts it to a Python
dictionary, updates this, and converts the dictionary to JSON data
again. '''
dict_data = json.loads(json_data) if json_data else {}
dict_data.update(update_data)
return json.dumps(dict_data, ensure_ascii=False)
def set_modifiers(self, element=False, modifiers_upd={}):
''' It updates the JSON modifiers with the specified data to indicate
if a XML tag is readonly or invisible or not. '''
if element is not False: # Do not write only if element:
modifiers = element.get('modifiers') or {}
modifiers_json = self.update_json_data(
modifiers, modifiers_upd)
element.set('modifiers', modifiers_json)
@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
submenu=False):
res = super(YourClass, self).fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)
doc = etree.XML(res['arch'])
if view_type == 'form':
if self.env.uid in FIRST_APPROVE:
upd_approve_btn_search = doc.xpath("//button[@name='update_approve']")
upd_approve_btn = upd_approve_btn_search[0] \
if upd_approve_btn_search else False
if upd_approve_btn:
self.set_modifiers(upd_approve_btn, {'invisible': False, })
if self.env.uid in SECOND_APPROVE:
upd_reject_btn_search = doc.xpath("//button[@name='update_reject']")
upd_reject_btn = upd_reject_btn_search[0] \
if upd_reject_btn_search else False
if upd_reject_btn:
self.set_modifiers(upd_reject_btn, {'invisible': False, })
res['arch'] = etree.tostring(doc)
return res
FIRST APPROVE
和SECOND_APPROVE
将是const,其中必须引入可以执行相应操作的用户的固定ID(例如:FIRST APPROVE = [2, 7, 9]
)。
YourClass
必须是您声明按钮方法的 class(您声明 update_approve
和 update_reject
的方法)。
重要提示:使用此代码,您的按钮必须始终不可见(在 XML 视图中写入 invisible="1"
),因为在加载 XML 代码后, fields_view_get
将覆盖 invisible
值以设置为 0。
这是一种管理目的的不常见方法,但不幸的是,如果您不想创建组,我认为这是最简单的方法。希望对您和其他用户有所帮助!