Skip to content

Commit

Permalink
Merge pull request #777 from batpad/756-custom-other-text
Browse files Browse the repository at this point in the history
Allow dropdown text for unlisted choice to be configurable
  • Loading branch information
consideRatio committed Sep 2, 2023
2 parents 156245d + b0ddaff commit 8cc569c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
12 changes: 10 additions & 2 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,9 @@ def _validate_image_pull_secrets(self, proposal):
selected "Other" as a choice:
- `enabled`: Boolean, whether the free form input should be enabled
- `display_name`: String, label for input field
- `display_name_in_choices`: Optional, display name for the choice
to specify an unlisted choice in the dropdown list of pre-defined
choices. Defaults to "Other...".
- `validation_regex`: Optional, regex that the free form input should match - eg. ^pangeo/.*$
- `validation_message`: Optional, validation message for the regex. Should describe the required
input format in a human-readable way.
Expand Down Expand Up @@ -1605,6 +1608,7 @@ def _validate_image_pull_secrets(self, proposal):
'unlisted_choice': {
'enabled': True,
'display_name': 'Other image',
'display_name_in_choices': 'Enter image manually',
'validation_regex': '^jupyter/.+:.+$',
'validation_message': 'Must be an image matching ^jupyter/<name>:<tag>$',
'kubespawner_override': {'image': '{value}'},
Expand Down Expand Up @@ -3234,15 +3238,19 @@ def _get_initialized_profile_list(self, profile_list: list):
profile['slug'] = slugify(profile['display_name'])

# ensure each option in profile_options has a default choice if
# pre-defined choices are available
# pre-defined choices are available, and initialize an
# unlisted_choice dictionary
for option_config in profile.get('profile_options', {}).values():
if option_config.get('choices') and not any(
c.get('default') for c in option_config['choices'].values()
):
# pre-defined choices were provided without a default choice
default_choice = list(option_config['choices'].keys())[0]
option_config['choices'][default_choice]["default"] = True

unlisted_choice = option_config.setdefault("unlisted_choice", {})
unlisted_choice.setdefault("enabled", False)
if unlisted_choice["enabled"]:
unlisted_choice.setdefault("display_name_in_choices", "Other...")
# ensure there is one default profile
if not any(p.get("default") for p in profile_list):
profile_list[0]["default"] = True
Expand Down
6 changes: 3 additions & 3 deletions kubespawner/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ <h3>{{ profile.display_name }}</h3>
{%- for k, choice in option['choices'].items() %}
<option value="{{ k }}" {% if choice.default %}selected{% endif %}>{{ choice.display_name }}</option>
{%- endfor %}
{%- if option['unlisted_choice'] and option['unlisted_choice']['enabled'] %}
<option value="unlisted-choice">Other...</option>
{%- if option['unlisted_choice']['enabled'] %}
<option value="unlisted-choice">{{ option['unlisted_choice']['display_name_in_choices'] }}</option>
{%- endif %}
</select>
</div>
{%- if option['unlisted_choice'] and option['unlisted_choice']['enabled'] %}
{%- if option['unlisted_choice']['enabled'] %}
<div class="option hidden js-other-input-container">
<label for="profile-option-{{ profile.slug }}--{{ k }}--unlisted-choice">
{{ option['unlisted_choice']['display_name'] }}
Expand Down
7 changes: 6 additions & 1 deletion tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
'profile_options': {
'no-defaults': {
'display_name': 'Some choice without a default set',
'unlisted_choice': {'enabled': False},
'choices': {
'option-1': {
'display_name': 'Option 1',
Expand All @@ -105,10 +106,14 @@
},
'only-unlisted': {
'display_name': 'Some option without any choices set',
'unlisted_choice': {'enabled': True},
'unlisted_choice': {
'enabled': True,
'display_name_in_choices': 'Other...',
},
},
'explicit-defaults': {
'display_name': 'Some choice with a default set',
'unlisted_choice': {'enabled': False},
'choices': {
'option-1': {
'display_name': 'Option 1',
Expand Down

0 comments on commit 8cc569c

Please sign in to comment.