Move Member by user and Departments by shop lead lookups to models

This commit is contained in:
Adam Goldsmith 2023-04-25 23:23:27 -04:00
parent 2c474bf751
commit 41d8ed691b
4 changed files with 31 additions and 26 deletions

View File

@ -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

View File

@ -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"
)

View File

@ -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]*$")],

View File

@ -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 = []