320 class URLIntegerType (URLType) : |
320 class URLIntegerType (URLType) : |
321 """ |
321 """ |
322 A URLType for simple integers |
322 A URLType for simple integers |
323 """ |
323 """ |
324 |
324 |
325 def __init__ (self, negative=True, zero=True, max=None) : |
325 def __init__ (self, allow_negative=True, allow_zero=True, max=None) : |
326 """ |
326 """ |
327 Pass in negative=False to disallow negative numbers, zero=False to disallow zero, or non-zero max |
327 Pass in allow_negative=False to disallow negative numbers, allow_zero=False to disallow zero, or non-zero |
328 to specifiy maximum value |
328 max to specifiy maximum value |
329 """ |
329 """ |
330 |
330 |
331 self.negative = negative |
331 self.allow_negative = allow_negative |
332 self.zero = zero |
332 self.allow_zero = allow_zero |
333 self.max = max |
333 self.max = max |
334 |
334 |
335 def _validate (self, value) : |
335 def _validate (self, value) : |
336 """ |
336 """ |
337 Test to make sure value fits our criteria |
337 Test to make sure value fits our criteria |
338 """ |
338 """ |
339 |
339 |
340 # negative? |
340 # negative? |
341 if self.negative and value < 0 : |
341 if not self.allow_negative and value < 0 : |
342 raise ValueError("value is negative") |
342 raise ValueError("value is negative") |
343 |
343 |
344 # zero? |
344 # zero? |
345 if self.zero and value == 0 : |
345 if not self.allow_zero and value == 0 : |
346 raise ValueError("value is zero") |
346 raise ValueError("value is zero") |
347 |
347 |
348 # max? |
348 # max? |
349 if self.max is not None and value > max : |
349 if self.max is not None and value > max : |
350 raise ValueError("value is too large: %d" % value) |
350 raise ValueError("value is too large: %d" % value) |