Skip to content

Widgets

hypha.addressfield.widgets

KeepOwnAttrsWidget

Bases: Widget

get_context

get_context(name, value, attrs)
Source code in hypha/addressfield/widgets.py
6
7
8
def get_context(self, name, value, attrs):
    attrs.update(self.attrs)
    return super().get_context(name, value, attrs)

CountrySelectWithChoices

CountrySelectWithChoices(*args, **kwargs)

Bases: KeepOwnAttrsWidget, Select

Source code in hypha/addressfield/widgets.py
def __init__(self, *args, **kwargs):
    kwargs["choices"] = countries
    super().__init__(*args, **kwargs)

is_required class-attribute instance-attribute

is_required = True

get_context

get_context(name, value, attrs)
Source code in hypha/addressfield/widgets.py
6
7
8
def get_context(self, name, value, attrs):
    attrs.update(self.attrs)
    return super().get_context(name, value, attrs)

KeepAttrsTextInput

Bases: KeepOwnAttrsWidget, TextInput

NestedMultiWidget

NestedMultiWidget(*args, **kwargs)

Bases: KeepOwnAttrsWidget, MultiWidget

Source code in hypha/addressfield/widgets.py
def __init__(self, *args, **kwargs):
    widgets = [
        widget(
            attrs={
                "class": classify(field),
                "required": False,
                "display": display(field),
            }
        )
        for field, widget in self.components.items()
    ]
    super().__init__(widgets, *args, **kwargs)

template_name class-attribute instance-attribute

template_name = 'addressfield/widgets/nested_with_label.html'

field_names property

field_names

get_context

get_context(name, value, attrs)
Source code in hypha/addressfield/widgets.py
6
7
8
def get_context(self, name, value, attrs):
    attrs.update(self.attrs)
    return super().get_context(name, value, attrs)

decompress

decompress(value)
Source code in hypha/addressfield/widgets.py
def decompress(self, value):
    if value:
        decompressed = []
        for i, widget in enumerate(self.widgets):
            if hasattr(widget, "components"):
                decompressed.append(widget.decompress(value))
            else:
                decompressed.append(value.get(self.field_names[i]))
        return decompressed
    return [None] * len(self.components)

value_from_datadict

value_from_datadict(data, files, name)
Source code in hypha/addressfield/widgets.py
def value_from_datadict(self, data, files, name):
    value = {}
    for i, widget in enumerate(self.widgets):
        widget_value = widget.value_from_datadict(data, files, name + "_%s" % i)
        # flatten the data structure to a single dict
        if hasattr(widget, "widgets"):
            value.update(widget_value)
        else:
            value[self.field_names[i]] = widget_value
    return value

LocalityWidget

LocalityWidget(*args, **kwargs)

Bases: NestedMultiWidget

Source code in hypha/addressfield/widgets.py
def __init__(self, *args, **kwargs):
    widgets = [
        widget(
            attrs={
                "class": classify(field),
                "required": False,
                "display": display(field),
            }
        )
        for field, widget in self.components.items()
    ]
    super().__init__(widgets, *args, **kwargs)

template_name class-attribute instance-attribute

template_name = 'addressfield/widgets/nested_with_label.html'

field_names property

field_names

components class-attribute instance-attribute

components = {'locality_name': KeepAttrsTextInput, 'administrative_area': KeepAttrsTextInput, 'postal_code': KeepAttrsTextInput}

get_context

get_context(name, value, attrs)
Source code in hypha/addressfield/widgets.py
6
7
8
def get_context(self, name, value, attrs):
    attrs.update(self.attrs)
    return super().get_context(name, value, attrs)

decompress

decompress(value)
Source code in hypha/addressfield/widgets.py
def decompress(self, value):
    if value:
        decompressed = []
        for i, widget in enumerate(self.widgets):
            if hasattr(widget, "components"):
                decompressed.append(widget.decompress(value))
            else:
                decompressed.append(value.get(self.field_names[i]))
        return decompressed
    return [None] * len(self.components)

value_from_datadict

value_from_datadict(data, files, name)
Source code in hypha/addressfield/widgets.py
def value_from_datadict(self, data, files, name):
    value = {}
    for i, widget in enumerate(self.widgets):
        widget_value = widget.value_from_datadict(data, files, name + "_%s" % i)
        # flatten the data structure to a single dict
        if hasattr(widget, "widgets"):
            value.update(widget_value)
        else:
            value[self.field_names[i]] = widget_value
    return value

AddressWidget

AddressWidget(*args, **kwargs)

Bases: NestedMultiWidget

Source code in hypha/addressfield/widgets.py
def __init__(self, *args, **kwargs):
    attrs = kwargs.get("attrs", {})
    attrs["class"] = "address"
    kwargs["attrs"] = attrs
    super().__init__(*args, **kwargs)

template_name class-attribute instance-attribute

template_name = 'addressfield/widgets/nested_with_label.html'

field_names property

field_names

components class-attribute instance-attribute

components = {'country': CountrySelectWithChoices, 'thoroughfare': KeepAttrsTextInput, 'premise': KeepAttrsTextInput, 'locality': LocalityWidget}

Media

js class-attribute instance-attribute
js = ('jquery.addressfield.min.js', 'address_form.js')

get_context

get_context(name, value, attrs)
Source code in hypha/addressfield/widgets.py
6
7
8
def get_context(self, name, value, attrs):
    attrs.update(self.attrs)
    return super().get_context(name, value, attrs)

decompress

decompress(value)
Source code in hypha/addressfield/widgets.py
def decompress(self, value):
    if value:
        decompressed = []
        for i, widget in enumerate(self.widgets):
            if hasattr(widget, "components"):
                decompressed.append(widget.decompress(value))
            else:
                decompressed.append(value.get(self.field_names[i]))
        return decompressed
    return [None] * len(self.components)

value_from_datadict

value_from_datadict(data, files, name)
Source code in hypha/addressfield/widgets.py
def value_from_datadict(self, data, files, name):
    value = {}
    for i, widget in enumerate(self.widgets):
        widget_value = widget.value_from_datadict(data, files, name + "_%s" % i)
        # flatten the data structure to a single dict
        if hasattr(widget, "widgets"):
            value.update(widget_value)
        else:
            value[self.field_names[i]] = widget_value
    return value

classify

classify(field)
Source code in hypha/addressfield/widgets.py
def classify(field):
    return field.replace("_", "")

display

display(field)
Source code in hypha/addressfield/widgets.py
def display(field):
    return field.replace("_", " ").title()