def ranking(self, count=50, min_count=3, strata=6): from urlparse import urlparse from coltrane.models import Link from coltrane.utils.cloud import calculate_cloud # Fetch all the domains domains = [urlparse(i.url)[1] for i in Link.objects.all()] # Create a dict to stuff the counts domain_count = {} # Loop through all the domains for d in domains: try: # If it exists in the dict, bump it up one domain_count[d]['count'] += 1 except KeyError: # If it doesn't exist yet, add it to the dict domain_count[d] = {'count': 1, 'font-size': None} # Sort the results as a list of tuples, from top to bottom domain_tuple = domain_count.items() domain_tuple.sort(lambda x,y:cmp(x[1], y[1]), reverse=True) # Slice the limit and convert it to a dictionary domain_dict = dict(domain_tuple[:count]) # Pass it into the cloud object_list = calculate_cloud( domain_dict, steps=strata, min_count=min_count, qs=False ) # Empty out the model and refill it with the new data self.model.objects.all().delete() for obj in object_list: self.model.objects.create( name=obj['tag'], count=obj['count'], stratum=obj['font_size'] ) return True
def ranking(self, count=100, strata=6): """ Refills the TopTag model with the most-commonly applied tags, using the provided count value as its cut-off. """ from django.db import connection from tagging.models import TaggedItem, Tag from coltrane.utils.cloud import calculate_cloud # Pull the cloud data, excluding tracks sql = """ SELECT t.name, count(*) FROM tagging_taggeditem as ti INNER JOIN tagging_tag as t ON ti.tag_id = t.id WHERE ti.content_type_id NOT IN (SELECT id FROM django_content_type WHERE name = 'track') GROUP BY t.name ORDER BY 2 DESC LIMIT %s; """ % count cursor = connection.cursor() cursor.execute(sql) tag_counts = {} for row in cursor.fetchall(): tag_counts[row[0]] = {'count': row[1], 'font-size': None} # Pass it into the cloud object_list = calculate_cloud(tag_counts, steps=strata, qs=False) # Empty out the model and refill it with the new data self.model.objects.all().delete() for obj in object_list: tag = Tag.objects.get(name=obj['tag']) self.model.objects.create( tag=tag, name=obj['tag'], count=obj['count'], stratum=obj['font_size'] ) return True
from django.conf.urls.defaults import * # Models from coltrane.models import Post, Link from tagging.models import Tag, TaggedItem # Utils from coltrane.utils.cloud import calculate_cloud urlpatterns = patterns('', # List url(r'^$', 'django.views.generic.list_detail.object_list', { 'queryset': Tag.objects.all(), 'template_name': 'coltrane/tag_list.html', 'extra_context': {'tag_cloud': calculate_cloud(TaggedItem.objects.select_related().all(), steps=6)} }, name='coltrane_tag_list'), # Detail url(r'^(?P<tag>[^/]+)/$', 'coltrane.views.tag_detail', name='coltrane_tag_detail'), )