"""Default variable filters."""importreimportrandomasrandom_moduleimportunicodedatafromdecimalimportDecimal,InvalidOperation,Context,ROUND_HALF_UPfromfunctoolsimportwrapsfrompprintimportpformatfromdjango.template.baseimportVariable,Library,VariableDoesNotExistfromdjango.confimportsettingsfromdjango.utilsimportformatsfromdjango.utils.dateformatimportformat,time_formatfromdjango.utils.encodingimportforce_unicode,iri_to_urifromdjango.utils.htmlimport(conditional_escape,escapejs,fix_ampersands,escape,urlizeasurlize_impl,linebreaks,strip_tags)fromdjango.utils.httpimporturlquotefromdjango.utils.textimportTruncator,wrap,phone2numericfromdjango.utils.safestringimportmark_safe,SafeData,mark_for_escapingfromdjango.utils.timesinceimporttimesince,timeuntilfromdjango.utils.translationimportugettext,ungettextfromdjango.utils.textimportnormalize_newlinesregister=Library()######################## STRING DECORATOR ########################defstringfilter(func):""" Decorator for filters which should only receive unicode objects. The object passed as the first positional argument will be converted to a unicode object. """def_dec(*args,**kwargs):ifargs:args=list(args)args[0]=force_unicode(args[0])if(isinstance(args[0],SafeData)andgetattr(_dec._decorated_function,'is_safe',False)):returnmark_safe(func(*args,**kwargs))returnfunc(*args,**kwargs)# Include a reference to the real function (used to check original# arguments by the template parser, and to bear the 'is_safe' attribute# when multiple decorators are applied)._dec._decorated_function=getattr(func,'_decorated_function',func)forattrin('is_safe','needs_autoescape'):ifhasattr(func,attr):importwarningswarnings.warn("Setting the %s attribute of a template filter ""function is deprecated; use @register.filter(%s=%s) ""instead"%(attr,attr,getattr(func,attr)),PendingDeprecationWarning)setattr(_dec,attr,getattr(func,attr))returnwraps(func)(_dec)#################### STRINGS ####################@register.filter(is_safe=True)@stringfilterdefaddslashes(value):""" Adds slashes before quotes. Useful for escaping strings in CSV, for example. Less useful for escaping JavaScript; use the ``escapejs`` filter instead. """returnvalue.replace('\\','\\\\').replace('"','\\"').replace("'","\\'")@register.filter(is_safe=True)@stringfilterdefcapfirst(value):"""Capitalizes the first character of the value."""returnvalueandvalue[0].upper()+value[1:]@register.filter("escapejs")@stringfilterdefescapejs_filter(value):"""Hex encodes characters for use in JavaScript strings."""returnescapejs(value)@register.filter("fix_ampersands",is_safe=True)@stringfilterdeffix_ampersands_filter(value):"""Replaces ampersands with ``&amp;`` entities."""returnfix_ampersands(value)# Values for testing floatformat input against infinity and NaN representations,# which differ across platforms and Python versions. Some (i.e. old Windows# ones) are not recognized by Decimal but we want to return them unchanged vs.# returning an empty string as we do for completley invalid input. Note these# need to be built up from values that are not inf/nan, since inf/nan values do# not reload properly from .pyc files on Windows prior to some level of Python 2.5# (see Python Issue757815 and Issue1080440).pos_inf=1e200*1e200neg_inf=-1e200*1e200nan=(1e200*1e200)//(1e200*1e200)special_floats=[str(pos_inf),str(neg_inf),str(nan)]@register.filter(is_safe=True)deffloatformat(text,arg=-1):""" Displays a float to a specified number of decimal places. If called without an argument, it displays the floating point number with one decimal place -- but only if there's a decimal place to be displayed: * num1 = 34.23234 * num2 = 34.00000 * num3 = 34.26000 * {{ num1|floatformat }} displays "34.2" * {{ num2|floatformat }} displays "34" * {{ num3|floatformat }} displays "34.3" If arg is positive, it will always display exactly arg number of decimal places: * {{ num1|floatformat:3 }} displays "34.232" * {{ num2|floatformat:3 }} displays "34.000" * {{ num3|floatformat:3 }} displays "34.260" If arg is negative, it will display arg number of decimal places -- but only if there are places to be displayed: * {{ num1|floatformat:"-3" }} displays "34.232" * {{ num2|floatformat:"-3" }} displays "34" * {{ num3|floatformat:"-3" }} displays "34.260" If the input float is infinity or NaN, the (platform-dependent) string representation of that value will be displayed. """try:input_val=force_unicode(text)d=Decimal(input_val)exceptUnicodeEncodeError:returnu''exceptInvalidOperation:ifinput_valinspecial_floats:returninput_valtry:d=Decimal(force_unicode(float(text)))except(ValueError,InvalidOperation,TypeError,UnicodeEncodeError):returnu''try:p=int(arg)exceptValueError:returninput_valtry:m=int(d)-dexcept(ValueError,OverflowError,InvalidOperation):returninput_valifnotmandp<0:returnmark_safe(formats.number_format(u'%d'%(int(d)),0))ifp==0:exp=Decimal(1)else:exp=Decimal(u'1.0')/(Decimal(10)**abs(p))try:# Set the precision high enough to avoid an exception, see #15789.tupl=d.as_tuple()units=len(tupl[1])-tupl[2]prec=abs(p)+units+1# Avoid conversion to scientific notation by accessing `sign`, `digits`# and `exponent` from `Decimal.as_tuple()` directly.sign,digits,exponent=d.quantize(exp,ROUND_HALF_UP,Context(prec=prec)).as_tuple()digits=[unicode(digit)fordigitinreversed(digits)]whilelen(digits)<=abs(exponent):digits.append(u'0')digits.insert(-exponent,u'.')ifsign:digits.append(u'-')number=u''.join(reversed(digits))returnmark_safe(formats.number_format(number,abs(p)))exceptInvalidOperation:returninput_val@register.filter(is_safe=True)@stringfilterdefiriencode(value):"""Escapes an IRI value for use in a URL."""returnforce_unicode(iri_to_uri(value))@register.filter(is_safe=True,needs_autoescape=True)@stringfilterdeflinenumbers(value,autoescape=None):"""Displays text with line numbers."""lines=value.split(u'\n')# Find the maximum width of the line count, for use with zero padding# string format commandwidth=unicode(len(unicode(len(lines))))ifnotautoescapeorisinstance(value,SafeData):fori,lineinenumerate(lines):lines[i]=(u"%0"+width+u"d. %s")%(i+1,line)else:fori,lineinenumerate(lines):lines[i]=(u"%0"+width+u"d. %s")%(i+1,escape(line))returnmark_safe(u'\n'.join(lines))@register.filter(is_safe=True)@stringfilterdeflower(value):"""Converts a string into all lowercase."""returnvalue.lower()@register.filter(is_safe=False)@stringfilterdefmake_list(value):""" Returns the value turned into a list. For an integer, it's a list of digits. For a string, it's a list of characters. """returnlist(value)@register.filter(is_safe=True)@stringfilterdefslugify(value):""" Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens. """value=unicodedata.normalize('NFKD',value).encode('ascii','ignore')value=unicode(re.sub('[^\w\s-]','',value).strip().lower())returnmark_safe(re.sub('[-\s]+','-',value))@register.filter(is_safe=True)defstringformat(value,arg):""" Formats the variable according to the arg, a string formatting specifier. This specifier uses Python string formating syntax, with the exception that the leading "%" is dropped. See http://docs.python.org/lib/typesseq-strings.html for documentation of Python string formatting """try:return(u"%"+unicode(arg))%valueexcept(ValueError,TypeError):returnu""@register.filter(is_safe=True)@stringfilterdeftitle(value):"""Converts a string into titlecase."""t=re.sub("([a-z])'([A-Z])",lambdam:m.group(0).lower(),value.title())returnre.sub("\d([A-Z])",lambdam:m.group(0).lower(),t)@register.filter(is_safe=True)@stringfilterdeftruncatechars(value,arg):""" Truncates a string after a certain number of characters. Argument: Number of characters to truncate after. """try:length=int(arg)exceptValueError:# Invalid literal for int().returnvalue# Fail silently.returnTruncator(value).chars(length)@register.filter(is_safe=True)@stringfilterdeftruncatewords(value,arg):""" Truncates a string after a certain number of words. Argument: Number of words to truncate after. Newlines within the string are removed. """try:length=int(arg)exceptValueError:# Invalid literal for int().returnvalue# Fail silently.returnTruncator(value).words(length,truncate=' ...')@register.filter(is_safe=True)@stringfilterdeftruncatewords_html(value,arg):""" Truncates HTML after a certain number of words. Argument: Number of words to truncate after. Newlines in the HTML are preserved. """try:length=int(arg)exceptValueError:# invalid literal for int()returnvalue# Fail silently.returnTruncator(value).words(length,html=True,truncate=' ...')@register.filter(is_safe=False)@stringfilterdefupper(value):"""Converts a string into all uppercase."""returnvalue.upper()@register.filter(is_safe=False)@stringfilterdefurlencode(value,safe=None):""" Escapes a value for use in a URL. Takes an optional ``safe`` parameter used to determine the characters which should not be escaped by Django's ``urlquote`` method. If not provided, the default safe characters will be used (but an empty string can be provided when *all* characters should be escaped). """kwargs={}ifsafeisnotNone:kwargs['safe']=safereturnurlquote(value,**kwargs)@register.filter(is_safe=True,needs_autoescape=True)@stringfilterdefurlize(value,autoescape=None):"""Converts URLs in plain text into clickable links."""returnmark_safe(urlize_impl(value,nofollow=True,autoescape=autoescape))@register.filter(is_safe=True,needs_autoescape=True)@stringfilterdefurlizetrunc(value,limit,autoescape=None):""" Converts URLs into clickable links, truncating URLs to the given character limit, and adding 'rel=nofollow' attribute to discourage spamming. Argument: Length to truncate URLs to. """returnmark_safe(urlize_impl(value,trim_url_limit=int(limit),nofollow=True,autoescape=autoescape))@register.filter(is_safe=False)@stringfilterdefwordcount(value):"""Returns the number of words."""returnlen(value.split())@register.filter(is_safe=True)@stringfilterdefwordwrap(value,arg):""" Wraps words at specified line length. Argument: number of characters to wrap the text at. """returnwrap(value,int(arg))@register.filter(is_safe=True)@stringfilterdefljust(value,arg):""" Left-aligns the value in a field of a given width. Argument: field size. """returnvalue.ljust(int(arg))@register.filter(is_safe=True)@stringfilterdefrjust(value,arg):""" Right-aligns the value in a field of a given width. Argument: field size. """returnvalue.rjust(int(arg))@register.filter(is_safe=True)@stringfilterdefcenter(value,arg):"""Centers the value in a field of a given width."""returnvalue.center(int(arg))@register.filter@stringfilterdefcut(value,arg):""" Removes all values of arg from the given string. """safe=isinstance(value,SafeData)value=value.replace(arg,u'')ifsafeandarg!=';':returnmark_safe(value)returnvalue#################### HTML STRINGS ####################@register.filter("escape",is_safe=True)@stringfilterdefescape_filter(value):""" Marks the value as a string that should not be auto-escaped. """returnmark_for_escaping(value)@register.filter(is_safe=True)@stringfilterdefforce_escape(value):""" Escapes a string's HTML. This returns a new string containing the escaped characters (as opposed to "escape", which marks the content for later possible escaping). """returnmark_safe(escape(value))@register.filter("linebreaks",is_safe=True,needs_autoescape=True)@stringfilterdeflinebreaks_filter(value,autoescape=None):""" Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (``<br />``) and a new line followed by a blank line becomes a paragraph break (``</p>``). """autoescape=autoescapeandnotisinstance(value,SafeData)returnmark_safe(linebreaks(value,autoescape))@register.filter(is_safe=True,needs_autoescape=True)@stringfilterdeflinebreaksbr(value,autoescape=None):""" Converts all newlines in a piece of plain text to HTML line breaks (``<br />``). """autoescape=autoescapeandnotisinstance(value,SafeData)value=normalize_newlines(value)ifautoescape:value=escape(value)returnmark_safe(value.replace('\n','<br />'))@register.filter(is_safe=True)@stringfilterdefsafe(value):""" Marks the value as a string that should not be auto-escaped. """returnmark_safe(value)@register.filter(is_safe=True)defsafeseq(value):""" A "safe" filter for sequences. Marks each element in the sequence, individually, as safe, after converting them to unicode. Returns a list with the results. """return[mark_safe(force_unicode(obj))forobjinvalue]@register.filter(is_safe=True)@stringfilterdefremovetags(value,tags):"""Removes a space separated list of [X]HTML tags from the output."""tags=[re.escape(tag)fortagintags.split()]tags_re=u'(%s)'%u'|'.join(tags)starttag_re=re.compile(ur'<%s(/?>|(\s+[^>]*>))'%tags_re,re.U)endtag_re=re.compile(u'</%s>'%tags_re)value=starttag_re.sub(u'',value)value=endtag_re.sub(u'',value)returnvalue@register.filter(is_safe=True)@stringfilterdefstriptags(value):"""Strips all [X]HTML tags."""returnstrip_tags(value)#################### LISTS ####################@register.filter(is_safe=False)defdictsort(value,arg):""" Takes a list of dicts, returns that list sorted by the property given in the argument. """try:returnsorted(value,key=Variable(arg).resolve)except(TypeError,VariableDoesNotExist):returnu''@register.filter(is_safe=False)defdictsortreversed(value,arg):""" Takes a list of dicts, returns that list sorted in reverse order by the property given in the argument. """try:returnsorted(value,key=Variable(arg).resolve,reverse=True)except(TypeError,VariableDoesNotExist):returnu''@register.filter(is_safe=False)deffirst(value):"""Returns the first item in a list."""try:returnvalue[0]exceptIndexError:returnu''@register.filter(is_safe=True,needs_autoescape=True)defjoin(value,arg,autoescape=None):""" Joins a list with a string, like Python's ``str.join(list)``. """value=map(force_unicode,value)ifautoescape:value=[conditional_escape(v)forvinvalue]try:data=conditional_escape(arg).join(value)exceptAttributeError:# fail silently but nicelyreturnvaluereturnmark_safe(data)@register.filter(is_safe=True)deflast(value):"Returns the last item in a list"try:returnvalue[-1]exceptIndexError:returnu''@register.filter(is_safe=True)deflength(value):"""Returns the length of the value - useful for lists."""try:returnlen(value)except(ValueError,TypeError):return''@register.filter(is_safe=False)deflength_is(value,arg):"""Returns a boolean of whether the value's length is the argument."""try:returnlen(value)==int(arg)except(ValueError,TypeError):return''@register.filter(is_safe=True)defrandom(value):"""Returns a random item from the list."""returnrandom_module.choice(value)@register.filter("slice",is_safe=True)defslice_filter(value,arg):""" Returns a slice of the list. Uses the same syntax as Python's list slicing; see http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice for an introduction. """try:bits=[]forxinarg.split(u':'):iflen(x)==0:bits.append(None)else:bits.append(int(x))returnvalue[slice(*bits)]except(ValueError,TypeError):returnvalue# Fail silently.@register.filter(is_safe=True,needs_autoescape=True)defunordered_list(value,autoescape=None):""" Recursively takes a self-nested list and returns an HTML unordered list -- WITHOUT opening and closing <ul> tags. The list is assumed to be in the proper format. For example, if ``var`` contains: ``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``, then ``{{ var|unordered_list }}`` would return:: <li>States <ul> <li>Kansas <ul> <li>Lawrence</li> <li>Topeka</li> </ul> </li> <li>Illinois</li> </ul> </li> """ifautoescape:escaper=conditional_escapeelse:escaper=lambdax:xdefconvert_old_style_list(list_):""" Converts old style lists to the new easier to understand format. The old list format looked like: ['Item 1', [['Item 1.1', []], ['Item 1.2', []]] And it is converted to: ['Item 1', ['Item 1.1', 'Item 1.2]] """ifnotisinstance(list_,(tuple,list))orlen(list_)!=2:returnlist_,Falsefirst_item,second_item=list_ifsecond_item==[]:return[first_item],Truetry:# see if second item is iterableiter(second_item)exceptTypeError:returnlist_,Falseold_style_list=Truenew_second_item=[]forsublistinsecond_item:item,old_style_list=convert_old_style_list(sublist)ifnotold_style_list:breaknew_second_item.extend(item)ifold_style_list:second_item=new_second_itemreturn[first_item,second_item],old_style_listdef_helper(list_,tabs=1):indent=u'\t'*tabsoutput=[]list_length=len(list_)i=0whilei<list_length:title=list_[i]sublist=''sublist_item=Noneifisinstance(title,(list,tuple)):sublist_item=titletitle=''elifi<list_length-1:next_item=list_[i+1]ifnext_itemandisinstance(next_item,(list,tuple)):# The next item is a sub-list.sublist_item=next_item# We've processed the next item now too.i+=1ifsublist_item:sublist=_helper(sublist_item,tabs+1)sublist='\n%s<ul>\n%s\n%s</ul>\n%s'%(indent,sublist,indent,indent)output.append('%s<li>%s%s</li>'%(indent,escaper(force_unicode(title)),sublist))i+=1return'\n'.join(output)value,converted=convert_old_style_list(value)returnmark_safe(_helper(value))#################### INTEGERS ####################@register.filter(is_safe=False)defadd(value,arg):"""Adds the arg to the value."""try:returnint(value)+int(arg)except(ValueError,TypeError):try:returnvalue+argexceptException:return''@register.filter(is_safe=False)defget_digit(value,arg):""" Given a whole number, returns the requested digit of it, where 1 is the right-most digit, 2 is the second-right-most digit, etc. Returns the original value for invalid input (if input or argument is not an integer, or if argument is less than 1). Otherwise, output is always an integer. """try:arg=int(arg)value=int(value)exceptValueError:returnvalue# Fail silently for an invalid argumentifarg<1:returnvaluetry:returnint(str(value)[-arg])exceptIndexError:return0#################### DATES ####################@register.filter(expects_localtime=True,is_safe=False)defdate(value,arg=None):"""Formats a date according to the given format."""ifnotvalue:returnu''ifargisNone:arg=settings.DATE_FORMATtry:returnformats.date_format(value,arg)exceptAttributeError:try:returnformat(value,arg)exceptAttributeError:return''@register.filter(expects_localtime=True,is_safe=False)deftime(value,arg=None):"""Formats a time according to the given format."""ifvaluein(None,u''):returnu''ifargisNone:arg=settings.TIME_FORMATtry:returnformats.time_format(value,arg)exceptAttributeError:try:returntime_format(value,arg)exceptAttributeError:return''@register.filter("timesince",is_safe=False)deftimesince_filter(value,arg=None):"""Formats a date as the time since that date (i.e. "4 days, 6 hours")."""ifnotvalue:returnu''try:ifarg:returntimesince(value,arg)returntimesince(value)except(ValueError,TypeError):returnu''@register.filter("timeuntil",is_safe=False)deftimeuntil_filter(value,arg=None):"""Formats a date as the time until that date (i.e. "4 days, 6 hours")."""ifnotvalue:returnu''try:returntimeuntil(value,arg)except(ValueError,TypeError):returnu''#################### LOGIC ####################@register.filter(is_safe=False)defdefault(value,arg):"""If value is unavailable, use given default."""returnvalueorarg@register.filter(is_safe=False)defdefault_if_none(value,arg):"""If value is None, use given default."""ifvalueisNone:returnargreturnvalue@register.filter(is_safe=False)defdivisibleby(value,arg):"""Returns True if the value is devisible by the argument."""returnint(value)%int(arg)==0@register.filter(is_safe=False)defyesno(value,arg=None):""" Given a string mapping values for true, false and (optionally) None, returns one of those strings according to the value: ========== ====================== ================================== Value Argument Outputs ========== ====================== ================================== ``True`` ``"yeah,no,maybe"`` ``yeah`` ``False`` ``"yeah,no,maybe"`` ``no`` ``None`` ``"yeah,no,maybe"`` ``maybe`` ``None`` ``"yeah,no"`` ``"no"`` (converts None to False if no mapping for None is given. ========== ====================== ================================== """ifargisNone:arg=ugettext('yes,no,maybe')bits=arg.split(u',')iflen(bits)<2:returnvalue# Invalid arg.try:yes,no,maybe=bitsexceptValueError:# Unpack list of wrong size (no "maybe" value provided).yes,no,maybe=bits[0],bits[1],bits[1]ifvalueisNone:returnmaybeifvalue:returnyesreturnno#################### MISC ####################@register.filter(is_safe=True)deffilesizeformat(bytes):""" Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102 bytes, etc). """try:bytes=float(bytes)except(TypeError,ValueError,UnicodeDecodeError):returnungettext("%(size)d byte","%(size)d bytes",0)%{'size':0}filesize_number_format=lambdavalue:formats.number_format(round(value,1),1)ifbytes<1024:returnungettext("%(size)d byte","%(size)d bytes",bytes)%{'size':bytes}ifbytes<1024*1024:returnugettext("%s KB")%filesize_number_format(bytes/1024)ifbytes<1024*1024*1024:returnugettext("%s MB")%filesize_number_format(bytes/(1024*1024))ifbytes<1024*1024*1024*1024:returnugettext("%s GB")%filesize_number_format(bytes/(1024*1024*1024))ifbytes<1024*1024*1024*1024*1024:returnugettext("%s TB")%filesize_number_format(bytes/(1024*1024*1024*1024))returnugettext("%s PB")%filesize_number_format(bytes/(1024*1024*1024*1024*1024))@register.filter(is_safe=False)defpluralize(value,arg=u's'):""" Returns a plural suffix if the value is not 1. By default, 's' is used as the suffix: * If value is 0, vote{{ value|pluralize }} displays "0 votes". * If value is 1, vote{{ value|pluralize }} displays "1 vote". * If value is 2, vote{{ value|pluralize }} displays "2 votes". If an argument is provided, that string is used instead: * If value is 0, class{{ value|pluralize:"es" }} displays "0 classes". * If value is 1, class{{ value|pluralize:"es" }} displays "1 class". * If value is 2, class{{ value|pluralize:"es" }} displays "2 classes". If the provided argument contains a comma, the text before the comma is used for the singular case and the text after the comma is used for the plural case: * If value is 0, cand{{ value|pluralize:"y,ies" }} displays "0 candies". * If value is 1, cand{{ value|pluralize:"y,ies" }} displays "1 candy". * If value is 2, cand{{ value|pluralize:"y,ies" }} displays "2 candies". """ifnotu','inarg:arg=u','+argbits=arg.split(u',')iflen(bits)>2:returnu''singular_suffix,plural_suffix=bits[:2]try:ifint(value)!=1:returnplural_suffixexceptValueError:# Invalid string that's not a number.passexceptTypeError:# Value isn't a string or a number; maybe it's a list?try:iflen(value)!=1:returnplural_suffixexceptTypeError:# len() of unsized object.passreturnsingular_suffix@register.filter("phone2numeric",is_safe=True)defphone2numeric_filter(value):"""Takes a phone number and converts it in to its numerical equivalent."""returnphone2numeric(value)@register.filter(is_safe=True)defpprint(value):"""A wrapper around pprint.pprint -- for debugging, really."""try:returnpformat(value)exceptException,e:returnu"Error in formatting: %s"%force_unicode(e,errors="replace")