Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dropdown text for unlisted choice to be configurable #777

Merged
merged 12 commits into from
Sep 2, 2023
Merged
7 changes: 6 additions & 1 deletion kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,7 @@ 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
- `other_text`: Optional, text to show in Select Dropdown for Other option
consideRatio marked this conversation as resolved.
Show resolved Hide resolved
- `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 +1606,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 @@ -3242,7 +3244,10 @@ def _get_initialized_profile_list(self, profile_list: list):
# 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