/kai/controllers/snippets.py
Python | 173 lines | 161 code | 10 blank | 2 comment | 16 complexity | 2172fdf5e8412b643970cf7b9169992d MD5 | raw file
1import logging 2import re 3 4from couchdb import ResourceConflict 5from formencode import htmlfill 6from pylons import cache, request, response, session, tmpl_context as c, url 7from pylons.controllers.util import abort, redirect 8from pylons.decorators import rest 9 10import kai.lib.pygmentsupport 11from kai.lib.base import BaseController, CMSObject, render 12from kai.lib.decorators import logged_in, validate 13from kai.lib.helpers import success_flash, failure_flash, rst_render 14from kai.lib.serialization import render_feed 15from kai.model import Human, Snippet, forms 16from kai.model.generics import all_doc_tags 17 18log = logging.getLogger(__name__) 19 20class SnippetsController(BaseController, CMSObject): 21 def __before__(self): 22 c.active_tab = 'Tools' 23 c.active_sub = 'Snippets' 24 25 def preview(self): 26 data = request.POST['content'] 27 return rst_render(data) 28 29 def index(self, format='html'): 30 """ Get the snippets by date and by author""" 31 snippets = list(Snippet.by_date(self.db, descending=True, limit=100)) 32 c.snippets = [x for x in snippets[:20] if x.content] 33 if format in ['atom', 'rss']: 34 response.content_type = 'application/atom+xml' 35 return render_feed( 36 title="PylonsHQ Snippet Feed", link=url.current(qualified=True), 37 description="Recent PylonsHQ snippets", objects=c.snippets, 38 pub_date='created') 39 40 authors = [] 41 for snippet in c.snippets: 42 displayname = snippet.displayname.strip() 43 if displayname not in authors: 44 authors.append(displayname) 45 c.unique_authors = authors[:10] 46 return render('/snippets/index.mako') 47 48 def new(self): 49 if not c.user: 50 abort(401) 51 c.tags = [row['name'] for row in list(all_doc_tags(self.db))] 52 return render('snippets/add.mako') 53 54 @validate(form=forms.snippet_form, error_handler='new') 55 def create(self): 56 if not c.user: 57 abort(401) 58 self.form_result['content'] = self.form_result.pop('snippet_form_content') 59 snippet = Snippet(**self.form_result) 60 snippet.human_id = c.user.id 61 snippet.email = c.user.email 62 snippet.displayname = c.user.displayname 63 64 ## generate the slug 65 slug = snippet.title.replace(" ", "_") 66 slug = slug.lower() 67 slug = re.sub('[^A-Za-z0-9_]+', '', slug) 68 69 snippet.slug = slug 70 if 'tags' in self.form_result: 71 snippet.tags = self.form_result['tags'].replace(',', ' ').strip().split(' ') 72 73 snippet.store(self.db) 74 success_flash('Snippet has been added') 75 redirect(url('snippets')) 76 77 @logged_in 78 def edit(self, id): 79 slug = id.lower().strip() 80 snippets = list(Snippet.by_slug(self.db)[slug]) or abort(404) 81 snippet = snippets[0] 82 if snippet.displayname: 83 author = Human.load(self.db, snippet.human_id) 84 is_owner = self._check_owner(snippet, c.user, check_session=True) 85 if not is_owner: 86 abort(401) 87 88 c.snippet = snippet 89 return render('/snippets/edit.mako') 90 91 @logged_in 92 @validate(form=forms.snippet_form, error_handler='edit') 93 def update(self, id): 94 slug = id.lower().strip() 95 snippets = list(Snippet.by_slug(self.db)[slug]) or abort(404) 96 snippet = snippets[0] 97 if snippet.displayname: 98 author = Human.load(self.db, snippet.human_id) 99 is_owner = self._check_owner(snippet, c.user, check_session=True) 100 if not is_owner: 101 abort(401) 102 103 snippet.title = self.form_result['title'] 104 snippet.content = self.form_result['content'] 105 snippet.description = self.form_result['description'] 106 107 ## generate the slug 108 slug = snippet.title.replace(" ", "_") 109 slug = slug.lower() 110 slug = re.sub('[^A-Za-z0-9_]+', '', slug) 111 112 snippet.slug = slug 113 if 'tags' in self.form_result: 114 snippet.tags = self.form_result['tags'].replace(',', ' ').strip().split(' ') 115 snippet.store(self.db) 116 success_flash('Snippet has been updated') 117 redirect(url('snippet', id=slug)) 118 119 @logged_in 120 def preview(self): 121 """Return a HTML version of the rST content""" 122 data = request.POST['content'] 123 return rst_render(data) 124 125 def show(self, id): 126 """Show a particular snippet 127 128 Keyword arguments: 129 id - actually a slug 130 131 """ 132 slug = id.lower().strip() 133 snippets = list(Snippet.by_slug(self.db)[slug]) or abort(404) 134 snippet = snippets[0] 135 c.snippet_content = rst_render(snippet.content) 136 c.snippet = snippet 137 if c.snippet.displayname: 138 c.author = Human.load(self.db, c.snippet.human_id) 139 c.is_owner = self._check_owner(c.snippet, c.user, check_session=True) 140 141 return render('/snippets/view.mako') 142 143 def by_author(self, id=None): 144 """View snippets by an authors human_id. 145 146 Keyword arguments: 147 id - authors human id. 148 149 """ 150 if not id: 151 c.authors = list(Snippet.author_totals(self.db)) 152 else: 153 snippets = list(Snippet.by_author(self.db)[id]) or abort(404) 154 c.username = snippets[0].displayname 155 c.snippets = snippets 156 return render('/snippets/byauthor.mako') 157 158 def by_tag(self, tag=None): 159 """View snippets by a particular tag. 160 161 Keyword arguments: 162 tag - a tag (string) 163 164 """ 165 snippets = list(Snippet.by_tag(self.db)[tag]) or abort(404) 166 c.snippets = snippets 167 c.tag = tag 168 return render('/snippets/bytag.mako') 169 170 def tagcloud(self): 171 c.tag_sizes = cache.get_cache('snippets.py_tagcloud').get_value( 172 'snippet', createfunc=lambda: Snippet.tag_sizes(), expiretime=180) 173 return render('/snippets/tagcloud.mako')