# Imports
from ..constants import LOCATION, ORIENTATION
# Exports
__all__ = (
"Choice",
"Filter",
)
# Classes
[docs]class Choice(object):
"""An individual choice within a filter."""
[docs] def __init__(self, label, value, count=None, is_active=None):
"""Initialize a choice.
:param label: The choice label that is shown to users.
:type label: str
:param value: The value of the choice. Used for filtering.
:param count: The total number of records matching the choice value.
:type count: int
:param is_active: Indicates the choice is active.
:type is_active: bool
"""
self.count = count
self.is_active = is_active
self.is_multi = False
self.label = label
self.value = value
[docs]class Filter(object):
"""A filter for a model list."""
[docs] def __init__(self, label, name, choices=None, keyword=None, location=None, orientation=None, template=None):
"""Initialize the filter.
:param label: The label of the filter that is show to users.
:type label: str
:param name: The programmatic name of the filter.
:type name: str
:param choices: The filter choices.
:type choices: list | QuerySet
:param keyword: The GET keyword used to identify the filter. Defaults to the filter name.
:type keyword: str
:param location: The location of the filter on the page.
:type location: str
:param orientation: The orientation of the filter.
:type orientation: str
:param template: The template to use for rendering the filter.
:type template: str
"""
self.choices = choices
self.keyword = keyword or name
self.label = label
self.location = location
self.name = name
self.orientation = orientation
self.template = template
def __repr__(self):
return "<%s %s:%s>" % (self.__class__.__name__, self.name, self.label)
@property
def is_bottom(self):
"""Indicates the intended location for the filter is at the bottom of the output.
:rtype: bool
"""
return self.location == LOCATION.BOTTOM
@property
def is_default(self):
"""Indicates the intended location for the filter is the default location.
:rtype: bool
"""
return self.location == LOCATION.DEFAULT
@property
def is_horizontal(self):
"""Indicates the intended orientation for the filter is horizontal.
:rtype: bool
"""
return self.orientation == ORIENTATION.HORIZONTAL
@property
def is_left(self):
"""Indicates the intended location for the filter is left of the output.
:rtype: bool
"""
return self.location == LOCATION.LEFT
@property
def is_right(self):
"""Indicates the intended location for the filter is right of the output.
:rtype: bool
"""
return self.location == LOCATION.RIGHT
@property
def is_sidebar(self):
"""Indicates the intended location for the filter is in the sidebar (left or right).
:rtype: bool
"""
return self.location in (LOCATION.LEFT, LOCATION.RIGHT)
@property
def is_top(self):
"""Indicates the intended location for the filter is at the top of the output.
:rtype: bool
"""
return self.location == LOCATION.TOP
@property
def is_vertical(self):
"""Indicates the intended orientation for the filter is vertical.
:rtype: bool
"""
return self.orientation == ORIENTATION.VERTICAL