[tor-commits] [bridgedb/master] Rewrite HTML templates to pass template namespace parameters.
isis at torproject.org
isis at torproject.org
Fri May 16 18:52:52 UTC 2014
commit 0380ffe042a1e9490a151771403062867bf5929b
Author: Isis Lovecruft <isis at torproject.org>
Date: Mon May 5 18:58:25 2014 +0000
Rewrite HTML templates to pass template namespace parameters.
In order to use translated strings from the bridgedb.strings module, we
need to pass the namespace parameters (i.e., the arguments in
HTTPServer.py to `template.render()`) from the parent template
(base.html) to the inheriting templates.
While we're at it, we should pass information on the requested lang, so
that translations don't suddenly turn off if you go to a different page,
and info on whether the page should be rendered RTL. This fixes a slight
bug where the pages deriving from
`bridgedb.HTTPServer.CaptchaProtectedResource` weren't being correctly
translated or RTL-rendered when they should be.
And also while we're at it, we should probably not do *anything* inside
of `bridgedb.HTTPServer.*Resource.render_*` methods outside of the
try/except block, because any error will be rendered as a traceback to
the client.
Additionally, we should set the content type and encoding in the headers
of every response before returning it.
---
lib/bridgedb/HTTPServer.py | 68 +++++++++++++++++++----------------
lib/bridgedb/templates/base.html | 8 +++--
lib/bridgedb/templates/bridges.html | 3 +-
lib/bridgedb/templates/captcha.html | 2 ++
lib/bridgedb/templates/howto.html | 1 +
lib/bridgedb/templates/index.html | 2 ++
lib/bridgedb/templates/options.html | 2 ++
7 files changed, 52 insertions(+), 34 deletions(-)
diff --git a/lib/bridgedb/HTTPServer.py b/lib/bridgedb/HTTPServer.py
index e11073e..7c4689a 100644
--- a/lib/bridgedb/HTTPServer.py
+++ b/lib/bridgedb/HTTPServer.py
@@ -206,17 +206,24 @@ class CaptchaProtectedResource(resource.Resource):
:returns: A rendered HTML page containing a ReCaptcha challenge image
for the client to solve.
"""
+ rtl = False
image, challenge = self.getCaptchaImage(request)
try:
+ langs = translations.getLocaleFromHTTPRequest(request)
+ rtl = translations.usingRTLLang(langs)
# TODO: this does not work for versions of IE < 8.0
imgstr = 'data:image/jpeg;base64,%s' % base64.b64encode(image)
template = lookup.get_template('captcha.html')
- rendered = template.render(imgstr=imgstr,
+ rendered = template.render(strings,
+ rtl=rtl,
+ lang=langs[0],
+ imgstr=imgstr,
challenge_field=challenge)
except Exception as err:
rendered = replaceErrorPage(err, 'captcha.html')
+ request.setHeader("Content-Type", "text/html; charset=utf-8")
return rendered
def render_POST(self, request):
@@ -236,6 +243,8 @@ class CaptchaProtectedResource(resource.Resource):
:returns: A rendered HTML page containing a ReCaptcha challenge image
for the client to solve.
"""
+ request.setHeader("Content-Type", "text/html; charset=utf-8")
+
if self.checkSolution(request) is True:
try:
rendered = self.resource.render(request)
@@ -559,15 +568,15 @@ class WebResourceOptions(resource.Resource):
def render_GET(self, request):
rtl = False
- langs = translations.getLocaleFromHTTPRequest(request)
-
try:
+ langs = translations.getLocaleFromHTTPRequest(request)
rtl = translations.usingRTLLang(langs)
+ template = lookup.get_template('options.html')
+ rendered = template.render(strings, rtl=rtl, lang=langs[0])
except Exception as err: # pragma: no cover
- logging.exception(err)
-
+ rendered = replaceErrorPage(err)
request.setHeader("Content-Type", "text/html; charset=utf-8")
- return lookup.get_template('options.html').render(rtl=rtl)
+ return rendered
render_POST = render_GET
@@ -584,15 +593,15 @@ class WebResourceHowto(resource.Resource):
def render_GET(self, request):
rtl = False
- langs = translations.getLocaleFromHTTPRequest(request)
-
try:
+ langs = translations.getLocaleFromHTTPRequest(request)
rtl = translations.usingRTLLang(langs)
+ template = lookup.get_template('howto.html')
+ rendered = template.render(strings, rtl=rtl, lang=langs[0])
except Exception as err: # pragma: no cover
- logging.exception(err)
-
+ rendered = replaceErrorPage(err)
request.setHeader("Content-Type", "text/html; charset=utf-8")
- return lookup.get_template('howto.html').render(rtl=rtl)
+ return rendered
render_POST = render_GET
@@ -683,11 +692,6 @@ class WebResourceBridges(resource.Resource):
if countryCode:
logging.debug("Client request from GeoIP CC: %s" % countryCode)
- langs = translations.getLocaleFromHTTPRequest(request)
- rtl = translations.usingRTLLang(langs)
- if rtl:
- logging.debug("Rendering RTL response.")
-
# XXX separate function again
format = request.args.get("format", None)
if format and len(format): format = format[0] # choose the first arg
@@ -749,10 +753,10 @@ class WebResourceBridges(resource.Resource):
request=bridgedb.Dist.uniformMap(ip)
) for b in bridges)
- answer = self.renderAnswer(request, bridgeLines, rtl, format)
+ answer = self.renderAnswer(request, bridgeLines, format)
return answer
- def renderAnswer(self, request, bridgeLines=None, rtl=False, format=None):
+ def renderAnswer(self, request, bridgeLines=None, format=None):
"""Generate a response for a client which includes **bridges**.
The generated response can be plaintext or HTML.
@@ -763,8 +767,6 @@ class WebResourceBridges(resource.Resource):
:type bridgeLines: list or None
:param bridgeLines: A list of strings used to configure a Tor client
to use a bridge.
- :param bool rtl: If ``True``, the language used for the response to
- the client should be rendered right-to-left.
:type format: str or None
:param format: If ``'plain'``, return a plaintext response. Otherwise,
use the :file:`bridgedb/templates/bridges.html` template to render
@@ -772,17 +774,21 @@ class WebResourceBridges(resource.Resource):
:rtype: str
:returns: A plaintext or HTML response to serve.
"""
+ rtl = False
+
if format == 'plain':
request.setHeader("Content-Type", "text/plain")
rendered = bridgeLines
else:
request.setHeader("Content-Type", "text/html; charset=utf-8")
try:
- # XXX FIXME the returned page from
- # ``WebResourceBridgesTests.test_render_GET_RTLlang``
- # is in Arabic and has `<html lang="en">`! Doh.
+ langs = translations.getLocaleFromHTTPRequest(request)
+ rtl = translations.usingRTLLang(langs)
template = lookup.get_template('bridges.html')
- rendered = template.render(answer=bridgeLines, rtl=rtl)
+ rendered = template.render(strings,
+ rtl=rtl,
+ lang=langs[0],
+ answer=bridgeLines)
except Exception as err:
rendered = replaceErrorPage(err)
@@ -804,17 +810,17 @@ class WebRoot(resource.Resource):
:param request: An incoming request.
"""
rtl = False
- langs = translations.getLocaleFromHTTPRequest(request)
-
try:
+ langs = translations.getLocaleFromHTTPRequest(request)
rtl = translations.usingRTLLang(langs)
+ template = lookup.get_template('index.html')
+ rendered = template.render(strings,
+ rtl=rtl,
+ lang=langs[0])
except Exception as err:
- logging.exception(err)
- logging.error("The gettext files were not properly installed.")
- logging.info("To install translations, try doing `python " \
- "setup.py compile_catalog`.")
+ rendered = replaceErrorPage(err)
- return lookup.get_template('index.html').render(rtl=rtl)
+ return rendered
def addWebServer(cfg, dist, sched):
diff --git a/lib/bridgedb/templates/base.html b/lib/bridgedb/templates/base.html
index 4228f9f..1ec49a3 100644
--- a/lib/bridgedb/templates/base.html
+++ b/lib/bridgedb/templates/base.html
@@ -1,6 +1,10 @@
## -*- coding: utf-8 -*-
+
+<%namespace name="base" file="base.html" inheritable="True"/>
+<%page args="strings, rtl=False, lang='en', **kwargs"/>
+
<!DOCTYPE html>
-<html lang="en">
+<html lang="${lang}">
<head>
<meta charset="utf-8">
<title>BridgeDB</title>
@@ -50,7 +54,7 @@ span {
</div>
-${self.body()}
+${next.body(strings, rtl=rtl, lang=lang, **kwargs)}
<div class="faq">
diff --git a/lib/bridgedb/templates/bridges.html b/lib/bridgedb/templates/bridges.html
index a118404..4058d54 100644
--- a/lib/bridgedb/templates/bridges.html
+++ b/lib/bridgedb/templates/bridges.html
@@ -1,5 +1,7 @@
## -*- coding: utf-8 -*-
+
<%inherit file="base.html"/>
+<%page args="strings, rtl=False, lang='en', answer=0, **kwargs"/>
<div class="container-fluid"
style="width: 98%; align: center; margin: auto;">
@@ -78,7 +80,6 @@ ${_(""" Select "Yes" and click "Next" in order to configure your""" \
</div>
</div>
<br />
-
% endif
<hr />
diff --git a/lib/bridgedb/templates/captcha.html b/lib/bridgedb/templates/captcha.html
index 23b9772..9f62258 100644
--- a/lib/bridgedb/templates/captcha.html
+++ b/lib/bridgedb/templates/captcha.html
@@ -1,5 +1,7 @@
## -*- coding: utf-8 -*-
+
<%inherit file="base.html"/>
+<%page args="strings, rtl=False, lang='en', imgstr=0, captcha_challenge=0, **kwargs"/>
<div class="container-narrow"
id="captchaSubmissionContainer"
diff --git a/lib/bridgedb/templates/howto.html b/lib/bridgedb/templates/howto.html
index 42e14ca..b143741 100644
--- a/lib/bridgedb/templates/howto.html
+++ b/lib/bridgedb/templates/howto.html
@@ -1,6 +1,7 @@
## -*- coding: utf-8 -*-
<%inherit file="base.html"/>
+<%page args="strings, rtl=False, lang='en', **kwargs"/>
<div class="container-fluid"
style="width: 98%; align: center; margin: auto;">
diff --git a/lib/bridgedb/templates/index.html b/lib/bridgedb/templates/index.html
index 243ba10..21a5281 100644
--- a/lib/bridgedb/templates/index.html
+++ b/lib/bridgedb/templates/index.html
@@ -1,5 +1,7 @@
## -*- coding: utf-8 -*-
+
<%inherit file="base.html"/>
+<%page args="strings, rtl=False, lang='en', **kwargs"/>
<div class="main-steps">
<div class="step row">
diff --git a/lib/bridgedb/templates/options.html b/lib/bridgedb/templates/options.html
index d786ac1..c25e418 100644
--- a/lib/bridgedb/templates/options.html
+++ b/lib/bridgedb/templates/options.html
@@ -1,5 +1,7 @@
## -*- coding: utf-8 -*-
+
<%inherit file="base.html"/>
+<%page args="strings, rtl=False, lang='en', **kwargs"/>
<div class="container-fluid"
style="width: 96%; align: center; margin: 2%">
More information about the tor-commits
mailing list