environment: parse float value as json integers in variables (#31332)
If you need a real integer you must always put a decimal point, i.e. write 1.0 not 1.
This commit is contained in:
parent
16f9122b7b
commit
a396773103
|
@ -1,3 +1,4 @@
|
|||
import re
|
||||
import datetime
|
||||
import json
|
||||
import random
|
||||
|
@ -22,6 +23,9 @@ from .mandayejs_app_settings import APP_SETTINGS_CLASSES, DEFAULT_APP_SETTINGS
|
|||
|
||||
SECRET_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
|
||||
|
||||
FLOAT_RE = re.compile(r'^\s*[0-9]+\.[0-9]+\s*')
|
||||
|
||||
|
||||
class Variable(models.Model):
|
||||
name = models.CharField(max_length=100, verbose_name=_('name'))
|
||||
label = models.CharField(max_length=100, blank=True, verbose_name=_('label'))
|
||||
|
@ -39,21 +43,26 @@ class Variable(models.Model):
|
|||
return self.label
|
||||
return self.name
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
if self.value and (self.value[0] in '{[' or self.value in ('true', 'false')):
|
||||
def _parse_value_as_json(self):
|
||||
if self.value and (
|
||||
self.value[0] in '{['
|
||||
or self.value in ('true', 'false')
|
||||
or FLOAT_RE.match(self.value)):
|
||||
try:
|
||||
return json.loads(self.value)
|
||||
except ValueError:
|
||||
pass
|
||||
raise ValidationError('invalid JSON document')
|
||||
return self.value
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
try:
|
||||
return self._parse_value_as_json()
|
||||
except ValidationError:
|
||||
return self.value
|
||||
|
||||
def clean(self):
|
||||
if self.value and (self.value[0] in '{[' or self.value in ('true', 'false')):
|
||||
try:
|
||||
json.loads(self.value)
|
||||
except ValueError:
|
||||
raise ValidationError('invalid JSON document')
|
||||
self._parse_value_as_json()
|
||||
|
||||
|
||||
class ServiceBase(models.Model):
|
||||
|
|
Loading…
Reference in New Issue