gpt4 book ai didi

python - 如何允许群组用户修改 Odoo 中表单的特定部分?

转载 作者:行者123 更新时间:2023-11-30 23:04:34 26 4
gpt4 key购买 nike

我创建了一个名为会计师的新组。例如,如果该组的用户打开 res.partner 表单,他必须能够读取所有字段,但只能修改某些特定字段(Accountancy 选项卡内的字段) ,例如)。

所以我将权限createwriteunlinkread设置为0 会计师的 res.partner 模型中的 code>、101强>组。

问题:如果我是 accountant 组的用户,并且我转到 res.partner 的表单,我将看到编辑 按钮,如果我点击它,我将能够修改我想要的任何字段(我不应该,只能修改选项卡内的字段)。

所以我想复制菜单项(将属性 groups="accountant" 放入副本)和表单(将除选项卡内容之外的所有字段设为只读)。

问题:如果我是 accountant 组之上的组的用户(accountant 在其 implied_ids 列表中),我会查看两个菜单项(采用正常形式的菜单项和采用只读字段的重复形式的菜单项)。

是否可以创建一个菜单项,根据单击上述菜单项的用户组打开一组特定的 View ?我有什么想法可以成功实现这一点吗?

最佳答案

我在谷歌上搜索了很多,有很多人在 Odoo 论坛上问同样的问题,但没有人给他们答案。

最后,我找到了这个解决方法,在我的例子中效果非常好:每个模型中都有的方法 field_view_get 会在返回 XML View 以进行显示之前获取它。这意味着您可以通过 Python 代码以任何您想要的方式操作 View 。

您只需要知道如何使用像lxml这样的库来用XML代码修改字符串变量。我举个例子:

RES.PARTNER模型(这里我们必须使用lxml库中的etree)

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
submenu=False):
res = super(res_partner, self).fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)

checking_obj = self.env['my.own.checkings']
doc = etree.XML(res['arch'])

if checking_obj.belongs_to_accountant_group():
doc.set('create', 'false')
doc.set('delete', 'false')
doc.set('edit', 'true')

if view_type == 'form':
accounting_pages = doc.xpath("//page[@name='accounting']")
accounting_page = accounting_pages[0] if accounting_pages \
else False

if accounting_page is not False:
if checking_obj.belongs_to_accountant_group():
all_fields = doc.xpath("//field")
for field in all_fields:
if accounting_page not in field.iterancestors():
checking_obj.set_modifiers(field,
{'readonly': True, })

res['arch'] = etree.tostring(doc)
return res

AUXILIAR CUSTOMIZED 模型(名为 MY.OWN.CHECKINGS)(这里我们必须使用 json 库)

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)

def is_accountant(self):
return self.env.ref(
'my_module.group_accountant').id in \
self.env.user.groups_id.mapped('id')

这样您就可以根据当前用户的组将某些字段设为只读。

关于python - 如何允许群组用户修改 Odoo 中表单的特定部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33627789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com