aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/organisations/utils.py
blob: 6cc973a9d06534dc86cab581fb0a953419d2ba2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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):
    """
    Given a QuerySet containing SingleDateTimeEvent objects,
    ensure that any objects whose user==user and private==True
    are filtered out. This supports OrganisationDetailView, which
    lists all events for an organisation but which must hide private
    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