eo_facture: rewrite check_percentage_per_year correctly (#81677)
gitea/barbacompta/pipeline/head This commit looks good
Details
gitea/barbacompta/pipeline/head This commit looks good
Details
This commit is contained in:
parent
a4547addd4
commit
25dc48669c
|
@ -36,28 +36,21 @@ class EuroField(models.DecimalField):
|
|||
super().__init__(*args, **kwargs)
|
||||
|
||||
|
||||
def assertion_error_to_validation_error(fun):
|
||||
def f(*args, **kwargs):
|
||||
try:
|
||||
return fun(*args, **kwargs)
|
||||
except (AssertionError, ValueError) as e:
|
||||
raise ValidationError('%s fails' % fun.func_name(), *e.args)
|
||||
|
||||
return f
|
||||
|
||||
|
||||
@assertion_error_to_validation_error
|
||||
def check_percentage_per_year(value):
|
||||
years = [a for a, b in value]
|
||||
percentages = [b for a, b in value]
|
||||
# ordered
|
||||
assert years == sorted(years), 'years are not ordered'
|
||||
if years != sorted(years):
|
||||
raise ValidationError(_('years are not ordered'))
|
||||
# sum equals 100
|
||||
assert sum(percentages) == 1, 'percentage does not sum to 100'
|
||||
if sum(percentages) != 1:
|
||||
raise ValidationError(_('percentage does not sum to 100'))
|
||||
# no duplicate year
|
||||
assert len(years) == len(set(years)), 'years are not unique'
|
||||
if len(years) != len(set(years)):
|
||||
raise ValidationError(_('years are not unique'))
|
||||
# consecutive
|
||||
assert years == list(range(years[0], years[0] + len(years))), 'years are not consecutive'
|
||||
if years != list(range(years[0], years[0] + len(years))):
|
||||
raise ValidationError(_('years are not consecutive'))
|
||||
|
||||
|
||||
def parse_percentage_per_year(value):
|
||||
|
@ -85,13 +78,14 @@ class PercentagePerYearFormField(forms.Field):
|
|||
default_error_messages = {
|
||||
'invalid': _('field must be numeric values separated by commas'),
|
||||
}
|
||||
default_validators = [check_percentage_per_year]
|
||||
|
||||
def to_python(self, value):
|
||||
if value in validators.EMPTY_VALUES:
|
||||
return None
|
||||
if isinstance(PercentagePerYear, value):
|
||||
if isinstance(value, PercentagePerYear):
|
||||
return value
|
||||
if not isinstance(str, value):
|
||||
if not isinstance(value, str):
|
||||
raise ValidationError(self.default_error_messages['invalid'])
|
||||
return parse_percentage_per_year(value)
|
||||
|
||||
|
|
Loading…
Reference in New Issue