cmsmanage/paperwork/dashboard.py

45 lines
1.4 KiB
Python
Raw Normal View History

from typing import Any
from django.urls import reverse
import dashboard
from membershipworks.models import Member
from .models import Department
@dashboard.register
class PaperworkDashboardFragment(dashboard.DashboardFragment):
name = "Paperwork"
template = "dashboard/links_card.dj.html"
@property
def context(self) -> Any:
links = {}
if hasattr(self.request.user, "ldap_user"):
uid = self.request.user.ldap_user.attrs["employeeNumber"][0]
links["Member Certifications"] = reverse("paperwork:member_certifications")
else:
uid = None
departments = Department.objects.prefetch_related("shop_lead_flag__members")
if self.request.user.is_superuser:
departments = departments.all()
elif uid is not None:
user_member = Member.objects.get(uid=uid)
# TODO: could be a lot simpler if membershipworks was in the same database
# TODO: should also select children
member_flags = list(user_member.flags.all().values_list("pk", flat=True))
departments = departments.filter(shop_lead_flag__in=member_flags)
if len(departments) > 0:
links["Department Certifications"] = reverse(
"paperwork:department_certifications"
)
return {"links": links}
@property
def visible(self) -> bool:
return self.request.user.is_authenticated