PageRenderTime 161ms CodeModel.GetById 80ms app.highlight 16ms RepoModel.GetById 45ms app.codeStats 0ms

/kai/controllers/pasties.py

https://bitbucket.org/bbangert/kai/
Python | 113 lines | 99 code | 13 blank | 1 comment | 22 complexity | 53755aee0f354eed612217492845d461 MD5 | raw file
  1import logging
  2
  3from pylons import cache, request, response, session, tmpl_context as c, url
  4from pylons.controllers.util import abort, redirect
  5from pylons.decorators import rest
  6
  7from kai.lib.base import BaseController, CMSObject, render
  8from kai.lib.decorators import validate
  9from kai.lib.helpers import success_flash
 10from kai.lib.serialization import render_feed
 11from kai.model import Paste, forms
 12from kai.model.generics import all_doc_tags
 13
 14log = logging.getLogger(__name__)
 15
 16class PastiesController(BaseController, CMSObject):
 17    _cms_object = Paste
 18    
 19    def __before__(self):
 20        c.active_tab = 'Tools'
 21        c.active_sub = 'Pastebin'
 22    
 23    @rest.dispatch_on(POST='_process_new')
 24    def new(self):
 25        c.tags = [row['name'] for row in list(all_doc_tags(self.db))]
 26        return render('/pasties/create.mako')
 27    
 28    @validate(form=forms.pastebin_form, error_handler='new')
 29    def _process_new(self):
 30        self.form_result.pop('notabot')
 31        paste = Paste(**self.form_result)
 32        if c.user:
 33            paste.human_id = c.user.id
 34            paste.email = c.user.email
 35            paste.displayname = c.user.displayname
 36        else:
 37            paste.session_id = session.id
 38        if 'tags' in self.form_result:
 39            paste.tags = self.form_result['tags'].replace(',', ' ').strip().split(' ')
 40
 41        paste.store(self.db)
 42        redirect(url('paste', id=paste.id))
 43    
 44    def show(self, id):
 45        doc = Paste.load(self.db, id)
 46        if not doc:
 47            docs = list(Paste.by_old_id(self.db)[id])
 48            if not docs:
 49                abort(404)
 50            doc = docs[0]
 51        c.paste = doc
 52        c.is_owner = self._check_owner(doc, c.user, check_session=True)
 53        return render('/pasties/show.mako')
 54    
 55    def download(self, id):
 56        doc = Paste.load(self.db, id)
 57        if not doc:
 58            docs = list(Paste.by_old_id(self.db)[id])
 59            if not docs:
 60                abort(404)
 61            doc = docs[0]
 62        id = doc.old_id or doc.id
 63        response.content_type = 'text/plain'
 64        response.headers['Content-disposition'] = 'attachment; filename=paste_%s.txt' % id
 65        return doc.code
 66    
 67    def index(self, format='html', tag=None):
 68        """ Get the pasties by date and by author"""
 69        start = request.GET.get('start', '1')
 70        startkey = request.GET.get('startkey')
 71        prevkey = request.GET.get('prevkey')
 72        kwargs = {}
 73        if startkey:
 74            kwargs = dict(descending=True, startkey=startkey, limit=11)
 75        elif prevkey:
 76            kwargs = dict(startkey=prevkey, limit=11)
 77            c.reverse = True
 78        else:
 79            kwargs = dict(descending=True, limit=11)
 80        if tag:
 81            if startkey:
 82                kwargs['startkey'] = [tag, startkey]
 83                kwargs['endkey'] = [tag]
 84            elif prevkey:
 85                kwargs['startkey'] = [tag, prevkey]
 86                kwargs['endkey'] = [tag, {}]
 87            else:
 88                kwargs['startkey'] = [tag, {}]
 89                kwargs['endkey'] = [tag]
 90            c.pasties = Paste.by_tag_time(self.db, **kwargs)
 91            total_count = list(Paste.all_tags(self.db)[tag]) or 0
 92            if total_count:
 93                total_count = total_count[0]['count']
 94            c.total = total_count
 95        else:    
 96            c.pasties = Paste.by_time(self.db, **kwargs)
 97        c.start = start
 98        if format in ['atom', 'rss']:
 99            response.content_type = 'application/atom+xml'
100            title = "PylonsHQ Pastie Feed"
101            if tag:
102                title += " - Tag: %s" % tag
103            return render_feed(
104                title=title, link=url.current(qualified=True),
105                description="Recent PylonsHQ pasties", objects=c.pasties[:10],
106                pub_date='created')
107        
108        return render('/pasties/index.mako')
109    
110    def tagcloud(self):
111        c.tag_sizes = cache.get_cache('pasties.py_tagcloud').get_value(
112            'tags', createfunc=lambda: Paste.tag_sizes(), expiretime=180)
113        return render('/pasties/tagcloud.mako')