diff options
Diffstat (limited to '')
-rw-r--r-- | ctrack/organisations/utils.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/ctrack/organisations/utils.py b/ctrack/organisations/utils.py index 121a694..6cc973a 100644 --- a/ctrack/organisations/utils.py +++ b/ctrack/organisations/utils.py @@ -1,6 +1,10 @@ +from typing import Dict, Set + from django.contrib.auth.models import User from django.db.models import QuerySet, Q +from ctrack.organisations.models import Submode + def filter_private_events(events: QuerySet, user: User): """ @@ -11,3 +15,22 @@ def filter_private_events(events: QuerySet, user: User): events for the logged-in user. """ return events.exclude(~Q(user=user) & Q(private=True)) + + +def inspectors_for_each_mode(lead_type="lead_inspector") -> Dict[str, Set[str]]: + """ + We want to be able to group lead inspectors by submode. + """ + if lead_type not in ["lead_inspector", "deputy_lead_inspector"]: + raise ValueError("Can only query for lead_inspector and deputy_lead_inspector attributes.") + submodes = Submode.objects.all() + out = {} + for sm in submodes: + insp = set() + orgs = sm.organisation_set.all() + for org in orgs: + insp.add(getattr(org, lead_type)) + insp = {x for x in insp if x is not None} + out[sm.descriptor] = insp + del insp + return out |