mirror of
https://github.com/jazzband/django-configurations.git
synced 2026-03-16 22:20:27 +00:00
Add PositiveIntegerValue to only allow positive integers (#186)
This commit is contained in:
parent
3883cc4fe4
commit
1c6fd0f505
3 changed files with 27 additions and 1 deletions
|
|
@ -178,6 +178,15 @@ class IntegerValue(CastingMixin, Value):
|
|||
caster = int
|
||||
|
||||
|
||||
class PositiveIntegerValue(IntegerValue):
|
||||
|
||||
def to_python(self, value):
|
||||
int_value = super(PositiveIntegerValue, self).to_python(value)
|
||||
if int_value < 0:
|
||||
raise ValueError(self.message.format(value))
|
||||
return int_value
|
||||
|
||||
|
||||
class FloatValue(CastingMixin, Value):
|
||||
caster = float
|
||||
|
||||
|
|
|
|||
|
|
@ -230,6 +230,14 @@ Type values
|
|||
|
||||
MYSITE_CACHE_TIMEOUT = values.IntegerValue(3600)
|
||||
|
||||
.. class:: PositiveIntegerValue
|
||||
|
||||
A :class:`~Value` subclass that handles positive integer values.
|
||||
|
||||
::
|
||||
|
||||
MYSITE_WORKER_POOL = values.PositiveIntegerValue(8)
|
||||
|
||||
.. class:: FloatValue
|
||||
|
||||
A :class:`~Value` subclass that handles float values.
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ from configurations.values import (Value, BooleanValue, IntegerValue,
|
|||
DatabaseURLValue, EmailURLValue,
|
||||
CacheURLValue, BackendsValue,
|
||||
CastingMixin, SearchURLValue,
|
||||
setup_value)
|
||||
setup_value, PositiveIntegerValue)
|
||||
|
||||
|
||||
@contextmanager
|
||||
|
|
@ -136,6 +136,15 @@ class ValueTests(TestCase):
|
|||
with env(DJANGO_TEST='noninteger'):
|
||||
self.assertRaises(ValueError, value.setup, 'TEST')
|
||||
|
||||
def test_positive_integer_values(self):
|
||||
value = PositiveIntegerValue(1)
|
||||
with env(DJANGO_TEST='2'):
|
||||
self.assertEqual(value.setup('TEST'), 2)
|
||||
with env(DJANGO_TEST='noninteger'):
|
||||
self.assertRaises(ValueError, value.setup, 'TEST')
|
||||
with env(DJANGO_TEST='-1'):
|
||||
self.assertRaises(ValueError, value.setup, 'TEST')
|
||||
|
||||
def test_float_values(self):
|
||||
value = FloatValue(1.0)
|
||||
with env(DJANGO_TEST='2.0'):
|
||||
|
|
|
|||
Loading…
Reference in a new issue