Project

General

Profile

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

Benjamin Dauvergne, 12 Mar 2019 05:41 PM

Download (1.93 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 | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 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'))
......
41 45

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

  
51 58
    def clean(self):
52
        if self.value and (self.value[0] in '{[' or self.value in ('true', 'false')):
59
        if self.value and (
60
                self.value[0] in '{['
61
                or self.value in ('true', 'false')
62
                or FLOAT_RE.match(self.value)):
53 63
            try:
54 64
                json.loads(self.value)
55 65
            except ValueError:
56
-