mrp_user_product_restriction/models/mrp_production.py
2026-01-23 15:33:44 +07:00

35 lines
1.5 KiB
Python

from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
class MrpProduction(models.Model):
_inherit = 'mrp.production'
def _get_default_allowed_products(self):
return self.env.user.allowed_manufacture_product_ids
allowed_product_ids = fields.Many2many(
'product.product',
compute='_compute_allowed_product_ids',
default=_get_default_allowed_products
)
@api.depends_context('uid')
def _compute_allowed_product_ids(self):
for record in self:
record.allowed_product_ids = self.env.user.allowed_manufacture_product_ids
@api.constrains('product_id')
def _check_allowed_product(self):
for record in self:
# Use sudo() isn't necessary for checking self.env.user, but if we were checking record.user_id it might be.
# self.env.user is the current logged in user performing the write/create.
user = self.env.user
# Check if user has any allowed products
# If allowed_manufacture_product_ids is empty, NO products are allowed.
if not user.allowed_manufacture_product_ids:
raise ValidationError(_("You are not allowed to manufacture any products. Please contact your administrator."))
if record.product_id and record.product_id not in user.allowed_manufacture_product_ids:
raise ValidationError(_("You are not allowed to manufacture this product: %s") % record.product_id.name)