Esempio n. 1
0
 def __init__(self, choices, *args, **kwargs):
     super().__init__(choices=choices, *args, **kwargs)
     self.choices = [(label, label)
                     for value, label in unpack_grouped_choices(choices)]
     self.choice_values = {
         label: value
         for value, label in unpack_grouped_choices(choices)
     }
Esempio n. 2
0
def get_selected_values(form, field_name):
    """
    Return the list of selected human-friendly values for a form field
    """
    if not hasattr(form, 'cleaned_data'):
        form.is_valid()
    filter_data = form.cleaned_data.get(field_name)

    # Selection field
    if hasattr(form.fields[field_name], 'choices'):
        try:
            choices = dict(
                unpack_grouped_choices(form.fields[field_name].choices))
            return [
                label for value, label in choices.items()
                if str(value) in filter_data
            ]
        except TypeError:
            # Field uses dynamic choices. Show all that have been populated.
            return [
                subwidget.choice_label
                for subwidget in form[field_name].subwidgets
            ]

    # Non-selection field
    return [str(filter_data)]
Esempio n. 3
0
def get_selected_values(form, field_name):
    """
    Return the list of selected human-friendly values for a form field
    """
    if not hasattr(form, 'cleaned_data'):
        form.is_valid()
    filter_data = form.cleaned_data.get(field_name)
    field = form.fields[field_name]

    # Non-selection field
    if not hasattr(field, 'choices'):
        return [str(filter_data)]

    # Get choice labels
    if type(field.choices) is forms.models.ModelChoiceIterator:
        # Field uses dynamic choices: show all that have been populated on the widget
        values = [
            subwidget.choice_label for subwidget in form[field_name].subwidgets
        ]

    else:
        # Static selection field
        choices = unpack_grouped_choices(field.choices)
        values = [
            label for value, label in choices
            if str(value) in filter_data or None in filter_data
        ]

    if hasattr(field, 'null_option'):
        # If the field has a `null_option` attribute set and it is selected,
        # add it to the field's grouped choices.
        if field.null_option is not None and None in filter_data:
            values.append(field.null_option)

    return values
Esempio n. 4
0
def example_choices(field, arg=3):
    """
    Returns a number (default: 3) of example choices for a ChoiceFiled (useful for CSV import forms).
    """
    examples = []
    if hasattr(field, 'queryset'):
        choices = [(obj.pk, getattr(obj, field.to_field_name))
                   for obj in field.queryset[:arg + 1]]
    else:
        choices = field.choices
    for value, label in unpack_grouped_choices(choices):
        if len(examples) == arg:
            examples.append('etc.')
            break
        if not value or not label:
            continue
        examples.append(label)
    return ', '.join(examples) or 'None'
Esempio n. 5
0
def get_selected_values(form, field_name):
    """
    Return the list of selected human-friendly values for a form field
    """
    if not hasattr(form, 'cleaned_data'):
        form.is_valid()
    filter_data = form.cleaned_data.get(field_name)
    field = form.fields[field_name]

    # Non-selection field
    if not hasattr(field, 'choices'):
        return [str(filter_data)]

    # Model choice field
    if type(field.choices) is forms.models.ModelChoiceIterator:
        # If this is a single-choice field, wrap its value in a list
        if not hasattr(filter_data, '__iter__'):
            values = [filter_data]
        else:
            values = filter_data

    else:
        # Static selection field
        choices = unpack_grouped_choices(field.choices)
        if type(filter_data) not in (list, tuple):
            filter_data = [filter_data]  # Ensure filter data is iterable
        values = [
            label for value, label in choices if str(value) in filter_data or None in filter_data
        ]

    if hasattr(field, 'null_option'):
        # If the field has a `null_option` attribute set and it is selected,
        # add it to the field's grouped choices.
        if field.null_option is not None and None in filter_data:
            values.append(field.null_option)

    return values
Esempio n. 6
0
 def __init__(self, *, choices=(), **kwargs):
     super().__init__(choices=choices, **kwargs)
     self.choices = unpack_grouped_choices(choices)