/kai/controllers/articles.py

https://bitbucket.org/bbangert/kai/ · Python · 68 lines · 57 code · 10 blank · 1 comment · 6 complexity · 2378fc5300486aaee3cb6514847eb1f2 MD5 · raw file

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