| 1 |
import cherrypy as cpy |
|---|
| 2 |
import time |
|---|
| 3 |
from cgi import escape |
|---|
| 4 |
from urllib import basejoin as urljoin |
|---|
| 5 |
|
|---|
| 6 |
from FileCabinet import get_most_recent, get_entries_by_meta |
|---|
| 7 |
from utils import config, run_callback |
|---|
| 8 |
|
|---|
| 9 |
class EntryStruct(object): |
|---|
| 10 |
def __init__(self): |
|---|
| 11 |
self.desc = '' |
|---|
| 12 |
self.link = '' |
|---|
| 13 |
self.relpath = '' |
|---|
| 14 |
self.text = '' |
|---|
| 15 |
self.time = '' |
|---|
| 16 |
self.title = '' |
|---|
| 17 |
|
|---|
| 18 |
class Atom(object): |
|---|
| 19 |
_cp_config = {"tools.etags.on": True, |
|---|
| 20 |
"tools.etags.autotags": True, |
|---|
| 21 |
"tools.response_headers.on": True, |
|---|
| 22 |
"tools.response_headers.headers": [('Content-Type', 'application/xml')]} |
|---|
| 23 |
|
|---|
| 24 |
def __init__(self, parent): |
|---|
| 25 |
self.parent = parent |
|---|
| 26 |
|
|---|
| 27 |
@cpy.expose |
|---|
| 28 |
def index(self): |
|---|
| 29 |
datadir = config('datadir') |
|---|
| 30 |
num_entries = config('num_entries', 10) |
|---|
| 31 |
entries = get_most_recent(datadir, num_entries) |
|---|
| 32 |
return self.prepare_atom_template(entries) |
|---|
| 33 |
|
|---|
| 34 |
def prepare_atom_template(self, entries): |
|---|
| 35 |
ns = cpy.config.get('/').copy() |
|---|
| 36 |
entry_structs = [] |
|---|
| 37 |
last_updated = '' |
|---|
| 38 |
for e in entries: |
|---|
| 39 |
es = EntryStruct() |
|---|
| 40 |
es.title = e.title |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
run_callback(self.parent.plugins, "cb_feed_story", e) |
|---|
| 46 |
fulltext = escape(e.text) |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
es.desc = fulltext |
|---|
| 52 |
es.text = fulltext |
|---|
| 53 |
es.time = time.strftime('%Y-%m-%dT%H:%M:%SZ', e.time_tuple) |
|---|
| 54 |
if not last_updated: last_updated = es.time |
|---|
| 55 |
es.link = urljoin(config('base_url'), e.relpath + '.html') |
|---|
| 56 |
entry_structs.append(es) |
|---|
| 57 |
ns['last_updated'] = last_updated |
|---|
| 58 |
ns['entries'] = entry_structs |
|---|
| 59 |
return ('atom', ns) |
|---|
| 60 |
|
|---|
| 61 |
@cpy.expose |
|---|
| 62 |
def default(self, *args, **kwargs): |
|---|
| 63 |
if args[0].lower().startswith('keyword') and len(args) > 1: |
|---|
| 64 |
return self.keyword_atom(args[1]) |
|---|
| 65 |
else: return self.index() |
|---|
| 66 |
|
|---|
| 67 |
def keyword_atom(self, kw): |
|---|
| 68 |
num_entries = config('num_entries', 10) |
|---|
| 69 |
entries = get_entries_by_meta('keywords') |
|---|
| 70 |
entries = [e for e in entries if kw in e.metadata['keywords']] |
|---|
| 71 |
return self.prepare_atom_template(entries[:num_entries]) |
|---|