2022-12-25 01:22:21 -05:00
|
|
|
from django.db.models import Prefetch
|
2022-12-25 01:21:30 -05:00
|
|
|
from rest_framework import routers, serializers, viewsets
|
2022-12-25 01:22:21 -05:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
2022-12-25 01:21:30 -05:00
|
|
|
|
2022-12-25 01:22:21 -05:00
|
|
|
from membershipworks.models import Member
|
2023-01-18 21:29:36 -05:00
|
|
|
from .models import (
|
|
|
|
Department,
|
|
|
|
Certification,
|
|
|
|
CertificationDefinition,
|
|
|
|
CertificationVersion,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DepartmentSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Department
|
2023-01-23 21:06:06 -05:00
|
|
|
fields = ["name", "parent", "shop_lead_flag", "list_reply_to_address"]
|
2023-01-18 21:29:36 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DepartmentViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = Department.objects.all()
|
|
|
|
serializer_class = DepartmentSerializer
|
2022-12-25 01:21:30 -05:00
|
|
|
|
2023-01-19 14:40:41 -05:00
|
|
|
@action(detail=False, methods=["get"])
|
|
|
|
def mailing_lists(self, request, format=None):
|
|
|
|
"""
|
|
|
|
Generate a mailing list for each department, containing all
|
|
|
|
certified users for tools in that department or child departments
|
|
|
|
"""
|
2023-02-02 18:55:48 -05:00
|
|
|
departments = self.queryset.prefetch_related(
|
2023-01-19 14:40:41 -05:00
|
|
|
"children",
|
2023-01-23 21:06:06 -05:00
|
|
|
"shop_lead_flag__members",
|
2023-01-19 14:40:41 -05:00
|
|
|
Prefetch(
|
|
|
|
"certificationdefinition_set__certificationversion_set__certification_set__member",
|
|
|
|
queryset=Member.objects.with_is_active(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
lists = {}
|
2023-02-02 18:55:48 -05:00
|
|
|
for department in departments.filter(has_mailing_list=True):
|
2023-01-23 21:06:06 -05:00
|
|
|
if department.shop_lead_flag is not None:
|
2023-01-23 15:07:01 -05:00
|
|
|
moderator_emails = {
|
2023-01-23 21:06:06 -05:00
|
|
|
member.email for member in department.shop_lead_flag.members.all()
|
2023-01-23 15:07:01 -05:00
|
|
|
}
|
2023-01-19 14:40:41 -05:00
|
|
|
else:
|
|
|
|
moderator_emails = []
|
|
|
|
|
|
|
|
# TODO: this could be done in SQL instead if
|
|
|
|
# membershipworks was in the same database
|
|
|
|
active_certified_members = {
|
2023-01-23 15:07:01 -05:00
|
|
|
member_cert.member.sanitized_mailbox()
|
2023-01-19 14:40:41 -05:00
|
|
|
for certification in department.certificationdefinition_set.all()
|
|
|
|
for version in certification.certificationversion_set.all()
|
|
|
|
for member_cert in version.certification_set.all()
|
|
|
|
if member_cert.member and member_cert.member.is_active
|
|
|
|
}
|
|
|
|
|
|
|
|
lists[department.list_name] = {
|
2023-01-22 11:21:46 -05:00
|
|
|
"config": {
|
|
|
|
"real_name": department.list_name,
|
|
|
|
"moderator": moderator_emails,
|
|
|
|
"subject_prefix": f"[CMS {department.name}] ",
|
|
|
|
"reply_to_address": department.list_reply_to_address,
|
|
|
|
},
|
2023-01-19 14:40:41 -05:00
|
|
|
"members": active_certified_members,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add child departments' members to their parents
|
|
|
|
# TODO: this seems wildly inefficient
|
|
|
|
def recurse_children(department):
|
|
|
|
for child in department.children.all():
|
|
|
|
recurse_children(child)
|
|
|
|
|
|
|
|
lists[department.list_name]["members"] |= lists[child.list_name][
|
|
|
|
"members"
|
|
|
|
]
|
|
|
|
|
2023-02-02 18:55:48 -05:00
|
|
|
for department in departments.filter(has_mailing_list=True):
|
2023-01-19 14:40:41 -05:00
|
|
|
if department.parent_id is None:
|
|
|
|
recurse_children(department)
|
|
|
|
|
2023-01-23 15:07:01 -05:00
|
|
|
shopleads = {}
|
2023-02-02 18:55:48 -05:00
|
|
|
for department in departments.filter(shop_lead_flag__isnull=False):
|
2023-01-23 21:06:06 -05:00
|
|
|
for member in department.shop_lead_flag.members.all():
|
2023-01-23 15:07:01 -05:00
|
|
|
if member not in shopleads:
|
|
|
|
shopleads[member] = []
|
|
|
|
shopleads[member].append(department)
|
|
|
|
|
|
|
|
# Add members to the Shop Leads mailing list, but don't configure it
|
|
|
|
lists["ShopLeads"] = {
|
|
|
|
"members": {
|
|
|
|
shoplead.sanitized_mailbox(
|
|
|
|
f" - {'/'.join(department.name for department in departments)}"
|
|
|
|
)
|
|
|
|
for shoplead, departments in shopleads.items()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:40:41 -05:00
|
|
|
return Response(lists)
|
|
|
|
|
2022-12-25 01:21:30 -05:00
|
|
|
|
|
|
|
class CertificationDefinitionSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = CertificationDefinition
|
2023-01-23 15:16:29 -05:00
|
|
|
fields = ["certification_name", "department"]
|
2022-12-25 01:21:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
class CertificationDefinitionViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = CertificationDefinition.objects.all()
|
|
|
|
serializer_class = CertificationDefinitionSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class CertificationVersionSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = CertificationVersion
|
|
|
|
fields = ["definition", "version"]
|
|
|
|
|
|
|
|
|
|
|
|
class CertificationVersionViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = CertificationVersion.objects.all()
|
|
|
|
serializer_class = CertificationVersionSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class CertificationSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Certification
|
|
|
|
fields = [
|
|
|
|
"name",
|
|
|
|
"member",
|
|
|
|
"certification_version",
|
|
|
|
"certified_by",
|
|
|
|
"date",
|
|
|
|
"shop_lead_notified",
|
|
|
|
"notes",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class CertificationViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = Certification.objects.all()
|
|
|
|
serializer_class = CertificationSerializer
|
|
|
|
|
|
|
|
|
|
|
|
router = routers.DefaultRouter()
|
2023-01-18 21:29:36 -05:00
|
|
|
router.register(r"paperwork/department", DepartmentViewSet)
|
2022-12-25 01:21:30 -05:00
|
|
|
router.register(r"paperwork/certification_definition", CertificationDefinitionViewSet)
|
|
|
|
router.register(r"paperwork/certification_version", CertificationVersionViewSet)
|
|
|
|
router.register(r"paperwork/certification", CertificationViewSet)
|