/kai/controllers/comments.py

https://bitbucket.org/bbangert/kai/ · Python · 80 lines · 63 code · 13 blank · 4 comment · 12 complexity · bb7b779206fc4328db2bc389b116baaa MD5 · raw file

  1. import logging
  2. from pylons import request, response, session, tmpl_context as c, url
  3. from pylons.controllers.util import abort, redirect
  4. from pylons.templating import render_mako_def
  5. from kai.lib.base import BaseController, render
  6. from kai.lib.helpers import textilize
  7. from kai.lib.serialization import render_feed
  8. from kai.model import Comment
  9. log = logging.getLogger(__name__)
  10. class CommentsController(BaseController):
  11. def preview(self):
  12. data = request.POST['content']
  13. return textilize(data)
  14. def create(self, doc_id):
  15. if not c.user:
  16. abort(401)
  17. # Ensure the doc exists
  18. doc = self.db.get(doc_id)
  19. if not doc:
  20. abort(404)
  21. comment = Comment(doc_id=doc_id, displayname=c.user.displayname,
  22. email=c.user.email, human_id=c.user.id,
  23. content=request.POST['content'])
  24. comment.store(self.db)
  25. return ''
  26. def delete(self, id):
  27. if not c.user or not c.user.in_group('admin'):
  28. abort(401)
  29. # Ensure doc exists
  30. doc = self.db.get(id)
  31. if not doc:
  32. abort(404)
  33. # Make sure its a comment
  34. if not doc['type'] == 'Comment':
  35. abort(404)
  36. self.db.delete(doc)
  37. return ''
  38. def index(self, format='html'):
  39. if format == 'html':
  40. abort(404)
  41. elif format in ['atom', 'rss']:
  42. # Pull comments and grab the docs with them for their info
  43. comments = list(Comment.by_anytime(c.db, descending=True, limit=20))
  44. commentdata = []
  45. for comment_doc in comments:
  46. comment = {}
  47. displayname = comment_doc.displayname or 'Anonymous'
  48. comment['created'] = comment_doc.created
  49. id = comment_doc.id
  50. doc = c.db.get(comment_doc.doc_id)
  51. if doc['type'] == 'Traceback':
  52. comment['title'] = '%s: %s' % (doc['exception_type'], doc['exception_value'])
  53. else:
  54. comment['title'] = doc.get('title', '-- No title --')
  55. comment['type'] = doc['type']
  56. comment['link'] = render_mako_def(
  57. '/widgets.mako', 'comment_link', title=comment['title'],
  58. comment_id=comment_doc.id, doc=doc, type=doc['type'],
  59. urlonly=True).strip()
  60. comment['doc_id'] = comment_doc.doc_id
  61. comment['description'] = textilize(comment_doc.content)
  62. commentdata.append(comment)
  63. response.content_type = 'application/atom+xml'
  64. return render_feed(
  65. title="PylonsHQ Comment Feed", link=url.current(qualified=True),
  66. description="Recent PylonsHQ comments", objects=commentdata,
  67. pub_date='created')