Project

General

Profile

0001-environment-parse-float-value-as-json-integers-in-va.patch

Benjamin Dauvergne, 12 Mar 2019 05:47 PM

Download (2.21 KB)

View differences:

Subject: [PATCH] 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.
 hobo/environment/models.py | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
hobo/environment/models.py
1
import re
1 2
import datetime
2 3
import json
3 4
import random
......
22 23

  
23 24
SECRET_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
24 25

  
26
FLOAT_RE = re.compile(r'^\s*[0-9]+\.[0-9]+\s*')
27

  
28

  
25 29
class Variable(models.Model):
26 30
    name = models.CharField(max_length=100, verbose_name=_('name'))
27 31
    label = models.CharField(max_length=100, blank=True, verbose_name=_('label'))
......
39 43
            return self.label
40 44
        return self.name
41 45

  
42
    @property
43
    def json(self):
44
        if self.value and (self.value[0] in '{[' or self.value in ('true', 'false')):
46
    def _parse_value_as_json(self):
47
        if self.value and (
48
                self.value[0] in '{['
49
                or self.value in ('true', 'false')
50
                or FLOAT_RE.match(self.value)):
45 51
            try:
46 52
                return json.loads(self.value)
47 53
            except ValueError:
48
                pass
54
                raise ValidationError('invalid JSON document')
49 55
        return self.value
50 56

  
57
    @property
58
    def json(self):
59
        try:
60
            return self._parse_value_as_json()
61
        except ValidationError:
62
            return self.value
63

  
51 64
    def clean(self):
52
        if self.value and (self.value[0] in '{[' or self.value in ('true', 'false')):
53
            try:
54
                json.loads(self.value)
55
            except ValueError:
56
                raise ValidationError('invalid JSON document')
65
        self._parse_value_as_json()
57 66

  
58 67

  
59 68
class ServiceBase(models.Model):
60
-