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