Jump to >

djblets.forms.widgets

Custom widgets for Django form fields.

This module contains widgets that correspond to fields provided in djblets.forms.fields.

class ConditionsWidget(choices, mode_widget, choice_widget, operator_widget, choice_kwargs=None, attrs=None)[source]

Bases: django.forms.widgets.Widget

A widget used to request a list of conditions from the user.

This is used by ConditionsField() to allow the user to specify a list of possible conditions, composed of choices, operators, and values.

The conditions are displayed as rows of fields. Each row contains a <select> for the condition choice, operator choice, and a choice-provided field for a value.

Additional conditions can be added by the user dynamically.

choices

djblets.conditions.choices.ConditionChoices – The condition choices for the field.

mode_widget

django.forms.widgets.RadioSelect – The widget for selecting the mode.

choice_widget

django.forms.widgets.Select – The widget for selecting choices. One of these will be rendered for every row.

operator_widget

django.forms.widgets.Select – The widget for selecting operators. One of these will be rendered for every row.

choice_kwargs

dict – Optional keyword arguments to pass to each BaseConditionChoice constructor. This is useful for more advanced conditions that need additional data from the form.

This can be updated dynamically by the form during initialization.

template_name = u'djblets_forms/conditions_widget.html'[source]

The name of the template used to render the widget.

__init__(choices, mode_widget, choice_widget, operator_widget, choice_kwargs=None, attrs=None)[source]

Initialize the widget.

Parameters:
  • choices (djblets.conditions.choices.ConditionChoices) – The condition choices for the field.
  • mode_widget (django.forms.widgets.RadioSelect) – The widget for selecting the mode.
  • choice_widget (django.forms.widgets.Select) – The widget for selecting choices. One of these will be rendered for every row.
  • operator_widget (django.forms.widgets.Select) – The widget for selecting operators. One of these will be rendered for every row.
  • choice_kwargs (dict) – Optional keyword arguments to pass to each BaseConditionChoice constructor. This is useful for more advanced conditions that need additional data from the form.
  • attrs (dict, optional) – Additional HTML element attributes for the widget.
value_from_datadict(data, files, name)[source]

Return a value for the field from a submitted form.

This serializes the data POSTed for the form into a format that the field can use and validate.

Parameters:
  • data (django.http.request.QueryDict) – The dictionary containing form data.
  • files (django.http.request.QueryDict) – The dictionary containing uploaded files.
  • name (unicode) – The field name for the value to load.
Returns:

The value from the form data.

Return type:

object

render(name, value, attrs=None)[source]

Render the widget to HTML.

This will serialize all the choices, operators, and existing conditions and render an HTML representation, along with setting up JavaScript support for configuring the conditions.

Parameters:
  • name (unicode) – The base form field name of the widget.
  • value (dict) – The serialized condition set for the field.
  • attrs (dict, optional) – Additional HTML element attributes for the fields.
Returns:

The rendered HTML for the widget.

Return type:

django.utils.safestring.SafeText

get_context(name, value, attrs)[source]

Return context for the widget.

This will serialize all the choices, operators, and existing conditions needed to render the widget.

Parameters:
  • name (unicode) – The base form field name of the widget.
  • value (dict) – The serialized condition set for the field.
  • attrs (dict) – Additional HTML element attributes for the fields.
Returns:

The context data for the widget.

Return type:

dict

__deepcopy__(memo)[source]

Return a deep copy of the widget.

This will return a deep copy of this widget and all subwidgets, so that a particular form instance can easily manipulate state without affecting other instances.

Only the state that may be manipulated by an instance will be deep copied.

Parameters:memo (dict) – The memo dictionary used to track IDs to objects.
Returns:A deep copy of this widget’s instance.
Return type:ConditionsWidget
class CopyableTextInput(attrs=None)[source]

Bases: django.forms.widgets.TextInput

A TextInput widget that renders a link to copy its contents.

template_name = u'djblets_forms/copyable_text_input.html'[source]
render(name, value, attrs=None)[source]

Render the widget.

Parameters:
  • name (unicode) – The name of the widget.
  • value (unicode) – The value of the widget.
  • attrs (dict) – The attributes of the widget.
Returns:

The rendered widget.

Return type:

django.utils.safestring.SafeText

class ListEditWidget(attrs=None, sep=u', ')[source]

Bases: django.forms.widgets.Widget

A widget for editing a list of separated values.

template_name = u'djblets_forms/list_edit_widget.html'[source]
__init__(attrs=None, sep=u', ')[source]

Initialize the widget.

Parameters:
  • attrs (dict, optional) – The attributes of the <input> elements.
  • sep (unicode, optional) – The item separator.
render(name, value, attrs=None)[source]

Render the widget.

Parameters:
  • name (unicode) – The field name.
  • value (unicode) – The field value.
  • attrs (dict, optional) – Additional attributes.
Returns:

The rendered widget.

Return type:

django.utils.safestring.SafeText

id_for_label(id_)[source]

Return the main ID to use for this widget.

This intentionally returns None since there are multiple fields under this widget.

Parameters:id (unicode) – The ID of the element.
Returns:None so that no for= attribute is rendered on the label.
Return type:unicode