def _get_void_value(survey_field): if survey_field["type"] == "multiplechoice": return [get_choices_from_string(survey_field["choices"])[1][1]] elif survey_field["type"] == "multiselect": return get_choices_from_string(survey_field["choices"])[1][1] elif survey_field["type"] == "integer": return 0 elif survey_field["type"] == "float": return 0.0 else: return survey_field["type"]
def __init__(self, user, *args, **kwargs): self.user = user request_id = kwargs.pop('request_id', None) super(AcceptRequestForm, self).__init__(*args, **kwargs) self.target_request = Request.objects.get(id=request_id) # load user provided fields and add admin field if exist if "spec" in self.target_request.operation.job_template.survey: for survey_definition in self.target_request.operation.job_template.survey[ "spec"]: if survey_definition["type"] == "text": new_field = forms.CharField( label=survey_definition['question_name'], required=survey_definition['required'], widget=forms.TextInput( attrs={'class': 'form-control'})) new_field.group = self._get_field_group( field_name=survey_definition['variable'], enabled_field=self.target_request.operation. enabled_survey_fields) self.fields[survey_definition['variable']] = new_field if survey_definition["type"] == "multiplechoice": new_field = forms. \ ChoiceField(label=survey_definition['question_name'], required=survey_definition['required'], choices=get_choices_from_string(survey_definition["choices"]), error_messages={'required': 'At least you must select one choice'}, widget=forms.Select(attrs={'class': 'form-control'})) new_field.group = self._get_field_group( field_name=survey_definition['variable'], enabled_field=self.target_request.operation. enabled_survey_fields) self.fields[survey_definition['variable']] = new_field
def __init__(self, user, *args, **kwargs): # get arguments from instance self.user = user service_id = kwargs.pop('service_id', None) super(ServiceRequestForm, self).__init__(*args, **kwargs) self.service = Service.objects.get(id=service_id) # get the create operation of this service self.create_operation = Operation.objects.get( service=self.service, type=OperationType.CREATE) # get all field that are not disabled by the admin purged_survey = FormUtils.get_available_fields( job_template_survey=self.create_operation.job_template.survey, operation_survey=self.create_operation.enabled_survey_fields) for survey_filed in purged_survey["spec"]: if survey_filed["type"] == "text": self.fields[survey_filed['variable']] = forms. \ CharField(label=survey_filed['question_name'], required=survey_filed['required'], widget=forms.TextInput(attrs={'class': 'form-control'})) if survey_filed["type"] == "multiplechoice": self.fields[survey_filed['variable']] = forms. \ ChoiceField(label=survey_filed['question_name'], required=survey_filed['required'], choices=get_choices_from_string(survey_filed["choices"]), error_messages={'required': 'At least you must select one choice'}, widget=forms.Select(attrs={'class': 'form-control'}))
def test_get_choices_from_string(self): test_string = "choice1\nchoice2\nchoice3" expected_value = [("choice1", "choice1"), ("choice2", "choice2"), ("choice3", "choice3")] self.assertEquals(expected_value, utils.get_choices_from_string(test_string))
def _get_fields_from_survey(self): fields = {} for survey_filed in self.survey["spec"]: if survey_filed["type"] == "text": fields[survey_filed['variable']] = CharField( label=survey_filed['question_name'], initial=survey_filed['default'], required=False if self.read_only_form else survey_filed['required'], help_text=survey_filed['question_description'], min_length=survey_filed['min'], max_length=survey_filed['max'] ) elif survey_filed["type"] == "textarea": fields[survey_filed['variable']] = CharField( label=survey_filed['question_name'], initial=survey_filed['default'], required=False if self.read_only_form else survey_filed['required'], help_text=survey_filed['question_description'], min_length=survey_filed['min'], max_length=survey_filed['max'] ) elif survey_filed["type"] == "password": fields[survey_filed['variable']] = CharField( label=survey_filed['question_name'], required=False if self.read_only_form else survey_filed['required'], help_text=survey_filed['question_description'], min_length=survey_filed['min'], max_length=survey_filed['max'], ) elif survey_filed["type"] == "multiplechoice": fields[survey_filed['variable']] = ChoiceField( label=survey_filed['question_name'], initial=survey_filed['default'], required=False if self.read_only_form else survey_filed['required'], help_text=survey_filed['question_description'], choices=get_choices_from_string(survey_filed["choices"]), error_messages={'required': 'At least you must select one choice'} ) elif survey_filed["type"] == "multiselect": fields[survey_filed['variable']] = MultipleChoiceField( label=survey_filed['question_name'], initial=survey_filed['default'].split("\n"), required=False if self.read_only_form else survey_filed['required'], help_text=survey_filed['question_description'], choices=get_choices_from_string(survey_filed["choices"]), ) elif survey_filed["type"] == "integer": fields[survey_filed['variable']] = IntegerField( label=survey_filed['question_name'], initial=0 if not survey_filed['default'] else int(survey_filed['default']), required=False if self.read_only_form else survey_filed['required'], help_text=survey_filed['question_description'], min_value=survey_filed['min'], max_value=survey_filed['max'], ) elif survey_filed["type"] == "float": fields[survey_filed['variable']] = FloatField( label=survey_filed['question_name'], initial=0 if not survey_filed['default'] else float(survey_filed['default']), required=False if self.read_only_form else survey_filed['required'], help_text=survey_filed['question_description'], min_value=survey_filed['min'], max_value=survey_filed['max'], ) if self.read_only_form: fields[survey_filed['variable']].default = survey_filed['default'] return fields