1. Blaise Laflamme
  2. kai

Commits

Ben Bangert  committed 1572dda

Fixed new snippet and added tag view

  • Participants
  • Parent commits 6ece117
  • Branches default

Comments (0)

Files changed (6)

File kai/controllers/snippets.py

View file
  • Ignore whitespace
         """ Get the snippets by date and by author"""
         snippets = list(Snippet.by_date(self.db, descending=True, count=100))
         c.snippets = snippets[:20]
-        unique = []
         authors = []
         for snippet in c.snippets:
             displayname = snippet.displayname.strip()
-            if displayname not in unique:
-                authors.append((displayname, int(snippet.human_id)))
-                unique.append(displayname)
-        
+            if displayname not in authors:
+                authors.append(displayname)        
         c.unique_authors = authors[:10]
         return render('/snippets/index.mako')
     
         id - authors human id.
         
         """
-        c.authors = None
-        c.snippets = None
-        
         if not id:
-            c.authors = list(Snippet.by_author(self.db))
+            c.authors = list(Snippet.author_totals(self.db))
         else:
-            snippets = list(Snippet.by_author_id(self.db, descending=True)[id]) or abort(404)
+            snippets = list(Snippet.by_author(self.db)[id]) or abort(404)
             c.username = snippets[0].displayname
             c.snippets = snippets
         return render('/snippets/byauthor.mako')
         
-    def by_tag(self, tag):
+    def by_tag(self, tag=None):
         """View snippets by a particular tag.
         
         Keyword arguments:
         tag - a tag (string)
         
-        """        
-        pass
-        
-    def report(self, slug):
-        """Report a possible bad snippet."""    
-        pass
+        """
+        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)
+            

File kai/model/snippet.py

View file
  • Ignore whitespace
     reported = BooleanField(default=False)
     reported_by = TextField() # human_id
     
+    all_tags = View('snippets', '''
+        function(doc) {
+          if (doc.type == 'Snippet' && doc.tags) {
+            for (var idx in doc.tags) {
+              var tag = doc.tags[idx];
+              tag = tag.replace(/ /g, '');
+              if (tag) {
+                emit(tag, 1);
+              }
+            }
+          }
+        }''', '''
+        function(keys, values) {
+          return sum(values);
+        }''',
+        wrapper=lambda row: row.key,
+        name='tags', group=True)
+    
     by_tag = View('snippets', '''
         function(doc) {
           if (doc.type == 'Snippet' && doc.tags) {
             for (var idx in doc.tags) {
-              emit(doc.tags[idx], null)
+            var tag = doc.tags[idx];
+            tag = tag.replace(/ /g, '');
+            if (tag) {
+              emit(tag, null);
+            }
             }
           }
         }''', include_docs=True)
         }''', include_docs=True)
     
     by_author = View('snippets', '''
+        function (doc) {
+          if (doc.type == 'Snippet') {
+            emit(doc.displayname, null);
+          }
+        }''', include_docs=True)
+    
+    author_totals = View('snippets', '''
         function(doc) {
           if (doc.type == 'Snippet') {
             emit([doc.displayname, doc.human_id], 1);
                              'amount':row.value},
         group=True)
     
+    by_author
+    
     by_author_id = View('snippets', '''
         function(doc) {
           if (doc.type == 'Snippet' && doc.human_id) {

File kai/templates/snippets/byauthor.mako

View file
  • Ignore whitespace
 <h1>Author List</h1>
 <ul>
 % for author in c.authors:
-    <li>${h.link_to(author['author'], url=url('snippet_author', id=author['id']))} - ${author['amount']} snippets</li>
+    <li>${h.link_to(author['author'], url=url('snippet_author', id=author['author']))} - ${author['amount']} snippets</li>
 % endfor
 </ul>
 % endif 

File kai/templates/snippets/bytag.mako

View file
  • Ignore whitespace
+<h1>${_('Snippets for tag: %s' % c.tag)}</h1>
+<ul>
+% for snippet in c.snippets:
+    <li>${h.link_to(snippet.title, url=url('snippet_view', id=snippet.slug))} - ${format.datetime(snippet.created, "medium")}</li>
+% endfor 
+</ul>
+<%def name="title()">${parent.title()} - ${_('Snippets for tag: %s' % c.tag)}</%def>
+<%inherit file="layout.mako" />

File kai/templates/snippets/index.mako

View file
  • Ignore whitespace
 <h1>Latest Authors</h1>
 <ul>
 % for author in c.unique_authors:
-    <li>${h.link_to(author[0], url=url('snippet_author', id=author[1]))}</li>
+    <li>${h.link_to(author, url=url('snippet_author', id=author))}</li>
 % endfor
 </ul>
 <%def name="title()">${parent.title()} - ${_('Snippet Home')}</%def>

File kai/websetup.py

View file
  • Ignore whitespace
         Rating.all_raters,
         
         Snippet.by_date, Snippet.by_author, Snippet.by_slug, Snippet.by_title,
-        Snippet.by_author_id, Snippet.by_tag,
+        Snippet.by_author_id, Snippet.by_tag, Snippet.all_tags,
+        Snippet.author_totals,
     ])