diff --git a/README.rst b/README.rst index 46d7123..e74d3e6 100644 --- a/README.rst +++ b/README.rst @@ -18,4 +18,4 @@ Using histograms in other pages You can use the built-in ``histogram_for`` template tag:: {% load histograms %} - {% histogram_for 'appname.Model' 'histogram_field' %} + {% histogram_for appname.Model 'histogram_field' 1 1 %} diff --git a/django_histograms/templates/histograms/report.html b/django_histograms/templates/histograms/report.html index 6aa60c4..bc7f167 100644 --- a/django_histograms/templates/histograms/report.html +++ b/django_histograms/templates/histograms/report.html @@ -20,7 +20,7 @@

{{ label }} ({{ count }})

{% if day_labels %} {{ forloop.counter }} {% endif %} - + ({{ num }}) diff --git a/django_histograms/templatetags/histograms.py b/django_histograms/templatetags/histograms.py index a200267..4dde26a 100644 --- a/django_histograms/templatetags/histograms.py +++ b/django_histograms/templatetags/histograms.py @@ -10,10 +10,10 @@ @tag(register, [Model(), Variable(), Optional([Variable(), Variable()])]) -def histogram_for(model, attname, months=2, day_labels=True): +def histogram_for(context, model, attname, months=2, day_labels=True): return Histogram(model, attname, months=months).render(css=True, day_labels=day_labels) @tag(register, [Model(), Variable(), Optional([Variable(), Variable()])]) -def histogram_for_days(model, attname, days=31, day_labels=True): +def histogram_for_days(context, model, attname, days=31, day_labels=True): return Histogram(model, attname, days=days).render(css=True, day_labels=day_labels) diff --git a/django_histograms/utils.py b/django_histograms/utils.py index 6f06dee..e88e8d1 100644 --- a/django_histograms/utils.py +++ b/django_histograms/utils.py @@ -64,7 +64,7 @@ def __init__(self, model, attname, queryset=None, months=None, days=None): self.model = model self.attname = attname self._queryset = None - assert(months or days, 'You must pass either months or days, not both.') + assert months or days, 'You must pass either months or days, not both.' self.months = months self.days = days @@ -104,14 +104,22 @@ def get_report(self): qs = self.get_query_set().values(self.attname).annotate( num=Count("pk") - ).filter(**{"%s__gt" % self.attname: cutoff}) + ).filter(**{"%s__gt" % str(self.attname): cutoff}) for data in qs.iterator(): idx = grouper(data[self.attname]) months[idx][1][day_grouper(data[self.attname])] += data["num"] months[idx][2] += data["num"] - print months + + total = sum(o for m in months.itervalues() for o in m[1]) + max_num = max(o for m in months.itervalues() for o in m[1]) + if not (total and max_num): + ratio = 0 + else: + ratio = total / max_num * 100 + return { "results": months.values(), - "total": sum(o for m in months.itervalues() for o in m[1]), + "total": total, + "ratio": ratio }