Skip to content

Commit

Permalink
Move select builder to toolshed
Browse files Browse the repository at this point in the history
  • Loading branch information
guerler committed Dec 1, 2017
1 parent 8d41a79 commit 26e8d89
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
52 changes: 0 additions & 52 deletions lib/galaxy/web/form_builder.py
Expand Up @@ -596,55 +596,3 @@ def get_suite():
import doctest
import sys
return doctest.DocTestSuite(sys.modules[__name__])


# --------- Utility methods -----------------------------
def build_select_field(trans, objs, label_attr, select_field_name, initial_value='none',
selected_value='none', refresh_on_change=False, multiple=False, display=None, size=None):
"""
Build a SelectField given a set of objects. The received params are:
- objs: the set of objects used to populate the option list
- label_attr: the attribute of each obj (e.g., name, email, etc ) whose value is used to populate each option label.
- If the string 'self' is passed as label_attr, each obj in objs is assumed to be a string, so the obj itself is used
- select_field_name: the name of the SelectField
- initial_value: the value of the first option in the SelectField - allows for an option telling the user to select something
- selected_value: the value of the currently selected option
- refresh_on_change: True if the SelectField should perform a refresh_on_change
"""
if initial_value == 'none':
values = [initial_value]
else:
values = []
for obj in objs:
if label_attr == 'self':
# Each obj is a string
values.append(obj)
else:
values.append(trans.security.encode_id(obj.id))
if refresh_on_change:
refresh_on_change_values = values
else:
refresh_on_change_values = []
select_field = SelectField(name=select_field_name,
multiple=multiple,
display=display,
refresh_on_change=refresh_on_change,
refresh_on_change_values=refresh_on_change_values,
size=size)
for obj in objs:
if label_attr == 'self':
# Each obj is a string
if str(selected_value) == str(obj):
select_field.add_option(obj, obj, selected=True)
else:
select_field.add_option(obj, obj)
else:
label = getattr(obj, label_attr)
if str(selected_value) == str(obj.id) or str(selected_value) == trans.security.encode_id(obj.id):
select_field.add_option(label, trans.security.encode_id(obj.id), selected=True)
else:
select_field.add_option(label, trans.security.encode_id(obj.id))
return select_field
52 changes: 51 additions & 1 deletion lib/tool_shed/util/repository_util.py
Expand Up @@ -11,7 +11,6 @@
import tool_shed.util.metadata_util as metadata_util
from galaxy import util
from galaxy import web
from galaxy.web.form_builder import build_select_field
from tool_shed.util import basic_util, common_util, encoding_util, hg_util
from tool_shed.util.web_util import escape

Expand All @@ -20,6 +19,57 @@
VALID_REPOSITORYNAME_RE = re.compile("^[a-z0-9\_]+$")


def build_select_field(trans, objs, label_attr, select_field_name, initial_value='none',
selected_value='none', refresh_on_change=False, multiple=False, display=None, size=None):
"""
Build a SelectField given a set of objects. The received params are:
- objs: the set of objects used to populate the option list
- label_attr: the attribute of each obj (e.g., name, email, etc ) whose value is used to populate each option label.
- If the string 'self' is passed as label_attr, each obj in objs is assumed to be a string, so the obj itself is used
- select_field_name: the name of the SelectField
- initial_value: the value of the first option in the SelectField - allows for an option telling the user to select something
- selected_value: the value of the currently selected option
- refresh_on_change: True if the SelectField should perform a refresh_on_change
"""
if initial_value == 'none':
values = [initial_value]
else:
values = []
for obj in objs:
if label_attr == 'self':
# Each obj is a string
values.append(obj)
else:
values.append(trans.security.encode_id(obj.id))
if refresh_on_change:
refresh_on_change_values = values
else:
refresh_on_change_values = []
select_field = SelectField(name=select_field_name,
multiple=multiple,
display=display,
refresh_on_change=refresh_on_change,
refresh_on_change_values=refresh_on_change_values,
size=size)
for obj in objs:
if label_attr == 'self':
# Each obj is a string
if str(selected_value) == str(obj):
select_field.add_option(obj, obj, selected=True)
else:
select_field.add_option(obj, obj)
else:
label = getattr(obj, label_attr)
if str(selected_value) == str(obj.id) or str(selected_value) == trans.security.encode_id(obj.id):
select_field.add_option(label, trans.security.encode_id(obj.id), selected=True)
else:
select_field.add_option(label, trans.security.encode_id(obj.id))
return select_field


def build_allow_push_select_field(trans, current_push_list, selected_value='none'):
options = []
for user in trans.sa_session.query(trans.model.User):
Expand Down

0 comments on commit 26e8d89

Please sign in to comment.