commit c4fe5b45325a59c445ee092f6be17982e0bcf3db
parent fd7fd94994a1a6ebfd0e7db25418fe90c58c9c16
Author: Dan Callaghan <djc@djc.id.au>
Date: Wed, 3 Dec 2008 17:37:12 +1000
indexes for item sets
Diffstat:
7 files changed, 52 insertions(+), 21 deletions(-)
diff --git a/app.py b/app.py
@@ -55,15 +55,39 @@ class Constance(object):
path_info = urllib.unquote(path_info).decode(self.encoding)
for item_set in self.item_sets:
try:
- item = item_set.get(path_info)
+ result = item_set.get(path_info)
except NotExistError, e:
pass
else:
- rendered = item.render('text/html').render('xhtml')
- return Response(rendered, content_type='text/html')
+ if hasattr(result, '__iter__'):
+ return self.render_multiple(result)
+ else:
+ return self.render_single(result)
# no matching URI found, so give a 404
raise exc.HTTPNotFound().exception
+ def render_single(self, item):
+ template = template_loader.load('single.xml')
+ rendered = template.generate(
+ config=self.config,
+ item=item
+ ).render('xhtml')
+ return Response(rendered, content_type='text/html')
+
+ def render_multiple(self, items):
+ try:
+ offset = int(self.req.GET.get('offset', 0))
+ except ValueError:
+ raise exc.HTTPBadRequest('Invalid offset %r' % self.GET['offset']).exception
+ template = template_loader.load('multiple.xml')
+ rendered = template.generate(
+ config=self.config,
+ items=items,
+ title=None,
+ offset=offset
+ ).render('xhtml')
+ return Response(rendered, content_type='text/html')
+
def index(self):
try:
offset = int(self.req.GET.get('offset', 0))
diff --git a/config.defaults b/config.defaults
@@ -10,14 +10,14 @@ author = Anonymous
# e-mail address.
email =
-# The maximum number of entries (of any kind) to be shown on each page.
-entries_per_page = 20
+# The maximum number of items (of any kind) to be shown on each page.
+items_per_page = 20
# Whether to include explicit previous/next <A> links for paged browsing (when needed). <LINK> elements are always included.
show_prev_next = False
-# The maximum number of entries to be included in feeds.
-entries_in_feed = 20
+# The maximum number of items to be included in feeds.
+items_in_feed = 20
# Character encoding to be used everywhere. That is, for:
# * all data read from disk (including this config)
diff --git a/itemtypes.py b/itemtypes.py
@@ -134,6 +134,8 @@ class BlogEntrySet(object):
self.entry_patt = re.compile(re.escape(prefix) + r'/([^/]+)/?$')
def get(self, path_info):
+ if path_info == self.prefix or path_info == self.prefix + '/':
+ return iter(self)
m = self.entry_patt.match(path_info)
if m is None:
raise NotExistError(path_info)
@@ -144,7 +146,7 @@ class BlogEntrySet(object):
def __iter__(self):
assert isinstance(self.base_dir, str)
- return (BlogEntry(self.base_dir, filename, self.prefix + '/' + filename.encode('utf8'))
+ return (BlogEntry(self.base_dir, filename, self.prefix + '/' + filename.decode('utf8'))
for filename in os.listdir(self.base_dir)
if not filename.startswith('.'))
@@ -182,6 +184,8 @@ class ReadingLogEntrySet(object):
self.prefix = prefix
def get(self, path_info):
+ if path_info == self.prefix or path_info == self.prefix + '/':
+ return iter(self)
raise NotExistError(path_info)
def __iter__(self):
diff --git a/templates/ReadingLogEntry.xml b/templates/ReadingLogEntry.xml
@@ -11,7 +11,8 @@ from recaptcha.client import captcha
?>
<span py:def="stars(rating)" py:strip="True">
-<img src="${uri('static', 'images', 'star.png')}" alt="[star]" py:for="_ in range(int(rating))" /><img src="${uri('static', 'images', 'star-half.png')}" alt="[half-star]" py:if="rating > int(rating)" /><img src="${uri('static', 'images', 'star-off.png')}" alt="" py:for="_ in range(int(5 - rating))" />
+<!-- XXX script_name -->
+<img src="/static/images/star.png" alt="[star]" py:for="_ in range(int(rating))" /><img src="/static/images/star-half.png" alt="[half-star]" py:if="rating > int(rating)" /><img src="/static/images/star-off.png" alt="" py:for="_ in range(int(5 - rating))" />
</span>
<!-- XXX img py:if="config.getboolean('readinglog', 'show_covers') and entry.isbn" class="cover"
@@ -19,7 +20,7 @@ from recaptcha.client import captcha
alt="Cover image for ${entry.title}" /-->
<h3 class="title" id="ReadingLogEntry-${item.id}">
- <a py:strip="not entry.url" href="${item.url}">${mini_markdown(item.title)}</a>
+ <a py:strip="not item.url" href="${item.url}">${mini_markdown(item.title)}</a>
<span py:if="item.author" class="author">by ${item.author}</span>
</h3>
diff --git a/templates/_commonwrapper.xml b/templates/_commonwrapper.xml
@@ -17,7 +17,7 @@ import yaml
<title py:if="not title">${config.getunicode('global', 'name')}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="generator" content="constance" />
- <link rel="stylesheet" type="text/css" href="${uri('static', 'css', 'common.css')}" />
+ <link rel="stylesheet" type="text/css" href="/static/css/common.css" /><!-- XXX script_name -->
</head>
</py:match>
diff --git a/templates/multiple.xml b/templates/multiple.xml
@@ -4,31 +4,34 @@
xmlns:xi="http://www.w3.org/2001/XInclude"
lang="en-AU">
<xi:include href="_commonwrapper.xml" />
-<xi:include href="_entry.xml" />
+
+<?python
+sorted_items = sorted(items, key=lambda item: item.publication_date, reverse=True)
+?>
<head>
<title py:if="title">${title}</title>
<link rel="alternate" type="application/atom+xml" title="Atom feed" href="?format=atom" />
<py:if test="defined('offset')">
<link py:if="bool(offset)" rel="prev" href="?offset=${max(0, offset - 20)}" />
- <link py:if="len(sorted_entries) > offset + config.getint('global', 'entries_per_page')" rel="next" href="?offset=${offset + 20}" />
+ <link py:if="len(sorted_items) > offset + config.getint('global', 'items_per_page')" rel="next" href="?offset=${offset + 20}" />
</py:if>
</head>
<body>
<h2 py:if="title">Archive of ${title}</h2>
-<py:for each="entry in (defined('offset') and sorted_entries[offset:offset + config.getint('global', 'entries_per_page')] or sorted_entries)">
- ${show_entry(entry, show_comments=False)}
+<py:for each="item in (defined('offset') and sorted_items[offset:offset + config.getint('global', 'items_per_page')] or sorted_items)">
+ ${item.render('text/html')}
</py:for>
<p id="prevnextlinks"
py:if="defined('offset') and config.getboolean('global', 'show_prev_next')"
py:with="show_prev = bool(offset);
- show_next = len(sorted_entries) > offset + config.getint('global', 'entries_per_page')">
- <a py:if="show_prev" rel="prev" href="?offset=${max(0, offset - 20)}">Newer entries</a>
+ show_next = len(sorted_items) > offset + config.getint('global', 'items_per_page')">
+ <a py:if="show_prev" rel="prev" href="?offset=${max(0, offset - 20)}">Newer items</a>
<py:if test="show_prev and show_next">—</py:if>
- <a py:if="show_next" rel="next" href="?offset=${offset + 20}">Older entries</a>
+ <a py:if="show_next" rel="next" href="?offset=${offset + 20}">Older items</a>
</p>
</body>
diff --git a/templates/single.xml b/templates/single.xml
@@ -4,14 +4,13 @@
xmlns:xi="http://www.w3.org/2001/XInclude"
lang="en-AU">
<xi:include href="_commonwrapper.xml" />
-<xi:include href="_entry.xml" />
<head>
- <title>${entry.title}</title>
+ <title>${item.title}</title>
</head>
<body>
-${show_entry(entry, show_comments=True)}
+${item.render('text/html')}
</body>
</html>