Move Member by user and Departments by shop lead lookups to models
This commit is contained in:
parent
2c474bf751
commit
41d8ed691b
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import django.core.mail.message
|
import django.core.mail.message
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@ -169,6 +171,11 @@ class Member(models.Model):
|
|||||||
models.Index(fields=["last_name"]),
|
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:
|
def sanitized_mailbox(self, name_ext: str = "", use_volunteer=False) -> str:
|
||||||
if use_volunteer and self.volunteer_email:
|
if use_volunteer and self.volunteer_email:
|
||||||
email = self.volunteer_email
|
email = self.volunteer_email
|
||||||
|
@ -16,23 +16,15 @@ class PaperworkDashboardFragment(dashboard.DashboardFragment):
|
|||||||
def context(self) -> Any:
|
def context(self) -> Any:
|
||||||
links = {}
|
links = {}
|
||||||
|
|
||||||
if hasattr(self.request.user, "ldap_user"):
|
member = Member.from_user(self.request.user)
|
||||||
uid = self.request.user.ldap_user.attrs["employeeNumber"][0]
|
if member is not None:
|
||||||
links["Member Certifications"] = reverse("paperwork:member_certifications")
|
links["Member Certifications"] = reverse(
|
||||||
else:
|
"paperwork:member_certifications", kwargs={"uid": member.uid}
|
||||||
uid = None
|
)
|
||||||
|
|
||||||
departments = Department.objects.prefetch_related("shop_lead_flag__members")
|
if self.request.user.is_superuser or (
|
||||||
if self.request.user.is_superuser:
|
member is not None and Department.filter_by_shop_lead(member).exists()
|
||||||
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(
|
links["Department Certifications"] = reverse(
|
||||||
"paperwork:department_certifications"
|
"paperwork:department_certifications"
|
||||||
)
|
)
|
||||||
|
@ -66,7 +66,20 @@ class CmsRedRiverVeteransScholarship(models.Model):
|
|||||||
db_table = "CMS Red River Veterans Scholarship"
|
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):
|
class Department(models.Model):
|
||||||
|
objects = DepartmentQuerySet.as_manager()
|
||||||
|
|
||||||
name = models.CharField(
|
name = models.CharField(
|
||||||
max_length=64,
|
max_length=64,
|
||||||
validators=[RegexValidator("^[-_ A-Za-z0-9]*$")],
|
validators=[RegexValidator("^[-_ A-Za-z0-9]*$")],
|
||||||
|
@ -32,17 +32,10 @@ class MemberCertificationListView(ListView):
|
|||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def department_certifications(request):
|
def department_certifications(request):
|
||||||
departments = Department.objects.prefetch_related("shop_lead_flag__members")
|
|
||||||
if request.user.is_superuser:
|
if request.user.is_superuser:
|
||||||
departments = departments.all()
|
departments = Department.prefetch_related("shop_lead_flag__members").all()
|
||||||
elif hasattr(request.user, "ldap_user"):
|
elif member := Member.from_user(request.user) is not None:
|
||||||
user_member = Member.objects.get(
|
departments = Department.objects.filter_by_shop_lead(member)
|
||||||
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)
|
|
||||||
else:
|
else:
|
||||||
departments = []
|
departments = []
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user