OpenERP - 根据状态限制Purchase \ User的产品模块编辑操作?

时间:2014-05-28 04:18:58

标签: python-2.7 openerp openerp-7

我继承了OpenERP Products模块并添加了一个名为stage的新字段。 代码是,

_columns =  {
    'stage': fields.selection([
        ('pending', 'Pending'),
        ('confirmed', 'Confirmed'),
        ('cancel', 'Cancelled'),
    ], 'Status', select=True, track_visibility='onchange', help='Product Workflow Stages')

现在,当产品处于Purchase/User阶段时,我需要一个阻止confirmed编辑操作的条件。

def write(self, cr, uid, ids, datas, context = {} ):
        product_stage = datas['stage']
        if product_stage == 'confirmed':
            return super(purchase_order, self).write(cr, uid, ids, datas, context)
        else:
            raise osv.except_osv('Warning!',"You are not allowed to make changes! '%s'." % product_stage)

此条件必须仅适用于Purchase\User

我是python代码的新手。任何人都可以帮我吗?。

1 个答案:

答案 0 :(得分:1)

以下代码对我来说很好。

def write(self, cr, uid, ids,  datas = {}, context = {} ):
    product_obj=self.pool.get('product.product')
    new_product_obj=product_obj.browse(cr, uid, ids[0], context=context)
    all_groups=self.pool.get('res.groups')
    edit_group = all_groups.browse(cr, uid, all_groups.search(cr,uid,[('name','=','User')])[3])
    groups_users=edit_group.users
    for groups_user in groups_users:
        if uid == groups_user.id:
            if 'confirmed' == new_product_obj.stage:
                raise osv.except_osv('Warning!',"You are not allowed to make any changes in confirmed products!")
            else:
                return super(purchase_order, self).write(cr, uid, ids, datas, context)
    return super(purchase_order, self).write(cr, uid, ids, datas, context)