Skip to content

Commit

Permalink
Use a TemplateColumn to render action buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
amstilp committed Feb 26, 2024
1 parent a108f9d commit 0c6eea3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
12 changes: 3 additions & 9 deletions primed/collaborative_analysis/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
ManagedGroup,
)
from django.urls import reverse
from django.utils.safestring import mark_safe

from primed.primed_anvil.tables import BooleanIconColumn

Expand Down Expand Up @@ -98,18 +97,13 @@ class AccessAuditResultsTable(tables.Table):
member = tables.Column(linkify=True)
has_access = BooleanIconColumn(show_false_icon=True)
note = tables.Column()
action = tables.Column()
action = tables.TemplateColumn(
template_name="snippets/collaborativeanalysis_audit_action_button.html"
)

class Meta:
attrs = {"class": "table align-middle"}

def render_action(self, record, value):
return mark_safe(
"""<a href="{}" class="btn btn-primary btn-sm">{}</a>""".format(
record["action_url"], value
)
)


class CollaborativeAnalysisWorkspaceAccessAudit:
"""Class to audit access to a CollaborativeAnalysisWorkspace."""
Expand Down
20 changes: 0 additions & 20 deletions primed/collaborative_analysis/tests/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,23 +1155,3 @@ def test_two_rows(self):
table = audit.AccessAuditResultsTable(data)
self.assertIsInstance(table, audit.AccessAuditResultsTable)
self.assertEqual(len(table.rows), 2)

def test_render_action(self):
"""Render action works as expected for grant access types."""
workspace = factories.CollaborativeAnalysisWorkspaceFactory.create()
member_group = ManagedGroupFactory.create()
data = [
{
"workspace": workspace,
"member": member_group,
"note": "a note",
"action": "Grant",
"action_url": "foo",
}
]

table = audit.AccessAuditResultsTable(data)
self.assertIsInstance(table, audit.AccessAuditResultsTable)
self.assertEqual(len(table.rows), 1)
self.assertIn("foo", table.rows[0].get_cell("action"))
self.assertIn("Grant", table.rows[0].get_cell("action"))
8 changes: 4 additions & 4 deletions primed/collaborative_analysis/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def test_context_verified_table_access(self):
table.rows[0].get_cell_value("note"),
audit.CollaborativeAnalysisWorkspaceAccessAudit.IN_SOURCE_AUTH_DOMAINS,
)
self.assertIsNone(table.rows[0].get_cell_value("action"))
self.assertEqual(table.rows[0].get_cell_value("action"), "&mdash;")

def test_context_verified_table_no_access(self):
"""verified_table shows a record when audit has one account with verified no access."""
Expand Down Expand Up @@ -601,7 +601,7 @@ def test_context_verified_table_no_access(self):
table.rows[0].get_cell_value("note"),
audit.CollaborativeAnalysisWorkspaceAccessAudit.NOT_IN_SOURCE_AUTH_DOMAINS,
)
self.assertIsNone(table.rows[0].get_cell_value("action"))
self.assertEqual(table.rows[0].get_cell_value("action"), "&mdash;")

def test_context_needs_action_table_grant(self):
"""needs_action_table shows a record when audit finds that access needs to be granted."""
Expand Down Expand Up @@ -872,7 +872,7 @@ def test_context_verified_table_access(self):
table.rows[0].get_cell_value("note"),
audit.CollaborativeAnalysisWorkspaceAccessAudit.IN_SOURCE_AUTH_DOMAINS,
)
self.assertIsNone(table.rows[0].get_cell_value("action"))
self.assertEqual(table.rows[0].get_cell_value("action"), "&mdash;")

def test_context_verified_table_no_access(self):
"""verified_table shows a record when audit has one account with verified no access."""
Expand Down Expand Up @@ -914,7 +914,7 @@ def test_context_verified_table_no_access(self):
table.rows[0].get_cell_value("note"),
audit.CollaborativeAnalysisWorkspaceAccessAudit.NOT_IN_SOURCE_AUTH_DOMAINS,
)
self.assertIsNone(table.rows[0].get_cell_value("action"))
self.assertEqual(table.rows[0].get_cell_value("action"), "&mdash;")

def test_context_needs_action_table_grant(self):
"""needs_action_table shows a record when audit finds that access needs to be granted."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div>
{% if record.action %}
<form
method="post"
action="{% url 'collaborative_analysis:audit:resolve' record.workspace.workspace.billing_project.name record.workspace.workspace.name record.member.email %}">

{% csrf_token %}
<button
type="submit"
class="btn btn-primary btn-sm"
hx-post="{% url 'collaborative_analysis:audit:resolve' record.workspace.workspace.billing_project.name record.workspace.workspace.name record.member.email %}"
hx-disabled-elt="this"
hx-target="closest div"
hx-swap="innerHTML">
{{ record.action }}
</button>
</form>

{% else %}
&mdash;
{% endif %}
</div>

0 comments on commit 0c6eea3

Please sign in to comment.