From 85bf8f88e7904f727827a96b0a4590b937f320d9 Mon Sep 17 00:00:00 2001 From: Cameron Lamb Date: Tue, 26 Nov 2024 17:23:31 +0000 Subject: [PATCH] Add team people directory filter --- src/core/templatetags/workspace_navigation.py | 13 ++--- .../peoplefinder/includes/listing_header.html | 5 +- .../peoplefinder/person-directory.html | 4 ++ .../templates/peoplefinder/team.html | 1 + src/peoplefinder/views/directory.py | 47 ++++++++++++++----- 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/core/templatetags/workspace_navigation.py b/src/core/templatetags/workspace_navigation.py index 20a7e8f29..96eacd941 100644 --- a/src/core/templatetags/workspace_navigation.py +++ b/src/core/templatetags/workspace_navigation.py @@ -22,7 +22,6 @@ def build_team_breadcrumbs(request, team: Team) -> list[tuple[str, str]]: (reverse("team-view", args=[parent_team.slug]), parent_team.short_name) for parent_team in parent_teams ] - breadcrumbs += [(reverse("team-view", args=[team.slug]), team.short_name)] return breadcrumbs @@ -43,17 +42,18 @@ def breadcrumbs(context) -> list[tuple[str, str]]: (ancestor.get_url(request), ancestor.title) for ancestor in ancestors ] return {"breadcrumbs": breadcrumbs + extra_breadcrumbs} - has_team_breadcrumbs = context.get("team_breadcrumbs", False) has_profile_breadcrumbs = context.get("profile_breadcrumbs", False) # Build team breadcrumbs if has_team_breadcrumbs or has_profile_breadcrumbs: if has_team_breadcrumbs: - team: Team = context["team"] - return { - "breadcrumbs": build_team_breadcrumbs(request, team) + extra_breadcrumbs - } + team: Team | None = context.get("team", None) + if team: + breadcrumbs = build_team_breadcrumbs(request, team) + else: + breadcrumbs = build_home_breadcrumbs(request) + return {"breadcrumbs": breadcrumbs + extra_breadcrumbs} # Build profile breadcrumbs if has_profile_breadcrumbs: @@ -67,5 +67,6 @@ def breadcrumbs(context) -> list[tuple[str, str]]: breadcrumbs.append( (reverse("profile-view", args=[profile.slug]), profile.full_name) ) + return {"breadcrumbs": breadcrumbs + extra_breadcrumbs} return {"breadcrumbs": breadcrumbs + extra_breadcrumbs} diff --git a/src/peoplefinder/templates/peoplefinder/includes/listing_header.html b/src/peoplefinder/templates/peoplefinder/includes/listing_header.html index 0eec5edd9..7e207f2e3 100644 --- a/src/peoplefinder/templates/peoplefinder/includes/listing_header.html +++ b/src/peoplefinder/templates/peoplefinder/includes/listing_header.html @@ -5,17 +5,16 @@

{{ page_title }}

+ {% if team %}{% endif %}
-
- Reset filters + value="Search" />
diff --git a/src/peoplefinder/templates/peoplefinder/person-directory.html b/src/peoplefinder/templates/peoplefinder/person-directory.html index cad66c8a4..b801dc735 100644 --- a/src/peoplefinder/templates/peoplefinder/person-directory.html +++ b/src/peoplefinder/templates/peoplefinder/person-directory.html @@ -7,6 +7,10 @@ {% block bookmark %} {% endblock bookmark %} +{% block title %} + {{ page_title }} +{% endblock title %} + {% block page_title_wrapper %} {% endblock page_title_wrapper %} diff --git a/src/peoplefinder/templates/peoplefinder/team.html b/src/peoplefinder/templates/peoplefinder/team.html index 7f9575872..733483c74 100644 --- a/src/peoplefinder/templates/peoplefinder/team.html +++ b/src/peoplefinder/templates/peoplefinder/team.html @@ -103,5 +103,6 @@

People in {{ team.short_name }}

{% endif %} + Team Directory {% endblock primary_content %} diff --git a/src/peoplefinder/views/directory.py b/src/peoplefinder/views/directory.py index ad41b3181..3279b0cf6 100644 --- a/src/peoplefinder/views/directory.py +++ b/src/peoplefinder/views/directory.py @@ -4,7 +4,7 @@ from django.db.models.query import QuerySet from django.views.generic import ListView -from peoplefinder.models import Person +from peoplefinder.models import Person, Team from search.search import PeopleSearchVector @@ -13,27 +13,52 @@ class PeopleDirectory(ListView): template_name = "peoplefinder/person-directory.html" paginate_by = 30 + def dispatch(self, request, *args, **kwargs): + self.query = self.request.GET.get("query", "") + + self.team = None + if team_pk := self.request.GET.get("team", ""): + self.team = Team.objects.get(pk=team_pk) + + return super().dispatch(request, *args, **kwargs) + def get_queryset(self) -> QuerySet[Any]: - query = self.request.GET.get("query", "") - if query: + if self.query: people_results = PeopleSearchVector(self.request).search( - query_str=self.request.GET.get("query", "") + query_str=self.query ) - return people_results + person_ids = [p.id for p in people_results] + queryset = Person.objects.filter(id__in=person_ids) + else: + queryset = super().get_queryset() + if not self.request.user.has_perm( + "peoplefinder.can_view_inactive_profiles" + ): + days = settings.SEARCH_SHOW_INACTIVE_PROFILES_WITHIN_DAYS + queryset = queryset.active_or_inactive_within(days=days) - queryset = super().get_queryset() + if self.team: + queryset = queryset.filter( + roles__team__pk__in=[self.team.pk] + + [tt.child.pk for tt in self.team.parents.all()] + ) + print(self.team.parents.all()) + print(self.team.children.all()) - if not self.request.user.has_perm("peoplefinder.can_view_inactive_profiles"): - days = settings.SEARCH_SHOW_INACTIVE_PROFILES_WITHIN_DAYS - queryset = queryset.active_or_inactive_within(days=days) return queryset def get_context_data(self, **kwargs: Any) -> dict[str, Any]: context = super().get_context_data(**kwargs) + page_title = "Find people" + + if self.team: + page_title = f"Find people in {self.team}" + context.update( - page_title="Find people", + page_title=page_title, + team=self.team, + team_breadcrumbs=True, extra_breadcrumbs=[ - ("/", "Home"), (None, "People directory"), ], search_query=self.request.GET.get("query", ""),