Ben Bangert avatar Ben Bangert committed 4de4d24

Handle paging on blog postings

Comments (0)

Files changed (3)

kai/controllers/articles.py

         c.active_sub = 'Blog'
     
     def index(self):
-        c.articles = list(Article.by_time(c.db, descending=True, count=10))
+        start = request.GET.get('start', '1')
+        startkey = request.GET.get('startkey')
+        prevkey = request.GET.get('prevkey')
+        if startkey:
+            c.articles = Article.by_time(self.db, descending=True, startkey=startkey, count=11)
+        elif prevkey:
+            c.articles = Article.by_time(self.db, startkey=prevkey, count=11)
+            c.reverse = True
+        else:
+            c.articles = Article.by_time(self.db, descending=True, count=11)
+        c.start = start
         return render('/articles/index.mako')
     
     def archives(self, year, month, slug):
     @validate(form=new_article_form, error_handler='new')
     def create(self):
         result = self.form_result
-        article = Article(slug=result['slug'], title=result['title'],
-                          summary=result['summary'], body=result['body'],
-                          published=result['publish_date'],
+        article = Article(title=result['title'], summary=result['summary'],
+                          body=result['body'], published=result['publish_date'],
                           human_id=c.user.id, author=c.user.displayname)
+        
+        ## generate the slug
+        slug = result['slug'].replace(" ", "_")
+        slug = slug.lower()
+        slug = re.sub('[^A-Za-z0-9_]+', '', slug)
+        article.slug = slug
         article.store(self.db)
         success_flash('Article saved and published')
         redirect_to('articles')

kai/model/forms.py

 
 class NewArticleForm(forms.TableForm):
     class fields(WidgetsList):
-        slug = forms.TextField(
-            validator = UnicodeString(not_empty=True))
         title = forms.TextField(
             validator = UnicodeString(not_empty=True))
         summary = forms.TextField(
-            validator = UnicodeString(not_empty=True))
+            validator = UnicodeString())
         body = forms.TextArea(
             rows = 15,
             validator = UnicodeString(not_empty=True))

kai/templates/articles/index.mako

  <span class="subtle">(${h.link_to('Add Posting', url=url('new_article'))})</span>\
 % endif
 </h1>
-    % for article in c.articles:
+    <%
+    if c.articles:
+        results = list(c.articles)
+    if c.reverse:
+        results.reverse()
+    %>
+    
+    ${widgets.pager(c.start, results, c.articles.total_rows, 'published')}
+    % for article in results:
     ${display_article(article)}
     % endfor
 </div>
     </div>
 </div>
 </%def>
+<%namespace name="widgets" file="/widgets.mako"/>
 <%def name="title()">${parent.title()} - ${_('Blog')}</%def>
 <%inherit file="/layout.mako" />
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.