48 lines
954 B
Python
48 lines
954 B
Python
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"
|
|
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
|
|
)
|