0002-environment-allow-forcing-JSON-interpreation-of-valu.patch
hobo/environment/migrations/0019_variable_interpret_as_json.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
# Generated by Django 1.11.13 on 2018-12-19 11:16 |
|
3 |
from __future__ import unicode_literals |
|
4 | ||
5 |
from django.db import migrations, models |
|
6 | ||
7 | ||
8 |
class Migration(migrations.Migration): |
|
9 | ||
10 |
dependencies = [ |
|
11 |
('environment', '0018_auto_20161102_1929'), |
|
12 |
] |
|
13 | ||
14 |
operations = [ |
|
15 |
migrations.AddField( |
|
16 |
model_name='variable', |
|
17 |
name='interpret_as_json', |
|
18 |
field=models.BooleanField(default=False, verbose_name='interpret as JSON'), |
|
19 |
), |
|
20 |
] |
hobo/environment/models.py | ||
---|---|---|
28 | 28 |
value = models.TextField(verbose_name=_('value'), |
29 | 29 |
blank=True, |
30 | 30 |
help_text=_('start with [ or { for a JSON document')) |
31 |
interpret_as_json = models.BooleanField(default=False, verbose_name=_('interpret as JSON')) |
|
31 | 32 |
auto = models.BooleanField(default=False) |
32 | 33 |
service_type = models.ForeignKey(ContentType, null=True) |
33 | 34 |
service_pk = models.PositiveIntegerField(null=True) |
... | ... | |
39 | 40 |
return self.label |
40 | 41 |
return self.name |
41 | 42 | |
43 |
@property |
|
44 |
def is_json_content(self): |
|
45 |
return (self.interpret_as_json or |
|
46 |
self.value[0] in '{[' or |
|
47 |
self.value in ('true', 'false')) |
|
48 | ||
42 | 49 |
@property |
43 | 50 |
def json(self): |
44 |
if self.value and (self.value[0] in '{[' or self.value in ('true', 'false')):
|
|
51 |
if self.value and self.is_json_content:
|
|
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 self.is_json_content:
|
|
53 | 60 |
try: |
54 | 61 |
json.loads(self.value) |
55 | 62 |
except ValueError: |
56 |
- |