Skip to content

Commit 42b4046

Browse files
committed
[POC] allow surveys to have boolean questions
This is just a proof of concept for boolean survey questions. Refs ansible#553 Refs ansible#1782 Refs ansible#1053 Signed-off-by: Rick Elrod <rick@elrod.me>
1 parent 974465e commit 42b4046

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

awx/api/templates/api/job_template_survey_spec.md

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Within each survey item `type` must be one of:
2828
* float: For survey questions expecting a decimal number
2929
* multiplechoice: For survey questions where one option from a list is required
3030
* multiselect: For survey questions where multiple items from a presented list can be selected
31+
* bool: For survey questions where the value must be a `true` or `false` literal
3132

3233
Each item must contain a `question_name` and `question_description` field that describes the survey question itself.
3334
The `variable` elements of each survey items represents the key that will be given to the playbook when the {{model_verbose_name}}
@@ -72,6 +73,17 @@ Here is a more comprehensive example showing the various question types and thei
7273
"required": true,
7374
"default": "NOT OPTIONAL"
7475
},
76+
{
77+
"type": "bool",
78+
"question_name": "This is true or false",
79+
"question_description": "I am a boolean",
80+
"variable": "bool_answer",
81+
"choices": "",
82+
"min": "",
83+
"max": "",
84+
"required": false,
85+
"default": false
86+
},
7587
{
7688
"type": "multiplechoice",
7789
"question_name": "achoice",

awx/main/constants.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,16 @@
9696
CONTAINER_VOLUMES_MOUNT_TYPES = ['z', 'O', 'ro', 'rw']
9797
MAX_ISOLATED_PATH_COLON_DELIMITER = 2
9898

99-
SURVEY_TYPE_MAPPING = {'text': str, 'textarea': str, 'password': str, 'multiplechoice': str, 'multiselect': str, 'integer': int, 'float': (float, int)}
99+
SURVEY_TYPE_MAPPING = {
100+
'text': str,
101+
'textarea': str,
102+
'password': str,
103+
'multiplechoice': str,
104+
'multiselect': str,
105+
'integer': int,
106+
'float': (float, int),
107+
'bool': bool,
108+
}
100109

101110
JOB_VARIABLE_PREFIXES = [
102111
'awx',

awx/main/models/mixins.py

+5
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ def _survey_element_validation(self, survey_element, data, validate_required=Tru
293293
if survey_element['variable'] in data:
294294
if data[survey_element['variable']] not in choice_list:
295295
errors.append("Value %s for '%s' expected to be one of %s." % (data[survey_element['variable']], survey_element['variable'], choice_list))
296+
elif survey_element['type'] == 'bool':
297+
if survey_element['variable'] in data:
298+
if type(data[survey_element['variable']]) != bool:
299+
errors.append("Value %s for '%s' expected to be a boolean." % (data[survey_element['variable']], survey_element['variable']))
300+
return errors
296301
return errors
297302

298303
def _accept_or_ignore_variables(self, data, errors=None, _exclude_errors=(), extra_passwords=None):

awx/ui/src/components/LaunchPrompt/steps/SurveyStep.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
combine,
1818
} from 'util/validators';
1919
import { Survey } from 'types';
20-
import FormField from '../../FormField';
20+
import FormField, { CheckboxField } from '../../FormField';
2121
import Popover from '../../Popover';
2222

2323
function SurveyStep({ surveyConfig }) {
@@ -29,6 +29,7 @@ function SurveyStep({ surveyConfig }) {
2929
multiselect: MultiSelectField,
3030
integer: NumberField,
3131
float: NumberField,
32+
bool: SurveyCheckboxField,
3233
};
3334
return (
3435
<div data-cy="survey-prompts">
@@ -70,6 +71,20 @@ function TextField({ question }) {
7071
);
7172
}
7273

74+
function SurveyCheckboxField({ question }) {
75+
const validators = [question.required ? required(null) : null];
76+
return (
77+
<CheckboxField
78+
id={`survey-question-${question.variable}`}
79+
name={`survey_${question.variable}`}
80+
label={question.question_name}
81+
tooltip={question.question_description}
82+
isRequired={question.required}
83+
validate={combine(validators)}
84+
/>
85+
);
86+
}
87+
7388
function NumberField({ question }) {
7489
const validators = [
7590
question.required ? required(null) : null,

awx/ui/src/screens/Template/Survey/SurveyQuestionForm.js

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function AnswerTypeField() {
9292
},
9393
{ key: 'integer', value: 'integer', label: t`Integer` },
9494
{ key: 'float', value: 'float', label: t`Float` },
95+
{ key: 'bool', value: 'bool', label: t`Boolean` },
9596
]}
9697
/>
9798
</FormGroup>

0 commit comments

Comments
 (0)