templatetags: introduce and use a unlazy function
This commit is contained in:
parent
edb3b7ea6d
commit
822e3311a2
|
@ -48,10 +48,14 @@ from wcs.qommon.admin.texts import TextsDirectory
|
|||
register = template.Library()
|
||||
|
||||
|
||||
def unlazy(x):
|
||||
return x.get_value() if hasattr(x, 'get_value') else x
|
||||
|
||||
|
||||
@register.filter
|
||||
def get(mapping, key):
|
||||
if hasattr(key, 'get_value'):
|
||||
key = key.get_value() # unlazy
|
||||
mapping = unlazy(mapping)
|
||||
key = unlazy(key)
|
||||
if hasattr(mapping, 'get'):
|
||||
return mapping.get(key)
|
||||
if isinstance(mapping, (tuple, list)):
|
||||
|
@ -148,8 +152,7 @@ def parse_date(date_string):
|
|||
|
||||
@register.filter(expects_localtime=True, is_safe=False)
|
||||
def date(value, arg=None):
|
||||
if hasattr(value, 'get_value'):
|
||||
value = value.get_value() # unlazy
|
||||
value = unlazy(value)
|
||||
if arg is None:
|
||||
value = parse_date(value)
|
||||
if not value:
|
||||
|
@ -177,8 +180,7 @@ def parse_datetime(datetime_string):
|
|||
|
||||
@register.filter(name='datetime', expects_localtime=True, is_safe=False)
|
||||
def datetime_(value, arg=None):
|
||||
if hasattr(value, 'get_value'):
|
||||
value = value.get_value() # unlazy
|
||||
value = unlazy(value)
|
||||
if arg is None:
|
||||
value = parse_datetime(value)
|
||||
if not value:
|
||||
|
@ -209,8 +211,7 @@ def parse_time(time_string):
|
|||
|
||||
@register.filter(expects_localtime=True, is_safe=False)
|
||||
def time(value, arg=None):
|
||||
if hasattr(value, 'get_value'):
|
||||
value = value.get_value() # unlazy
|
||||
value = unlazy(value)
|
||||
if arg is None:
|
||||
parsed = parse_time(value)
|
||||
return parsed if parsed is not None else '' # because bool(midnight) == False
|
||||
|
@ -220,8 +221,7 @@ def time(value, arg=None):
|
|||
|
||||
|
||||
def parse_decimal(value, do_raise=False):
|
||||
if hasattr(value, 'get_value'):
|
||||
value = value.get_value() # unlazy
|
||||
value = unlazy(value)
|
||||
if isinstance(value, bool):
|
||||
# treat all booleans as 0 (contrary to Python behaviour where
|
||||
# decimal(True) == 1).
|
||||
|
@ -243,8 +243,7 @@ def decimal(value, arg=None):
|
|||
value = parse_decimal(value)
|
||||
if arg is None:
|
||||
return value
|
||||
if hasattr(arg, 'get_value'):
|
||||
arg = arg.get_value() # unlazy
|
||||
arg = unlazy(arg)
|
||||
return defaultfilters.floatformat(value, arg=arg)
|
||||
|
||||
|
||||
|
@ -490,11 +489,7 @@ def add(term1, term2):
|
|||
pass
|
||||
|
||||
# fallback to django add filter
|
||||
if hasattr(term1, 'get_value'):
|
||||
term1 = term1.get_value() # unlazy
|
||||
if hasattr(term2, 'get_value'):
|
||||
term2 = term2.get_value() # unlazy
|
||||
return defaultfilters.add(term1, term2)
|
||||
return defaultfilters.add(unlazy(term1), unlazy(term2))
|
||||
|
||||
|
||||
@register.filter
|
||||
|
@ -519,8 +514,7 @@ def divide(term1, term2):
|
|||
|
||||
@register.filter(name='sum')
|
||||
def sum_(list_):
|
||||
if hasattr(list_, 'get_value'):
|
||||
list_ = list_.get_value() # unlazy
|
||||
list_ = unlazy(list_)
|
||||
if isinstance(list_, str):
|
||||
# do not consider string as iterable, to avoid misusage
|
||||
return ''
|
||||
|
@ -585,8 +579,7 @@ def get_latlon(obj):
|
|||
if 'base' in obj.geoloc:
|
||||
return obj.geoloc['base']['lat'], obj.geoloc['base']['lon']
|
||||
return None, None
|
||||
if hasattr(obj, 'get_value'):
|
||||
obj = obj.get_value() # unlazy
|
||||
obj = unlazy(obj)
|
||||
if isinstance(obj, dict) and 'lat' in obj and 'lon' in obj:
|
||||
try:
|
||||
return float(obj['lat']), float(obj['lon'])
|
||||
|
@ -625,9 +618,7 @@ def set_geo_center(queryset, lazy_formdata):
|
|||
|
||||
@register.filter
|
||||
def distance_filter(queryset, distance=1000):
|
||||
if hasattr(distance, 'get_value'):
|
||||
distance = distance.get_value() # unlazy
|
||||
return queryset.distance_filter(distance=int(distance))
|
||||
return queryset.distance_filter(distance=int(unlazy(distance)))
|
||||
|
||||
|
||||
@register.filter
|
||||
|
@ -672,10 +663,8 @@ def done(queryset):
|
|||
|
||||
@register.filter
|
||||
def objects(forms_source, slug):
|
||||
if hasattr(slug, 'get_value'):
|
||||
slug = slug.get_value() # unlazy
|
||||
# assume formdef_source is an instance of CardsSource of FormsSource
|
||||
return getattr(forms_source, slug).objects
|
||||
return getattr(forms_source, unlazy(slug)).objects
|
||||
|
||||
|
||||
@register.filter
|
||||
|
@ -685,36 +674,27 @@ def with_custom_view(queryset, custom_view_slug):
|
|||
|
||||
@register.filter
|
||||
def order_by(queryset, attribute):
|
||||
if hasattr(attribute, 'get_value'):
|
||||
attribute = attribute.get_value() # unlazy
|
||||
return queryset.order_by(attribute)
|
||||
return queryset.order_by(unlazy(attribute))
|
||||
|
||||
|
||||
@register.filter
|
||||
def filter_by(queryset, attribute):
|
||||
if hasattr(attribute, 'get_value'):
|
||||
attribute = attribute.get_value() # unlazy
|
||||
return queryset.filter_by(attribute)
|
||||
return queryset.filter_by(unlazy(attribute))
|
||||
|
||||
|
||||
@register.filter
|
||||
def filter_value(queryset, value):
|
||||
if hasattr(value, 'get_value'):
|
||||
value = value.get_value() # unlazy
|
||||
return queryset.apply_filter_value(value)
|
||||
return queryset.apply_filter_value(unlazy(value))
|
||||
|
||||
|
||||
@register.filter
|
||||
def exclude_value(queryset, value):
|
||||
if hasattr(value, 'get_value'):
|
||||
value = value.get_value() # unlazy
|
||||
return queryset.apply_exclude_value(value)
|
||||
return queryset.apply_exclude_value(unlazy(value))
|
||||
|
||||
|
||||
@register.filter
|
||||
def count(queryset):
|
||||
if hasattr(queryset, 'get_value'):
|
||||
queryset = queryset.get_value() # unlazy
|
||||
queryset = unlazy(queryset)
|
||||
if queryset is None:
|
||||
return 0
|
||||
return len(queryset)
|
||||
|
@ -765,8 +745,7 @@ def language_detect(value):
|
|||
def phonenumber_fr(value, separator=' '):
|
||||
DROMS = ('262', '508', '590', '594', '596')
|
||||
|
||||
if hasattr(value, 'get_value'):
|
||||
value = value.get_value() # unlazy
|
||||
value = unlazy(value)
|
||||
if not value or not isinstance(value, str):
|
||||
return value
|
||||
number = value.strip()
|
||||
|
@ -811,6 +790,4 @@ def is_empty(value):
|
|||
|
||||
@register.filter
|
||||
def strip_metadata(value):
|
||||
if hasattr(value, 'get_value'):
|
||||
value = value.get_value() # unlazy
|
||||
return value.strip_metadata()
|
||||
return unlazy(value).strip_metadata()
|
||||
|
|
Loading…
Reference in New Issue