diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-20 15:01:20 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-20 15:01:20 +0100 |
commit | e4c95417112ffe8ba684b4f1d7bd957f87b2f2ed (patch) | |
tree | 9b61947660b28196dc7f33df42c82dc1c7ffd979 /ctrack | |
parent | 4ea13b2ed79e95d2cf710c8d2d6e95fb454e054a (diff) |
func to group lead inspectors by submode
Diffstat (limited to 'ctrack')
-rw-r--r-- | ctrack/conftest.py | 6 | ||||
-rw-r--r-- | ctrack/core/tests/test_views.py | 41 | ||||
-rw-r--r-- | ctrack/core/views.py | 5 | ||||
-rw-r--r-- | ctrack/organisations/views.py | 20 | ||||
-rw-r--r-- | ctrack/templates/pages/home.html | 7 |
5 files changed, 69 insertions, 10 deletions
diff --git a/ctrack/conftest.py b/ctrack/conftest.py index 6ae06c2..3c310c8 100644 --- a/ctrack/conftest.py +++ b/ctrack/conftest.py @@ -35,16 +35,16 @@ def user() -> User: @pytest.fixture def inspector1() -> User: - return UserFactory() + return UserFactory(first_name="Cyril", last_name="Bloanssette_Ridgewell") @pytest.fixture def inspector2() -> User: - return UserFactory() + return UserFactory(first_name="Ogilvie", last_name="Cathroyd-Marylls") @pytest.fixture -def submode(inspector1, inspector2): +def submode(): return Submode.objects.create( descriptor="Light Rail", mode=Mode.objects.create(descriptor="Rail") ) diff --git a/ctrack/core/tests/test_views.py b/ctrack/core/tests/test_views.py new file mode 100644 index 0000000..53dc1da --- /dev/null +++ b/ctrack/core/tests/test_views.py @@ -0,0 +1,41 @@ +import pytest + +from ctrack.organisations.models import Organisation, Submode, Mode +from ctrack.organisations.views import inspectors_for_each_mode +from ctrack.organisations.tests.factories import OrganisationFactory + +pytestmark = pytest.mark.django_db + + +def test_group_lead_inspector_by_submode( + submode, mode, org, org_with_people, inspector1, inspector2 +): + # because I can't be bothered making another fixture + submode1 = submode # this is Light Rail by the way + submode2 = Submode.objects.create( + descriptor="Metro Rail", mode=Mode.objects.create(descriptor="Rail")) + + org1 = OrganisationFactory(submode=submode1, lead_inspector=inspector1) + org2 = OrganisationFactory(submode=submode1, lead_inspector=inspector1) + org3 = OrganisationFactory(submode=submode1, lead_inspector=inspector1) + org4 = OrganisationFactory(submode=submode1, lead_inspector=inspector1) + + org5 = OrganisationFactory(submode=submode2, lead_inspector=inspector2) + org6 = OrganisationFactory(submode=submode2, lead_inspector=inspector2) + org7 = OrganisationFactory(submode=submode2, lead_inspector=inspector2) + org8 = OrganisationFactory(submode=submode2, lead_inspector=inspector2) + + # We have two lead inspectors for submode1! + org8 = OrganisationFactory(submode=submode1, lead_inspector=inspector2) + + orgs = Organisation.objects.filter(submode=submode) + leads = [org.lead_inspector for org in orgs] + for lead in leads[:3]: + assert lead.first_name == "Cyril" + + for lead in leads[4:]: + assert lead.first_name == "Ogilvie" + + inspector_dict = inspectors_for_each_mode(lead_type="lead_inspector") + assert inspector_dict["Light Rail"] == [inspector1, inspector2] + assert inspector_dict["Metro Rail"] == [inspector2] diff --git a/ctrack/core/views.py b/ctrack/core/views.py index 254fb86..499aedd 100644 --- a/ctrack/core/views.py +++ b/ctrack/core/views.py @@ -1,9 +1,8 @@ from django.contrib.auth import get_user_model -from django.contrib.auth.decorators import login_required from django.shortcuts import render from ctrack.caf.models import EssentialService, CAF, ApplicableSystem -from ctrack.organisations.models import IncidentReport, Organisation, Person +from ctrack.organisations.models import IncidentReport, Organisation, Person, Submode from ctrack.register.models import EngagementEvent @@ -33,6 +32,7 @@ def home_page(request): no_cafs = CAF.objects.count() no_essential_services = EssentialService.objects.count() no_systems = ApplicableSystem.objects.count() + submodes = Submode.objects.all().order_by("descriptor") context = { "no_orgs": no_orgs, "no_people": no_people, @@ -40,5 +40,6 @@ def home_page(request): "no_essential_services": no_essential_services, "no_systems": no_systems, "caf_users": caf_users, + "submodes": submodes, } return render(request, "pages/home.html", context) diff --git a/ctrack/organisations/views.py b/ctrack/organisations/views.py index fa811eb..45a86bb 100644 --- a/ctrack/organisations/views.py +++ b/ctrack/organisations/views.py @@ -1,5 +1,5 @@ import itertools -from typing import Any +from typing import Any, Dict, List, Set from django.contrib.auth.mixins import PermissionRequiredMixin from django.db import transaction @@ -11,8 +11,9 @@ from django.views.generic import CreateView, DetailView, FormView, ListView from ctrack.caf.models import CAF, EssentialService from ctrack.register.models import EngagementEvent + from .forms import AddressInlineFormSet, IncidentReportForm, OrganisationCreateForm -from .models import IncidentReport, Organisation, Person +from .models import IncidentReport, Organisation, Person, Submode from .utils import filter_private_events @@ -142,3 +143,18 @@ class IncidentReportCreateView(FormView): def form_valid(self, form): form.save() return HttpResponseRedirect(reverse("core:home")) + + +def inspectors_for_each_mode(lead_type="lead_inspector") -> Dict[str, List[str]]: + 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)) + out[sm.descriptor] = list(insp) + del insp + return out diff --git a/ctrack/templates/pages/home.html b/ctrack/templates/pages/home.html index 9ef16ac..1a3348f 100644 --- a/ctrack/templates/pages/home.html +++ b/ctrack/templates/pages/home.html @@ -91,9 +91,10 @@ Inspectors </h3> <ul class="list-group"> - {% for user in caf_users %} - <li class="list-group-item list-group-item-dark">{{ user.name }}</li> - {% endfor %} + {% for submode in submodes %} + {{ submode }} + {% endfor %} + </ul> </div> </div> |