hgweb: move templater instantiation to requestcontext
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 22 Aug 2015 16:38:51 -0700
changeset 26183 bf1b24785f13
parent 26182 6832ba528d1a
child 26184 327d09f0b5d4
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.
mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py	Wed Sep 02 13:39:46 2015 -0400
+++ b/mercurial/hgweb/hgweb_mod.py	Sat Aug 22 16:38:51 2015 -0700
@@ -135,6 +135,74 @@
             if typ in allowed or self.configbool('web', 'allow%s' % typ):
                 yield {'type': typ, 'extension': spec[2], 'node': nodeid}
 
+    def templater(self, req):
+        # determine scheme, port and server name
+        # this is needed to create absolute urls
+
+        proto = req.env.get('wsgi.url_scheme')
+        if proto == 'https':
+            proto = 'https'
+            default_port = '443'
+        else:
+            proto = 'http'
+            default_port = '80'
+
+        port = req.env['SERVER_PORT']
+        port = port != default_port and (':' + port) or ''
+        urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port)
+        logourl = self.config('web', 'logourl', 'http://mercurial.selenic.com/')
+        logoimg = self.config('web', 'logoimg', 'hglogo.png')
+        staticurl = self.config('web', 'staticurl') or req.url + 'static/'
+        if not staticurl.endswith('/'):
+            staticurl += '/'
+
+        # some functions for the templater
+
+        def motd(**map):
+            yield self.config('web', 'motd', '')
+
+        # figure out which style to use
+
+        vars = {}
+        styles = (
+            req.form.get('style', [None])[0],
+            self.config('web', 'style'),
+            'paper',
+        )
+        style, mapfile = templater.stylemap(styles, self.templatepath)
+        if style == styles[0]:
+            vars['style'] = style
+
+        start = req.url[-1] == '?' and '&' or '?'
+        sessionvars = webutil.sessionvars(vars, start)
+
+        if not self.reponame:
+            self.reponame = (self.config('web', 'name')
+                             or req.env.get('REPO_NAME')
+                             or req.url.strip('/') or self.repo.root)
+
+        def websubfilter(text):
+            return websub(text, self.websubtable)
+
+        # create the templater
+
+        tmpl = templater.templater(mapfile,
+                                   filters={'websub': websubfilter},
+                                   defaults={'url': req.url,
+                                             'logourl': logourl,
+                                             'logoimg': logoimg,
+                                             'staticurl': staticurl,
+                                             'urlbase': urlbase,
+                                             'repo': self.reponame,
+                                             'encoding': encoding.encoding,
+                                             'motd': motd,
+                                             'sessionvars': sessionvars,
+                                             'pathdef': makebreadcrumb(req.url),
+                                             'style': style,
+                                            })
+        return tmpl
+
+
 class hgweb(object):
     """HTTP server for individual repositories.
 
@@ -173,12 +241,6 @@
         self.reponame = name
         self.websubtable = webutil.getwebsubs(r)
 
-    # The CGI scripts are often run by a user different from the repo owner.
-    # Trust the settings from the .hg/hgrc files by default.
-    def config(self, section, name, default=None, untrusted=True):
-        return self.repo.ui.config(section, name, default,
-                                   untrusted=untrusted)
-
     def _getview(self, repo):
         """The 'web.view' config controls changeset filter to hgweb. Possible
         values are ``served``, ``visible`` and ``all``. Default is ``served``.
@@ -336,7 +398,7 @@
         # process the web interface request
 
         try:
-            tmpl = self.templater(req)
+            tmpl = rctx.templater(req)
             ctype = tmpl('mimetype', encoding=encoding.encoding)
             ctype = templater.stringify(ctype)
 
@@ -379,74 +441,6 @@
                 return ['']
             return tmpl('error', error=inst.message)
 
-    def templater(self, req):
-
-        # determine scheme, port and server name
-        # this is needed to create absolute urls
-
-        proto = req.env.get('wsgi.url_scheme')
-        if proto == 'https':
-            proto = 'https'
-            default_port = "443"
-        else:
-            proto = 'http'
-            default_port = "80"
-
-        port = req.env["SERVER_PORT"]
-        port = port != default_port and (":" + port) or ""
-        urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port)
-        logourl = self.config("web", "logourl", "http://mercurial.selenic.com/")
-        logoimg = self.config("web", "logoimg", "hglogo.png")
-        staticurl = self.config("web", "staticurl") or req.url + 'static/'
-        if not staticurl.endswith('/'):
-            staticurl += '/'
-
-        # some functions for the templater
-
-        def motd(**map):
-            yield self.config("web", "motd", "")
-
-        # figure out which style to use
-
-        vars = {}
-        styles = (
-            req.form.get('style', [None])[0],
-            self.config('web', 'style'),
-            'paper',
-        )
-        style, mapfile = templater.stylemap(styles, self.templatepath)
-        if style == styles[0]:
-            vars['style'] = style
-
-        start = req.url[-1] == '?' and '&' or '?'
-        sessionvars = webutil.sessionvars(vars, start)
-
-        if not self.reponame:
-            self.reponame = (self.config("web", "name")
-                             or req.env.get('REPO_NAME')
-                             or req.url.strip('/') or self.repo.root)
-
-        def websubfilter(text):
-            return websub(text, self.websubtable)
-
-        # create the templater
-
-        tmpl = templater.templater(mapfile,
-                                   filters={"websub": websubfilter},
-                                   defaults={"url": req.url,
-                                             "logourl": logourl,
-                                             "logoimg": logoimg,
-                                             "staticurl": staticurl,
-                                             "urlbase": urlbase,
-                                             "repo": self.reponame,
-                                             "encoding": encoding.encoding,
-                                             "motd": motd,
-                                             "sessionvars": sessionvars,
-                                             "pathdef": makebreadcrumb(req.url),
-                                             "style": style,
-                                            })
-        return tmpl
-
     def check_perm(self, rctx, req, op):
         for permhook in permhooks:
             permhook(rctx, req, op)