2017-06-14 03:35:15 +00:00
|
|
|
from django import template
|
|
|
|
from django.core.urlresolvers import resolve, reverse
|
|
|
|
from django.utils.translation import activate, get_language
|
|
|
|
|
|
|
|
register = template.Library()
|
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=True)
|
|
|
|
def change_lang(context, lang=None, *args, **kwargs):
|
|
|
|
|
|
|
|
path = context['request'].path
|
2017-06-29 14:34:40 +00:00
|
|
|
url_parts = resolve(path)
|
2017-06-14 03:35:15 +00:00
|
|
|
|
|
|
|
url = path
|
|
|
|
cur_language = get_language()
|
|
|
|
try:
|
|
|
|
activate(lang)
|
2017-06-29 14:34:40 +00:00
|
|
|
url = reverse(url_parts.view_name, kwargs=url_parts.kwargs)
|
2017-06-14 03:35:15 +00:00
|
|
|
finally:
|
|
|
|
activate(cur_language)
|
|
|
|
|
2017-06-29 07:35:06 +00:00
|
|
|
return "%s" % url
|
|
|
|
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-29 07:35:06 +00:00
|
|
|
@register.filter('get_value_from_dict')
|
|
|
|
def get_value_from_dict(dict_data, key):
|
|
|
|
"""
|
|
|
|
usage example {{ your_dict|get_value_from_dict:your_key }}
|
|
|
|
"""
|
|
|
|
if key:
|
|
|
|
return dict_data.get(key)
|
2017-07-01 13:53:25 +00:00
|
|
|
else:
|
2017-06-30 07:03:28 +00:00
|
|
|
return ""
|
2017-11-21 23:16:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.filter('multiply')
|
|
|
|
def multiply(value, arg):
|
|
|
|
"""
|
|
|
|
usage: {{ quantity|multiply:price }}
|
|
|
|
:param value:
|
|
|
|
:param arg:
|
|
|
|
:return:
|
|
|
|
"""
|
2018-03-21 19:22:06 +00:00
|
|
|
return value * arg
|
|
|
|
|
|
|
|
|
|
|
|
@register.filter('instance_class')
|
|
|
|
def instance_class(obj):
|
|
|
|
return obj.__class__.__name__
|