<s> from __future__ import unicode_literals <EOL> import re <EOL> from datetime import date , datetime <EOL> from django import template <EOL> from django . conf import settings <EOL> from django . template import defaultfilters <EOL> from django . utils . encoding import force_text <EOL> from django . utils . formats import number_format <EOL> from django . utils . translation import pgettext , ungettext , ugettext as _ <EOL> from django . utils . timezone import is_aware , utc <EOL> register = template . Library ( ) <EOL> @ register . filter ( is_safe = True ) <EOL> def ordinal ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> value = int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> return value <EOL> suffixes = ( _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) <EOL> if value % <NUM_LIT:100> in ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT> ) : <EOL> return "<STR_LIT>" % ( value , suffixes [ <NUM_LIT:0> ] ) <EOL> return "<STR_LIT>" % ( value , suffixes [ value % <NUM_LIT:10> ] ) <EOL> @ register . filter ( is_safe = True ) <EOL> def intcomma ( value , use_l10n = True ) : <EOL> """<STR_LIT>""" <EOL> if settings . USE_L10N and use_l10n : <EOL> try : <EOL> if not isinstance ( value , float ) : <EOL> value = int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> return intcomma ( value , False ) <EOL> else : <EOL> return number_format ( value , force_grouping = True ) <EOL> orig = force_text ( value ) <EOL> new = re . sub ( "<STR_LIT>" , '<STR_LIT>' , orig ) <EOL> if orig == new : <EOL> return new <EOL> else : <EOL> return intcomma ( new , use_l10n ) <EOL> intword_converters = ( <EOL> ( <NUM_LIT:6> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT:9> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT:12> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT:15> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT:30> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ( <NUM_LIT:100> , lambda number : ( <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ungettext ( '<STR_LIT>' , '<STR_LIT>' , number ) , <EOL> ) ) , <EOL> ) <EOL> @ register . filter ( is_safe = False ) <EOL> def intword ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> value = int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> return value <EOL> if value < <NUM_LIT> : <EOL> return value <EOL> def _check_for_i18n ( value , float_formatted , string_formatted ) : <EOL> """<STR_LIT>""" <EOL> if settings . USE_L10N : <EOL> value = defaultfilters . floatformat ( value , <NUM_LIT:1> ) <EOL> template = string_formatted <EOL> else : <EOL> template = float_formatted <EOL> return template % { '<STR_LIT:value>' : value } <EOL> for exponent , converters in intword_converters : <EOL> large_number = <NUM_LIT:10> ** exponent <EOL> if value < large_number * <NUM_LIT:1000> : <EOL> new_value = value / float ( large_number ) <EOL> return _check_for_i18n ( new_value , * converters ( new_value ) ) <EOL> return value <EOL> @ register . filter ( is_safe = True ) <EOL> def apnumber ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> value = int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> return value <EOL> if not <NUM_LIT:0> < value < <NUM_LIT:10> : <EOL> return value <EOL> return ( _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) [ value - <NUM_LIT:1> ] <EOL> @ register . filter ( expects_localtime = True ) <EOL> def naturalday ( value , arg = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> tzinfo = getattr ( value , '<STR_LIT>' , None ) <EOL> value = date ( value . year , value . month , value . day ) <EOL> except AttributeError : <EOL> return value <EOL> except ValueError : <EOL> return value <EOL> today = datetime . now ( tzinfo ) . date ( ) <EOL> delta = value - today <EOL> if delta . days == <NUM_LIT:0> : <EOL> return _ ( '<STR_LIT>' ) <EOL> elif delta . days == <NUM_LIT:1> : <EOL> return _ ( '<STR_LIT>' ) <EOL> elif delta . days == - <NUM_LIT:1> : <EOL> return _ ( '<STR_LIT>' ) <EOL> return defaultfilters . date ( value , arg ) <EOL> @ register . filter <EOL> def naturaltime ( value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , date ) : <EOL> return value <EOL> now = datetime . now ( utc if is_aware ( value ) else None ) <EOL> if value < now : <EOL> delta = now - value <EOL> if delta . days != <NUM_LIT:0> : <EOL> return pgettext ( <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ) % { '<STR_LIT>' : defaultfilters . timesince ( value , now ) } <EOL> elif delta . seconds == <NUM_LIT:0> : <EOL> return _ ( '<STR_LIT>' ) <EOL> elif delta . seconds < <NUM_LIT> : <EOL> return ungettext ( <EOL> '<STR_LIT>' , '<STR_LIT>' , delta . seconds <EOL> ) % { '<STR_LIT:count>' : delta . seconds } <EOL> elif delta . seconds // <NUM_LIT> < <NUM_LIT> : <EOL> count = delta . seconds // <NUM_LIT> <EOL> return ungettext ( <EOL> '<STR_LIT>' , '<STR_LIT>' , count <EOL> ) % { '<STR_LIT:count>' : count } <EOL> else : <EOL> count = delta . seconds // <NUM_LIT> // <NUM_LIT> <EOL> return ungettext ( <EOL> '<STR_LIT>' , '<STR_LIT>' , count <EOL> ) % { '<STR_LIT:count>' : count } <EOL> else : <EOL> delta = value - now <EOL> if delta . days != <NUM_LIT:0> : <EOL> return pgettext ( <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ) % { '<STR_LIT>' : defaultfilters . timeuntil ( value , now ) } <EOL> elif delta . seconds == <NUM_LIT:0> : <EOL> return _ ( '<STR_LIT>' ) <EOL> elif delta . seconds < <NUM_LIT> : <EOL> return ungettext ( <EOL> '<STR_LIT>' , '<STR_LIT>' , delta . seconds <EOL> ) % { '<STR_LIT:count>' : delta . seconds } <EOL> elif delta . seconds // <NUM_LIT> < <NUM_LIT> : <EOL> count = delta . seconds // <NUM_LIT> <EOL> return ungettext ( <EOL> '<STR_LIT>' , '<STR_LIT>' , count <EOL> ) % { '<STR_LIT:count>' : count } <EOL> else : <EOL> count = delta . seconds // <NUM_LIT> // <NUM_LIT> <EOL> return ungettext ( <EOL> '<STR_LIT>' , '<STR_LIT>' , count <EOL> ) % { '<STR_LIT:count>' : count } </s>
