diff --git a/membershipworks/models.py b/membershipworks/models.py index e9efbaf..d4968ec 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -1,3 +1,5 @@ +from typing import Optional + import django.core.mail.message from django.conf import settings from django.db import models @@ -169,6 +171,11 @@ class Member(models.Model): models.Index(fields=["last_name"]), ] + @classmethod + def from_user(cls, user) -> Optional["Member"]: + if hasattr(user, "ldap_user"): + return cls.objects.get(uid=user.ldap_user.attrs["employeeNumber"][0]) + def sanitized_mailbox(self, name_ext: str = "", use_volunteer=False) -> str: if use_volunteer and self.volunteer_email: email = self.volunteer_email diff --git a/paperwork/dashboard.py b/paperwork/dashboard.py index 6b01057..c0e442b 100644 --- a/paperwork/dashboard.py +++ b/paperwork/dashboard.py @@ -16,23 +16,15 @@ class PaperworkDashboardFragment(dashboard.DashboardFragment): 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 + member = Member.from_user(self.request.user) + if member is not None: + links["Member Certifications"] = reverse( + "paperwork:member_certifications", kwargs={"uid": member.uid} + ) - 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: + if self.request.user.is_superuser or ( + member is not None and Department.filter_by_shop_lead(member).exists() + ): links["Department Certifications"] = reverse( "paperwork:department_certifications" ) diff --git a/paperwork/models.py b/paperwork/models.py index 93106aa..10a186b 100644 --- a/paperwork/models.py +++ b/paperwork/models.py @@ -66,7 +66,20 @@ class CmsRedRiverVeteransScholarship(models.Model): db_table = "CMS Red River Veterans Scholarship" +class DepartmentQuerySet(models.QuerySet): + def filter_by_shop_lead(self, member: Member) -> models.QuerySet["Department"]: + """Get departments for which `member` is a shop lead""" + # TODO: could be a lot simpler if membershipworks was in the same database + # TODO: should also select children + member_flags = list(member.flags.all().values_list("pk", flat=True)) + return self.prefetch_related("shop_lead_flag__members").filter( + shop_lead_flag__in=member_flags + ) + + class Department(models.Model): + objects = DepartmentQuerySet.as_manager() + name = models.CharField( max_length=64, validators=[RegexValidator("^[-_ A-Za-z0-9]*$")], diff --git a/paperwork/views.py b/paperwork/views.py index 420a4b0..6f3594c 100644 --- a/paperwork/views.py +++ b/paperwork/views.py @@ -32,17 +32,10 @@ class MemberCertificationListView(ListView): @login_required def department_certifications(request): - departments = Department.objects.prefetch_related("shop_lead_flag__members") if request.user.is_superuser: - departments = departments.all() - elif hasattr(request.user, "ldap_user"): - user_member = Member.objects.get( - uid=request.user.ldap_user.attrs["employeeNumber"][0] - ) - # 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) + departments = Department.prefetch_related("shop_lead_flag__members").all() + elif member := Member.from_user(request.user) is not None: + departments = Department.objects.filter_by_shop_lead(member) else: departments = []