Skip to content

Commit bafce58

Browse files
committed
Don't titlise explicitly specified verbose_name's
1 parent 7853b24 commit bafce58

File tree

5 files changed

+17
-33
lines changed

5 files changed

+17
-33
lines changed

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ globally, use::
6868
Change log
6969
==========
7070

71+
- `BoundColumn.verbose_name` now titlises only if no verbose_name was given.
72+
``verbose_name`` is used verbatim.
73+
7174
v0.15.0
7275
-------
7376

django_tables2/columns/base.py

+5-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import absolute_import, unicode_literals
33
from django.db.models.fields import FieldDoesNotExist
44
from django.utils.datastructures import SortedDict
5-
from django.utils.safestring import SafeData
65
from django_tables2.templatetags.django_tables2 import title
76
from django_tables2.utils import A, AttributeDict, OrderBy, OrderByTuple
87
from itertools import islice
@@ -116,9 +115,6 @@ class Column(object): # pylint: disable=R0902
116115
117116
:type: `unicode`
118117
119-
This should not defined in title case, but rather natural case. It is
120-
converted to title case for use in column headers.
121-
122118
123119
.. attribute:: visible
124120
@@ -188,9 +184,7 @@ def header(self):
188184
"""
189185
The value used for the column heading (e.g. inside the ``<th>`` tag).
190186
191-
By default this titlises the `~.Column.verbose_name`. If
192-
`~.Column.verbose_name` is an instance of `~.safestring.SafeData`, it's
193-
used unmodified.
187+
By default this returns `~.Column.verbose_name`.
194188
195189
:returns: `unicode` or `None`
196190
@@ -203,12 +197,7 @@ def header(self):
203197
accessing that first) when this property doesn't return something
204198
useful.
205199
"""
206-
if self.verbose_name:
207-
if isinstance(self.verbose_name, SafeData):
208-
# If the author has used mark_safe, we're going to assume the
209-
# author wants the value used verbatim.
210-
return self.verbose_name
211-
return title(self.verbose_name)
200+
return self.verbose_name
212201

213202
def render(self, value):
214203
"""
@@ -350,13 +339,7 @@ def header(self):
350339
if column_header:
351340
return column_header
352341
# fall back to automatic best guess
353-
verbose_name = self.verbose_name # avoid calculating multiple times
354-
if isinstance(verbose_name, SafeData):
355-
# If the verbose_name has come from a model field, it's possible
356-
# that the author used mark_safe to include HTML in the value. If
357-
# this is the case, we leave it verbatim.
358-
return verbose_name
359-
return title(verbose_name)
342+
return self.verbose_name
360343

361344
@property
362345
def order_by(self):
@@ -453,25 +436,22 @@ def orderable(self):
453436
@property
454437
def verbose_name(self):
455438
"""
456-
Return the verbose name for this column, or fallback to prettified
439+
Return the verbose name for this column, or fallback to the titlised
457440
column name.
458441
459442
If the table is using queryset data, then use the corresponding model
460443
field's `~.db.Field.verbose_name`. If it's traversing a relationship,
461444
then get the last field in the accessor (i.e. stop when the
462445
relationship turns from ORM relationships to object attributes [e.g.
463446
person.upper should stop at person]).
464-
465-
If the model field's `~.db.Field.verbose_name` is a
466-
`~.safestring.SafeData`, it's used unmodified.
467447
"""
468448
# Favor an explicit defined verbose_name
469449
if self.column.verbose_name:
470450
return self.column.verbose_name
471451

472452
# This is our reasonable fallback, should the next section not result
473453
# in anything useful.
474-
name = self.name.replace('_', ' ')
454+
name = title(self.name.replace('_', ' '))
475455

476456
# Try to use a tmodel field's verbose_name
477457
if hasattr(self.table.data, 'queryset'):

docs/index.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ Customising column headings
280280
===========================
281281

282282
The header cell for each column comes from `~.Column.header`. By default this
283-
method returns a titlised version of the `~.Column.verbose_name`.
283+
method returns `~.Column.verbose_name`, falling back to the titlised attribute
284+
name of the column in the table class.
284285

285286
When using queryset data and a verbose name hasn't been explicitly
286287
defined for a column, the corresponding model field's verbose name will be

tests/columns.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ class TestTable(tables.Table):
134134

135135

136136
@general.test
137-
def column_header_should_use_titlised_verbose_name():
137+
def column_header_should_use_titlised_verbose_name_unless_given_explicitly():
138138
class SimpleTable(tables.Table):
139139
basic = tables.Column()
140140
acronym = tables.Column(verbose_name="has FBI help")
141141

142142
table = SimpleTable([])
143143
assert table.columns["basic"].header == "Basic"
144-
assert table.columns["acronym"].header == "Has FBI Help"
144+
assert table.columns["acronym"].header == "has FBI help"
145145

146146

147147
@general.test

tests/templates.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ def attrs(xml):
3939
class CountryTable(tables.Table):
4040
name = tables.Column()
4141
capital = tables.Column(orderable=False,
42-
verbose_name=ugettext_lazy("capital"))
43-
population = tables.Column(verbose_name='population size')
42+
verbose_name=ugettext_lazy("Capital"))
43+
population = tables.Column(verbose_name='Population Size')
4444
currency = tables.Column(visible=False)
45-
tld = tables.Column(visible=False, verbose_name='domain')
45+
tld = tables.Column(visible=False, verbose_name='Domain')
4646
calling_code = tables.Column(accessor='cc',
47-
verbose_name='phone ext.')
47+
verbose_name='Phone Ext.')
4848

4949

5050
MEMORY_DATA = [
@@ -182,7 +182,7 @@ def render_table_supports_queryset():
182182
'request': build_request('/')}))
183183

184184
root = parse(html)
185-
assert [e.text for e in root.findall('.//thead/tr/th/a')] == ["ID", "Name", "Mayor"]
185+
assert [e.text for e in root.findall('.//thead/tr/th/a')] == ["ID", "name", "mayor"]
186186
td = [[td.text for td in tr.findall('td')] for tr in root.findall('.//tbody/tr')]
187187
db = []
188188
for region in Region.objects.all():

0 commit comments

Comments
 (0)