]> gitweb.fperrin.net Git - djsite.git/blob - quotes/tagcloud.py
First iteration towards doing a tag cloud
[djsite.git] / quotes / tagcloud.py
1 from quotes.models import QuoteTag
2 from django.db.models import Count, Max
3
4 def build_cloud():
5     maxtag = QuoteTag.objects \
6                      .all() \
7                      .aggregate(max=Max('quote'))['max']
8     print maxtag
9     tags = QuoteTag.objects \
10                    .annotate(num_quotes=Count('quote'),
11                              size=Count(1)) \
12                    .order_by('-num_quotes')[:20]
13
14     for tag in tags:
15         tag.size = int(10 * float(tag.num_quotes) / maxtag)
16     
17     return tags