Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'title' template filter #443

Merged
merged 2 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions django_tables2/templatetags/django_tables2.py
Expand Up @@ -195,9 +195,6 @@ class Meta:
return RenderTableNode(table, template)


RE_UPPERCASE = re.compile('[A-Z]')


@register.filter
@stringfilter
def title(value):
Expand All @@ -206,16 +203,25 @@ def title(value):

Same as Django's builtin `~django.template.defaultfilters.title` filter,
but operates on individual words and leaves words unchanged if they already
have a capital letter.
have a capital letter or a digit. Actually Django's filter also skips
words with digits but only for latin letters (or at least not for
cyrillic ones).
'''

def title_word(w):
return w if RE_UPPERCASE.search(w) else old_title(w)

return re.sub('(\S+)', lambda m: title_word(m.group(0)), value)
return ' '.join([
any([c.isupper() or c.isdigit() for c in w]) and w or old_title(w)
for w in value.split()
])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on the performance implementation vs the old one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 2.7.6, django 1.10.7

image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!
I guess the lazyness will add a bit of overhead too, but since django is littered with lazy evaluation, it should not be too much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with keep_lazy_text decorator

image



title.is_safe = True

try:
from django.utils.functional import keep_lazy_text
title = keep_lazy_text(title)
except ImportError:
# to keep backward (Django < 1.10) compatibility
from django.utils.functional import lazy
title = lazy(title, six.text_type)

register.filter('localize', l10n_register.filters['localize'])
register.filter('unlocalize', l10n_register.filters['unlocalize'])
Binary file added tests/app/locale/ua/LC_MESSAGES/django.mo
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/app/locale/ua/LC_MESSAGES/django.po
@@ -0,0 +1,11 @@
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"

msgid "translation test"
msgstr "тест перекладу"

msgid "translation test lazy"
msgstr "тест ленивого перекладу"
15 changes: 14 additions & 1 deletion tests/test_models.py
@@ -1,9 +1,11 @@
# coding: utf-8
from __future__ import unicode_literals

import django_tables2 as tables
import pytest
from django.db.models.functions import Length
from django.utils import six
from django_tables2 import RequestConfig
from django.utils.translation import override as translation_override

from .app.models import Occupation, Person, PersonProxy
from .utils import assertNumQueries, build_request
Expand Down Expand Up @@ -155,6 +157,17 @@ class Meta:
assert 'Birthdate' == table.columns['birthdate'].verbose_name
assert 'OVERRIDE' == table.columns['first_name'].verbose_name

# Verbose name should be lazy if it comes from the model field and
# the column was not declared explicitly
class PersonTable(tables.Table):
class Meta:
model = Person

table = PersonTable(Person.objects.all())
assert type(table.columns['trans_test_lazy'].verbose_name) is not six.text_type
with translation_override('ua'):
assert 'Тест Ленивого Перекладу' == table.columns['trans_test_lazy'].verbose_name


def test_data_verbose_name():
table = tables.Table(Person.objects.all())
Expand Down
8 changes: 8 additions & 0 deletions tests/test_templatetags.py
Expand Up @@ -171,6 +171,14 @@ def test_title_should_only_apply_to_words_without_uppercase_letters():
'black FBI': 'Black FBI',
'f.b.i': 'F.B.I',
'start 6pm': 'Start 6pm',

# Some cyrillic samples
'руда лисиця': 'Руда Лисиця',
'руда лисицЯ': 'Руда лисицЯ',
'діяльність СБУ': 'Діяльність СБУ',
'а.б.в': 'А.Б.В',
'вага 6кг': 'Вага 6кг',
'у 80-их роках': 'У 80-их Роках',
}

for raw, expected in expectations.items():
Expand Down