如何限制(拒绝)某些用户的产品创建?

时间:2019-01-08 10:07:14

标签: python xml python-2.7 odoo odoo-9

enter image description here我正在使用Odoo 9,但我想拒绝某些用户创建产品,例如,对于销售人员,我希望他们只能访问已经创建的产品。他们无权创建新产品。我该怎么做?有任何想法吗?

product.py

class product_product(models.Model):
_inherit = "product.product"

@api.model
def create(self, vals):
if self.env.user.has_group('yor_module.your_group'):
    raise Warning(
        _('Sorry, you are not allowed to create new products.'),
    )
else:
    return super(product_product, self).create(vals)

security.xml

 <?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
    <record model="res.groups" id="your_group">
        <field name="name">Group of users who cannot create new products</field>
    </record>
</data>

1 个答案:

答案 0 :(得分:0)

此功能应通过规则来完成,但我认为它们是不可预测的,在这种情况下,它们不能为您提供灵活性来限制您的条件(如果需要)。我将执行以下操作:

首先在模块的XML文件中创建一个新的安全组(不要忘记在__manifest__.py / __openerp__.py中添加文件):

<record model="res.groups" id="your_group">
    <field name="name">Group of users who cannot create new products</field>
</record>

然后覆盖product.product ORM create方法并在其中添加条件:

@api.model
def create(self, vals):
    if self.env.user.has_group('your_module.your_group'):
        raise Warning(
            _('Sorry, you are not allowed to create new products.'),
        )
    else:
        return super(ProductProduct, self).create(vals)

或者以product.template ORM create方法执行操作,这与产品通过委派从模板继承无关。

如果您希望该组的用户不要写或删除产品,请也从那些ORM方法中继承并向它们添加类似的条件。

您应该通过界面手动将无法创建产品的用户添加到您的新组中,或者通过您在其中创建组的XML(通过参数{{ 1}}-限制特定用户-或users-限制整个组-)。