Mercurial > hg > commentator
comparison commentator/middleware.py @ 5:451169e51935
work to make cache expiry work
author | k0s <k0scist@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 12:27:25 -0500 |
parents | d0d8524d9495 |
children |
comparison
equal
deleted
inserted
replaced
4:f02a3672254e | 5:451169e51935 |
---|---|
3 data persisting across requests should go here | 3 data persisting across requests should go here |
4 """ | 4 """ |
5 | 5 |
6 import os | 6 import os |
7 import re | 7 import re |
8 import time | |
8 | 9 |
9 from handlers import PostComment | 10 from handlers import PostComment |
10 #from model import CouchComments | 11 #from model import CouchComments |
11 from model import PickleComments | 12 from model import PickleComments |
12 from genshi.template import TemplateLoader | 13 from genshi.template import TemplateLoader |
71 path = request.path_info.strip('/').split('/') | 72 path = request.path_info.strip('/').split('/') |
72 if path == ['']: | 73 if path == ['']: |
73 path = [] | 74 path = [] |
74 request.environ['path'] = path | 75 request.environ['path'] = path |
75 | 76 |
76 # save the path; not sure why i need to do this | 77 # XXX save the path; not sure why i need to do this |
77 environ['commentator.path_info'] = request.path_info | 78 environ['commentator.path_info'] = request.path_info |
78 | 79 |
79 # match the request to a handler | 80 # match the request to a handler |
80 for h in self.handlers: | 81 for h in self.handlers: |
81 handler = h.match(self, request) | 82 handler = h.match(self, request) |
96 # make string template of the groups | 97 # make string template of the groups |
97 groups_dict = dict([(str(index+1), value) | 98 groups_dict = dict([(str(index+1), value) |
98 for index, value in enumerate(url_match.groups())]) | 99 for index, value in enumerate(url_match.groups())]) |
99 | 100 |
100 last_modified = None | 101 last_modified = None |
102 commentable = False | |
101 for element in tree.findall(self.xpath_pattern): | 103 for element in tree.findall(self.xpath_pattern): |
104 commentable = True | |
102 | 105 |
103 # get url | 106 # get url |
104 str_dict = groups_dict.copy() | 107 str_dict = groups_dict.copy() |
105 for key in element.keys(): | 108 for key in element.keys(): |
106 str_dict[key] = element.get(key) | 109 str_dict[key] = element.get(key) |
108 | 111 |
109 # genshi data | 112 # genshi data |
110 data = {} | 113 data = {} |
111 data['comments'] = self.model.comments(uri) | 114 data['comments'] = self.model.comments(uri) |
112 if data['comments']: | 115 if data['comments']: |
113 last_modified = data['comments'][-1]['date'] | 116 _last_modified = data['comments'][-1]['date'] |
117 if last_modified is None: | |
118 last_modified = _last_modified | |
119 else: | |
120 last_modified = max(last_modified, _last_modified) | |
114 data['action'] = '%s/%s' % (uri, self.url) | 121 data['action'] = '%s/%s' % (uri, self.url) |
115 data['date_format'] = self.date_format | 122 data['date_format'] = self.date_format |
116 data['request'] = request | 123 data['request'] = request |
117 | 124 |
118 # render template | 125 # render template |
119 template = self.loader.load(self.template) | 126 template = self.loader.load(self.template) |
120 comments = template.generate(**data).render() | 127 comments = template.generate(**data).render() |
121 comments = etree.fromstring(comments) | 128 comments = etree.fromstring(comments) |
122 element.append(comments) | 129 element.append(comments) |
123 | 130 |
124 if last_modified: | 131 if commentable: |
125 import pdb; pdb.set_trace() | 132 response.cache_expires(0) |
133 | |
134 if last_modified: # there are comments | |
135 page_age = time.mktime(response.last_modified.timetuple()) | |
136 comments_age = time.mktime(last_modified.timetuple()) | |
137 if comments_age > page_age: | |
138 response.last_modified = comments_age | |
139 | |
126 return tree | 140 return tree |