MCPcopy
hub / github.com/django/django / yesno

Function yesno

django/template/defaultfilters.py:862–892  ·  view source on GitHub ↗

Given a string mapping values for true, false, and (optionally) None, return one of those strings according to the value: ========== ====================== ================================== Value Argument Outputs ========== ====================== =====

(value, arg=None)

Source from the content-addressed store, hash-verified

860
861@register.filter(is_safe=False)
862def yesno(value, arg=None):
863 """
864 Given a string mapping values for true, false, and (optionally) None,
865 return one of those strings according to the value:
866
867 ========== ====================== ==================================
868 Value Argument Outputs
869 ========== ====================== ==================================
870 ``True`` ``"yeah,no,maybe"`` ``yeah``
871 ``False`` ``"yeah,no,maybe"`` ``no``
872 ``None`` ``"yeah,no,maybe"`` ``maybe``
873 ``None`` ``"yeah,no"`` ``"no"`` (converts None to False
874 if no mapping for None is given.
875 ========== ====================== ==================================
876 """
877 if arg is None:
878 # Translators: Please do not add spaces around commas.
879 arg = gettext("yes,no,maybe")
880 bits = arg.split(",")
881 if len(bits) < 2:
882 return value # Invalid arg.
883 try:
884 yes, no, maybe = bits
885 except ValueError:
886 # Unpack list of wrong size (no "maybe" value provided).
887 yes, no, maybe = bits[0], bits[1], bits[1]
888 if value is None:
889 return maybe
890 if value:
891 return yes
892 return no
893
894
895###################

Callers 8

test_trueMethod · 0.90
test_falseMethod · 0.90
test_noneMethod · 0.90
test_true_argumentsMethod · 0.90
test_false_argumentsMethod · 0.90
test_invalid_valueMethod · 0.90

Calls 2

gettextFunction · 0.90
splitMethod · 0.45

Tested by 8

test_trueMethod · 0.72
test_falseMethod · 0.72
test_noneMethod · 0.72
test_true_argumentsMethod · 0.72
test_false_argumentsMethod · 0.72
test_invalid_valueMethod · 0.72