Replace _constraints with validate in ModelStorage and improve user messages.

issue2616
review371002
This commit is contained in:
Albert Cervera i Areny
2013-03-07 16:55:39 +01:00
parent 00aa0a67f8
commit 166b583ade
3 changed files with 30 additions and 22 deletions
+9 -6
View File
@@ -78,12 +78,9 @@ class BOMInput(ModelSQL, ModelView):
('product_bom_uniq', 'UNIQUE(product, bom)',
'product_bom_uniq'),
]
cls._constraints += [
('check_bom_recursion', 'recursive_bom'),
]
cls._error_messages.update({
'product_bom_uniq': 'Product must be unique per BOM!',
'recursive_bom': 'You can not create recursive BOMs!',
'product_bom_uniq': 'Product must be unique per BOM.',
'recursive_bom': 'You can not create recursive BOMs.',
})
def on_change_product(self):
@@ -109,11 +106,17 @@ class BOMInput(ModelSQL, ModelView):
return self.uom.digits
return 2
@classmethod
def validate(cls, boms):
super(BOMInput, cls).validate(boms)
for bom in boms:
bom.check_bom_recursion()
def check_bom_recursion(self):
'''
Check BOM recursion
'''
return self.product.check_bom_recursion()
self.product.check_bom_recursion()
def compute_quantity(self, factor):
Uom = Pool().get('product.uom')
+11 -9
View File
@@ -21,13 +21,17 @@ class Product:
@classmethod
def __setup__(cls):
super(Product, cls).__setup__()
cls._constraints += [
('check_bom_recursion', 'recursive_bom'),
]
cls._error_messages.update({
'recursive_bom': 'You can not create recursive BOMs!',
'recursive_bom': ('You are trying to create a recursive BOM '
'with product "%s" which is not allowed.'),
})
@classmethod
def validate(cls, products):
super(Product, cls).validate(products)
for product in products:
product.check_bom_recursion()
def check_bom_recursion(self, product=None):
'''
Check BOM recursion
@@ -36,11 +40,9 @@ class Product:
product = self
for product_bom in self.boms:
for input_ in product_bom.bom.inputs:
if input_.product == product:
return False
if not input_.product.check_bom_recursion(product=product):
return False
return True
if (input_.product == product
or input_.product.check_bom_recursion(product=product)):
self.raise_user_error('recursive_bom', (product.rec_name,))
class ProductBom(ModelSQL, ModelView):
+10 -7
View File
@@ -133,11 +133,9 @@ class Production(Workflow, ModelSQL, ModelView):
@classmethod
def __setup__(cls):
super(Production, cls).__setup__()
cls._constraints += [
('check_cost', 'missing_cost'),
]
cls._error_messages.update({
'missing_cost': 'It misses some cost on the outputs!',
'missing_cost': ('Production "%s" misses costs on '
'some of its outputs.'),
})
cls._transitions |= set((
('request', 'draft'),
@@ -436,16 +434,21 @@ class Production(Workflow, ModelSQL, ModelView):
move.save()
self._set_move_planned_date()
@classmethod
def validate(cls, productions):
super(Production, cls).validate(productions)
for production in productions:
production.check_cost()
def check_cost(self):
if self.state != 'done':
return True
return
cost_price = Decimal(0)
for output in self.outputs:
cost_price += (Decimal(str(output.quantity))
* output.unit_price)
if not self.company.currency.is_zero(self.cost - cost_price):
return False
return True
self.raise_user_error('missing_cost', (self.rec_name,))
@classmethod
def create(cls, vlist):