Created
March 23, 2009 21:00
-
-
Save nathanborror/83774 to your computer and use it in GitHub Desktop.
Django template comparison filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.template import Library | |
from django.template.defaultfilters import lower | |
register = Library() | |
@register.filter | |
def gt(value, arg): | |
"Returns a boolean of whether the value is greater than the argument" | |
return value > int(arg) | |
@register.filter | |
def lt(value, arg): | |
"Returns a boolean of whether the value is less than the argument" | |
return value < int(arg) | |
@register.filter | |
def gte(value, arg): | |
"Returns a boolean of whether the value is greater than or equal to the argument" | |
return value >= int(arg) | |
@register.filter | |
def lte(value, arg): | |
"Returns a boolean of whether the value is less than or equal to the argument" | |
return value <= int(arg) | |
@register.filter | |
def length_gt(value, arg): | |
"Returns a boolean of whether the value's length is greater than the argument" | |
return len(value) > int(arg) | |
@register.filter | |
def length_lt(value, arg): | |
"Returns a boolean of whether the value's length is less than the argument" | |
return len(value) < int(arg) | |
@register.filter | |
def length_gte(value, arg): | |
"Returns a boolean of whether the value's length is greater than or equal to the argument" | |
return len(value) >= int(arg) | |
@register.filter | |
def length_lte(value, arg): | |
"Returns a boolean of whether the value's length is less than or equal to the argument" | |
return len(value) <= int(arg) | |
@register.filter | |
def is_content_type(obj, arg): | |
ct = lower(obj._meta.object_name) | |
return ct == arg | |
@register.filter | |
def is_equal(obj, arg): | |
"Returns a boolean of whether the value is equal to the argument" | |
return obj == arg | |
@register.filter | |
def round(obj): | |
"Returns a number rounded." | |
return round(obj) | |
@register.filter | |
def has(obj, arg): | |
try: | |
if arg in obj: | |
return True | |
except TypeError: | |
pass | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment