Warning
This document is for an in-development version of Galaxy. You can alternatively view this page in the latest release if it exists or view the top of the latest release's documentation.
Source code for galaxy.web.form_builder
"""
Classes for generating HTML forms
"""
from __future__ import print_function
import logging
from galaxy.util import (
asbool
)
log = logging.getLogger(__name__)
[docs]class BaseField(object):
[docs] def __init__(self, name, value=None, label=None, **kwds):
self.name = name
self.label = label
self.value = value
self.disabled = kwds.get('disabled', False)
if 'optional' in kwds:
self.optional = asbool(kwds.get('optional'))
else:
self.optional = kwds.get('required', 'optional') == 'optional'
self.help = kwds.get('helptext')
[docs] def to_dict(self):
return {
'name' : self.name,
'label' : self.label,
'disabled' : self.disabled,
'optional' : self.optional,
'value' : self.value,
'help' : self.help
}
[docs]class TextArea(BaseField):
"""
A standard text area box.
"""
[docs] def to_dict(self):
d = super(TextArea, self).to_dict()
d['type'] = 'text'
d['area'] = True
return d
[docs]class CheckboxField(BaseField):
"""
A checkbox (boolean input)
"""
[docs] @staticmethod
def is_checked(value):
if value in [True, "True", "true"]:
return True
return False
[docs]class SelectField(BaseField):
"""
A select field.
"""
[docs] def __init__(self, name, multiple=None, display=None, field_id=None, value=None, selectlist=None, refresh_on_change=False, **kwds):
super(SelectField, self).__init__(name, value, **kwds)
self.field_id = field_id
self.multiple = multiple or False
self.refresh_on_change = refresh_on_change
self.selectlist = selectlist or []
self.options = list()
if display == "checkboxes":
assert multiple, "Checkbox display only supported for multiple select"
elif display == "radio":
assert not(multiple), "Radio display only supported for single select"
elif display is not None:
raise Exception("Unknown display type: %s" % display)
self.display = display
[docs] def add_option(self, text, value, selected=False):
self.options.append((text, value, selected))
[docs] def to_dict(self):
d = super(SelectField, self).to_dict()
d['type'] = 'select'
d['display'] = self.display
d['multiple'] = self.multiple
d['data'] = []
for value in self.selectlist:
d['data'].append({'label': value, 'value': value})
d['options'] = [{'label': t[0], 'value': t[1]} for t in self.options]
return d
[docs]class AddressField(BaseField):
[docs] @staticmethod
def fields():
return [("desc", "Short address description", "Required"),
("name", "Name", ""),
("institution", "Institution", ""),
("address", "Address", ""),
("city", "City", ""),
("state", "State/Province/Region", ""),
("postal_code", "Postal Code", ""),
("country", "Country", ""),
("phone", "Phone", "")]
[docs] def __init__(self, name, user=None, value=None, security=None, **kwds):
super(AddressField, self).__init__(name, value, **kwds)
self.user = user
self.security = security
[docs] def to_dict(self):
d = super(AddressField, self).to_dict()
d['type'] = 'select'
d['data'] = []
if self.user and self.security:
for a in self.user.addresses:
if not a.deleted:
d['data'].append({'label': a.desc, 'value': self.security.encode_id(a.id)})
return d
[docs]class WorkflowField(BaseField):
[docs] def __init__(self, name, user=None, value=None, security=None, **kwds):
super(WorkflowField, self).__init__(name, value, **kwds)
self.user = user
self.value = value
self.security = security
[docs] def to_dict(self):
d = super(WorkflowField, self).to_dict()
d['type'] = 'select'
d['data'] = []
if self.user and self.security:
for a in self.user.stored_workflows:
if not a.deleted:
d['data'].append({'label': a.name, 'value': self.security.encode_id(a.id)})
return d
[docs]class WorkflowMappingField(BaseField):
[docs] def __init__(self, name, user=None, value=None, **kwds):
super(WorkflowMappingField, self).__init__(name, value, **kwds)
self.user = user
[docs]class HistoryField(BaseField):
[docs] def __init__(self, name, user=None, value=None, security=None, **kwds):
super(HistoryField, self).__init__(name, value, **kwds)
self.user = user
self.value = value
self.security = security
[docs] def to_dict(self):
d = super(HistoryField, self).to_dict()
d['type'] = 'select'
d['data'] = [{'label': 'New History', 'value': 'new'}]
if self.user and self.security:
for a in self.user.histories:
if not a.deleted:
d['data'].append({'label': a.name, 'value': self.security.encode_id(a.id)})
return d
[docs]def get_suite():
"""Get unittest suite for this module"""
import doctest
import sys
return doctest.DocTestSuite(sys.modules[__name__])