Mercurial > hg > bitsyblog
changeset 58:b97d11e2cf41
* document defaults
* allow text blogs
* begin replacing the annoying get_response objects
author | k0s <k0scist@gmail.com> |
---|---|
date | Sun, 27 Dec 2009 01:25:38 -0500 |
parents | 54ebc7eb8384 |
children | 0af1f4ae328d |
files | bitsyblog/bitsyblog.py bitsyblog/factory.py setup.py |
diffstat | 3 files changed, 55 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/bitsyblog/bitsyblog.py Sat Dec 26 22:37:34 2009 -0500 +++ b/bitsyblog/bitsyblog.py Sun Dec 27 01:25:38 2009 -0500 @@ -31,7 +31,9 @@ from docutils.utils import SystemMessage from genshi.builder import Markup from genshi.template import TemplateLoader +from paste.fileapp import FileApp from pkg_resources import resource_filename +from StringIO import StringIO from urlparse import urlparse from user import FilespaceUsers from webob import Request, Response, exc @@ -47,15 +49,16 @@ """a very tiny blog""" ### class level variables - defaults = { 'date_format': '%H:%M %F', - 'file_dir': os.path.dirname(__file__), - 'subject': '[ %(date)s ]:', + defaults = { 'date_format': '%H:%M %F', # default date format + 'file_dir': os.path.dirname(__file__), # where the blog is kept + 'subject': '[ %(date)s ]:', # default subject 'n_links': 5, # number of links for navigation - 'site_name': 'bitsyblog', - 'header': None, - 'template_directories': '', - 'auto_reload': 'True', - 'help_file': None, + 'site_name': 'bitsyblog', # name of the site (needed?) + 'header': None, # text to insert as first child of body' + 'template_directories': '', # space separated template_directories + 'auto_reload': 'True', # reload the genshi templates + 'help_file': None, # help to display + 'feed_items': 10, # number of RSS/atom items to display } @@ -74,6 +77,7 @@ self.users = FilespaceUsers(self.file_dir) self.blog = FileBlog(self.file_dir) self.cooker = self.restructuredText + self.feed_items = int(self.feed_items) # template renderer self.auto_reload = self.auto_reload.lower()=='true' @@ -137,7 +141,7 @@ ### the front page if not path: - return self.get_response(self.index(request, n_links)) + return Response(content_type='text/html', body=self.index(request, n_links)) ### feeds @@ -146,32 +150,32 @@ # site rss if path == 'rss': if n_posts is None: - n_posts = 10 - return self.get_response(self.site_rss(request, n_posts), content_type='text/xml') + n_posts = self.feed_items + return Response(content_type='text/xml', body=self.site_rss(request, n_posts)) # site atom if path == 'atom': if n_posts is None: - n_posts = 10 - return self.get_response(self.atom(request, self.blog.latest(self.users.users(), n_posts)), content_type='text/xml') + n_posts = self.feed_items + return Response(content_type='text/xml', body=self.atom(request, self.blog.latest(self.users.users(), n_posts))) ### help if path == 'help' and hasattr(self, 'help'): - return self.get_response(self.help) + return Response(content_type='text/html', body=self.help) ### static files # site.css if path == 'css/site.css': css_file = os.path.join(self.file_dir, 'site.css') - return self.get_response(file(css_file).read(), content_type='text/css') # should use FileApp + return FileApp(css_file) # logo if path == 'bitsyblog.png': logo = os.path.join(self.file_dir, 'bitsyblog.png') if not self.logo(request): raise exc.HTTPNotFound - return self.get_response(file(logo, 'rb').read(), content_type='image/png') # should use FileApp + return FileApp(logo) def get_user_space(self, user, path, request): """returns a part of the user space""" @@ -186,31 +190,31 @@ if path == [ 'post' ]: if check is not None: return check - return self.get_response(self.form_post(request, user)) + return Response(content_type='text/html', body=self.form_post(request, user)) if path == [ 'preferences' ]: if check is not None: return check - return self.get_response(self.preferences(request, user)) + return Response(content_type='text/html', body=self.preferences(request, user)) if path == [ 'rss' ]: feed = 'rss' path = [] if n_posts is None: - n_posts = 10 # TODO: allow to be configurable + n_posts = self.feed_items # TODO: allow to be configurable if path == [ 'atom' ]: feed = 'atom' path = [] if n_posts is None: - n_posts = 10 # TODO: allow to be configurable + n_posts = self.feed_items # TODO: allow to be configurable if len(path) == 2: if path[0] == 'css': for i in request.user.settings['CSS']: # find the right CSS file if i['filename'] == path[1]: - return self.get_response(i['css'], content_type='text/css') + return Response(content_type='text/css', body=i['css']) else: return exc.HTTPNotFound('CSS "%s" not found' % path[1]) @@ -226,11 +230,11 @@ if feed == 'rss': content = self.rss(request, user, blog) # XXX different signatures - return self.get_response(content, content_type='text/xml') + return Response(content_type='text/xml', body=content) if feed == 'atom': content = self.atom(request, blog, user) # XXX different signatures - return self.get_response(content, content_type='text/xml') + return Response(content_type='text/xml', body=content) # reverse the blog if necessary if request.GET.get('sort') == 'forward': @@ -239,14 +243,19 @@ n_links = self.number_of_links(request, user) # write the blog - content = self.write_blog(user, blog, request.path_info, n_links, request) + if request.GET.get('format') == 'text': + content = self.text_blog(blog) + content_type = 'text/plain' + else: + content = self.write_blog(user, blog, request.path_info, n_links, request) + content_type = 'text/html' # return the content - return self.get_response(content) + return Response(content_type=content_type, body=content) def get(self, request): """ - display the blog or respond to a get request + respond to a get request """ # front matter of the site @@ -256,7 +265,6 @@ ### user space user, path = self.userpath(request) - return self.get_user_space(user, path, request) def post(self, request): @@ -614,6 +622,17 @@ # render the template template = self.loader.load('blog.html') return template.generate(**data).render() + + def text_blog(self, blog): + + if len(blog) == 1: + return blog[0].body.strip() + '\n' + + buffer = StringIO() + for entry in blog: + print >> buffer, '[ %s ]\n' % entry.date.strftime(self.date_format) + print >> buffer, entry.body.strip() + '\n' + return buffer.getvalue() def restructuredText(self, string): """renders a string with restructured text"""
--- a/bitsyblog/factory.py Sat Dec 26 22:37:34 2009 -0500 +++ b/bitsyblog/factory.py Sun Dec 27 01:25:38 2009 -0500 @@ -4,7 +4,14 @@ from paste.httpexceptions import HTTPExceptionHandler # accepted configuration keys, e.g. 'bitsyblog.file_dir' -config = [ 'file_dir', 'date_format', 'subject', 'n_links', 'help_file', 'header', 'template_directories' ] +config = set(['file_dir', + 'date_format', + 'subject', + 'n_links', + 'help_file', + 'header', + 'template_directories', + 'feed_items']) def factory(global_conf, **app_conf): """make bitsyauth app and wrap it in middleware"""