0001-templatetags-fallback-to-len-if-count-filter-is-call.patch
tests/test_formdata.py | ||
---|---|---|
2249 | 2249 |
assert tmpl.render(context) == '3.14' |
2250 | 2250 | |
2251 | 2251 | |
2252 |
def test_lazy_formdata_count_as_len_filter(pub): |
|
2253 |
formdef = FormDef() |
|
2254 |
formdef.name = 'foobar' |
|
2255 |
formdef.url_name = 'foobar' |
|
2256 |
formdef.fields = [ |
|
2257 |
fields.StringField(id='0', label='value', varname='value'), |
|
2258 |
] |
|
2259 |
formdef.store() |
|
2260 |
formdata = formdef.data_class()() |
|
2261 |
formdata.data = {'0': 'coin'} |
|
2262 |
formdata.store() |
|
2263 |
pub.substitutions.feed(formdata) |
|
2264 |
for mode in (None, 'lazy'): |
|
2265 |
context = pub.substitutions.get_context_variables(mode=mode) |
|
2266 |
tmpl = Template('{{ form_var_value|count }}') |
|
2267 |
assert tmpl.render(context) == '4' |
|
2268 | ||
2269 | ||
2252 | 2270 |
def test_rounding_and_abs_conditions_django(pub, variable_test_data): |
2253 | 2271 |
for true_condition_value in ( |
2254 | 2272 |
# reminder |
wcs/qommon/templatetags/qommon.py | ||
---|---|---|
642 | 642 | |
643 | 643 |
@register.filter |
644 | 644 |
def count(queryset): |
645 |
return queryset.count |
|
645 |
if hasattr(queryset, 'get_value'): |
|
646 |
queryset = queryset.get_value() # unlazy |
|
647 |
if isinstance(queryset, six.string_types): |
|
648 |
# str.count() exist, but we don't want to use it |
|
649 |
return len(queryset) |
|
650 |
try: |
|
651 |
return queryset.count |
|
652 |
except AttributeError: |
|
653 |
return len(queryset) |
|
646 | 654 | |
647 | 655 | |
648 | 656 |
@register.filter |
649 |
- |