Skip to content

Commit

Permalink
Merge pull request #628 from UW-GAC/feature/add-search-to-study-list
Browse files Browse the repository at this point in the history
Add search to Study List
  • Loading branch information
amstilp authored Jul 1, 2024
2 parents 1e212d8 + 9db27d8 commit ddecf59
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
9 changes: 9 additions & 0 deletions primed/primed_anvil/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
from anvil_consortium_manager.models import Account
from django_filters import FilterSet

from .models import Study


class AccountListFilter(FilterSet):
class Meta:
model = Account
fields = {"email": ["icontains"], "user__name": ["icontains"]}
form = FilterForm


class StudyListFilter(FilterSet):
class Meta:
model = Study
fields = {"short_name": ["icontains"], "full_name": ["icontains"]}
form = FilterForm
62 changes: 61 additions & 1 deletion primed/primed_anvil/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from primed.primed_anvil.tests.factories import AvailableDataFactory, StudyFactory
from primed.users.tests.factories import UserFactory

from .. import models, tables, views
from .. import filters, models, tables, views
from . import factories

# from .utils import AnVILAPIMockTestMixin
Expand Down Expand Up @@ -724,6 +724,66 @@ def test_view_with_two_objects(self):
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 2)

def test_filterset_class(self):
self.model_factory.create()
request = self.factory.get(self.get_url())
request.user = self.user
response = self.get_view()(request)
self.assertIn("filter", response.context_data)
self.assertIsInstance(response.context_data["filter"], filters.StudyListFilter)

def test_view_with_filter_return_no_object(self):
self.model_factory.create(short_name="TEST", full_name="Test study")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"short_name__icontains": "abc"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 0)
response = self.client.get(self.get_url(), {"full_name__icontains": "abc"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 0)

def test_view_with_filter_returns_one_object_exact(self):
self.model_factory.create(short_name="TEST", full_name="Test Study")
self.model_factory.create(short_name="ABC", full_name="ABC Study")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"short_name__icontains": "TEST"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
response = self.client.get(self.get_url(), {"full_name__icontains": "Test Study"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)

def test_view_with_filter_returns_one_object_case_insensitive(self):
self.model_factory.create(short_name="TEST", full_name="TEST Study")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"short_name__icontains": "test"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
response = self.client.get(self.get_url(), {"full_name__icontains": "test study"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)

def test_view_with_filter_returns_one_object_contains(self):
self.model_factory.create(short_name="TEST", full_name="TEST Study")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"short_name__icontains": "TES"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)
response = self.client.get(self.get_url(), {"full_name__icontains": "TES"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 1)

def test_view_with_filter_returns_all_objects(self):
self.model_factory.create(short_name="Test 1", full_name="Test 1 Study")
self.model_factory.create(short_name="Test 2", full_name="Test 2 Study")
self.client.force_login(self.user)
response = self.client.get(self.get_url(), {"short_name__icontains": "Test"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 2)
response = self.client.get(self.get_url(), {"full_name__icontains": "Test"})
self.assertIn("table", response.context_data)
self.assertEqual(len(response.context_data["table"].rows), 2)


class StudySiteDetailTest(TestCase):
"""Tests for the StudySiteDetail view."""
Expand Down
9 changes: 7 additions & 2 deletions primed/primed_anvil/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.contrib.messages.views import SuccessMessageMixin
from django.db.models import Q
from django.views.generic import CreateView, DetailView, TemplateView
from django_filters.views import FilterView
from django_tables2 import MultiTableMixin, SingleTableMixin, SingleTableView

from primed.cdsa.models import DataAffiliateAgreement, MemberAgreement
Expand All @@ -34,7 +35,7 @@
)
from primed.users.tables import UserTable

from . import helpers, models, tables
from . import filters, helpers, models, tables

User = get_user_model()

Expand Down Expand Up @@ -77,11 +78,15 @@ def get_tables(self):
)


class StudyList(AnVILConsortiumManagerViewRequired, SingleTableView):
class StudyList(AnVILConsortiumManagerViewRequired, SingleTableView, FilterView, autocomplete.Select2QuerySetView):
"""View to show a list of `Study`s."""

model = models.Study
table_class = tables.StudyTable
template_name = "primed_anvil/study_list.html"

filterset_class = filters.StudyListFilter
queryset = models.Study.objects.order_by("short_name")


class StudyCreate(AnVILConsortiumManagerStaffEditRequired, SuccessMessageMixin, CreateView):
Expand Down
3 changes: 3 additions & 0 deletions primed/templates/primed_anvil/study_list.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{% extends "anvil_consortium_manager/base.html" %}
{% load render_table from django_tables2 %}
{% load crispy_forms_tags %}

{% block title %}Study List{% endblock %}

{% block content %}
<h1>Studies</h1>

{% crispy filter.form %}

{% render_table table %}

{% endblock content %}

0 comments on commit ddecf59

Please sign in to comment.