[web] add option to make resources-concat optional (implements #1910615)
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Tue, 06 Sep 2011 12:20:11 +0200
changeset 7762 a3f9ba4d44eb
parent 7761 46a8cf85d186
child 7769 8af09eeee130
[web] add option to make resources-concat optional (implements #1910615)
test/unittest_utils.py
utils.py
web/request.py
web/webconfig.py
--- a/test/unittest_utils.py	Tue Sep 06 12:20:09 2011 +0200
+++ b/test/unittest_utils.py	Tue Sep 06 12:20:11 2011 +0200
@@ -159,9 +159,17 @@
         self.assertEqual(self.encode(TestCase), 'null')
 
 class HTMLHeadTC(CubicWebTC):
+
+    def htmlhead(self, datadir_url):
+        req = self.request()
+        base_url = u'http://test.fr/data/'
+        req.datadir_url = base_url
+        head = HTMLHead(req)
+        return head
+
     def test_concat_urls(self):
         base_url = u'http://test.fr/data/'
-        head = HTMLHead(base_url)
+        head = self.htmlhead(base_url)
         urls = [base_url + u'bob1.js',
                 base_url + u'bob2.js',
                 base_url + u'bob3.js']
@@ -171,7 +179,7 @@
 
     def test_group_urls(self):
         base_url = u'http://test.fr/data/'
-        head = HTMLHead(base_url)
+        head = self.htmlhead(base_url)
         urls_spec = [(base_url + u'bob0.js', None),
                      (base_url + u'bob1.js', None),
                      (u'http://ext.com/bob2.js', None),
@@ -196,7 +204,7 @@
 
     def test_getvalue_with_concat(self):
         base_url = u'http://test.fr/data/'
-        head = HTMLHead(base_url)
+        head = self.htmlhead(base_url)
         head.add_js(base_url + u'bob0.js')
         head.add_js(base_url + u'bob1.js')
         head.add_js(u'http://ext.com/bob2.js')
@@ -224,20 +232,22 @@
         self.assertEqual(result, expected)
 
     def test_getvalue_without_concat(self):
-        base_url = u'http://test.fr/data/'
-        head = HTMLHead()
-        head.add_js(base_url + u'bob0.js')
-        head.add_js(base_url + u'bob1.js')
-        head.add_js(u'http://ext.com/bob2.js')
-        head.add_js(u'http://ext.com/bob3.js')
-        head.add_css(base_url + u'bob4.css')
-        head.add_css(base_url + u'bob5.css')
-        head.add_css(base_url + u'bob6.css', 'print')
-        head.add_css(base_url + u'bob7.css', 'print')
-        head.add_ie_css(base_url + u'bob8.css')
-        head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]')
-        result = head.getvalue()
-        expected = u"""<head>
+        self.config.global_set_option('concat-resources', False)
+        try:
+            base_url = u'http://test.fr/data/'
+            head = self.htmlhead(base_url)
+            head.add_js(base_url + u'bob0.js')
+            head.add_js(base_url + u'bob1.js')
+            head.add_js(u'http://ext.com/bob2.js')
+            head.add_js(u'http://ext.com/bob3.js')
+            head.add_css(base_url + u'bob4.css')
+            head.add_css(base_url + u'bob5.css')
+            head.add_css(base_url + u'bob6.css', 'print')
+            head.add_css(base_url + u'bob7.css', 'print')
+            head.add_ie_css(base_url + u'bob8.css')
+            head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]')
+            result = head.getvalue()
+            expected = u"""<head>
 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob4.css"/>
 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob5.css"/>
 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob6.css"/>
@@ -253,7 +263,9 @@
 <script type="text/javascript" src="http://ext.com/bob3.js"></script>
 </head>
 """
-        self.assertEqual(result, expected)
+            self.assertEqual(result, expected)
+        finally:
+            self.config.global_set_option('concat-resources', True)
 
 class DocTest(DocTest):
     from cubicweb import utils as module
--- a/utils.py	Tue Sep 06 12:20:09 2011 +0200
+++ b/utils.py	Tue Sep 06 12:20:11 2011 +0200
@@ -227,7 +227,7 @@
     xhtml_safe_script_opening = u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n'
     xhtml_safe_script_closing = u'\n//--><!]]></script>'
 
-    def __init__(self, datadir_url=None):
+    def __init__(self, req):
         super(HTMLHead, self).__init__()
         self.jsvars = []
         self.jsfiles = []
@@ -235,8 +235,8 @@
         self.ie_cssfiles = []
         self.post_inlined_scripts = []
         self.pagedata_unload = False
-        self.datadir_url = datadir_url
-
+        self._cw = req
+        self.datadir_url = req.datadir_url
 
     def add_raw(self, rawheader):
         self.write(rawheader)
@@ -348,20 +348,26 @@
                 w(vardecl + u'\n')
             w(self.xhtml_safe_script_closing)
         # 2/ css files
-        for cssfile, media in (self.group_urls(self.cssfiles) if self.datadir_url else self.cssfiles):
+        ie_cssfiles = ((x, (y, z)) for x, y, z in self.ie_cssfiles)
+        if self.datadir_url and self._cw.vreg.config['concat-resources']:
+            cssfiles = self.group_urls(self.cssfiles)
+            ie_cssfiles = self.group_urls(ie_cssfiles)
+            jsfiles = (x for x, _ in self.group_urls((x, None) for x in self.jsfiles))
+        else:
+            cssfiles = self.cssfiles
+            jsfiles = self.jsfiles
+        for cssfile, media in cssfiles:
             w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' %
               (media, xml_escape(cssfile)))
         # 3/ ie css if necessary
-        if self.ie_cssfiles:
-            ie_cssfiles = ((x, (y, z)) for x, y, z in self.ie_cssfiles)
-            for cssfile, (media, iespec) in (self.group_urls(ie_cssfiles) if self.datadir_url else ie_cssfiles):
+        if self.ie_cssfiles: # use self.ie_cssfiles because `ie_cssfiles` is a genexp
+            for cssfile, (media, iespec) in ie_cssfiles:
                 w(u'<!--%s>\n' % iespec)
                 w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' %
                   (media, xml_escape(cssfile)))
             w(u'<![endif]--> \n')
         # 4/ js files
-        jsfiles = ((x, None) for x in self.jsfiles)
-        for jsfile, media in self.group_urls(jsfiles) if self.datadir_url else jsfiles:
+        for jsfile in jsfiles:
             if skiphead:
                 # Don't insert <script> tags directly as they would be
                 # interpreted directly by some browsers (e.g. IE).
--- a/web/request.py	Tue Sep 06 12:20:09 2011 +0200
+++ b/web/request.py	Tue Sep 06 12:20:11 2011 +0200
@@ -92,7 +92,7 @@
             self.uiprops = vreg.config.uiprops
             self.datadir_url = vreg.config.datadir_url
         # raw html headers that can be added from any view
-        self.html_headers = HTMLHead(self.datadir_url)
+        self.html_headers = HTMLHead(self)
         # form parameters
         self.setup_params(form)
         # dictionnary that may be used to store request data that has to be
@@ -262,7 +262,7 @@
         """used by AutomaticWebTest to clear html headers between tests on
         the same resultset
         """
-        self.html_headers = HTMLHead(self.datadir_url)
+        self.html_headers = HTMLHead(self)
         return self
 
     # web state helpers #######################################################
--- a/web/webconfig.py	Tue Sep 06 12:20:09 2011 +0200
+++ b/web/webconfig.py	Tue Sep 06 12:20:11 2011 +0200
@@ -202,6 +202,12 @@
           'help': 'use cubicweb.old.css instead of 3.9 cubicweb.css',
           'group': 'web', 'level': 2,
           }),
+        ('concat-resources',
+         {'type' : 'yn',
+          'default': True,
+          'help': 'use modconcat-like URLS to concat and serve JS / CSS files',
+          'group': 'web', 'level': 2,
+          }),
         ))
 
     def fckeditor_installed(self):