<s> import math <EOL> import jinja2 <EOL> from jingo import register , get_env <EOL> from django . utils . translation import ugettext as _ <EOL> from olympia . addons . helpers import new_context <EOL> from olympia . amo . helpers import login_link <EOL> from olympia . amo . urlresolvers import reverse <EOL> from olympia . amo . utils import chunked <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> @ jinja2 . contextfunction <EOL> def collection_listing_items ( context , collections , field = None ) : <EOL> c = dict ( context . items ( ) ) <EOL> c . update ( collections = collections , field = field ) <EOL> return c <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> @ jinja2 . contextfunction <EOL> def impala_collection_listing_items ( context , collections , field = None ) : <EOL> c = dict ( context . items ( ) ) <EOL> c . update ( collections = collections , field = field ) <EOL> return c <EOL> @ register . function <EOL> def user_collection_list ( collections = [ ] , heading = '<STR_LIT>' , id = '<STR_LIT>' , link = None ) : <EOL> """<STR_LIT>""" <EOL> c = { '<STR_LIT>' : collections , '<STR_LIT>' : heading , '<STR_LIT>' : link , <EOL> '<STR_LIT:id>' : id } <EOL> template = get_env ( ) . get_template ( '<STR_LIT>' ) <EOL> return jinja2 . Markup ( template . render ( c ) ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> @ jinja2 . contextfunction <EOL> def barometer ( context , collection ) : <EOL> """<STR_LIT>""" <EOL> c = dict ( context . items ( ) ) <EOL> request = c [ '<STR_LIT>' ] <EOL> user_vote = None <EOL> if request . user . is_authenticated ( ) : <EOL> up_action = collection . upvote_url ( ) <EOL> down_action = collection . downvote_url ( ) <EOL> up_title = _ ( '<STR_LIT>' ) <EOL> down_title = _ ( '<STR_LIT>' ) <EOL> cancel_title = _ ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in context : <EOL> user_vote = context [ '<STR_LIT>' ] . get ( collection . id ) <EOL> else : <EOL> votes = request . user . votes . filter ( collection = collection ) <EOL> if votes : <EOL> user_vote = votes [ <NUM_LIT:0> ] <EOL> else : <EOL> up_action = down_action = login_link ( c ) <EOL> login_title = _ ( '<STR_LIT>' ) <EOL> up_title = down_title = cancel_title = login_title <EOL> up_class = '<STR_LIT>' <EOL> down_class = '<STR_LIT>' <EOL> cancel_class = '<STR_LIT>' <EOL> total_votes = collection . upvotes + collection . downvotes <EOL> if total_votes : <EOL> up_ratio = int ( math . ceil ( round ( <NUM_LIT:100> * collection . upvotes <EOL> / total_votes , <NUM_LIT:2> ) ) ) <EOL> down_ratio = <NUM_LIT:100> - up_ratio <EOL> up_width = max ( up_ratio - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> down_width = max ( down_ratio - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> if user_vote : <EOL> if user_vote . vote > <NUM_LIT:0> : <EOL> up_class += '<STR_LIT>' <EOL> else : <EOL> down_class += '<STR_LIT>' <EOL> else : <EOL> cancel_class += '<STR_LIT>' <EOL> c . update ( locals ( ) ) <EOL> c . update ( { '<STR_LIT:c>' : collection } ) <EOL> return c <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> @ register . function <EOL> @ jinja2 . contextfunction <EOL> def collection_add_widget ( context , addon , condensed = False ) : <EOL> """<STR_LIT>""" <EOL> c = dict ( context . items ( ) ) <EOL> c . update ( locals ( ) ) <EOL> return c <EOL> @ register . filter <EOL> @ jinja2 . contextfilter <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def collection_grid ( context , collections , src = None , pagesize = <NUM_LIT:4> , cols = <NUM_LIT:2> ) : <EOL> pages = chunked ( collections , pagesize ) <EOL> columns = '<STR_LIT>' % cols <EOL> return new_context ( ** locals ( ) ) <EOL> @ register . function <EOL> @ jinja2 . contextfunction <EOL> def favorites_widget ( context , addon , condensed = False ) : <EOL> """<STR_LIT>""" <EOL> c = dict ( context . items ( ) ) <EOL> request = c [ '<STR_LIT>' ] <EOL> if request . user . is_authenticated ( ) : <EOL> is_favorite = addon . id in request . user . favorite_addons <EOL> faved_class = '<STR_LIT>' if is_favorite else '<STR_LIT>' <EOL> unfaved_text = '<STR_LIT>' if condensed else _ ( '<STR_LIT>' ) <EOL> faved_text = _ ( '<STR_LIT>' ) if condensed else _ ( '<STR_LIT>' ) <EOL> add_url = reverse ( '<STR_LIT>' , <EOL> args = [ request . user . username , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> remove_url = reverse ( '<STR_LIT>' , <EOL> args = [ request . user . username , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> c . update ( locals ( ) ) <EOL> t = get_env ( ) . get_template ( '<STR_LIT>' ) . render ( c ) <EOL> return jinja2 . Markup ( t ) <EOL> @ register . function <EOL> @ jinja2 . contextfunction <EOL> def collection_widgets ( context , collection , condensed = False ) : <EOL> """<STR_LIT>""" <EOL> c = dict ( context . items ( ) ) <EOL> if collection : <EOL> c . update ( { '<STR_LIT>' : condensed , <EOL> '<STR_LIT:c>' : collection } ) <EOL> template = get_env ( ) . get_template ( '<STR_LIT>' ) <EOL> return jinja2 . Markup ( template . render ( c ) ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def collection_listing_items_mobile ( collections ) : <EOL> return locals ( ) </s>
