Skip to content

Commit

Permalink
Add a custom AccountListFilter with linked user's name
Browse files Browse the repository at this point in the history
  • Loading branch information
amstilp committed Oct 27, 2023
1 parent a28241e commit c9fdefc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
5 changes: 2 additions & 3 deletions gregor_django/gregor_anvil/adapters.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from anvil_consortium_manager.adapters.account import BaseAccountAdapter
from anvil_consortium_manager.adapters.workspace import BaseWorkspaceAdapter
from anvil_consortium_manager.filters import AccountListFilter
from anvil_consortium_manager.forms import WorkspaceForm
from django.db.models import Q

from . import forms, models, tables
from . import filters, forms, models, tables


class AccountAdapter(BaseAccountAdapter):
"""Custom account adapter for PRIMED."""

list_table_class = tables.AccountTable
list_filterset_class = AccountListFilter
list_filterset_class = filters.AccountListFilter

def get_autocomplete_queryset(self, queryset, q):
"""Filter to Accounts where the email or the associated user name matches the query `q`."""
Expand Down
10 changes: 10 additions & 0 deletions gregor_django/gregor_anvil/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from anvil_consortium_manager.forms import FilterForm
from anvil_consortium_manager.models import Account
from django_filters import FilterSet


class AccountListFilter(FilterSet):
class Meta:
model = Account
fields = {"email": ["icontains"], "user__name": ["icontains"]}
form = FilterForm
28 changes: 28 additions & 0 deletions gregor_django/gregor_anvil/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,34 @@ def test_view_with_two_objects(self):
self.assertEqual(len(response.context_data["table"].rows), 2)


class AccountListTest(TestCase):
def setUp(self):
"""Set up test class."""
self.factory = RequestFactory()
# Create a user with both view and edit permission.
self.user = User.objects.create_user(username="test", password="test")
self.user.user_permissions.add(
Permission.objects.get(
codename=acm_models.AnVILProjectManagerAccess.VIEW_PERMISSION_CODENAME
)
)

def test_filter_by_name(self):
"""Filtering by name works as expected."""
user = UserFactory.create(name="First Last")
account = acm_factories.AccountFactory.create(user=user)
other_account = acm_factories.AccountFactory.create(verified=True)
self.client.force_login(self.user)
response = self.client.get(
reverse("anvil_consortium_manager:accounts:list"),
{"user__name__icontains": "First"},
)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
self.assertIn(account, response.context_data["table"].data)
self.assertNotIn(other_account, response.context_data["table"].data)


class UploadWorkspaceDetailTest(TestCase):
"""Tests of the anvil_consortium_manager WorkspaceDetail view using the UploadWorkspace adapter."""

Expand Down

0 comments on commit c9fdefc

Please sign in to comment.