/kai/controllers/articles.py
Python | 68 lines | 57 code | 10 blank | 1 comment | 5 complexity | 2378fc5300486aaee3cb6514847eb1f2 MD5 | raw file
1import logging 2import re 3 4from pylons import request, response, session, tmpl_context as c, url 5from pylons.controllers.util import abort, redirect 6from pylons.decorators import rest 7 8from kai.lib.base import BaseController, render 9from kai.lib.decorators import in_group, validate 10from kai.lib.helpers import failure_flash, success_flash 11from kai.lib.serialization import render_feed 12from kai.model.forms import new_article_form 13from kai.model import Article 14 15log = logging.getLogger(__name__) 16 17class ArticlesController(BaseController): 18 def __before__(self): 19 c.active_tab = 'Community' 20 c.active_sub = 'Blog' 21 22 def index(self, format='html'): 23 start = request.GET.get('start', '1') 24 startkey = request.GET.get('startkey') 25 prevkey = request.GET.get('prevkey') 26 if startkey: 27 c.articles = Article.by_time(self.db, descending=True, startkey=startkey, limit=11) 28 elif prevkey: 29 c.articles = Article.by_time(self.db, startkey=prevkey, limit=11) 30 c.reverse = True 31 else: 32 c.articles = Article.by_time(self.db, descending=True, limit=11) 33 c.start = start 34 35 if format == 'html': 36 return render('/articles/index.mako') 37 elif format in ['atom', 'rss']: 38 response.content_type = 'application/atom+xml' 39 return render_feed( 40 title="PylonsHQ Article Feed", link=url.current(qualified=True), 41 description="Recent PylonsHQ articles", objects=c.articles, 42 pub_date='published') 43 44 def archives(self, year, month, slug): 45 articles = list(Article.by_slug(c.db, include_docs=True)[(int(year), int(month), slug)]) or abort(404) 46 c.article = articles[0] 47 return render('/articles/show.mako') 48 49 @in_group('admin') 50 def new(self): 51 return render('/articles/new.mako') 52 53 @in_group('admin') 54 @validate(form=new_article_form, error_handler='new') 55 def create(self): 56 result = self.form_result 57 article = Article(title=result['title'], summary=result['summary'], 58 body=result['body'], published=result['publish_date'], 59 human_id=c.user.id, author=c.user.displayname) 60 61 ## generate the slug 62 slug = result['title'].replace(" ", "_") 63 slug = slug.lower() 64 slug = re.sub('[^A-Za-z0-9_]+', '', slug) 65 article.slug = slug 66 article.store(self.db) 67 success_flash('Article saved and published') 68 redirect(url('articles'))