|
1 |
| -"""Filters used in organization dashboard.""" |
| 1 | +"""Filters used in the organization dashboard views.""" |
2 | 2 |
|
3 | 3 | import structlog
|
4 | 4 | from django.db.models import F
|
5 |
| -from django.forms.widgets import HiddenInput |
6 | 5 | from django.utils.translation import gettext_lazy as _
|
7 |
| -from django_filters import CharFilter, FilterSet, OrderingFilter |
| 6 | +from django_filters import ChoiceFilter, FilterSet, OrderingFilter |
| 7 | + |
| 8 | +from readthedocs.core.filters import FilteredModelChoiceFilter |
| 9 | +from readthedocs.organizations.constants import ACCESS_LEVELS |
| 10 | +from readthedocs.organizations.models import Organization, Team |
8 | 11 |
|
9 | 12 | log = structlog.get_logger(__name__)
|
10 | 13 |
|
11 | 14 |
|
| 15 | +class OrganizationFilterSet(FilterSet): |
| 16 | + |
| 17 | + """ |
| 18 | + Organization base filter set. |
| 19 | +
|
| 20 | + Adds some methods that are used for organization related queries and common |
| 21 | + base querysets for filter fields. |
| 22 | +
|
| 23 | + Note, the querysets here are also found in the organization base views and |
| 24 | + mixin classes. These are redefined here instead of passing in the querysets |
| 25 | + from the view. |
| 26 | +
|
| 27 | + :param organization: Organization instance for current view |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self, *args, organization=None, **kwargs): |
| 31 | + self.organization = organization |
| 32 | + super().__init__(*args, **kwargs) |
| 33 | + |
| 34 | + def get_organization_queryset(self): |
| 35 | + return Organization.objects.for_user(user=self.request.user) |
| 36 | + |
| 37 | + def get_team_queryset(self): |
| 38 | + return Team.objects.member( |
| 39 | + self.request.user, |
| 40 | + organization=self.organization, |
| 41 | + ).prefetch_related("organization") |
| 42 | + |
| 43 | + def is_valid(self): |
| 44 | + # This differs from the default logic as we want to consider unbound |
| 45 | + # data as a valid filterset state. |
| 46 | + return (self.is_bound is False) or self.form.is_valid() |
| 47 | + |
| 48 | + |
12 | 49 | class OrganizationSortOrderingFilter(OrderingFilter):
|
13 | 50 |
|
14 | 51 | """Organization list sort ordering django_filters filter."""
|
@@ -53,13 +90,152 @@ def filter(self, qs, value):
|
53 | 90 | return qs.order_by(*order_bys)
|
54 | 91 |
|
55 | 92 |
|
56 |
| -class OrganizationListFilterSet(FilterSet): |
| 93 | +class OrganizationListFilterSet(OrganizationFilterSet): |
57 | 94 |
|
58 | 95 | """Filter and sorting for organization listing page."""
|
59 | 96 |
|
60 |
| - slug = CharFilter(field_name="slug", widget=HiddenInput) |
| 97 | + slug = FilteredModelChoiceFilter( |
| 98 | + label=_("Organization"), |
| 99 | + empty_label=_("All organizations"), |
| 100 | + to_field_name="slug", |
| 101 | + queryset_method="get_organization_queryset", |
| 102 | + method="get_organization", |
| 103 | + ) |
61 | 104 |
|
62 | 105 | sort = OrganizationSortOrderingFilter(
|
63 | 106 | field_name="sort",
|
64 | 107 | label=_("Sort by"),
|
65 | 108 | )
|
| 109 | + |
| 110 | + def get_organization(self, queryset, field_name, organization): |
| 111 | + return queryset.filter(slug=organization.slug) |
| 112 | + |
| 113 | + |
| 114 | +class OrganizationProjectListFilterSet(OrganizationFilterSet): |
| 115 | + |
| 116 | + """ |
| 117 | + Filter and sorting set for organization project listing page. |
| 118 | +
|
| 119 | + This filter set creates the following filters in the organization project |
| 120 | + listing UI: |
| 121 | +
|
| 122 | + Project |
| 123 | + A list of project names that the user has permissions to, using ``slug`` |
| 124 | + as a lookup field. This is used when linking directly to a project in |
| 125 | + this filter list, and also for quick lookup in the list UI. |
| 126 | +
|
| 127 | + Team |
| 128 | + A list of team names that the user has access to, using ``slug`` as a |
| 129 | + lookup field. This filter is linked to directly by the team listing |
| 130 | + view, as a shortcut for listing projects managed by the team. |
| 131 | + """ |
| 132 | + |
| 133 | + slug = FilteredModelChoiceFilter( |
| 134 | + label=_("Project"), |
| 135 | + empty_label=_("All projects"), |
| 136 | + to_field_name="slug", |
| 137 | + queryset_method="get_project_queryset", |
| 138 | + method="get_project", |
| 139 | + ) |
| 140 | + |
| 141 | + teams__slug = FilteredModelChoiceFilter( |
| 142 | + label=_("Team"), |
| 143 | + empty_label=_("All teams"), |
| 144 | + field_name="teams", |
| 145 | + to_field_name="slug", |
| 146 | + queryset_method="get_team_queryset", |
| 147 | + ) |
| 148 | + |
| 149 | + def get_project_queryset(self): |
| 150 | + return self.queryset |
| 151 | + |
| 152 | + def get_project(self, queryset, field_name, project): |
| 153 | + return queryset.filter(slug=project.slug) |
| 154 | + |
| 155 | + |
| 156 | +class OrganizationTeamListFilterSet(OrganizationFilterSet): |
| 157 | + |
| 158 | + """ |
| 159 | + Filter and sorting for organization team listing page. |
| 160 | +
|
| 161 | + This filter set creates the following filters in the organization team |
| 162 | + listing UI: |
| 163 | +
|
| 164 | + Team |
| 165 | + A list of team names that the user has access to, using ``slug`` as a |
| 166 | + lookup field. This filter is used mostly for direct linking to a |
| 167 | + specific team in the listing UI, but can be used for quick filtering |
| 168 | + with the dropdown too. |
| 169 | + """ |
| 170 | + |
| 171 | + slug = FilteredModelChoiceFilter( |
| 172 | + label=_("Team"), |
| 173 | + empty_label=_("All teams"), |
| 174 | + field_name="teams", |
| 175 | + to_field_name="slug", |
| 176 | + queryset_method="get_team_queryset", |
| 177 | + method="get_team", |
| 178 | + ) |
| 179 | + |
| 180 | + def get_team_queryset(self): |
| 181 | + return self.queryset |
| 182 | + |
| 183 | + def get_team(self, queryset, field_name, team): |
| 184 | + return queryset.filter(slug=team.slug) |
| 185 | + |
| 186 | + |
| 187 | +class OrganizationTeamMemberListFilterSet(OrganizationFilterSet): |
| 188 | + |
| 189 | + """ |
| 190 | + Filter and sorting set for organization member listing page. |
| 191 | +
|
| 192 | + This filter set's underlying queryset from the member listing view is the |
| 193 | + manager method ``Organization.members``. The model described in this filter |
| 194 | + is effectively ``User``, but through a union of ``TeamMembers.user`` and |
| 195 | + ``Organizations.owners``. |
| 196 | +
|
| 197 | + This filter set will result in the following filters in the UI: |
| 198 | +
|
| 199 | + Team |
| 200 | + A list of ``Team`` names, using ``Team.slug`` as the lookup field. This |
| 201 | + is linked to directly from the team listing page, to show the users that |
| 202 | + are members of a particular team. |
| 203 | +
|
| 204 | + Access |
| 205 | + This is an extension of ``Team.access`` in a way, but with a new option |
| 206 | + (``ACCESS_OWNER``) to describe ownership privileges through organization |
| 207 | + ownership. |
| 208 | +
|
| 209 | + Our modeling is not ideal here, so instead of aiming for model purity and |
| 210 | + a confusing UI/UX, this aims for hiding confusing modeling from the user |
| 211 | + with clear UI/UX. Otherwise, two competing filters are required for "user |
| 212 | + has privileges granted through a team" and "user has privileges granted |
| 213 | + through ownership". |
| 214 | + """ |
| 215 | + |
| 216 | + ACCESS_OWNER = "owner" |
| 217 | + |
| 218 | + teams__slug = FilteredModelChoiceFilter( |
| 219 | + label=_("Team"), |
| 220 | + empty_label=_("All teams"), |
| 221 | + field_name="teams", |
| 222 | + to_field_name="slug", |
| 223 | + queryset_method="get_team_queryset", |
| 224 | + ) |
| 225 | + |
| 226 | + access = ChoiceFilter( |
| 227 | + label=_("Access"), |
| 228 | + empty_label=_("All access levels"), |
| 229 | + choices=ACCESS_LEVELS + ((ACCESS_OWNER, _("Owner")),), |
| 230 | + method="get_access", |
| 231 | + ) |
| 232 | + |
| 233 | + def get_access(self, queryset, field_name, value): |
| 234 | + # Note: the queryset here is effectively against the ``User`` model, and |
| 235 | + # is from Organization.members, a union of TeamMember.user and |
| 236 | + # Organization.owners. |
| 237 | + if value == self.ACCESS_OWNER: |
| 238 | + return queryset.filter(owner_organizations=self.organization) |
| 239 | + if value is not None: |
| 240 | + return queryset.filter(teams__access=value) |
| 241 | + return queryset |
0 commit comments