Skip to content

Feature/translatable #10

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

Open
wants to merge 1 commit 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
10 changes: 5 additions & 5 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ 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>{% sort_anchor "first_name" "Name" %}</th>
<th>{% sort_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
snippet will be rendered like this:
one(optional) is a title that would be displayed. Both arguments can be
be context variables. The previous snippet will be rendered like this:

<tr>
<th><a href="/path/to/your/view/?sort=first_name" title="Name">Name</a></th>
<th><a href="/path/to/your/view/?sort=creation_date" title="Name">Creation</a></th>
<th><a href="/path/to/your/view/?sort=creation_date" title="Name">Erstellungsdatum</a></th>
...
</tr>

Expand Down
71 changes: 69 additions & 2 deletions django_sorting/templatetags/sorting_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

def anchor(parser, token):
"""
depreciated: will be removed in a later version

Parses a tag that's supposed to be in this format: {% anchor field title %}
"""
bits = [b.strip('"\'') for b in token.split_contents()]
Expand All @@ -26,11 +28,13 @@ def anchor(parser, token):
title = bits[2]
except IndexError:
title = bits[1].capitalize()
return SortAnchorNode(bits[1].strip(), title.strip())
return OldSortAnchorNode(bits[1].strip(), title.strip())


class SortAnchorNode(template.Node):
class OldSortAnchorNode(template.Node):
"""
depreciated: will be removed in a later version

Renders an <a> HTML tag with a link which href attribute
includes the field on which we sort and the direction.
and adds an up or down arrow if the field is the one
Expand Down Expand Up @@ -76,6 +80,68 @@ def render(self, context):
return '<a href="%s" title="%s">%s</a>' % (url, self.title, title)


def sort_anchor(parser, token):
"""
Parses a tag that's supposed to be in this format: {% sort_anchor "field" "title" %}
"""
bits = token.split_contents()
if len(bits) < 2:
raise TemplateSyntaxError, "anchor tag takes at least 1 argument"
try:
title = bits[2]
except IndexError:
title = bits[1].capitalize()
return SortAnchorNode(bits[1].strip(), title.strip())

class SortAnchorNode(template.Node):
"""
Renders an <a> HTML tag with a link which href attribute
includes the field on which we sort and the direction.
and adds an up or down arrow if the field is the one
currently being sorted on.

Eg.
{% anchor "name" "Name" %} generates
<a href="/the/current/path/?sort=name" title="Name">Name</a>

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

def render(self, context):
request = context['request']
getvars = request.GET.copy()
field = self.field.resolve(context)
title = self.title.resolve(context)
if 'sort' in getvars:
sortby = getvars['sort']
del getvars['sort']
else:
sortby = ''
if 'dir' in getvars:
sortdir = getvars['dir']
del getvars['dir']
else:
sortdir = ''
if sortby == field:
getvars['dir'] = sort_directions[sortdir]['inverse']
icon = sort_directions[sortdir]['icon']
else:
icon = ''
if len(getvars.keys()) > 0:
urlappend = "&%s" % getvars.urlencode()
else:
urlappend = ''
if icon:
title_with_sort_ordering = "%s %s" % (title, icon)
else:
title_with_sort_ordering = title

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


def autosort(parser, token):
bits = [b.strip('"\'') for b in token.split_contents()]
if len(bits) != 2:
Expand Down Expand Up @@ -108,5 +174,6 @@ def render(self, context):
return ''

anchor = register.tag(anchor)
sort_anchor = register.tag(sort_anchor)
autosort = register.tag(autosort)