from django.template.loader import render_to_string
import random
class NavSection:
def __init__(self, name, order=100):
self.name = name
self.order = order
self.items: list[NavItem | NavCollapse] = []
def add_item(self, item):
self.items.append(item)
def get_html(self, active=None) -> str:
html = f"""
"""
self.items.sort(key=lambda x: x.order)
for item in self.items:
html += item.get_html()
return html
class NavItem:
def __init__(self, name, url, icon="fas fa-fw fa-smile", title=None, order=100):
self.name = name
self.url = url
self.icon = icon
self.title = title or name
self.order = order
def get_html(self, active=None) -> str:
return f"""
{self.name}
"""
class NavCollapse:
def __init__(self, name, icon="fas fa-fw fa-smile", order=100):
self.name = name
self.icon = icon
self.order = order
self.items: list[NavItem] = []
def add_item(self, item):
self.items.append(item)
def get_html(self, active=None) -> str:
element_id = random.randint(1000, 9999)
html = f"""
{self.name}
"""
self.items.sort(key=lambda x: x.order)
for item in self.items:
html += (
"""
{item.name}
"""
)
html += """
"""
return html
class DashboardSection:
def __init__(self, name, template, context=None):
self.name = name
self.template = template
self.context = context or {}
self.styles = []
self.scripts = []
def get_html(self, request):
return render_to_string(self.template, self.context, request)
def add_style(self, style):
self.styles.append(style)
def add_script(self, script):
self.scripts.append(script)