Skip to content

Commit

Permalink
[POC] allow surveys to have boolean questions
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
relrod committed Nov 4, 2022
1 parent 83c48bb commit c73d0a0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
12 changes: 12 additions & 0 deletions awx/api/templates/api/job_template_survey_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Within each survey item `type` must be one of:
* float: For survey questions expecting a decimal number
* multiplechoice: For survey questions where one option from a list is required
* multiselect: For survey questions where multiple items from a presented list can be selected
* bool: For survey questions where the value must be a `true` or `false` literal

Each item must contain a `question_name` and `question_description` field that describes the survey question itself.
The `variable` elements of each survey items represents the key that will be given to the playbook when the {{model_verbose_name}}
Expand Down Expand Up @@ -72,6 +73,17 @@ Here is a more comprehensive example showing the various question types and thei
"required": true,
"default": "NOT OPTIONAL"
},
{
"type": "bool",
"question_name": "This is true or false",
"question_description": "I am a boolean",
"variable": "bool_answer",
"choices": "",
"min": "",
"max": "",
"required": false,
"default": false
},
{
"type": "multiplechoice",
"question_name": "achoice",
Expand Down
11 changes: 10 additions & 1 deletion awx/main/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@
CONTAINER_VOLUMES_MOUNT_TYPES = ['z', 'O', 'ro', 'rw']
MAX_ISOLATED_PATH_COLON_DELIMITER = 2

SURVEY_TYPE_MAPPING = {'text': str, 'textarea': str, 'password': str, 'multiplechoice': str, 'multiselect': str, 'integer': int, 'float': (float, int)}
SURVEY_TYPE_MAPPING = {
'text': str,
'textarea': str,
'password': str,
'multiplechoice': str,
'multiselect': str,
'integer': int,
'float': (float, int),
'bool': bool,
}

JOB_VARIABLE_PREFIXES = [
'awx',
Expand Down
5 changes: 5 additions & 0 deletions awx/main/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ def _survey_element_validation(self, survey_element, data, validate_required=Tru
if survey_element['variable'] in data:
if data[survey_element['variable']] not in choice_list:
errors.append("Value %s for '%s' expected to be one of %s." % (data[survey_element['variable']], survey_element['variable'], choice_list))
elif survey_element['type'] == 'bool':
if survey_element['variable'] in data:
if type(data[survey_element['variable']]) != bool:
errors.append("Value %s for '%s' expected to be a boolean." % (data[survey_element['variable']], survey_element['variable']))
return errors
return errors

def _accept_or_ignore_variables(self, data, errors=None, _exclude_errors=(), extra_passwords=None):
Expand Down
19 changes: 18 additions & 1 deletion awx/ui/src/components/LaunchPrompt/steps/SurveyStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
combine,
} from 'util/validators';
import { Survey } from 'types';
import FormField from '../../FormField';
import FormField, { CheckboxField } from '../../FormField';
import Popover from '../../Popover';

function SurveyStep({ surveyConfig }) {
Expand All @@ -29,6 +29,7 @@ function SurveyStep({ surveyConfig }) {
multiselect: MultiSelectField,
integer: NumberField,
float: NumberField,
bool: SurveyCheckboxField,
};
return (
<div data-cy="survey-prompts">
Expand Down Expand Up @@ -70,6 +71,22 @@ function TextField({ question }) {
);
}

function SurveyCheckboxField({ question }) {
const validators = [
question.required ? required(null) : null,
];
return (
<CheckboxField
id={`survey-question-${question.variable}`}
name={`survey_${question.variable}`}
label={question.question_name}
tooltip={question.question_description}
isRequired={question.required}
validate={combine(validators)}
/>
);
}

function NumberField({ question }) {
const validators = [
question.required ? required(null) : null,
Expand Down
1 change: 1 addition & 0 deletions awx/ui/src/screens/Template/Survey/SurveyQuestionForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function AnswerTypeField() {
},
{ key: 'integer', value: 'integer', label: t`Integer` },
{ key: 'float', value: 'float', label: t`Float` },
{ key: 'bool', value: 'bool', label: t`Boolean` },
]}
/>
</FormGroup>
Expand Down

0 comments on commit c73d0a0

Please sign in to comment.