Mercurial > public > mercurial-scm > hg
comparison mercurial/hgweb/hgweb_mod.py @ 26183:bf1b24785f13
hgweb: move templater instantiation to requestcontext
This code needs to access a lot of config options. All our config
lookups have moved to requestcontext. It makes sense to move this
function there.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 22 Aug 2015 16:38:51 -0700 |
parents | 7187f6e923d5 |
children | 461e7b700fdf |
comparison
equal
deleted
inserted
replaced
26182:6832ba528d1a | 26183:bf1b24785f13 |
---|---|
132 def archivelist(self, nodeid): | 132 def archivelist(self, nodeid): |
133 allowed = self.configlist('web', 'allow_archive') | 133 allowed = self.configlist('web', 'allow_archive') |
134 for typ, spec in self.archivespecs.iteritems(): | 134 for typ, spec in self.archivespecs.iteritems(): |
135 if typ in allowed or self.configbool('web', 'allow%s' % typ): | 135 if typ in allowed or self.configbool('web', 'allow%s' % typ): |
136 yield {'type': typ, 'extension': spec[2], 'node': nodeid} | 136 yield {'type': typ, 'extension': spec[2], 'node': nodeid} |
137 | |
138 def templater(self, req): | |
139 # determine scheme, port and server name | |
140 # this is needed to create absolute urls | |
141 | |
142 proto = req.env.get('wsgi.url_scheme') | |
143 if proto == 'https': | |
144 proto = 'https' | |
145 default_port = '443' | |
146 else: | |
147 proto = 'http' | |
148 default_port = '80' | |
149 | |
150 port = req.env['SERVER_PORT'] | |
151 port = port != default_port and (':' + port) or '' | |
152 urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port) | |
153 logourl = self.config('web', 'logourl', 'http://mercurial.selenic.com/') | |
154 logoimg = self.config('web', 'logoimg', 'hglogo.png') | |
155 staticurl = self.config('web', 'staticurl') or req.url + 'static/' | |
156 if not staticurl.endswith('/'): | |
157 staticurl += '/' | |
158 | |
159 # some functions for the templater | |
160 | |
161 def motd(**map): | |
162 yield self.config('web', 'motd', '') | |
163 | |
164 # figure out which style to use | |
165 | |
166 vars = {} | |
167 styles = ( | |
168 req.form.get('style', [None])[0], | |
169 self.config('web', 'style'), | |
170 'paper', | |
171 ) | |
172 style, mapfile = templater.stylemap(styles, self.templatepath) | |
173 if style == styles[0]: | |
174 vars['style'] = style | |
175 | |
176 start = req.url[-1] == '?' and '&' or '?' | |
177 sessionvars = webutil.sessionvars(vars, start) | |
178 | |
179 if not self.reponame: | |
180 self.reponame = (self.config('web', 'name') | |
181 or req.env.get('REPO_NAME') | |
182 or req.url.strip('/') or self.repo.root) | |
183 | |
184 def websubfilter(text): | |
185 return websub(text, self.websubtable) | |
186 | |
187 # create the templater | |
188 | |
189 tmpl = templater.templater(mapfile, | |
190 filters={'websub': websubfilter}, | |
191 defaults={'url': req.url, | |
192 'logourl': logourl, | |
193 'logoimg': logoimg, | |
194 'staticurl': staticurl, | |
195 'urlbase': urlbase, | |
196 'repo': self.reponame, | |
197 'encoding': encoding.encoding, | |
198 'motd': motd, | |
199 'sessionvars': sessionvars, | |
200 'pathdef': makebreadcrumb(req.url), | |
201 'style': style, | |
202 }) | |
203 return tmpl | |
204 | |
137 | 205 |
138 class hgweb(object): | 206 class hgweb(object): |
139 """HTTP server for individual repositories. | 207 """HTTP server for individual repositories. |
140 | 208 |
141 Instances of this class serve HTTP responses for a particular | 209 Instances of this class serve HTTP responses for a particular |
170 hook.redirect(True) | 238 hook.redirect(True) |
171 self.repostate = None | 239 self.repostate = None |
172 self.mtime = -1 | 240 self.mtime = -1 |
173 self.reponame = name | 241 self.reponame = name |
174 self.websubtable = webutil.getwebsubs(r) | 242 self.websubtable = webutil.getwebsubs(r) |
175 | |
176 # The CGI scripts are often run by a user different from the repo owner. | |
177 # Trust the settings from the .hg/hgrc files by default. | |
178 def config(self, section, name, default=None, untrusted=True): | |
179 return self.repo.ui.config(section, name, default, | |
180 untrusted=untrusted) | |
181 | 243 |
182 def _getview(self, repo): | 244 def _getview(self, repo): |
183 """The 'web.view' config controls changeset filter to hgweb. Possible | 245 """The 'web.view' config controls changeset filter to hgweb. Possible |
184 values are ``served``, ``visible`` and ``all``. Default is ``served``. | 246 values are ``served``, ``visible`` and ``all``. Default is ``served``. |
185 The ``served`` filter only shows changesets that can be pulled from the | 247 The ``served`` filter only shows changesets that can be pulled from the |
334 req.form['type'] = [type_] | 396 req.form['type'] = [type_] |
335 | 397 |
336 # process the web interface request | 398 # process the web interface request |
337 | 399 |
338 try: | 400 try: |
339 tmpl = self.templater(req) | 401 tmpl = rctx.templater(req) |
340 ctype = tmpl('mimetype', encoding=encoding.encoding) | 402 ctype = tmpl('mimetype', encoding=encoding.encoding) |
341 ctype = templater.stringify(ctype) | 403 ctype = templater.stringify(ctype) |
342 | 404 |
343 # check read permissions non-static content | 405 # check read permissions non-static content |
344 if cmd != 'static': | 406 if cmd != 'static': |
377 if inst.code == HTTP_NOT_MODIFIED: | 439 if inst.code == HTTP_NOT_MODIFIED: |
378 # Not allowed to return a body on a 304 | 440 # Not allowed to return a body on a 304 |
379 return [''] | 441 return [''] |
380 return tmpl('error', error=inst.message) | 442 return tmpl('error', error=inst.message) |
381 | 443 |
382 def templater(self, req): | |
383 | |
384 # determine scheme, port and server name | |
385 # this is needed to create absolute urls | |
386 | |
387 proto = req.env.get('wsgi.url_scheme') | |
388 if proto == 'https': | |
389 proto = 'https' | |
390 default_port = "443" | |
391 else: | |
392 proto = 'http' | |
393 default_port = "80" | |
394 | |
395 port = req.env["SERVER_PORT"] | |
396 port = port != default_port and (":" + port) or "" | |
397 urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port) | |
398 logourl = self.config("web", "logourl", "http://mercurial.selenic.com/") | |
399 logoimg = self.config("web", "logoimg", "hglogo.png") | |
400 staticurl = self.config("web", "staticurl") or req.url + 'static/' | |
401 if not staticurl.endswith('/'): | |
402 staticurl += '/' | |
403 | |
404 # some functions for the templater | |
405 | |
406 def motd(**map): | |
407 yield self.config("web", "motd", "") | |
408 | |
409 # figure out which style to use | |
410 | |
411 vars = {} | |
412 styles = ( | |
413 req.form.get('style', [None])[0], | |
414 self.config('web', 'style'), | |
415 'paper', | |
416 ) | |
417 style, mapfile = templater.stylemap(styles, self.templatepath) | |
418 if style == styles[0]: | |
419 vars['style'] = style | |
420 | |
421 start = req.url[-1] == '?' and '&' or '?' | |
422 sessionvars = webutil.sessionvars(vars, start) | |
423 | |
424 if not self.reponame: | |
425 self.reponame = (self.config("web", "name") | |
426 or req.env.get('REPO_NAME') | |
427 or req.url.strip('/') or self.repo.root) | |
428 | |
429 def websubfilter(text): | |
430 return websub(text, self.websubtable) | |
431 | |
432 # create the templater | |
433 | |
434 tmpl = templater.templater(mapfile, | |
435 filters={"websub": websubfilter}, | |
436 defaults={"url": req.url, | |
437 "logourl": logourl, | |
438 "logoimg": logoimg, | |
439 "staticurl": staticurl, | |
440 "urlbase": urlbase, | |
441 "repo": self.reponame, | |
442 "encoding": encoding.encoding, | |
443 "motd": motd, | |
444 "sessionvars": sessionvars, | |
445 "pathdef": makebreadcrumb(req.url), | |
446 "style": style, | |
447 }) | |
448 return tmpl | |
449 | |
450 def check_perm(self, rctx, req, op): | 444 def check_perm(self, rctx, req, op): |
451 for permhook in permhooks: | 445 for permhook in permhooks: |
452 permhook(rctx, req, op) | 446 permhook(rctx, req, op) |