PageRenderTime 168ms CodeModel.GetById 60ms app.highlight 58ms RepoModel.GetById 43ms app.codeStats 0ms

/kai/controllers/comments.py

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