<s> import hashlib <EOL> import urllib <EOL> from django . conf import settings <EOL> from django . utils . encoding import force_str <EOL> from django . utils . translation import ugettext as _ <EOL> from django_jinja import library <EOL> from jinja2 import escape , Markup <EOL> from kitsune . sumo . templatetags . jinja_helpers import urlparams <EOL> from kitsune . sumo . urlresolvers import reverse <EOL> from kitsune . users . models import Profile <EOL> @ library . global_function <EOL> def get_profile ( user ) : <EOL> try : <EOL> return Profile . objects . get ( user_id = user . id ) <EOL> except Profile . DoesNotExist : <EOL> return None <EOL> @ library . global_function <EOL> def profile_url ( user , edit = False ) : <EOL> """<STR_LIT>""" <EOL> if edit : <EOL> return reverse ( '<STR_LIT>' , args = [ user . username ] ) <EOL> return reverse ( '<STR_LIT>' , args = [ user . username ] ) <EOL> @ library . global_function <EOL> def profile_avatar ( user , size = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> profile = Profile . objects . get ( user_id = user . id ) <EOL> except ( Profile . DoesNotExist , AttributeError ) : <EOL> avatar = settings . STATIC_URL + settings . DEFAULT_AVATAR <EOL> else : <EOL> avatar = ( profile . avatar . url if profile and profile . avatar else <EOL> settings . STATIC_URL + settings . DEFAULT_AVATAR ) <EOL> if avatar . startswith ( '<STR_LIT>' ) : <EOL> avatar = '<STR_LIT>' % avatar <EOL> if user and hasattr ( user , '<STR_LIT:email>' ) : <EOL> email_hash = hashlib . md5 ( force_str ( user . email . lower ( ) ) ) . hexdigest ( ) <EOL> else : <EOL> email_hash = '<STR_LIT>' <EOL> url = '<STR_LIT>' % ( email_hash , size ) <EOL> if avatar . startswith ( '<STR_LIT:http>' ) : <EOL> url = url + '<STR_LIT>' % urllib . quote ( avatar ) <EOL> return url <EOL> @ library . global_function <EOL> def display_name ( user ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> profile = Profile . objects . get ( user_id = user . id ) <EOL> except ( Profile . DoesNotExist , AttributeError ) : <EOL> return user . username <EOL> return profile . display_name if profile else user . username <EOL> @ library . filter <EOL> def public_email ( email ) : <EOL> """<STR_LIT>""" <EOL> return Markup ( '<STR_LIT>' % unicode_to_html ( email ) ) <EOL> def unicode_to_html ( text ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( [ u'<STR_LIT>' % ord ( i ) for i in text ] ) <EOL> @ library . global_function <EOL> def user_list ( users ) : <EOL> """<STR_LIT>""" <EOL> link = u'<STR_LIT>' <EOL> list = u'<STR_LIT:U+002CU+0020>' . join ( [ link % ( escape ( profile_url ( u ) ) , escape ( u . username ) ) for <EOL> u in users ] ) <EOL> return Markup ( list ) <EOL> @ library . global_function <EOL> def private_message ( user ) : <EOL> """<STR_LIT>""" <EOL> url = urlparams ( reverse ( '<STR_LIT>' ) , to = user . username ) <EOL> msg = _ ( '<STR_LIT>' ) <EOL> return Markup ( u'<STR_LIT>' . format ( <EOL> url = url , msg = msg ) ) <EOL> @ library . global_function <EOL> def is_contributor ( user ) : <EOL> """<STR_LIT>""" <EOL> return ( user . is_authenticated ( ) and <EOL> user . groups . filter ( name = '<STR_LIT>' ) . count ( ) > <NUM_LIT:0> ) </s>
