Source code for superdjango.ui.runtime.tools
# Imports
import os
from ..constants import LOCATION
# Exports
__all__ = (
"Tool",
"Tools",
)
# Classes
[docs]class Tool(object):
"""Placeholder for any tool."""
[docs] def __init__(self, name, enabled=True, location=None, sort_order=1, template=None):
"""Initialize a tool instance.
:param name: The name of the tool.
:type name: str
:param enabled: Indicates the tool is enabled.
:type enabled: bool
:param location: The intended location of the tool on the page.
:type location: str
:param sort_order: The order in which the tool appears with other tools in the same tool set.
:type sort_order: int
:param template: The template to use for the tool. Defaults to the name of the tool.
:type template: str
"""
self.enabled = enabled
self.location = location
self.name = name
self.sort_order = sort_order
self.template = template or os.path.join("superdjango", "ui", "includes", "%s.html" % name)
[docs]class Tools(object):
"""A collection of tools used to manage models, usually from a change list."""
[docs] def __init__(self, items=None):
"""Initialize a tools instance.
:param items: The tools to be included.
:type items: list[Tool]
"""
self.items = items or list()
def __iter__(self):
return iter(self.items)
def __len__(self):
return len(self.items)
[docs] def add(self, name, enabled=True, location=None, sort_order=1, template=None):
"""Add a tool to the set.
See :py:class:`Tool`.
:rtype: Tool
"""
tool = Tool(
name,
enabled=enabled,
location=location,
sort_order=sort_order,
template=template
)
self.items.append(tool)
return tool
@property
def bottom(self):
"""Indicates there are tools with location of ``LOCATION.BOTTOM``.
:rtype: bool
"""
for item in self.items:
if item.location == LOCATION.BOTTOM:
return True
return False
@property
def left(self):
"""Indicates there are tools with location of ``LOCATION.LEFT``.
:rtype: bool
"""
for item in self.items:
if item.location == LOCATION.LEFT:
return True
return False
@property
def right(self):
"""Indicates there are tools with location of ``LOCATION.RIGHT``.
:rtype: bool
"""
for item in self.items:
if item.location == LOCATION.RIGHT:
return True
return False
[docs] def sort(self):
"""Sort tool instances by the ``sort_order`` attribute."""
self.items.sort(key=lambda x: x.sort_order)
@property
def top(self):
"""Indicates there are tools with location of ``LOCATION.TOP``.
:rtype: bool
"""
for item in self.items:
if item.location == LOCATION.TOP:
return True
return False