<s> import urllib <EOL> import hashlib <EOL> from django import template <EOL> from django . contrib . auth . models import User <EOL> from django . utils . translation import ugettext as _ <EOL> from avatar import AVATAR_DEFAULT_URL , AVATAR_GRAVATAR_BACKUP <EOL> from avatar import AVATAR_GRAVATAR_DEFAULT <EOL> register = template . Library ( ) <EOL> def avatar_url ( user , size = <NUM_LIT> ) : <EOL> if not isinstance ( user , User ) : <EOL> try : <EOL> user = User . objects . get ( username = user ) <EOL> except User . DoesNotExist : <EOL> return AVATAR_DEFAULT_URL <EOL> avatars = user . avatar_set . order_by ( '<STR_LIT>' ) <EOL> primary = avatars . filter ( primary = True ) <EOL> if primary . count ( ) > <NUM_LIT:0> : <EOL> avatar = primary [ <NUM_LIT:0> ] <EOL> elif avatars . count ( ) > <NUM_LIT:0> : <EOL> avatar = avatars [ <NUM_LIT:0> ] <EOL> else : <EOL> avatar = None <EOL> if avatar is not None : <EOL> if not avatar . thumbnail_exists ( size ) : <EOL> avatar . create_thumbnail ( size ) <EOL> return avatar . avatar_url ( size ) <EOL> else : <EOL> if AVATAR_GRAVATAR_BACKUP : <EOL> params = { '<STR_LIT:s>' : str ( size ) } <EOL> if AVATAR_GRAVATAR_DEFAULT : <EOL> params [ '<STR_LIT:d>' ] = AVATAR_GRAVATAR_DEFAULT <EOL> return "<STR_LIT>" % ( <EOL> hashlib . md5 ( user . email ) . hexdigest ( ) , <EOL> urllib . urlencode ( params ) ) <EOL> else : <EOL> return AVATAR_DEFAULT_URL <EOL> register . simple_tag ( avatar_url ) <EOL> def avatar ( user , size = <NUM_LIT> ) : <EOL> if not isinstance ( user , User ) : <EOL> try : <EOL> user = User . objects . get ( username = user ) <EOL> alt = unicode ( user ) <EOL> url = avatar_url ( user , size ) <EOL> except User . DoesNotExist : <EOL> url = AVATAR_DEFAULT_URL <EOL> alt = _ ( "<STR_LIT>" ) <EOL> else : <EOL> alt = unicode ( user ) <EOL> url = avatar_url ( user , size ) <EOL> return """<STR_LIT>""" % ( url , alt , <EOL> size , size ) <EOL> register . simple_tag ( avatar ) <EOL> def render_avatar ( avatar , size = <NUM_LIT> ) : <EOL> if not avatar . thumbnail_exists ( size ) : <EOL> avatar . create_thumbnail ( size ) <EOL> return """<STR_LIT>""" % ( <EOL> avatar . avatar_url ( size ) , str ( avatar ) , size , size ) <EOL> register . simple_tag ( render_avatar ) </s>
