From 21849d6d5c1a37ed2400e890d64d146d3d3368c1 Mon Sep 17 00:00:00 2001 From: Jet Li Date: Mon, 6 Feb 2012 16:52:11 +0800 Subject: [PATCH 1/4] add translation feature to sort "title", and make tag syntax of sort "field" as literal string , eg. {% anchor "list_price" _("Price") %} --- django_sorting/templatetags/sorting_tags.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/django_sorting/templatetags/sorting_tags.py b/django_sorting/templatetags/sorting_tags.py index 7cdeb42..46c5ffe 100644 --- a/django_sorting/templatetags/sorting_tags.py +++ b/django_sorting/templatetags/sorting_tags.py @@ -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: @@ -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: @@ -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: @@ -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 = self.title - url = '%s?sort=%s%s' % (request.path, self.field, urlappend) - return '%s' % (url, self.title, title) + url = '%s?sort=%s%s' % (request.path, field, urlappend) + return u'%s' % (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]) From 819557519e149ce1581c9486c7546746438fadb9 Mon Sep 17 00:00:00 2001 From: Jet Li Date: Mon, 6 Feb 2012 16:58:53 +0800 Subject: [PATCH 2/4] add translation feature to sort "title", and make tag syntax of sort "field" as explicit literal string , eg. {% anchor "list_price" _("Price") %} --- README.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.txt b/README.txt index b833068..0e2dcee 100644 --- a/README.txt +++ b/README.txt @@ -48,13 +48,13 @@ There are really 5 steps to setting it up with your projects. your objects_list: - {% anchor first_name Name %} - {% anchor creation_date Creation %} + {% anchor "first_name" "Name" %} + {% anchor "creation_date" _("Creation") %} ... 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: From de126d7e712ae2ed91ba7824e303f8fd90a4519f Mon Sep 17 00:00:00 2001 From: Jet Li Date: Mon, 6 Feb 2012 17:02:33 +0800 Subject: [PATCH 3/4] shorten the line --- README.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.txt b/README.txt index 0e2dcee..7c4738e 100644 --- a/README.txt +++ b/README.txt @@ -54,7 +54,8 @@ your objects_list: The first argument is a field of the objects list, and the second - one(optional) is a title that would be displayed, you can use django translation syntax _("") for the second argument. 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: From 81be9a395a6d86c095a11b20235f8553ae39d8f9 Mon Sep 17 00:00:00 2001 From: Jet Li Date: Mon, 6 Feb 2012 17:08:26 +0800 Subject: [PATCH 4/4] correct missing modification --- django_sorting/templatetags/sorting_tags.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_sorting/templatetags/sorting_tags.py b/django_sorting/templatetags/sorting_tags.py index 46c5ffe..acb5cae 100644 --- a/django_sorting/templatetags/sorting_tags.py +++ b/django_sorting/templatetags/sorting_tags.py @@ -72,7 +72,7 @@ def render(self, context): if icon: title_icon = u"%s %s" % (title, icon) else: - title_icon = self.title + title_icon = title url = '%s?sort=%s%s' % (request.path, field, urlappend) return u'%s' % (url, title, title_icon)