| 1 |
import re |
|---|
| 2 |
import cherrypy as cpy |
|---|
| 3 |
from urllib import quote_plus, unquote_plus |
|---|
| 4 |
from FileCabinet import get_entries_by_meta |
|---|
| 5 |
from utils import config |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
META_RE = re.compile('^<!-- ?([ \w]+): ?([\w ,.:\/-]+)-->') |
|---|
| 9 |
|
|---|
| 10 |
def keysplit(str): |
|---|
| 11 |
try: |
|---|
| 12 |
return [s.strip() for s in str.split(',')] |
|---|
| 13 |
except: |
|---|
| 14 |
return [str] |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
class Keywords(object): |
|---|
| 18 |
def __init__(self, parent): |
|---|
| 19 |
self.keyword = '' |
|---|
| 20 |
self.atom_link = '' |
|---|
| 21 |
self.parent = parent |
|---|
| 22 |
|
|---|
| 23 |
@cpy.expose |
|---|
| 24 |
def default(self, *args, **kargs): |
|---|
| 25 |
if len(args) > 1: |
|---|
| 26 |
return self.parent.error_page("too many arguments to Keywords") |
|---|
| 27 |
elif len(args) < 1: |
|---|
| 28 |
return self.parent.error_page("Too few arguments to Keywords") |
|---|
| 29 |
|
|---|
| 30 |
try: |
|---|
| 31 |
offset = int(kargs.get('offset', 0)) |
|---|
| 32 |
except ValueError: |
|---|
| 33 |
offset = 0 |
|---|
| 34 |
|
|---|
| 35 |
self.keyword = unquote_plus(args[0]) |
|---|
| 36 |
print "Key = %s" % self.keyword |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
if 'Atom' in config("plugins"): |
|---|
| 40 |
self.atom_link = config('base_url').rstrip('/') + \ |
|---|
| 41 |
'/Atom/keywords/' + self.keyword |
|---|
| 42 |
|
|---|
| 43 |
entries = get_entries_by_meta('keywords') |
|---|
| 44 |
entries = [e for e in entries if self.keyword in keysplit(e.metadata['keywords'])] |
|---|
| 45 |
entries = entries[offset:offset + config("num_entries")] |
|---|
| 46 |
return self.parent.render_page(entries, self.keyword, offset) |
|---|
| 47 |
|
|---|
| 48 |
def cb_add_data(self): |
|---|
| 49 |
if self.atom_link: |
|---|
| 50 |
return {'keywordatom': self.atom_link, 'keyword': self.keyword} |
|---|
| 51 |
|
|---|
| 52 |
def cb_story(self, entry): |
|---|
| 53 |
"""Add a $keywords variable to an entry which is a linkified, |
|---|
| 54 |
comma-seperated string""" |
|---|
| 55 |
kwstring = '' |
|---|
| 56 |
base_url = config('base_url') |
|---|
| 57 |
base_url = base_url.rstrip('/') |
|---|
| 58 |
|
|---|
| 59 |
kws = [k.strip() for k in keysplit(entry.metadata.get('keywords', "")) if k != ''] |
|---|
| 60 |
links = ['<a href=%s/Keywords/%s>%s</a>' % (base_url, quote_plus(kw), kw) |
|---|
| 61 |
for kw in kws] |
|---|
| 62 |
|
|---|
| 63 |
entry.keywords = ', '.join(links) |
|---|
| 64 |
|
|---|
| 65 |
def cb_page_end(self): |
|---|
| 66 |
"""clean up our data""" |
|---|
| 67 |
self.keyword = '' |
|---|
| 68 |
self.atom_link = '' |
|---|