cmsmanage/dashboard/__init__.py

48 lines
954 B
Python
Raw Normal View History

import dataclasses
from typing import Any
from django.http import HttpRequest
DASHBOARD_CARDS = {}
def register(fragment):
DASHBOARD_CARDS[fragment.name] = fragment
return fragment
@dataclasses.dataclass
class Link:
text: str
href: str
tooltip: str | None = None
body: str | None = None
_: dataclasses.KW_ONLY
permission: str | None
class DashboardFragment:
name: str
template: str
context: Any = None
visible: bool = True
def __init__(self, request: HttpRequest):
self.request = request
class LinksCardDashboardFragment(DashboardFragment):
template = "dashboard/links_card.dj.html"
2024-03-15 12:12:05 -04:00
links: list[Link] = []
@property
def context(self):
return {"links": self.links}
@property
def visible(self) -> bool:
return any(
link.permission is None or self.request.user.has_perm(link.permission)
for link in self.links
)