[web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 10 Mar 2011 12:04:46 +0100
changeset 7070 5f8e52d722c5
parent 7069 8bf2337a6f3b
child 7075 4751d77394b1
child 7076 0eed6045d785
[web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
web/facet.py
web/formfields.py
web/formwidgets.py
web/request.py
web/views/reledit.py
web/views/tabs.py
--- a/web/facet.py	Thu Mar 10 12:03:37 2011 +0100
+++ b/web/facet.py	Thu Mar 10 12:04:46 2011 +0100
@@ -1169,11 +1169,11 @@
     def _render(self):
         if self.selected:
             cssclass = ' facetValueSelected'
-            imgsrc = self._cw.datadir_url + self.selected_img
+            imgsrc = self._cw.data_url(self.selected_img)
             imgalt = self._cw._('selected')
         else:
             cssclass = ''
-            imgsrc = self._cw.datadir_url + self.unselected_img
+            imgsrc = self._cw.data_url(self.unselected_img)
             imgalt = self._cw._('not selected')
         self.w(u'<div class="facetValue facetCheckBox%s" cubicweb:value="%s">\n'
                % (cssclass, xml_escape(unicode(self.value))))
@@ -1198,11 +1198,11 @@
         self.w(u'<div id="%s" class="facet">\n' % facetid)
         if self.selected:
             cssclass = ' facetValueSelected'
-            imgsrc = self._cw.datadir_url + self.selected_img
+            imgsrc = self._cw.data_url(self.selected_img)
             imgalt = self._cw._('selected')
         else:
             cssclass = ''
-            imgsrc = self._cw.datadir_url + self.unselected_img
+            imgsrc = self._cw.data_url(self.unselected_img)
             imgalt = self._cw._('not selected')
         self.w(u'<div class="facetValue facetCheckBox%s" cubicweb:value="%s">\n'
                % (cssclass, xml_escape(unicode(self.value))))
--- a/web/formfields.py	Thu Mar 10 12:03:37 2011 +0100
+++ b/web/formfields.py	Thu Mar 10 12:04:46 2011 +0100
@@ -733,7 +733,7 @@
             wdgs.append(u'<a href="%s" title="%s"><img src="%s" alt="%s"/></a>' %
                         (xml_escape(uilib.toggle_action(divid)),
                          form._cw._('show advanced fields'),
-                         xml_escape(form._cw.build_url('data/puce_down.png')),
+                         xml_escape(form._cw.data_url('puce_down.png')),
                          form._cw._('show advanced fields')))
             wdgs.append(u'<div id="%s" class="hidden">' % divid)
             if self.name_field:
--- a/web/formwidgets.py	Thu Mar 10 12:03:37 2011 +0100
+++ b/web/formwidgets.py	Thu Mar 10 12:04:46 2011 +0100
@@ -770,7 +770,7 @@
             fname = entity.autocomplete_initfuncs[field.name]
         else:
             fname = self.autocomplete_initfunc
-        return entity._cw.datadir_url + fname
+        return entity._cw.data_url(fname)
 
 
 class RestrictedAutoCompletionWidget(AutoCompletionWidget):
--- a/web/request.py	Thu Mar 10 12:03:37 2011 +0100
+++ b/web/request.py	Thu Mar 10 12:04:46 2011 +0100
@@ -420,7 +420,7 @@
         self.add_js('fckeditor/fckeditor.js')
         self.html_headers.define_var('fcklang', self.lang)
         self.html_headers.define_var('fckconfigpath',
-                                     self.build_url('data/cubicweb.fckcwconfig.js'))
+                                     self.data_url('cubicweb.fckcwconfig.js'))
     def use_fckeditor(self):
         return self.vreg.config.fckeditor_installed() and self.property_value('ui.fckeditor')
 
@@ -559,7 +559,7 @@
             jsfiles = (jsfiles,)
         for jsfile in jsfiles:
             if localfile:
-                jsfile = self.datadir_url + jsfile
+                jsfile = self.data_url(jsfile)
             self.html_headers.add_js(jsfile)
 
     def add_css(self, cssfiles, media=u'all', localfile=True, ieonly=False,
@@ -588,7 +588,7 @@
             add_css = self.html_headers.add_css
         for cssfile in cssfiles:
             if localfile:
-                cssfile = self.datadir_url + cssfile
+                cssfile = self.data_url(cssfile)
             add_css(cssfile, media, *extraargs)
 
     @deprecated('[3.9] use ajax_replace_url() instead, naming rql and vid arguments')
@@ -645,6 +645,10 @@
         """returns the absolute path of the base url"""
         return urlsplit(self.base_url())[2]
 
+    def data_url(self, relpath):
+        """returns the absolute path for a data resouce"""
+        return self.datadir_url + relpath
+
     @cached
     def from_controller(self):
         """return the id (string) of the controller issuing the request"""
--- a/web/views/reledit.py	Thu Mar 10 12:03:37 2011 +0100
+++ b/web/views/reledit.py	Thu Mar 10 12:04:46 2011 +0100
@@ -56,12 +56,15 @@
     _cancelclick = "cw.reledit.cleanupAfterCancel('%s')"
 
     # ui side actions/buttons
-    _addzone = u'<img title="%(msg)s" src="data/plus.png" alt="%(msg)s"/>'
+    _addzone = u'<img title="%(msg)s" src="%(logo)s" alt="%(msg)s"/>'
     _addmsg = _('click to add a value')
-    _deletezone = u'<img title="%(msg)s" src="data/cancel.png" alt="%(msg)s"/>'
+    _addlogo = 'plus.png'
+    _deletezone = u'<img title="%(msg)s" src="%(logo)s" alt="%(msg)s"/>'
     _deletemsg = _('click to delete this value')
-    _editzone = u'<img title="%(msg)s" src="data/pen_icon.png" alt="%(msg)s"/>'
+    _deletelogo = 'cancel.png'
+    _editzone = u'<img title="%(msg)s" src="%(logo)s" alt="%(msg)s"/>'
     _editzonemsg = _('click to edit this field')
+    _editlogo = 'pen_icon.png'
 
     # renderer
     _form_renderer_id = 'base'
@@ -210,14 +213,18 @@
         # NOTE: should be sufficient given a well built schema/security
         return rschema.has_perm(self._cw, 'delete', **kwargs)
 
+    def _build_zone(self, zonedef, msg, logo):
+        return zonedef % {'msg': xml_escape(self._cw._(msg)),
+                          'logo': xml_escape(self._cw.data_url(logo))}
+
     def _build_edit_zone(self):
-        return self._editzone % {'msg' : xml_escape(self._cw._(self._editzonemsg))}
+        return self._build_zone(self._editzone, self._editzonemsg, self._editlogo)
 
     def _build_delete_zone(self):
-        return self._deletezone % {'msg': xml_escape(self._cw._(self._deletemsg))}
+        return self._build_zone(self._deletezone, self._deletezonemsg, self._deletelogo)
 
     def _build_add_zone(self):
-        return self._addzone % {'msg': xml_escape(self._cw._(self._addmsg))}
+        return self._build_zone(self._addzone, self._addzonemsg, self._addlogo)
 
     def _build_divid(self, rtype, role, entity_eid):
         """ builds an id for the root div of a reledit widget """
--- a/web/views/tabs.py	Thu Mar 10 12:03:37 2011 +0100
+++ b/web/views/tabs.py	Thu Mar 10 12:04:46 2011 +0100
@@ -60,8 +60,9 @@
         w(u'<div id="lazy-%s" cubicweb:loadurl="%s">' % (
             tabid, xml_escape(self._cw.build_url('json', **urlparams))))
         if show_spinbox:
-            w(u'<img src="%sloading.gif" id="%s-hole" alt="%s"/>'
-              % (self._cw.datadir_url, tabid, self._cw._('(loading ...)')))
+            w(u'<img src="%s" id="%s-hole" alt="%s"/>'
+              % (xml_escape(self._cw.data_url('loading.gif')),
+                 tabid, self._cw._('(loading ...)')))
         else:
             w(u'<div id="%s-hole"></div>' % tabid)
         w(u'<noscript><p><a class="style: hidden" id="seo-%s" href="%s">%s</a></p></noscript>'