"""
Classes for generating HTML forms
"""
import logging
from galaxy.util import asbool
log = logging.getLogger(__name__)
[docs]class BaseField:
[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 TextField(BaseField):
"""
A standard text input box.
"""
[docs] def to_dict(self):
d = super().to_dict()
d["type"] = "text"
return d
[docs]class PasswordField(BaseField):
"""
A password input box. text appears as "******"
"""
[docs] def to_dict(self):
d = super().to_dict()
d["type"] = "password"
return d
[docs]class TextArea(BaseField):
"""
A standard text area box.
"""
[docs] def to_dict(self):
d = super().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] def to_dict(self):
d = super().to_dict()
d["type"] = "boolean"
return d
[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().__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 = []
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(f"Unknown display type: {display}")
self.display = display
[docs] def add_option(self, label, value, selected=False):
self.options.append((str(label), value, selected))
[docs] def to_dict(self):
d = super().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().__init__(name, value, **kwds)
self.user = user
self.security = security
[docs] def to_dict(self):
d = super().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().__init__(name, value, **kwds)
self.user = user
self.value = value
self.security = security
[docs] def to_dict(self):
d = super().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().__init__(name, value, **kwds)
self.user = user
[docs]class HistoryField(BaseField):
[docs] def __init__(self, name, user=None, value=None, security=None, **kwds):
super().__init__(name, value, **kwds)
self.user = user
self.value = value
self.security = security
[docs] def to_dict(self):
d = super().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