/kai/controllers/home.py

https://bitbucket.org/bbangert/kai/ · Python · 68 lines · 54 code · 12 blank · 2 comment · 6 complexity · 6c25b691faae5f7cb23f662f3e92b1e0 MD5 · raw file

  1. import logging
  2. from pylons import request, response, session, tmpl_context as c
  3. from pylons.controllers.util import abort, redirect
  4. from kai.lib.base import BaseController, render
  5. from kai.model import Article, Comment, Snippet, Paste
  6. from kai.websetup import sync_db
  7. log = logging.getLogger(__name__)
  8. class HomeController(BaseController):
  9. def sync(self):
  10. if not request.environ['toppcloud.internal']:
  11. abort(404)
  12. sync_db(c.db)
  13. def index(self):
  14. c.articles = list(Article.by_time(c.db, descending=True, limit=5))
  15. c.snippets = list(Snippet.by_date(c.db, descending=True, limit=7))
  16. c.pastes = list(Paste.by_time(c.db, descending=True, limit=5))
  17. return render('/home/index.mako')
  18. def robots(self):
  19. response.content_type = 'text/plain'
  20. return render('/home/robots.mako')
  21. def history(self):
  22. c.active_sub = 'History'
  23. return render('/home/history.mako')
  24. def features(self):
  25. c.active_sub = 'Features'
  26. return render('/home/features.mako')
  27. def search(self):
  28. c.active_sub = 'Search'
  29. return render('/search.mako')
  30. def community(self):
  31. c.active_tab = 'Community'
  32. c.active_sub = True
  33. # Load various latest data
  34. c.snippets = list(Snippet.by_date(c.db, descending=True, limit=5))
  35. c.pastes = list(Paste.by_time(c.db, descending=True, limit=10))
  36. # Pull comments and grab the docs with them for their info
  37. comments = list(Comment.by_anytime(c.db, descending=True, limit=10))
  38. commentdata = []
  39. for comment_doc in comments:
  40. comment = {}
  41. comment['displayname'] = comment_doc.displayname or 'Anonymous'
  42. comment['created'] = comment_doc.created
  43. comment['email'] = comment_doc.email or 'anonymous'
  44. comment['content'] = comment_doc.content
  45. comment['id'] = comment_doc.id
  46. doc = c.db.get(comment_doc.doc_id)
  47. if doc['type'] == 'Traceback':
  48. comment['title'] = '%s: %s' % (doc['exception_type'], doc['exception_value'])
  49. else:
  50. comment['title'] = doc.get('title', '-- No title --')
  51. comment['type'] = doc['type']
  52. comment['doc'] = doc
  53. comment['doc_id'] = comment_doc.doc_id
  54. commentdata.append(comment)
  55. c.comments = commentdata
  56. return render('/home/community.mako')