def barometer(context, collection): """Shows a barometer for a collection.""" c = dict(context.items()) request = c['request'] user_vote = None # Non-zero if logged in and voted. if request.user.is_authenticated(): # TODO: Use reverse when bandwagon is on Zamboni. up_action = collection.upvote_url() down_action = collection.downvote_url() up_title = _('Add a positive vote for this collection') down_title = _('Add a negative vote for this collection') cancel_title = _('Remove my vote for this collection') if 'collection_votes' in context: user_vote = context['collection_votes'].get(collection.id) else: votes = request.amo_user.votes.filter(collection=collection) if votes: user_vote = votes[0] else: up_action = down_action = login_link(c) login_title = _('Log in to vote for this collection') up_title = down_title = cancel_title = login_title up_class = 'upvotes' down_class = 'downvotes' cancel_class = 'cancel_vote' total_votes = collection.upvotes + collection.downvotes if total_votes: up_ratio = int(math.ceil(round(100 * collection.upvotes / total_votes, 2))) down_ratio = 100 - up_ratio up_width = max(up_ratio - 1, 0) down_width = max(down_ratio - 1, 0) if user_vote: if user_vote.vote > 0: up_class += ' voted' else: down_class += ' voted' else: cancel_class += ' hidden' c.update(locals()) c.update({'c': collection}) return c
def barometer(context, collection): """Shows a barometer for a collection.""" c = dict(context.items()) request = c['request'] user_vote = None # Non-zero if logged in and voted. if request.user.is_authenticated(): # TODO: Use reverse when bandwagon is on Zamboni. up_action = collection.upvote_url() down_action = collection.downvote_url() up_title = _('Add a positive vote for this collection') down_title = _('Add a negative vote for this collection') cancel_title = _('Remove my vote for this collection') if 'collection_votes' in context: user_vote = context['collection_votes'].get(collection.id) else: votes = request.user.votes.filter(collection=collection) if votes: user_vote = votes[0] else: up_action = down_action = login_link(c) login_title = _('Log in to vote for this collection') up_title = down_title = cancel_title = login_title up_class = 'upvotes' down_class = 'downvotes' cancel_class = 'cancel_vote' total_votes = collection.upvotes + collection.downvotes if total_votes: up_ratio = int( math.ceil(round(100 * collection.upvotes / total_votes, 2))) down_ratio = 100 - up_ratio up_width = max(up_ratio - 1, 0) down_width = max(down_ratio - 1, 0) if user_vote: if user_vote.vote > 0: up_class += ' voted' else: down_class += ' voted' else: cancel_class += ' hidden' c.update(locals()) c.update({'c': collection}) return c
def barometer(context, collection): """Shows a barometer for a collection.""" c = dict(context.items()) request = c["request"] user_vote = None # Non-zero if logged in and voted. if request.user.is_authenticated(): # TODO: Use reverse when bandwagon is on Zamboni. up_action = collection.upvote_url() down_action = collection.downvote_url() up_title = _("Add a positive vote for this collection") down_title = _("Add a negative vote for this collection") cancel_title = _("Remove my vote for this collection") if "collection_votes" in context: user_vote = context["collection_votes"].get(collection.id) else: votes = request.user.votes.filter(collection=collection) if votes: user_vote = votes[0] else: up_action = down_action = login_link(c) login_title = _("Log in to vote for this collection") up_title = down_title = cancel_title = login_title up_class = "upvotes" down_class = "downvotes" cancel_class = "cancel_vote" total_votes = collection.upvotes + collection.downvotes if total_votes: up_ratio = int(math.ceil(round(100 * collection.upvotes / total_votes, 2))) down_ratio = 100 - up_ratio up_width = max(up_ratio - 1, 0) down_width = max(down_ratio - 1, 0) if user_vote: if user_vote.vote > 0: up_class += " voted" else: down_class += " voted" else: cancel_class += " hidden" c.update(locals()) c.update({"c": collection}) return c
def barometer(context, collection): """Shows a barometer for a collection.""" c = dict(context.items()) request = c["request"] user_vote = 0 # Non-zero if logged in and voted. if request.user.is_authenticated(): # TODO: Use reverse when bandwagon is on Zamboni. base_action = remora_url(u"collections/vote/%s" % collection.url_slug) up_action = base_action + "/up" down_action = base_action + "/down" cancel = base_action + "/cancel" up_title = _("Add a positive vote for this collection") down_title = _("Add a negative vote for this collection") cancel_title = _("Remove my vote for this collection") votes = request.amo_user.votes.filter(collection=collection) if votes: user_vote = votes[0] else: up_action = down_action = cancel_action = login_link(c) login_title = _("Log in to vote for this collection") up_title = down_title = cancel_title = login_title up_class = "upvotes" down_class = "downvotes" total_votes = collection.up_votes + collection.down_votes if total_votes: up_ratio = int(math.ceil(round(100 * collection.up_votes / total_votes, 2))) down_ratio = 100 - up_ratio up_width = max(up_ratio - 1, 0) down_width = max(down_ratio - 1, 0) if user_vote: up_class += " voted" down_class += " voted" up_class c.update(locals()) c.update({"c": collection}) return c