[英]Odoo - Hide button for specific user
我正在使用 odoo 10 企業版。 我想向特定用戶顯示按鈕而不是組,因為一個組中會有很多用戶,我只想在按鈕下方顯示具有拒絕/批准對象的特權。 這是按鈕
<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,您必須在其中引入可以執行相應操作的用戶的固定 IDS(例如: FIRST APPROVE = [2, 7, 9]
)。
YourClass
必須是您在其中聲明按鈕方法的類(您在其中聲明了update_approve
和update_reject
)。
重要提示:使用此代碼,您的按鈕必須始終不可見(在 XML 視圖中寫入invisible="1"
),因為在加載 XML 代碼后, fields_view_get
將覆蓋invisible
值以設置 0。
這是一種不常見的管理目的的方法,但不幸的是,如果您不想創建組,我認為這是最簡單的方法。 我希望它可以幫助您和其他用戶!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.