def get_form_field_instances(self, request=None, form_entry=None, form_element_entries=None, **kwargs): """Get form field instances.""" choices = get_select_field_choices(self.data.choices) field_kwargs = { 'label': self.data.label, 'help_text': self.data.help_text, 'initial': self.data.initial, 'required': self.data.required, 'choices': choices, 'widget': SelectMultiple(attrs={'class': theme.form_element_html_class}), } if self.data.max_choices is not None: field_kwargs['max_choices'] = self.data.max_choices return [(self.data.name, MultipleChoiceWithMaxField, field_kwargs)]
def submit_plugin_form_data(self, form_entry, request, form, form_element_entries=None, **kwargs): """Submit plugin form data/process. :param fobi.models.FormEntry form_entry: Instance of ``fobi.models.FormEntry``. :param django.http.HttpRequest request: :param django.forms.Form form: """ # Get the object value = form.cleaned_data.get(self.data.name, None) if value: choices = dict(get_select_field_choices(self.data.choices)) # Handle the submitted form value value = '{0}'.format(safe_text(choices.get(value))) # Overwrite ``cleaned_data`` of the ``form`` with object qualifier. form.cleaned_data[self.data.name] = value # It's critically important to return the ``form`` with updated # ``cleaned_data`` return form
def submit_plugin_form_data(self, form_entry, request, form, form_element_entries=None, **kwargs): """ Submit plugin form data/process. :param fobi.models.FormEntry form_entry: Instance of ``fobi.models.FormEntry``. :param django.http.HttpRequest request: :param django.forms.Form form: """ # In case if we should submit value as is, we don't return anything. # In other cases, we proceed further. if SUBMIT_VALUE_AS != SUBMIT_VALUE_AS_VAL: # Get the object values = form.cleaned_data.get(self.data.name, None) # Get choices choices = dict(get_select_field_choices(self.data.choices)) # Returned value ret_values = [] for value in values: # Handle the submitted form value if value in choices: label = safe_text(choices.get(value)) # Should be returned as repr if SUBMIT_VALUE_AS == SUBMIT_VALUE_AS_REPR: value = label # Should be returned as mix else: value = "{0} ({1})".format(label, value) ret_values.append(value) # Overwrite ``cleaned_data`` of the ``form`` with object # qualifier. if ret_values: form.cleaned_data[self.data.name] = ret_values # It's critically important to return the ``form`` with updated # ``cleaned_data`` return form
def clean(self): """Validating the values.""" super(SliderInputForm, self).clean() max_value = self.cleaned_data['max_value'] min_value = self.cleaned_data['min_value'] initial = self.cleaned_data['initial'] step = self.cleaned_data['step'] show_endpoints_as = self.cleaned_data['show_endpoints_as'] handle = self.cleaned_data['handle'] custom_ticks = self.cleaned_data['custom_ticks'] if max_value < min_value: self.add_error('max_value', _("`max_value` should be > than `min_value`.")) if step > max_value - min_value: self.add_error( 'step', _("`step` should be > than `max_value` - `min_value`.")) if max_value < initial: self.add_error('initial', _("`max_value` should be >= than `initial`.")) if min_value > initial: self.add_error('min_value', _("`initial` should be >= than `min_value`.")) label_handles = (SLIDER_HANDLE_TRIANGLE, SLIDER_HANDLE_CUSTOM) tick_endpoints = (SLIDER_SHOW_ENDPOINTS_AS_LABELED_TICKS, SLIDER_SHOW_ENDPOINTS_AS_TICKS) if handle in label_handles and show_endpoints_as in tick_endpoints: self.add_error( 'handle', _("You are not allowed to use Triangle or Custom handles " "with ticks enabled.")) if custom_ticks: ticks = get_select_field_choices(custom_ticks, key_type=int, value_type=str, fail_silently=False) if ticks is None: self.add_error( 'custom_ticks', _("Invalid format. First value should be an integer, " "second value should be a string."))
def get_form_field_instances(self): """ Get form field instances. """ choices = get_select_field_choices(self.data.choices) kwargs = { 'label': self.data.label, 'help_text': self.data.help_text, 'initial': self.data.initial, 'required': self.data.required, 'choices': choices, 'widget': CheckboxSelectMultiple(attrs={'class': theme.form_element_html_class}), } return [(self.data.name, MultipleChoiceField, kwargs)]
def get_form_field_instances(self, request=None, form_entry=None, form_element_entries=None, **kwargs): """Get form field instances.""" choices = get_select_field_choices(self.data.choices) widget_attrs = {'class': theme.form_radio_element_html_class} field_kwargs = { 'label': self.data.label, 'help_text': self.data.help_text, 'initial': self.data.initial, 'required': self.data.required, 'choices': choices, 'widget': RadioSelect(attrs=widget_attrs), } return [(self.data.name, ChoiceField, field_kwargs)]
def get_form_field_instances(self): """ Get form field instances. """ choices = get_select_field_choices(self.data.choices) kwargs = { 'label': self.data.label, 'help_text': self.data.help_text, 'initial': self.data.initial, 'required': self.data.required, 'choices': choices, 'widget': Select(attrs={'class': theme.form_element_html_class}), } return [(self.data.name, ChoiceField, kwargs)]
def submit_plugin_form_data(self, form_entry, request, form, form_element_entries=None, **kwargs): """Submit plugin form data/process. :param fobi.models.FormEntry form_entry: Instance of ``fobi.models.FormEntry``. :param django.http.HttpRequest request: :param django.forms.Form form: """ # In case if we should submit value as is, we don't return anything. # In other cases, we proceed further. if SUBMIT_VALUE_AS != SUBMIT_VALUE_AS_VAL: # Get the object values = form.cleaned_data.get(self.data.name, None) # Get choices choices = dict(get_select_field_choices(self.data.choices)) # Returned value ret_values = [] for value in values: # Handle the submitted form value if value in choices: label = safe_text(choices.get(value)) # Should be returned as repr if SUBMIT_VALUE_AS == SUBMIT_VALUE_AS_REPR: value = label # Should be returned as mix else: value = "{0} ({1})".format(label, value) ret_values.append(value) # Overwrite ``cleaned_data`` of the ``form`` with object # qualifier. form.cleaned_data[self.data.name] = ret_values # It's critically important to return the ``form`` with updated # ``cleaned_data`` return form
def get_form_field_instances(self, request=None): """ Get form field instances. """ choices = get_select_field_choices(self.data.choices) kwargs = { 'label': self.data.label, 'help_text': self.data.help_text, 'initial': self.data.initial, 'required': self.data.required, 'choices': choices, 'widget': SelectMultiple(attrs={'class': theme.form_element_html_class}), } if self.data.max_choices: kwargs['max_choices'] = self.data.max_choices return [(self.data.name, MultipleChoiceWithMaxField, kwargs)]
def get_choices(self): """Get choices. Might be used in integration plugins. """ return get_select_field_choices(self.data.choices)
def get_form_field_instances(self, request=None, form_entry=None, form_element_entries=None, **kwargs): """Get form field instances.""" initial = self.get_initial() max_value = int(self.data.max_value) \ if self.data.max_value is not None \ else INITIAL_MAX_VALUE min_value = int(self.data.min_value) \ if self.data.min_value is not None \ else INITIAL_MIN_VALUE step = int(self.data.step) if self.data.step is not None else STEP tooltip = self.data.tooltip \ if self.data.tooltip is not None \ else SLIDER_DEFAULT_TOOLTIP handle = self.data.handle \ if self.data.handle is not None \ else SLIDER_DEFAULT_HANDLE custom_ticks = get_select_field_choices(self.data.custom_ticks, key_type=int, value_type=text_type) \ if self.data.custom_ticks \ else [] choices = self.get_choices() # slider_html_class = "slider-no-background" \ # if self.data.disable_slider_background \ # else "slider" slider_html_class = "slider" widget_attrs = { 'class': "{0} {1}".format(slider_html_class, theme.form_element_html_class), 'data-slider-min': min_value, 'data-slider-max': max_value, 'data-slider-step': step, 'data-slider-value': initial, 'data-slider-tooltip': tooltip, 'data-slider-handle': handle, } show_endpoints_as = self.data.show_endpoints_as \ if self.data.show_endpoints_as \ else SLIDER_DEFAULT_SHOW_ENDPOINTS_AS prepend_html_list = [] append_html_list = [] # Show endpoints as labeled ticks if SLIDER_SHOW_ENDPOINTS_AS_LABELED_TICKS == show_endpoints_as: if custom_ticks: ticks_data = generate_ticks(custom_ticks) else: ticks_data = generate_ticks([ (min_value, self.data.label_start), (max_value, self.data.label_end), ]) # label_start = self.data.label_start \ # if self.data.label_start \ # else text_type(min_value) # # label_end = self.data.label_end \ # if self.data.label_end \ # else text_type(max_value) # widget_attrs.update({ # 'data-slider-ticks': "[{0}, {1}]".format( # min_value, max_value # ), # 'data-slider-ticks-labels': '["{0!s}", "{1!s}"]'.format( # label_start.encode('utf8'), label_end.encode('utf8') # ), # }) widget_attrs.update(ticks_data) # Show endpoints as ticks elif SLIDER_SHOW_ENDPOINTS_AS_TICKS == show_endpoints_as: if custom_ticks: ticks_data = generate_ticks(custom_ticks, empty_labels=True) else: ticks_data = generate_ticks([ (min_value, ""), (max_value, ""), ]) # widget_attrs.update({ # 'data-slider-ticks': "[{0}, {1}]".format( # min_value, max_value # ), # 'data-slider-ticks-labels': '["{0}", "{1}"]'.format( # "", "" # ), # }) widget_attrs.update(ticks_data) # Show endpoints as labels else: if self.data.label_start: prepend_html_list.append( format_html( " <span {}>", flatatt({'class': "slider-endpoint-label-start"}))) prepend_html_list.append(format_html(self.data.label_start)) prepend_html_list.append(format_html(" </span>")) if self.data.label_end: append_html_list.append( format_html( " <span {}>", flatatt({'class': "slider-endpoint-label-end"}))) append_html_list.append(format_html(self.data.label_end)) append_html_list.append(format_html(" </span>")) widget_kwargs = {'attrs': widget_attrs} # For showing endpoints as labels if prepend_html_list: widget_kwargs.update({ 'prepend_html': mark_safe(''.join(prepend_html_list)), }) if append_html_list: widget_kwargs.update({ 'append_html': mark_safe(''.join(append_html_list)), }) field_kwargs = { 'label': self.data.label, 'help_text': self.data.help_text, 'initial': initial, 'required': self.data.required, 'choices': choices, 'widget': RichSelectInverseQuotes(**widget_kwargs), } return [(self.data.name, ChoiceField, field_kwargs)]
def clean(self): """Validating the values.""" super(SliderInputForm, self).clean() max_value = self.cleaned_data['max_value'] min_value = self.cleaned_data['min_value'] initial = self.cleaned_data['initial'] step = self.cleaned_data['step'] show_endpoints_as = self.cleaned_data['show_endpoints_as'] handle = self.cleaned_data['handle'] custom_ticks = self.cleaned_data['custom_ticks'] if max_value < min_value: self.add_error( 'max_value', _("`max_value` should be > than `min_value`.") ) if step > max_value - min_value: self.add_error( 'step', _("`step` should be > than `max_value` - `min_value`.") ) if max_value < initial: self.add_error( 'initial', _("`max_value` should be >= than `initial`.") ) if min_value > initial: self.add_error( 'min_value', _("`initial` should be >= than `min_value`.") ) label_handles = (SLIDER_HANDLE_TRIANGLE, SLIDER_HANDLE_CUSTOM) tick_endpoints = ( SLIDER_SHOW_ENDPOINTS_AS_LABELED_TICKS, SLIDER_SHOW_ENDPOINTS_AS_TICKS ) if handle in label_handles and show_endpoints_as in tick_endpoints: self.add_error( 'handle', _("You are not allowed to use Triangle or Custom handles " "with ticks enabled.") ) if custom_ticks: ticks = get_select_field_choices( custom_ticks, key_type=int, value_type=str, fail_silently=False ) if ticks is None: self.add_error( 'custom_ticks', _("Invalid format. First value should be an integer, " "second value should be a string.") )
def get_form_field_instances(self, request=None, form_entry=None, form_element_entries=None, **kwargs): """Get form field instances.""" initial = self.get_initial() max_value = int(self.data.max_value) \ if self.data.max_value is not None \ else INITIAL_MAX_VALUE min_value = int(self.data.min_value) \ if self.data.min_value is not None \ else INITIAL_MIN_VALUE step = int(self.data.step) if self.data.step is not None else STEP tooltip = self.data.tooltip \ if self.data.tooltip is not None \ else SLIDER_DEFAULT_TOOLTIP handle = self.data.handle \ if self.data.handle is not None \ else SLIDER_DEFAULT_HANDLE custom_ticks = get_select_field_choices(self.data.custom_ticks, key_type=int, value_type=text_type) \ if self.data.custom_ticks \ else [] choices = self.get_choices() # slider_html_class = "slider-no-background" \ # if self.data.disable_slider_background \ # else "slider" slider_html_class = "slider" widget_attrs = { 'class': "{0} {1}".format( slider_html_class, theme.form_element_html_class ), 'data-slider-min': min_value, 'data-slider-max': max_value, 'data-slider-step': step, 'data-slider-value': initial, 'data-slider-tooltip': tooltip, 'data-slider-handle': handle, } show_endpoints_as = self.data.show_endpoints_as \ if self.data.show_endpoints_as \ else SLIDER_DEFAULT_SHOW_ENDPOINTS_AS prepend_html_list = [] append_html_list = [] # Show endpoints as labeled ticks if SLIDER_SHOW_ENDPOINTS_AS_LABELED_TICKS == show_endpoints_as: if custom_ticks: ticks_data = generate_ticks(custom_ticks) else: ticks_data = generate_ticks([ (min_value, self.data.label_start), (max_value, self.data.label_end), ]) # label_start = self.data.label_start \ # if self.data.label_start \ # else text_type(min_value) # # label_end = self.data.label_end \ # if self.data.label_end \ # else text_type(max_value) # widget_attrs.update({ # 'data-slider-ticks': "[{0}, {1}]".format( # min_value, max_value # ), # 'data-slider-ticks-labels': '["{0!s}", "{1!s}"]'.format( # label_start.encode('utf8'), label_end.encode('utf8') # ), # }) widget_attrs.update(ticks_data) # Show endpoints as ticks elif SLIDER_SHOW_ENDPOINTS_AS_TICKS == show_endpoints_as: if custom_ticks: ticks_data = generate_ticks(custom_ticks, empty_labels=True) else: ticks_data = generate_ticks([ (min_value, ""), (max_value, ""), ]) # widget_attrs.update({ # 'data-slider-ticks': "[{0}, {1}]".format( # min_value, max_value # ), # 'data-slider-ticks-labels': '["{0}", "{1}"]'.format( # "", "" # ), # }) widget_attrs.update(ticks_data) # Show endpoints as labels else: if self.data.label_start: prepend_html_list.append( format_html( " <span {}>", flatatt({'class': "slider-endpoint-label-start"}) ) ) prepend_html_list.append(format_html(self.data.label_start)) prepend_html_list.append(format_html(" </span>")) if self.data.label_end: append_html_list.append( format_html( " <span {}>", flatatt({'class': "slider-endpoint-label-end"}) ) ) append_html_list.append(format_html(self.data.label_end)) append_html_list.append(format_html(" </span>")) widget_kwargs = {'attrs': widget_attrs} # For showing endpoints as labels if prepend_html_list: widget_kwargs.update({ 'prepend_html': mark_safe(''.join(prepend_html_list)), }) if append_html_list: widget_kwargs.update({ 'append_html': mark_safe(''.join(append_html_list)), }) field_kwargs = { 'label': self.data.label, 'help_text': self.data.help_text, 'initial': initial, 'required': self.data.required, 'choices': choices, 'widget': RichSelectInverseQuotes(**widget_kwargs), } return [(self.data.name, ChoiceField, field_kwargs)]