min_size: Smallest size count accepted for a tag
        order_by: asc or desc by count
        limit: maximum number of tags to display 
        
        TODO: convert the hand-written query to an ORM call. Right now I know
                this works with Sqlite3 and PostGreSQL.

def index(request, template_name="tagging_ext/index.html", min_size=0,limit=10):
    """
        min_size: Smallest size count accepted for a tag
        order_by: asc or desc by count
        limit: maximum number of tags to display 
        
        TODO: convert the hand-written query to an ORM call. Right now I know
                this works with Sqlite3 and PostGreSQL.
    """
    query = """
        SELECT tag_item.tag_id as tag_id, COUNT(tag_item.tag_id) as counter 
        FROM tagging_taggeditem as tag_item 
        GROUP BY tag_id
        HAVING COUNT(tag_item.tag_id) > %s
        ORDER BY counter desc
        LIMIT %s
    """

    cursor = connection.cursor()
    cursor.execute(query, [min_size, limit])
    
    results = []
    
    for row in cursor.fetchall():
        try:
            tag=Tag.objects.get(id=row[0])
        except ObjectDoesNotExist:
            continue
            
        if ' ' in tag.name:
            continue
        
        record = dict(
            tag=tag,
            count=row[1]
        )
        results.append(record)    
        
    dictionary = {
        'tags':results
    
    }
    

    return render_to_response(template_name, dictionary,
        context_instance=RequestContext(request))