blob: bd23fdb6c21735ab3b067fe3aa93f8bdfa870d87 [file] [log] [blame]
Matteo Scandolo292cc2a2017-07-31 19:02:12 -07001from base import xproto_string_type, unquote
2
3def xproto_type_to_ui_type(f):
4 try:
5 content_type = f['options']['content_type']
6 content_type = eval(content_type)
7 except:
8 content_type = None
9 pass
10
11 if 'choices' in f['options']:
12 return 'select';
13 elif content_type == 'date':
14 return 'date'
15 elif f['type'] == 'bool':
16 return 'boolean'
17 elif f['type'] == 'string':
18 return xproto_string_type(f['options'])
19 elif f['type'] in ['int','uint32','int32'] or 'link' in f:
20 return 'number'
21 elif f['type'] in ['double','float']:
22 return 'string'
23
24def xproto_options_choices_to_dict(choices):
25 list = []
26
27 for c in eval(choices):
28 list.append({'id': c[0], 'label': c[1]})
29 if len(list) > 0:
30 return list
31 else:
32 return None
33
34def xproto_validators(f):
35 # To be cleaned up when we formalize validation in xproto
36 validators = []
37
38 # bound-based validators
39 bound_validators = [('max_length','maxlength'), ('min', 'min'), ('max', 'max')]
40
41 for v0, v1 in bound_validators:
42 try:
43 validators.append({'name':v1, 'int_value':int(f['options'][v0])})
44 except KeyError:
45 pass
46
47 # validators based on content_type
48 content_type_validators = ['ip', 'url', 'email']
49
50 for v in content_type_validators:
51 #if f['name']=='ip': pdb.set_trace()
52 try:
53 val = unquote(f['options']['content_type'])==v
54 if not val:
55 raise KeyError
56
57 validators.append({'name':v, 'bool_value': True})
58 except KeyError:
59 pass
60
61 # required validator
62 try:
63 required = f['options']['blank']=='False' and f['options']['null']=='False'
64 if required:
65 validators.append({'name':'required', 'bool_value':required})
66 except KeyError:
67 pass
68
69 return validators
70
71def is_number(s):
72 try:
73 float(s)
74 return True
75 except ValueError:
76 return False
77
78def xproto_default_to_gui(default):
79 val = "null"
80 if is_number(default):
81 val = str(default)
82 elif eval(default) == True:
83 val = 'true'
84 elif eval(default) == False:
85 val = 'false'
86 elif eval(default) == None:
87 val = 'null'
88 else:
89 val = str(default)
90 return val