Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add an optional link_classes to tables.Column, this is used as the cl…

…ass of the displayed link in the cell.

Fixes bug 1079464.

Change-Id: Ibaf58c7f47d1cc8ea8f2ca074bf6c21142df5579
  • Loading branch information...
commit fd6bc63e5bcf02c91801af112808d69c2f5c0cb1 1 parent 8734ea4
Lin Hua Cheng authored
Showing with 18 additions and 5 deletions.
  1. +12 −2 horizon/tables/base.py
  2. +6 −3 horizon/test/tests/tables.py
View
14 horizon/tables/base.py
@@ -160,6 +160,13 @@ class Column(html.HTMLElement):
this column will be truncated and an ellipsis will be appended to the
truncated data.
Defaults to ``None``.
+
+ .. attribute:: link_classes
+
+ An iterable of CSS classes which will be added when the column's text
+ is displayed as a link.
+ Example: ``classes=('link-foo', 'link-bar')``.
+ Defaults to ``None``.
"""
summation_methods = {
"sum": sum,
@@ -191,7 +198,7 @@ def __init__(self, transform, verbose_name=None, sortable=True,
link=None, allowed_data_types=[], hidden=False, attrs=None,
status=False, status_choices=None, display_choices=None,
empty_value=None, filters=None, classes=None, summation=None,
- auto=None, truncate=None):
+ auto=None, truncate=None, link_classes=None):
self.classes = list(classes or getattr(self, "classes", []))
super(Column, self).__init__()
self.attrs.update(attrs or {})
@@ -219,6 +226,7 @@ def __init__(self, transform, verbose_name=None, sortable=True,
self.empty_value = empty_value or '-'
self.filters = filters or []
self.truncate = truncate
+ self.link_classes = link_classes or []
if status_choices:
self.status_choices = status_choices
@@ -551,8 +559,10 @@ def value(self):
exc_info = sys.exc_info()
raise template.TemplateSyntaxError, exc_info[1], exc_info[2]
if self.url:
+ link_classes = ' '.join(self.column.link_classes)
# Escape the data inside while allowing our HTML to render
- data = mark_safe('<a href="%s">%s</a>' % (self.url, escape(data)))
+ data = mark_safe('<a href="%s" class="%s">%s</a>' %
+ (self.url, link_classes, escape(data)))
return data
@property
View
9 horizon/test/tests/tables.py
@@ -161,7 +161,8 @@ class MyTable(tables.DataTable):
link='http://example.com/',
attrs={'class': 'green blue'},
summation="average",
- truncate=35)
+ truncate=35,
+ link_classes=('link-modal',))
status = tables.Column('status', link=get_link)
optional = tables.Column('optional', empty_value='N/A')
excluded = tables.Column('excluded')
@@ -352,8 +353,9 @@ def test_table_column(self):
self.assertIn("hide", id_col.get_final_attrs().get('class', ""))
self.assertEqual(name_col.hidden, False)
self.assertNotIn("hide", name_col.get_final_attrs().get('class', ""))
- # link and get_link_url
+ # link, link_classes and get_link_url
self.assertIn('href="http://example.com/"', row.cells['value'].value)
+ self.assertIn('class="link-modal"', row.cells['value'].value)
self.assertIn('href="/auth/login/"', row.cells['status'].value)
# empty_value
self.assertEqual(row3.cells['optional'].value, "N/A")
@@ -434,7 +436,8 @@ def test_table_rendering(self):
self.assertContains(resp, update_string, 3)
self.assertContains(resp, "data-update-interval", 3)
# Verify our XSS protection
- self.assertContains(resp, '<a href="http://example.com/">'
+ self.assertContains(resp, '<a href="http://example.com/" '
+ 'class="link-modal">'
'&lt;strong&gt;evil&lt;/strong&gt;</a>', 1)
# Filter = False hides the search box
self.table._meta.filter = False

0 comments on commit fd6bc63

Please sign in to comment.
Something went wrong with that request. Please try again.