Source

brainstill / brainstill / templatetags / brainstill.py

Full commit
# coding: utf-8
from __future__ import absolute_import

import math

from django.conf import settings
from django import template

from brainstill import models


register = template.Library()


_VOTE_CHOICES = tuple(
    {'class': 'vote%d vote-%s' % (value, 'promote' if value > 0
                                         else 'demote' if value < 0
                                         else 'dontcare'),
     'value': value,
     'title': title,
     'label': '+%d' % value if value > 0 else str(value)}
    for value, title in models.Vote.RANK_CHOICES
)

DEFAULT_GRAPH_LENGTH_FUNC = lambda x: math.log(x, 1.9) * 15 + 2


@register.inclusion_tag('brainstill/include/ranks_and_votes.html')
def ranks_and_votes(idea):
    num_votes = idea.num_votes
    if num_votes == 0:
        promotes = demotes = dontcares = score = graph_length = 0
    else:
        score = idea.score
        graph_length_func = getattr(settings, 'BRAINSTILL_GRAPH_LENGTH_FUNC',
                                    DEFAULT_GRAPH_LENGTH_FUNC)
        graph_length = graph_length_func(num_votes)
        promotes = int(round(graph_length * idea.promotes / num_votes))
        demotes = -int(round(graph_length * idea.demotes / num_votes))
        dontcares = int(round(graph_length * idea.dontcares / num_votes))
    return {
        'idea': idea,
        'vote_choices': _VOTE_CHOICES,
        'num_votes': num_votes,
        'promotes_length': promotes,
        'demotes_length': demotes,
        'dontcares_length': dontcares,
        'score': '+%d' % score if score > 0 else str(score)
    }