17
17
# along with this program; if not, write to the Free Software
18
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
from paste.httpexceptions import HTTPNotFound, HTTPSeeOther
22
25
from paste.request import path_info_pop, parse_querystring
24
27
from loggerhead import util
65
69
self.__history = self._history_callable()
66
70
return self.__history
68
def __call__(self, environ, start_response):
72
def parse_args(self, environ):
70
73
kwargs = dict(parse_querystring(environ))
71
74
util.set_context(kwargs)
74
77
arg = path_info_pop(environ)
81
84
path = unicode('/'.join(args[1:]), 'utf-8')
89
def add_template_values(self, values):
85
91
'static_url': self._branch.static_url,
86
92
'branch': self._branch,
88
94
'url': self._branch.context_url,
90
vals.update(templatefunctions)
96
values.update(templatefunctions)
98
def __call__(self, environ, start_response):
100
if environ.get('loggerhead.as_json') and not self.supports_json:
102
path = self.parse_args(environ)
93
vals.update(self.get_values(path, kwargs, headers))
95
self.log.info('Getting information for %s: %r secs' % (
104
values = self.get_values(path, self.kwargs, headers)
106
self.log.info('Getting information for %s: %.3f secs' % (
96
107
self.__class__.__name__, time.time() - z))
97
if 'Content-Type' not in headers:
108
if environ.get('loggerhead.as_json'):
109
headers['Content-Type'] = 'application/json'
110
elif 'Content-Type' not in headers:
98
111
headers['Content-Type'] = 'text/html'
99
112
writer = start_response("200 OK", headers.items())
100
template = load_template(self.template_path)
113
if environ.get('REQUEST_METHOD') == 'HEAD':
114
# No content for a HEAD request
102
117
w = BufferingWriter(writer, 8192)
103
template.expand_into(w, **vals)
118
if environ.get('loggerhead.as_json'):
119
w.write(simplejson.dumps(values,
120
default=util.convert_to_json_ready))
122
self.add_template_values(values)
123
template = load_template(self.template_path)
124
template.expand_into(w, **values)
106
'Rendering %s: %r secs, %s bytes' % (
127
'Rendering %s: %.3f secs, %s bytes' % (
107
128
self.__class__.__name__, time.time() - z, w.bytes))