Commits

Ben Bangert committed b61e3a1

Adding tagcloud

  • Participants
  • Parent commits d499b45

Comments (0)

Files changed (7)

File kai/config/routing.py

     map.connect('snippet_view', '/snippets/view/{id}', controller='snippets', action='view')
     map.connect('snippet_author', '/snippets/by_author/{id}', controller='snippets', action='by_author')
     map.connect('snippet_tag', '/snippets/by_tag/{tag}', controller='snippets', action='by_tag')
+    map.connect('snippet_tagcloud', '/snippets/tagcloud', controller='snippets', action='tagcloud')
     
     # Resources
     map.resource('traceback', 'tracebacks', member={'reown':'GET'})

File kai/controllers/snippets.py

         tag - a tag (string)
         
         """
-        if tag:
-            snippets = list(Snippet.by_tag(self.db)[tag]) or abort(404)
-            c.snippets = snippets
-            c.tag = tag
-            return render('/snippets/bytag.mako')
-        else:
-            tags = list(Snippet.all_tags(self.db)) or abort(404)
-            
+        snippets = list(Snippet.by_tag(self.db)[tag]) or abort(404)
+        c.snippets = snippets
+        c.tag = tag
+        return render('/snippets/bytag.mako')
+    
+    def tagcloud(self):
+        c.tag_sizes = Snippet.tag_sizes()
+        return render('/snippets/tagcloud.mako')

File kai/model/snippet.py

+import math
 from datetime import datetime
 
 import pylons
         function(keys, values) {
           return sum(values);
         }''',
-        wrapper=lambda row: row.key,
+        wrapper=lambda row: {'name':row.key, 'count':row.value},
         name='tags', group=True)
     
     by_tag = View('snippets', '''
     def exists(cls, title):
         rows = pylons.c.db.view('snippets/by_title')[title]
         return len(rows) > 0
+    
+    @classmethod
+    def tag_sizes(cls):
+        """This method returns all the tags and their relative size for
+        a tagcloud"""
+        tags = list(cls.all_tags(pylons.c.db))
+        totalcounts = []
+        for tag in tags:
+            weight = (math.log(tag['count'] or 1) * 4) + 10
+            totalcounts.append((tag['name'], tag['count'], weight))
+        return sorted(totalcounts, cmp=lambda x,y: cmp(x[0], y[0]))

File kai/templates/snippets/add.mako

-<h1>Snippet Repository</h1>
-<p>Pylons snippet repository is a place to share their code snippets with the community.
+<h1>${_('Add Snippet')}</h1>
+<p>${_("""Pylons snippet repository is a place to share their code snippets with the community.
     Please only share fully functioning and tested snippets and include instructions so
-    that novice and experienced Pylons users can benefit.</p>
+    that novice and experienced Pylons users can benefit.""")}</p>
 
 % if c.exists:
     <h3><span class="error-message">A snippet with that title already exists, please choose another title.</span></h3>

File kai/templates/snippets/layout.mako

 <div class="yui-b content">
     <div class="relnav" id="relnav">\
-        ${h.link_to('Snippet Home', url=url('snippet_home'))} |
+        ${h.link_to(_('Snippet Home'), url=url('snippet_home'))} |
         % if c.user:
-        ${h.link_to('Add Snippet', url=url('snippet_add'))} | \
+        ${h.link_to(_('Add Snippet'), url=url('snippet_add'))} | \
         % endif
-        ${h.link_to('View By Author', url=url(controller='snippets', action='by_author'))}
+        ${h.link_to(_('View By Author'), url=url(controller='snippets', action='by_author'))} | \
+        ${h.link_to(_('View By Tag'), url=url('snippet_tagcloud'))}
     </div>
     ${next.body()}
 </div>

File kai/templates/snippets/tagcloud.mako

+<h1>${_('All Tags')}</h1>
+
+<div class="tagcloud">
+    % for tag in c.tag_sizes:
+        <span style="font-size: ${tag[2] * 1.3}px;">${h.link_to(tag[0], url=url('snippet_tag', tag=tag[0]))}</span> 
+    % endfor
+</div>
+<%def name="title()">${parent.title()} - ${_('All Tags')}</%def>
+<%inherit file="layout.mako" />

File kai/templates/snippets/view.mako

 % endfor
 </h5>
 </p>
-<%def name="title()">${parent.title()} - Home</%def>
+<%def name="title()">${parent.title()} - ${_('View Snippet: %s' % c.snippet.title)}</%def>
 <%inherit file="layout.mako" />
 <%def name="styles()">
 ${parent.styles()}