PageRenderTime 245ms CodeModel.GetById 121ms app.highlight 8ms RepoModel.GetById 113ms app.codeStats 0ms

/kai/model/blog.py

https://bitbucket.org/bbangert/kai/
Python | 126 lines | 122 code | 3 blank | 1 comment | 0 complexity | a0e875397af048e0865449f9453ebfe1 MD5 | raw file
  1import os
  2import sha
  3from datetime import datetime
  4
  5import pylons
  6from couchdb.schema import DateTimeField, Document, TextField, ListField, View
  7
  8from kai.lib.helpers import textilize
  9
 10class Article(Document):
 11    """Represents an article"""
 12    type = TextField(default='Article')
 13    slug = TextField()
 14    title = TextField()
 15    human_id = TextField()
 16    author = TextField(default='anonymous')
 17    summary = TextField(default='')
 18    body = TextField(default='')
 19    extended = TextField(default='')
 20    published = DateTimeField()
 21    updated = DateTimeField()
 22
 23    tags = ListField(TextField())
 24
 25    @property
 26    def feed_title(self):
 27        return self.title
 28    
 29    @property
 30    def feed_link(self):
 31        return pylons.url('article_archives', article=self, qualified=True)
 32    
 33    @property
 34    def feed_description(self):
 35        return textilize(self.body)
 36    
 37    @property
 38    def atom_id(self):
 39        host = pylons.request.host
 40        if ':' in host:
 41            host = host.split(':', 1)[0]
 42        return 'tag:%s,%04d-%02d-%02d:%s' % (
 43            host, self.published.year, self.published.month,
 44            self.published.day, self.id
 45        )
 46
 47    def store(self, db, update_timestamp=True):
 48        if not self.published:
 49            self.published = datetime.utcnow()
 50        if update_timestamp or not self.updated:
 51            self.updated = datetime.utcnow()
 52        Document.store(self, db)
 53
 54    all_months = View('articles', '''
 55        function(doc) {
 56          if (doc.type == 'Article') {
 57            var year = parseInt(doc.published.substr(0, 4), 10);
 58            var month = parseInt(doc.published.substr(5, 2), 10);
 59            emit([year, month], 1);
 60          }
 61        }''', '''
 62        function(keys, values) {
 63          return sum(values);
 64        }''',
 65        wrapper=lambda row: datetime(row.key[0], row.key[1], 1),
 66        name='months', group=True)
 67
 68    all_tags = View('articles', '''
 69        function(doc) {
 70          if (doc.type == 'Article' && doc.tags) {
 71            for (var idx in doc.tags) {
 72              emit(doc.tags[idx], 1);
 73            }
 74          }
 75        }''', '''
 76        function(keys, values) {
 77          return sum(values);
 78        }''',
 79        wrapper=lambda row: row.key,
 80        name='tags', group=True)
 81    
 82    by_month = View('articles', '''
 83        function(doc) {
 84          if (doc.type == 'Article') {
 85            var year = parseInt(doc.published.substr(0, 4), 10);
 86            var month = parseInt(doc.published.substr(5, 2), 10);
 87            emit([year, month, doc.published], {
 88              slug: doc.slug, title: doc.title, author: doc.author,
 89              summary: doc.summary, published: doc.published,
 90              updated: doc.updated, tags: doc.tags
 91            });
 92          }
 93        }''')
 94
 95    by_tag = View('articles', u'''
 96        function(doc) {
 97          if (doc.type == 'Article' && doc.tags) {
 98            for (var idx in doc.tags) {
 99              emit([doc.tags[idx], doc.published], {
100                slug: doc.slug, title: doc.title, author: doc.author,
101                published: doc.published, updated: doc.updated,
102                summary: doc.summary, body: doc.body, tags: doc.tags,
103                extended: doc.extended != '' ? '...' : null
104              });
105            }
106          }
107        }''')
108
109    by_time = View('articles', '''
110        function(doc) {
111          if (doc.type == 'Article') {
112            emit(doc.published, 
113                 {slug:doc.slug, title:doc.title, 
114                  author:doc.author, body:doc.body,
115                  published:doc.published, summary:doc.summary});
116          }
117        }''')
118    
119    by_slug = View('articles', '''
120        function(doc) {
121          if (doc.type == 'Article') {
122            var year = parseInt(doc.published.substr(0, 4), 10);
123            var month = parseInt(doc.published.substr(5, 2), 10);
124            emit([year, month, doc.slug], null)
125          }
126        }''')