From 0000f9ce55bce712a7af5eeb74e8377ea6f34563 Mon Sep 17 00:00:00 2001 From: Erik Stein Date: Wed, 1 May 2019 06:24:37 +0200 Subject: [PATCH] Template tags. --- CHANGES | 3 +++ shared/multilingual/templatetags/__init__.py | 0 .../templatetags/multilingual_helpers.py | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 shared/multilingual/templatetags/__init__.py create mode 100644 shared/multilingual/templatetags/multilingual_helpers.py diff --git a/CHANGES b/CHANGES index 41e006f..e5862a0 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +0.2.6 2019-04-30 +- Template tags. + 0.2.5 2019-03-15 - Hide dynamic localized field class from migrations/serializations. - i18n_mainlang function diff --git a/shared/multilingual/templatetags/__init__.py b/shared/multilingual/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shared/multilingual/templatetags/multilingual_helpers.py b/shared/multilingual/templatetags/multilingual_helpers.py new file mode 100644 index 0000000..010cf01 --- /dev/null +++ b/shared/multilingual/templatetags/multilingual_helpers.py @@ -0,0 +1,18 @@ +from django import template +from django.db.models.functions import Lower +from django.db.models.query import QuerySet + +from shared.utils.translation import lang_suffix + + +register = template.Library() + + +def orderable(field_name): + return Lower(lang_suffix(fieldname=field_name)) + + +@register.filter +def i18nsort(queryset, field_name): + assert isinstance(queryset, QuerySet), "i18nsort only supports querysets." + return queryset.order_by(orderable(field_name))