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

Added translation feature to sort title : _("some title") #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ There are really 5 steps to setting it up with your projects.
your objects_list:

<tr>
<th>{% anchor first_name Name %}</th>
<th>{% anchor creation_date Creation %}</th>
<th>{% anchor "first_name" "Name" %}</th>
<th>{% anchor "creation_date" _("Creation") %}</th>
...
</tr>

The first argument is a field of the objects list, and the second
one(optional) is a title that would be displayed. The previous
one(optional) is a title that would be displayed, you can use
django translation syntax _("") for the second argument. The previous
snippet will be rendered like this:

<tr>
Expand Down
20 changes: 11 additions & 9 deletions django_sorting/templatetags/sorting_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def anchor(parser, token):
"""
Parses a tag that's supposed to be in this format: {% anchor field title %}
"""
bits = [b.strip('"\'') for b in token.split_contents()]
bits = token.split_contents()
if len(bits) < 2:
raise TemplateSyntaxError, "anchor tag takes at least 1 argument"
try:
Expand All @@ -42,10 +42,12 @@ class SortAnchorNode(template.Node):

"""
def __init__(self, field, title):
self.field = field
self.title = title
self.field = template.Variable(field)
self.title = template.Variable(title)

def render(self, context):
field = self.field.resolve(context)
title = self.title.resolve(context)
request = context['request']
getvars = request.GET.copy()
if 'sort' in getvars:
Expand All @@ -58,7 +60,7 @@ def render(self, context):
del getvars['dir']
else:
sortdir = ''
if sortby == self.field:
if sortby == field:
getvars['dir'] = sort_directions[sortdir]['inverse']
icon = sort_directions[sortdir]['icon']
else:
Expand All @@ -68,16 +70,16 @@ def render(self, context):
else:
urlappend = ''
if icon:
title = "%s %s" % (self.title, icon)
title_icon = u"%s %s" % (title, icon)
else:
title = self.title
title_icon = title

url = '%s?sort=%s%s' % (request.path, self.field, urlappend)
return '<a href="%s" title="%s">%s</a>' % (url, self.title, title)
url = '%s?sort=%s%s' % (request.path, field, urlappend)
return u'<a href="%s" title="%s">%s</a>' % (url, title, title_icon)


def autosort(parser, token):
bits = [b.strip('"\'') for b in token.split_contents()]
bits = token.split_contents()
if len(bits) != 2:
raise TemplateSyntaxError, "autosort tag takes exactly one argument"
return SortedDataNode(bits[1])
Expand Down