comparison commentator/middleware.py @ 2:689b9d928dc8

add date, reflect api change of lxmlmiddleware
author k0s <k0scist@gmail.com>
date Tue, 26 Jan 2010 23:00:51 -0500
parents 1c95a3fa76c1
children d0d8524d9495
comparison
equal deleted inserted replaced
1:aebfbb10aefb 2:689b9d928dc8
5 5
6 import os 6 import os
7 import re 7 import re
8 8
9 from handlers import PostComment 9 from handlers import PostComment
10 from model import CouchComments, PickleComments 10 #from model import CouchComments
11 from model import PickleComments
11 from genshi.template import TemplateLoader 12 from genshi.template import TemplateLoader
12 from lxml import etree 13 from lxml import etree
13 from lxmlmiddleware import LXMLMiddleware 14 from lxmlmiddleware import LXMLMiddleware
14 from paste.fileapp import FileApp 15 from paste.fileapp import FileApp
15 from pkg_resources import resource_filename 16 from pkg_resources import resource_filename
22 class Commentator(LXMLMiddleware): 23 class Commentator(LXMLMiddleware):
23 24
24 ### class level variables 25 ### class level variables
25 defaults = { 'auto_reload': 'False', 26 defaults = { 'auto_reload': 'False',
26 'database': 'commentator', 27 'database': 'commentator',
28 'date_format': '%H:%M %m/%d/%Y',
27 'template_dirs': '', 29 'template_dirs': '',
28 'pattern': '.*', 30 'pattern': '.*',
29 'path': 'html', 31 'path': 'html',
30 'url': '.comment', 32 'url': '.comment',
31 'template': 'comment.html' } 33 'template': 'comment.html' }
84 86
85 # get response 87 # get response
86 res = handler() 88 res = handler()
87 return res(environ, start_response) 89 return res(environ, start_response)
88 90
89 def manipulate(self, environ, tree): 91 def manipulate(self, request, response, tree):
90 url_match = re.match(self.url_pattern, environ['commentator.path_info']) 92 url_match = re.match(self.url_pattern, request.environ['commentator.path_info'])
91 if not url_match: 93 if not url_match:
92 return tree 94 return tree
93 95
94 # make string template of the groups 96 # make string template of the groups
95 groups_dict = dict([(str(index+1), value) 97 groups_dict = dict([(str(index+1), value)
108 110
109 # genshi data 111 # genshi data
110 data = {} 112 data = {}
111 data['comments'] = self.model.comments(uri) 113 data['comments'] = self.model.comments(uri)
112 data['action'] = '%s/%s' % (uri, self.url) 114 data['action'] = '%s/%s' % (uri, self.url)
115 data['date_format'] = self.date_format
116 data['request'] = request
113 117
114 # render template 118 # render template
115 template = self.loader.load(self.template) 119 template = self.loader.load(self.template)
116 comments = template.generate(**data).render() 120 comments = template.generate(**data).render()
117 comments = etree.fromstring(comments) 121 comments = etree.fromstring(comments)