Commit e2308819 authored by Nicolas Chauvat's avatar Nicolas Chauvat
Browse files

refactor: remove `w = self.w`

parent 4b518e778396
Pipeline #66702 passed with stage
in 14 minutes and 36 seconds
......@@ -273,9 +273,8 @@ class QUnitView(View):
test_file = None
def call(self, **kwargs):
w = self.w
req = self._cw
w(
self.w(
"""<!DOCTYPE html>
<html>
<head>
......@@ -286,10 +285,10 @@ class QUnitView(View):
<script src="/devtools/cwmock.js" type="text/javascript"></script>
<script src="/devtools/qunit.js" type="text/javascript"></script>"""
)
w("<!-- result report tools -->")
w('<script type="text/javascript">')
w("var BASE_URL = '%s';" % req.base_url())
w(
self.w("<!-- result report tools -->")
self.w('<script type="text/javascript">')
self.w("var BASE_URL = '%s';", req.base_url())
self.w(
"""
QUnit.moduleStart(function (details) {
jQuery.ajax({
......@@ -330,15 +329,15 @@ class QUnitView(View):
async: false});
});"""
)
w("</script>")
w("<!-- Test script dependencies (tested code for example) -->")
self.w("</script>")
self.w("<!-- Test script dependencies (tested code for example) -->")
for dep in self.depends:
w(' <script src="%s" type="text/javascript"></script>\n' % dep)
self.w(' <script src="%s" type="text/javascript"></script>\n', dep)
w(" <!-- Test script itself -->")
w(' <script src="%s" type="text/javascript"></script>' % self.test_file)
w(
self.w(" <!-- Test script itself -->")
self.w(' <script src="%s" type="text/javascript"></script>', self.test_file)
self.w(
""" </head>
<body>
<div id="qunit-fixture"></div>
......
......@@ -80,7 +80,7 @@ class LoginTest(PyramidCWTest):
)
finally:
rmtree(join(self.config.apphome, "i18n"))
self.assertIn("\xc9chec de l'authentification", res.text)
self.assertIn("\xc9chec de l&#39;authentification", res.text)
def test_same_site_lax_by_default(self):
res = self.webapp.post(
......
......@@ -297,11 +297,13 @@ class View(AppObject):
"""shortcut to self.view method automatically passing self.w as argument"""
self._cw.view(__vid, rset, __fallback_vid, w=self._w, **kwargs)
def whead(self, data):
self._cw.html_headers.write(data)
def whead(self, data, *args, **kwargs):
self._cw.html_headers.write(data, *args, **kwargs)
def wdata(self, data):
"""simple helper that escapes `data` and writes into `self.w`"""
"""
Simple helper that escapes `data` and writes into `self.w`
"""
self.w(xml_escape(data))
def html_headers(self):
......@@ -359,16 +361,16 @@ class View(AppObject):
if tr:
label = display_name(self._cw, label)
if table:
w("<th>%s</th>" % label)
w("<th>%s</th>", label)
else:
w('<span class="label">%s</span> ' % label)
w('<span class="label">%s</span> ', label)
if table:
if not (show_label and label):
w('<td colspan="2">%s</td></tr>' % value)
w('<td colspan="2">%s</td></tr>', value)
else:
w("<td>%s</td></tr>" % value)
w("<td>%s</td></tr>", value)
else:
w("<span>%s</span></div>" % value)
w("<span>%s</span></div>", value)
# concrete views base classes #################################################
......
......@@ -1624,19 +1624,20 @@ class FacetVocabularyWidget(htmlwidgets.HTMLWidget):
scrollbar_padding_factor = 4
def _render(self):
w = self.w
title = xml_escape(self.facet.title)
facetid = domid(make_uid(self.facet.__regid__))
w('<div id="%s" class="facet">\n' % facetid)
self.w('<div id="%s" class="facet">\n' % facetid)
cssclass = "facetTitle"
if self.facet.allow_hide:
cssclass += " hideFacetBody"
w(
'<div class="%s" cubicweb:facetName="%s">%s</div>\n'
% (cssclass, xml_escape(self.facet.__regid__), title)
self.w(
'<div class="%s" cubicweb:facetName="%s">%s</div>\n',
cssclass,
xml_escape(self.facet.__regid__),
title,
)
if self.facet.support_and:
self._render_and_or(w)
self._render_and_or(self.w)
cssclass = "facetBody vocabularyFacet"
if not self.facet.start_unfolded:
cssclass += " hidden"
......@@ -1646,27 +1647,25 @@ class FacetVocabularyWidget(htmlwidgets.HTMLWidget):
cssclass += " vocabularyFacetBodyWithLogicalSelector"
else:
cssclass += " vocabularyFacetBody"
w('<div class="%s">\n' % cssclass)
self.w('<div class="%s">\n', cssclass)
for value, label, selected in self.items:
if value is None:
continue
self._render_value(w, value, label, selected, overflow)
w("</div>\n")
w("</div>\n")
self._render_value(self.w, value, label, selected, overflow)
self.w("</div>\n")
self.w("</div>\n")
def _render_and_or(self, w):
_ = self.facet._cw._
w(
self.w(
"""<select name='%s' class='radio facetOperator' title='%s'>
<option value='OR'>%s</option>
<option value='AND'>%s</option>
</select>"""
% (
xml_escape(self.facet.__regid__) + "_andor",
_("and/or between different values"),
_("OR"),
_("AND"),
)
</select>""",
xml_escape(self.facet.__regid__) + "_andor",
_("and/or between different values"),
_("OR"),
_("AND"),
)
def _render_value(self, w, value, label, selected, overflow):
......@@ -1677,7 +1676,7 @@ class FacetVocabularyWidget(htmlwidgets.HTMLWidget):
# If it is overflowed one must add padding to compensate for the vertical
# scrollbar; given current css values, 4 blanks work perfectly ...
padding = "&#160;" * self.scrollbar_padding_factor if overflow else ""
w("<span>%s</span>" % xml_escape(label))
w("<span>%s</span>", xml_escape(label))
w(padding)
w("</div>")
......@@ -1692,27 +1691,28 @@ class FacetStringWidget(htmlwidgets.HTMLWidget):
return 2.5
def _render(self):
w = self.w
title = xml_escape(self.facet.title)
facetid = make_uid(self.facet.__regid__)
w('<div id="%s" class="facet">\n' % facetid)
self.w('<div id="%s" class="facet">\n', facetid)
cssclass = "facetTitle"
if self.facet.allow_hide:
cssclass += " hideFacetBody"
w(
'<div class="%s" cubicweb:facetName="%s">%s</div>\n'
% (cssclass, xml_escape(self.facet.__regid__), title)
self.w(
'<div class="%s" cubicweb:facetName="%s">%s</div>\n',
cssclass,
xml_escape(self.facet.__regid__),
title,
)
cssclass = "facetBody"
if not self.facet.start_unfolded:
cssclass += " hidden"
w('<div class="%s">\n' % cssclass)
w(
self.w('<div class="%s">\n' % cssclass)
self.w(
'<input name="%s" type="text" value="%s" />\n'
% (xml_escape(self.facet.__regid__), self.value or "")
)
w("</div>\n")
w("</div>\n")
self.w("</div>\n")
self.w("</div>\n")
class FacetRangeWidget(htmlwidgets.HTMLWidget):
......@@ -1750,7 +1750,6 @@ class FacetRangeWidget(htmlwidgets.HTMLWidget):
return 2.5
def _render(self):
w = self.w
facet = self.facet
facet._cw.add_js("jquery.ui.js")
facet._cw.add_css("jquery.ui.css")
......@@ -1770,41 +1769,42 @@ class FacetRangeWidget(htmlwidgets.HTMLWidget):
)
title = xml_escape(self.facet.title)
facetname = xml_escape(facetname)
w('<div id="%s" class="facet rangeFacet">\n' % facetid)
self.w('<div id="%s" class="facet rangeFacet">\n', facetid)
cssclass = "facetTitle"
if facet.allow_hide:
cssclass += " hideFacetBody"
w(
'<div class="%s" cubicweb:facetName="%s">%s</div>\n'
% (cssclass, facetname, title)
self.w(
'<div class="%s" cubicweb:facetName="%s">%s</div>\n',
cssclass,
facetname,
title,
)
cssclass = "facetBody"
if not self.facet.start_unfolded:
cssclass += " hidden"
w('<div class="%s">\n' % cssclass)
w(
'<span id="%s_inf"></span> - <span id="%s_sup"></span>'
% (sliderid, sliderid)
self.w('<div class="%s">\n', cssclass)
self.w(
'<span id="%s_inf"></span> - <span id="%s_sup"></span>', sliderid, sliderid
)
w(
'<input type="hidden" name="%s_inf" value="%s" />'
% (facetname, self.minvalue)
self.w(
'<input type="hidden" name="%s_inf" value="%s" />', facetname, self.minvalue
)
w(
'<input type="hidden" name="%s_sup" value="%s" />'
% (facetname, self.maxvalue)
self.w(
'<input type="hidden" name="%s_sup" value="%s" />', facetname, self.maxvalue
)
w(
'<input type="hidden" name="min_%s_inf" value="%s" />'
% (facetname, self.minvalue)
self.w(
'<input type="hidden" name="min_%s_inf" value="%s" />',
facetname,
self.minvalue,
)
w(
'<input type="hidden" name="max_%s_sup" value="%s" />'
% (facetname, self.maxvalue)
self.w(
'<input type="hidden" name="max_%s_sup" value="%s" />',
facetname,
self.maxvalue,
)
w('<div id="%s"></div>' % sliderid)
w("</div>\n")
w("</div>\n")
self.w('<div id="%s"></div>', sliderid)
self.w("</div>\n")
self.w("</div>\n")
class DateFacetRangeWidget(FacetRangeWidget):
......@@ -1841,10 +1841,9 @@ class CheckBoxFacetWidget(htmlwidgets.HTMLWidget):
return 1.5
def _render(self):
w = self.w
title = xml_escape(self.facet.title)
facetid = make_uid(self.facet.__regid__)
w('<div id="%s" class="facet">\n' % facetid)
self.w('<div id="%s" class="facet">\n', facetid)
cssclass = "facetValue facetCheckBox"
if self.selected:
cssclass += " facetValueSelected"
......@@ -1853,19 +1852,23 @@ class CheckBoxFacetWidget(htmlwidgets.HTMLWidget):
else:
imgsrc = self._cw.data_url(self.unselected_img)
imgalt = self._cw._("not selected")
w(
'<div class="%s" cubicweb:value="%s">\n'
% (cssclass, xml_escape(str(self.value)))
self.w(
'<div class="%s" cubicweb:value="%s">\n',
cssclass,
xml_escape(str(self.value)),
)
self.w("<div>")
self.w(
'<img src="%s" alt="%s" cubicweb:unselimg="true" />&#160;', imgsrc, imgalt
)
w("<div>")
w('<img src="%s" alt="%s" cubicweb:unselimg="true" />&#160;' % (imgsrc, imgalt))
w(
'<label class="facetTitle" cubicweb:facetName="%s">%s</label>'
% (xml_escape(self.facet.__regid__), title)
self.w(
'<label class="facetTitle" cubicweb:facetName="%s">%s</label>',
xml_escape(self.facet.__regid__),
title,
)
w("</div>\n")
w("</div>\n")
w("</div>\n")
self.w("</div>\n")
self.w("</div>\n")
self.w("</div>\n")
# other classes ################################################################
......
......@@ -38,30 +38,29 @@ from cubicweb.web.views import forms
class LogInOutTemplate(MainTemplate):
def call(self):
self.set_request_content_type()
w = self.w
self.write_doctype()
self.template_header("text/html", self._cw._("login_action"))
w("<body>\n")
self.content(w)
w("</body>")
self.w("<body>\n")
self.content(self.w)
self.w("</body>")
def template_header(
self, content_type, view=None, page_title="", additional_headers=()
):
self._cw.html_headers.define_var("BASE_URL", self._cw.base_url())
self._cw.html_headers.define_var("DATA_URL", self._cw.datadir_url)
w = self.whead
# explictly close the <base> tag to avoid IE 6 bugs while browsing DOM
w('<base href="%s"></base>' % xml_escape(self._cw.base_url()))
w(
'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
% (content_type, self._cw.encoding)
self.whead('<base href="%s"></base>', self._cw.base_url())
self.whead(
'<meta http-equiv="content-type" content="%s; charset=%s"/>\n',
content_type,
self._cw.encoding,
)
w(NOINDEX)
w(NOFOLLOW)
w("\n".join(additional_headers) + "\n")
self.whead(NOINDEX)
self.whead(NOFOLLOW)
self.whead("\n".join(additional_headers) + "\n")
self.wview("htmlheader", rset=self.cw_rset)
w("<title>%s</title>\n" % xml_escape(page_title))
self.whead("<title>%s</title>\n", xml_escape(page_title))
def content(self):
raise NotImplementedError()
......@@ -157,26 +156,25 @@ class TheMainTemplate(MainTemplate):
def call(self, view):
self.set_request_content_type()
self.template_header(self.content_type, view)
w = self.w
w('<div id="pageContent">\n')
self.w('<div id="pageContent">\n')
vtitle = self._cw.form.get("vtitle")
if vtitle:
w('<div class="vtitle">%s</div>\n', vtitle)
self.w('<div class="vtitle">%s</div>\n', vtitle)
# display entity type restriction component
etypefilter = self._cw.vreg["components"].select_or_none(
"etypenavigation", self._cw, rset=self.cw_rset
)
if etypefilter and etypefilter.cw_propval("visible"):
etypefilter.render(w=w)
w('<div id="contentmain">\n')
etypefilter.render(w=self.w)
self.w('<div id="contentmain">\n')
nav_html = UStringIO()
if view and not view.handle_pagination:
view.paginate(w=nav_html.write)
w(nav_html.getvalue())
view.render(w=w)
w(nav_html.getvalue())
w("</div>\n") # close id=contentmain
w("</div>\n") # closes id=pageContent
self.w(nav_html.getvalue())
view.render(w=self.w)
self.w(nav_html.getvalue())
self.w("</div>\n") # close id=contentmain
self.w("</div>\n") # closes id=pageContent
self.template_footer(view)
def template_header(
......@@ -188,26 +186,25 @@ class TheMainTemplate(MainTemplate):
self.template_body_header(view)
def template_html_header(self, content_type, page_title, additional_headers=()):
w = self.whead
self.write_doctype()
self._cw.html_headers.define_var("BASE_URL", self._cw.base_url())
self._cw.html_headers.define_var("DATA_URL", self._cw.datadir_url)
w(
'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
% (content_type, self._cw.encoding)
self.whead(
'<meta http-equiv="content-type" content="%s; charset=%s"/>\n',
content_type,
self._cw.encoding,
)
w("\n".join(additional_headers) + "\n")
self.whead("\n".join(additional_headers) + "\n")
self.wview("htmlheader", rset=self.cw_rset)
if page_title:
w("<title>%s</title>\n" % xml_escape(page_title))
self.whead("<title>%s</title>\n", xml_escape(page_title))
def template_body_header(self, view):
w = self.w
w("<body>\n")
self.w("<body>\n")
self.wview("header", rset=self.cw_rset, view=view)
w('<div id="page"><table width="100%" border="0" id="mainLayout"><tr>\n')
self.w('<div id="page"><table width="100%" border="0" id="mainLayout"><tr>\n')
self.nav_column(view, "left")
w('<td id="contentColumn">\n')
self.w('<td id="contentColumn">\n')
components = self._cw.vreg["components"]
rqlcomp = components.select_or_none("rqlinput", self._cw, rset=self.cw_rset)
if rqlcomp:
......@@ -272,15 +269,15 @@ class ErrorTemplate(TheMainTemplate):
def template_header(
self, content_type, view=None, page_title="", additional_headers=()
):
w = self.whead
self.write_doctype()
w(
'<meta http-equiv="content-type" content="%s; charset=%s"/>\n'
% (content_type, self._cw.encoding)
self.whead(
'<meta http-equiv="content-type" content="%s; charset=%s"/>\n',
content_type,
self._cw.encoding,
)
w("\n".join(additional_headers))
self.whead("\n".join(additional_headers))
self.wview("htmlheader", rset=self.cw_rset)
w("<title>%s</title>\n" % xml_escape(page_title))
self.w("<title>%s</title>\n", page_title)
self.w("<body>\n")
def template_footer(self, view=None):
......@@ -304,12 +301,11 @@ class SimpleMainTemplate(TheMainTemplate):
)
whead("\n".join(additional_headers) + "\n")
self.wview("htmlheader", rset=self.cw_rset)
w = self.w
whead("<title>%s</title>\n" % xml_escape(page_title))
w("<body>\n")
w('<div id="page">')
w('<table width="100%" border="0" id="mainLayout"><tr>\n')
w('<td id="navColumnLeft">\n')
self.w("<body>\n")
self.w('<div id="page">')
self.w('<table width="100%" border="0" id="mainLayout"><tr>\n')
self.w('<td id="navColumnLeft">\n')
self.topleft_header()
boxes = list(
self._cw.vreg["ctxcomponents"].poss_visible_objects(
......@@ -317,24 +313,23 @@ class SimpleMainTemplate(TheMainTemplate):
)
)
if boxes:
w('<div class="navboxes">\n')
self.w('<div class="navboxes">\n')
for box in boxes:
box.render(w=w)
box.render(w=self.w)
self.w("</div>\n")
w("</td>")
w('<td id="contentColumn" rowspan="2">')
self.w("</td>")
self.w('<td id="contentColumn" rowspan="2">')
def topleft_header(self):
logo = self._cw.vreg["components"].select_or_none(
"logo", self._cw, rset=self.cw_rset
)
if logo and logo.cw_propval("visible"):
w = self.w
w('<table id="header"><tr>\n')
w("<td>")
logo.render(w=w)
w("</td>\n")
w("</tr></table>\n")
self.w('<table id="header"><tr>\n')
self.w("<td>")
logo.render(w=self.w)
self.w("</td>\n")
self.w("</tr></table>\n")
# page parts templates ########################################################
......@@ -400,18 +395,17 @@ class HTMLPageHeader(View):
def main_header(self, view):
"""build the top menu with authentification info and the rql box"""
w = self.w
w('<table id="header"><tr>\n')
self.w('<table id="header"><tr>\n')
for colid, context in self.headers:
w('<td id="%s">' % colid)
self.w('<td id="%s">', colid)
components = self._cw.vreg["ctxcomponents"].poss_visible_objects(
self._cw, rset=self.cw_rset, view=view, context=context
)
for comp in components:
comp.render(w=w)
w("&#160;")
w("</td>")
w("</tr></table>\n")
comp.render(w=self.w)
self.w("&#160;")
self.w("</td>")
self.w("</tr></table>\n")
def state_header(self):
state = self._cw.search_state
......@@ -555,25 +549,24 @@ class LogFormView(View):
title = "log in"
def call(self, id, klass, title=True, showmessage=True):
w = self.w
w('<div id="%s" class="%s">' % (id, klass))
self.w('<div id="%s" class="%s">', id, klass)
if title:
stitle = self._cw.property_value("ui.site-title")
if stitle:
stitle = xml_escape(stitle)
else:
stitle = "&#160;"
w('<div class="loginTitle">%s</div>' % stitle)
w('<div class="loginContent">\n')
self.w('<div class="loginTitle">%s</div>', stitle)
self.w('<div class="loginContent">\n')
# don't call self._cw.message twice since it pops the id
message = self._cw.message
if showmessage and message:
w('<div class="loginMessage">%s</div>\n' % message)
self.w('<div class="loginMessage">%s</div>\n', message)
config = self._cw.vreg.config
if config["auth-mode"] != "http":
self.login_form(id) # Cookie authentication
w("</div>")
w("</div>\n")
self.w("</div>")
self.w("</div>\n")
def login_form(self, id):
cw = self._cw
......
......@@ -594,17 +594,16 @@ class GroupByView(EntityView):
else:
needmore = len(index) > maxentries
displayed = tuple(displayed)[:maxentries]
w = self.w
w('<ul class="boxListing">')
self.w('<ul class="boxListing">')
for key in displayed:
if key:
w("<li>%s</li>\n" % self.index_link(basepath, key, index[key]))
self.w("<li>%s</li>\n", self.index_link(basepath, key, index[key]))
if needmore:
url = self._cw.build_url(
"view", vid=self.__regid__, rql=self.cw_rset.printable_rql()
)
w("<li>%s</li>\n" % tags.a("[%s]" % self._cw._("see more"), href=url))
w("</ul>\n")
self.w("<li>%s</li>\n", tags.a("[%s]" % self._cw._("see more"), href=url))
self.w("</ul>\n")
class ArchiveView(GroupByView):
......
......@@ -257,32 +257,35 @@ class SystemCWPropertiesForm(FormViewMixIn, StartupView):
self.w("</div>")