Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/galaxy/util/form_builder.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 """ | |
2 Classes for generating HTML forms | |
3 """ | |
4 from __future__ import absolute_import | |
5 from __future__ import print_function | |
6 | |
7 import logging | |
8 | |
9 from galaxy.util import ( | |
10 asbool | |
11 ) | |
12 | |
13 log = logging.getLogger(__name__) | |
14 | |
15 | |
16 class BaseField(object): | |
17 def __init__(self, name, value=None, label=None, **kwds): | |
18 self.name = name | |
19 self.label = label | |
20 self.value = value | |
21 self.disabled = kwds.get('disabled', False) | |
22 if 'optional' in kwds: | |
23 self.optional = asbool(kwds.get('optional')) | |
24 else: | |
25 self.optional = kwds.get('required', 'optional') == 'optional' | |
26 self.help = kwds.get('helptext') | |
27 | |
28 def to_dict(self): | |
29 return { | |
30 'name' : self.name, | |
31 'label' : self.label, | |
32 'disabled' : self.disabled, | |
33 'optional' : self.optional, | |
34 'value' : self.value, | |
35 'help' : self.help | |
36 } | |
37 | |
38 | |
39 class TextField(BaseField): | |
40 """ | |
41 A standard text input box. | |
42 """ | |
43 def to_dict(self): | |
44 d = super(TextField, self).to_dict() | |
45 d['type'] = 'text' | |
46 return d | |
47 | |
48 | |
49 class PasswordField(BaseField): | |
50 """ | |
51 A password input box. text appears as "******" | |
52 """ | |
53 def to_dict(self): | |
54 d = super(PasswordField, self).to_dict() | |
55 d['type'] = 'password' | |
56 return d | |
57 | |
58 | |
59 class TextArea(BaseField): | |
60 """ | |
61 A standard text area box. | |
62 """ | |
63 def to_dict(self): | |
64 d = super(TextArea, self).to_dict() | |
65 d['type'] = 'text' | |
66 d['area'] = True | |
67 return d | |
68 | |
69 | |
70 class CheckboxField(BaseField): | |
71 """ | |
72 A checkbox (boolean input) | |
73 """ | |
74 @staticmethod | |
75 def is_checked(value): | |
76 if value in [True, "True", "true"]: | |
77 return True | |
78 return False | |
79 | |
80 def to_dict(self): | |
81 d = super(CheckboxField, self).to_dict() | |
82 d['type'] = 'boolean' | |
83 return d | |
84 | |
85 | |
86 class SelectField(BaseField): | |
87 """ | |
88 A select field. | |
89 """ | |
90 | |
91 def __init__(self, name, multiple=None, display=None, field_id=None, value=None, selectlist=None, refresh_on_change=False, **kwds): | |
92 super(SelectField, self).__init__(name, value, **kwds) | |
93 self.field_id = field_id | |
94 self.multiple = multiple or False | |
95 self.refresh_on_change = refresh_on_change | |
96 self.selectlist = selectlist or [] | |
97 self.options = list() | |
98 if display == "checkboxes": | |
99 assert multiple, "Checkbox display only supported for multiple select" | |
100 elif display == "radio": | |
101 assert not(multiple), "Radio display only supported for single select" | |
102 elif display is not None: | |
103 raise Exception("Unknown display type: %s" % display) | |
104 self.display = display | |
105 | |
106 def add_option(self, text, value, selected=False): | |
107 self.options.append((text, value, selected)) | |
108 | |
109 def to_dict(self): | |
110 d = super(SelectField, self).to_dict() | |
111 d['type'] = 'select' | |
112 d['display'] = self.display | |
113 d['multiple'] = self.multiple | |
114 d['data'] = [] | |
115 for value in self.selectlist: | |
116 d['data'].append({'label': value, 'value': value}) | |
117 d['options'] = [{'label': t[0], 'value': t[1]} for t in self.options] | |
118 return d | |
119 | |
120 | |
121 class AddressField(BaseField): | |
122 @staticmethod | |
123 def fields(): | |
124 return [("desc", "Short address description", "Required"), | |
125 ("name", "Name", ""), | |
126 ("institution", "Institution", ""), | |
127 ("address", "Address", ""), | |
128 ("city", "City", ""), | |
129 ("state", "State/Province/Region", ""), | |
130 ("postal_code", "Postal Code", ""), | |
131 ("country", "Country", ""), | |
132 ("phone", "Phone", "")] | |
133 | |
134 def __init__(self, name, user=None, value=None, security=None, **kwds): | |
135 super(AddressField, self).__init__(name, value, **kwds) | |
136 self.user = user | |
137 self.security = security | |
138 | |
139 def to_dict(self): | |
140 d = super(AddressField, self).to_dict() | |
141 d['type'] = 'select' | |
142 d['data'] = [] | |
143 if self.user and self.security: | |
144 for a in self.user.addresses: | |
145 if not a.deleted: | |
146 d['data'].append({'label': a.desc, 'value': self.security.encode_id(a.id)}) | |
147 return d | |
148 | |
149 | |
150 class WorkflowField(BaseField): | |
151 def __init__(self, name, user=None, value=None, security=None, **kwds): | |
152 super(WorkflowField, self).__init__(name, value, **kwds) | |
153 self.user = user | |
154 self.value = value | |
155 self.security = security | |
156 | |
157 def to_dict(self): | |
158 d = super(WorkflowField, self).to_dict() | |
159 d['type'] = 'select' | |
160 d['data'] = [] | |
161 if self.user and self.security: | |
162 for a in self.user.stored_workflows: | |
163 if not a.deleted: | |
164 d['data'].append({'label': a.name, 'value': self.security.encode_id(a.id)}) | |
165 return d | |
166 | |
167 | |
168 class WorkflowMappingField(BaseField): | |
169 def __init__(self, name, user=None, value=None, **kwds): | |
170 super(WorkflowMappingField, self).__init__(name, value, **kwds) | |
171 self.user = user | |
172 | |
173 | |
174 class HistoryField(BaseField): | |
175 def __init__(self, name, user=None, value=None, security=None, **kwds): | |
176 super(HistoryField, self).__init__(name, value, **kwds) | |
177 self.user = user | |
178 self.value = value | |
179 self.security = security | |
180 | |
181 def to_dict(self): | |
182 d = super(HistoryField, self).to_dict() | |
183 d['type'] = 'select' | |
184 d['data'] = [{'label': 'New History', 'value': 'new'}] | |
185 if self.user and self.security: | |
186 for a in self.user.histories: | |
187 if not a.deleted: | |
188 d['data'].append({'label': a.name, 'value': self.security.encode_id(a.id)}) | |
189 return d | |
190 | |
191 | |
192 def get_suite(): | |
193 """Get unittest suite for this module""" | |
194 import doctest | |
195 import sys | |
196 return doctest.DocTestSuite(sys.modules[__name__]) |