Skip to content

Commit

Permalink
Choose user table depending on whether member_group is set
Browse files Browse the repository at this point in the history
If member_group is set, use the UserAccountSingleGroupMembershipTable;
otherwise, use a newly-defined table (UserAccountTable). This lets
CC staff see who from the site has a) linked their account and b)
been added to the member group (if it is set).
  • Loading branch information
amstilp committed Jul 10, 2024
1 parent 5e1b03e commit bcf962c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
16 changes: 12 additions & 4 deletions primed/primed_anvil/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, extra_columns=extra_columns, **kwargs)


class UserAccountSingleGroupMembershipTable(tables.Table):
"""A table with users and info about whether they are members of a group."""
class UserAccountTable(tables.Table):
"""A table for `User`s with `Account` information."""

name = tables.Column(linkify=True)
account = tables.Column(linkify=True, verbose_name="AnVIL account")

class Meta:
model = User
Expand All @@ -167,8 +170,13 @@ class Meta:
)
order_by = ("name",)

name = tables.Column(linkify=lambda record: record.get_absolute_url())
account = tables.Column(verbose_name="AnVIL account")

class UserAccountSingleGroupMembershipTable(UserAccountTable):
"""A table with users and info about whether they are members of a group."""

class Meta(UserAccountTable.Meta):
pass

is_group_member = tables.BooleanColumn(verbose_name="Has access?", default=False)

def __init__(self, *args, managed_group=None, **kwargs):
Expand Down
29 changes: 29 additions & 0 deletions primed/primed_anvil/tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,35 @@ def test_render_not_workspace(self):
column.render(None, "foo", None)


class UserAccountTableTest(TestCase):
"""Tests for the UserAccountTable class."""

def setUp(self):
self.managed_group = ManagedGroupFactory.create()

def test_row_count_with_no_objects(self):
table = tables.UserAccountTable(User.objects.all())
self.assertEqual(len(table.rows), 0)

def test_row_count_with_one_object(self):
UserFactory.create()
table = tables.UserAccountTable(User.objects.all())
self.assertEqual(len(table.rows), 1)

def test_row_count_with_two_objects(self):
UserFactory.create_batch(2)
table = tables.UserAccountTable(User.objects.all())
self.assertEqual(len(table.rows), 2)

def test_ordering(self):
"""Users are ordered alphabetically by name"""
user_foo = UserFactory.create(name="Foo")
user_bar = UserFactory.create(name="Bar")
table = tables.UserAccountTable(User.objects.all())
self.assertEqual(table.data[0], user_bar)
self.assertEqual(table.data[1], user_foo)


class UserAccountSingleGroupMembershipTableTest(TestCase):
"""Tests for the UserAccountSingleGroupMembershipTable class."""

Expand Down
13 changes: 11 additions & 2 deletions primed/primed_anvil/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
)
from primed.miscellaneous_workspaces.tests.factories import OpenAccessWorkspaceFactory
from primed.primed_anvil.tests.factories import AvailableDataFactory, StudyFactory
from primed.users.tables import UserTable
from primed.users.tests.factories import UserFactory

from .. import filters, models, tables, views
Expand Down Expand Up @@ -852,7 +851,7 @@ def test_table_classes(self):
response = self.client.get(self.get_url(obj.pk))
self.assertIn("tables", response.context_data)
self.assertEqual(len(response.context_data["tables"]), 4)
self.assertIsInstance(response.context_data["tables"][0], UserTable)
self.assertIsInstance(response.context_data["tables"][0], tables.UserAccountTable)
self.assertEqual(len(response.context_data["tables"][0].data), 0)
self.assertIsInstance(response.context_data["tables"][1], dbGaPApplicationTable)
self.assertEqual(len(response.context_data["tables"][1].data), 0)
Expand Down Expand Up @@ -901,6 +900,16 @@ def test_cdsa_table(self):
self.assertIn(site_cdsa, table.data)
self.assertNotIn(other_cdsa, table.data)

def test_site_user_table_when_member_group_is_set(self):
"""The site user table is the correct class when the member group is set."""
member_group = ManagedGroupFactory.create()
obj = self.model_factory.create(member_group=member_group)
self.client.force_login(self.user)
response = self.client.get(self.get_url(obj.pk))
table = response.context_data["tables"][0]
self.assertIsInstance(table, tables.UserAccountSingleGroupMembershipTable)
self.assertEqual(table.managed_group, member_group)

def test_member_group_table(self):
member_group = ManagedGroupFactory.create()
obj = self.model_factory.create(member_group=member_group)
Expand Down
9 changes: 6 additions & 3 deletions primed/primed_anvil/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
OpenAccessWorkspaceStaffTable,
OpenAccessWorkspaceUserTable,
)
from primed.users.tables import UserTable

from . import filters, helpers, models, tables

Expand Down Expand Up @@ -127,17 +126,21 @@ class StudySiteDetail(AnVILConsortiumManagerStaffViewRequired, MultiTableMixin,

def get_tables(self):
user_qs = User.objects.filter(study_sites=self.object)
if self.object.member_group:
user_table = tables.UserAccountSingleGroupMembershipTable(user_qs, managed_group=self.object.member_group)
else:
user_table = tables.UserAccountTable(user_qs, exclude="study_sites")
dbgap_qs = dbGaPApplication.objects.filter(principal_investigator__study_sites=self.object)
cdsa_qs = MemberAgreement.objects.filter(study_site=self.object)
if self.object.member_group:
account_qs = Account.objects.filter(groupaccountmembership__group=self.object.member_group)
else:
account_qs = Account.objects.none()
return [
UserTable(user_qs),
user_table,
dbGaPApplicationTable(dbgap_qs),
MemberAgreementTable(cdsa_qs),
tables.AccountTable(account_qs, exclude=("number_groups",)),
tables.AccountTable(account_qs, exclude=("number_groups")),
]


Expand Down

0 comments on commit bcf962c

Please sign in to comment.