Skip to content

Commit

Permalink
Add filtering to IgnoredManagedGroupMembership list view
Browse files Browse the repository at this point in the history
  • Loading branch information
amstilp committed Dec 19, 2024
1 parent c190299 commit 2e0ce85
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 3 deletions.
12 changes: 12 additions & 0 deletions anvil_consortium_manager/auditor/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django_filters import FilterSet

from anvil_consortium_manager.forms import FilterForm

from . import models


class IgnoredManagedGroupMembershipFilter(FilterSet):
class Meta:
model = models.IgnoredManagedGroupMembership
fields = {"group__name": ["icontains"], "ignored_email": ["icontains"]}
form = FilterForm
113 changes: 113 additions & 0 deletions anvil_consortium_manager/auditor/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,119 @@ def test_view_with_two_objects(self):
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 2)

def test_view_with_filter_group_name_return_no_object(self):
factories.IgnoredManagedGroupMembershipFactory.create(group__name="foo")
factories.IgnoredManagedGroupMembershipFactory.create(group__name="bar")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"group__name__icontains": "abc"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 0)

def test_view_with_filter_group_name_returns_one_object_exact(self):
instance = factories.IgnoredManagedGroupMembershipFactory.create(group__name="foo")
factories.IgnoredManagedGroupMembershipFactory.create(group__name="bar")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"group__name__icontains": "foo"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
self.assertIn(instance, response.context_data["table"].data)

def test_view_with_filter_group_name_returns_one_object_case_insensitive(self):
instance = factories.IgnoredManagedGroupMembershipFactory.create(group__name="Foo")
factories.IgnoredManagedGroupMembershipFactory.create(group__name="bar")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"group__name__icontains": "foo"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
self.assertIn(instance, response.context_data["table"].data)

def test_view_with_filter_group_name_returns_one_object_case_contains(self):
instance = factories.IgnoredManagedGroupMembershipFactory.create(group__name="foo")
factories.IgnoredManagedGroupMembershipFactory.create(group__name="bar")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"group__name__icontains": "oo"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
self.assertIn(instance, response.context_data["table"].data)

def test_view_with_filter_group_name_returns_multiple_objects(self):
instance_1 = factories.IgnoredManagedGroupMembershipFactory.create(group__name="group1")
instance_2 = factories.IgnoredManagedGroupMembershipFactory.create(group__name="group2")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"group__name__icontains": "group"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 2)
self.assertIn(instance_1, response.context_data["table"].data)
self.assertIn(instance_2, response.context_data["table"].data)

def test_view_with_filter_email_return_no_object(self):
factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="[email protected]")
factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="bar")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"ignored_email__icontains": "abc"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 0)

def test_view_with_filter_email_returns_one_object_exact(self):
instance = factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="[email protected]")
factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="[email protected]")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"ignored_email__icontains": "[email protected]"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
self.assertIn(instance, response.context_data["table"].data)

def test_view_with_filter_email_returns_one_object_case_insensitive(self):
instance = factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="[email protected]")
factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="[email protected]")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"ignored_email__icontains": "[email protected]"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
self.assertIn(instance, response.context_data["table"].data)

def test_view_with_filter_email_returns_one_object_case_contains(self):
instance = factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="[email protected]")
factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="[email protected]")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"ignored_email__icontains": "oo"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
self.assertIn(instance, response.context_data["table"].data)

def test_view_with_filter_email_returns_multiple_objects(self):
instance_1 = factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="[email protected]")
instance_2 = factories.IgnoredManagedGroupMembershipFactory.create(ignored_email="[email protected]")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"ignored_email__icontains": "foo"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 2)
self.assertIn(instance_1, response.context_data["table"].data)
self.assertIn(instance_2, response.context_data["table"].data)

def test_view_with_filter_group_name_and_email(self):
factories.IgnoredManagedGroupMembershipFactory.create(group__name="abc", ignored_email="[email protected]")
instance = factories.IgnoredManagedGroupMembershipFactory.create(
group__name="def", ignored_email="[email protected]"
)
factories.IgnoredManagedGroupMembershipFactory.create(group__name="def", ignored_email="[email protected]")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"group__name__icontains": "def", "ignored_email__icontains": "foo"})
self.assertEqual(response.status_code, 200)
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
self.assertIn(instance, response.context_data["table"].data)


class IgnoredManagedGroupMembershipUpdateTest(TestCase):
def setUp(self):
Expand Down
9 changes: 6 additions & 3 deletions anvil_consortium_manager/auditor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, DeleteView, DetailView, TemplateView, UpdateView
from django.views.generic.detail import SingleObjectMixin
from django_tables2.views import SingleTableView
from django_filters.views import FilterView
from django_tables2.views import SingleTableMixin

from anvil_consortium_manager import auth
from anvil_consortium_manager.models import ManagedGroup, Workspace

from . import forms, models, tables, viewmixins
from . import filters, forms, models, tables, viewmixins
from .audit import accounts as account_audit
from .audit import billing_projects as billing_project_audit
from .audit import managed_groups as managed_group_audit
Expand Down Expand Up @@ -158,11 +159,13 @@ def form_valid(self, form):
return super().form_valid(form)


class IgnoredManagedGroupMembershipList(auth.AnVILConsortiumManagerStaffViewRequired, SingleTableView):
class IgnoredManagedGroupMembershipList(auth.AnVILConsortiumManagerStaffViewRequired, SingleTableMixin, FilterView):
"""View to display a list of models.IgnoredManagedGroupMembership."""

model = models.IgnoredManagedGroupMembership
table_class = tables.IgnoredManagedGroupMembershipTable
template_name = "auditor/ignoredmanagedgroupmembership_list.html"
filterset_class = filters.IgnoredManagedGroupMembershipFilter


class IgnoredManagedGroupMembershipUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load static %}

{% load render_table from django_tables2 %}
{% load crispy_forms_tags %}

{% block title %}Ignored Managed Group membership audit records{% endblock %}

Expand All @@ -16,6 +17,11 @@ <h2>Ignored Managed Group membership audit records</h2>
<p>
The following records will be ignored when running AnVIL audits for group membership.
</p>

<div class="container pt-3">
{% crispy filter.form %}
</div>

{% render_table table %}

</div>
Expand Down

0 comments on commit 2e0ce85

Please sign in to comment.