Basic validation for zoom values

This commit is contained in:
Florian Bruhin 2014-04-10 12:57:57 +02:00
parent c845db4799
commit cba2d4d450
2 changed files with 51 additions and 3 deletions

View File

@ -126,12 +126,13 @@ def configdata():
"entered."),
('zoomlevels',
SettingValue(types.IntList, "25,33,50,67,75,90,100,110,125,150,"
"175,200,250,300,400,500"),
SettingValue(types.PercList, "25%,33%,50%,67%,75%,90%,100%,110%,"
"125%,150%,175%,200%,250%,300%,400%,"
"500%"),
"The available zoom levels, separated by commas."),
('defaultzoom',
SettingValue(types.Int, "100"),
SettingValue(types.ZoomPerc, "100%"),
"The default zoom level."),
)),

View File

@ -234,6 +234,53 @@ class IntList(List):
raise ValidationError(value, "must be a list of integers!")
class Perc(BaseType):
"""Percentage which may be >100 but needs to be positive."""
def transform(self, value):
return int(value.rstrip('%'))
def validate(self, value):
if not value.endswith('%'):
raise ValidationError(value, "does not end with %")
try:
intval = int(value.rstrip('%'))
except ValueError:
raise ValidationError(value, "invalid percentage!")
else:
if not 0 <= intval:
raise ValidationError(value, "percentage needs to be >= 0!")
class PercList(List):
"""Base class for a list of percentages."""
typestr = 'perc-list'
def transform(self, value):
vals = super().transform(value)
return [int(val.rstrip('%')) for val in vals]
def validate(self, value):
vals = super().transform(value)
try:
for val in vals:
Perc.validate(self, val)
except ValidationError:
raise ValidationError(value, "must be a list of percentages!")
class ZoomPerc(Perc):
"""A percentage which needs to be in the current zoom percentages."""
def validate(self, value):
super().validate(value)
# FIXME we should validate the percentage is in the list here.
class PercOrInt(BaseType):
"""Percentage or integer."""