backport stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 02 Jul 2010 14:47:44 +0200
changeset 5868 c4380d8cfc25
parent 5863 4495b9bc49df (current diff)
parent 5867 7ef636d24ab2 (diff)
child 5869 8a129b3a5aff
backport stable
etwist/request.py
web/application.py
web/views/basecontrollers.py
web/views/idownloadable.py
--- a/etwist/request.py	Fri Jul 02 11:52:51 2010 +0200
+++ b/etwist/request.py	Fri Jul 02 14:47:44 2010 +0200
@@ -82,32 +82,22 @@
             # Expires header seems to be required by IE7
             self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')
             return
-
         # when using both 'Last-Modified' and 'ETag' response headers
         # (i.e. using respectively If-Modified-Since and If-None-Match request
         # headers, see
         # http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4 for
         # reference
-
-        cached_because_not_modified_since = False
-
         last_modified = self.headers_out.getHeader('last-modified')
         if last_modified is not None:
-            cached_because_not_modified_since = (self._twreq.setLastModified(last_modified)
-                                                 == http.CACHED)
-
-        if not cached_because_not_modified_since:
-            return
-
-        cached_because_etag_is_same = False
+            status = self._twreq.setLastModified(last_modified)
+            if status != http.CACHED:
+                return
         etag = self.headers_out.getRawHeaders('etag')
         if etag is not None:
-            cached_because_etag_is_same = self._twreq.setETag(etag[0]) == http.CACHED
-
-        if cached_because_etag_is_same:
-            response = not_modified_response(self._twreq, self._headers_in)
-            raise DirectResponse(response)
-
+            status = self._twreq.setETag(etag[0])
+            if status == http.CACHED:
+                response = not_modified_response(self._twreq, self._headers_in)
+                raise DirectResponse(response)
         # Expires header seems to be required by IE7
         self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')
 
--- a/web/application.py	Fri Jul 02 11:52:51 2010 +0200
+++ b/web/application.py	Fri Jul 02 14:47:44 2010 +0200
@@ -373,6 +373,7 @@
         # remove user callbacks on a new request (except for json controllers
         # to avoid callbacks being unregistered before they could be called)
         tstart = clock()
+        commited = False
         try:
             try:
                 ctrlid, rset = self.url_resolver.process(req, path)
@@ -390,6 +391,7 @@
                     # displaying some anonymous enabled view such as the cookie
                     # authentication form
                     req.cnx.commit()
+                    commited = True
             except (StatusResponse, DirectResponse):
                 if req.cnx:
                     req.cnx.commit()
@@ -433,7 +435,7 @@
                 self.critical('Catch all triggered!!!')
                 self.exception('this is what happened')
         finally:
-            if req.cnx:
+            if req.cnx and not commited:
                 try:
                     req.cnx.rollback()
                 except:
--- a/web/httpcache.py	Fri Jul 02 11:52:51 2010 +0200
+++ b/web/httpcache.py	Fri Jul 02 14:47:44 2010 +0200
@@ -15,10 +15,8 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-"""HTTP cache managers
+"""HTTP cache managers"""
 
-
-"""
 __docformat__ = "restructuredtext en"
 
 from time import mktime
--- a/web/views/basecontrollers.py	Fri Jul 02 11:52:51 2010 +0200
+++ b/web/views/basecontrollers.py	Fri Jul 02 14:47:44 2010 +0200
@@ -167,10 +167,6 @@
         if view.add_to_breadcrumbs and not view.binary:
             self._cw.update_breadcrumbs()
 
-    def validate_cache(self, view):
-        view.set_http_cache_headers()
-        self._cw.validate_cache()
-
     def execute_linkto(self, eid=None):
         """XXX __linkto parameter may cause security issue
 
--- a/web/views/idownloadable.py	Fri Jul 02 11:52:51 2010 +0200
+++ b/web/views/idownloadable.py	Fri Jul 02 14:47:44 2010 +0200
@@ -26,7 +26,7 @@
 from cubicweb.selectors import (one_line_rset, implements, match_context_prop,
                                 adaptable, has_mimetype)
 from cubicweb.mttransforms import ENGINE
-from cubicweb.web.box import EntityBoxTemplate
+from cubicweb.web import box, httpcache
 from cubicweb.web.views import primary, baseviews
 
 
@@ -46,7 +46,7 @@
     w(u'</div></div>\n')
 
 
-class DownloadBox(EntityBoxTemplate):
+class DownloadBox(box.EntityBoxTemplate):
     __regid__ = 'download_box'
     # no download box for images
     # XXX primary_view selector ?
@@ -69,6 +69,7 @@
     templatable = False
     content_type = 'application/octet-stream'
     binary = True
+    http_cache_manager = httpcache.EntityHTTPCacheManager
     add_to_breadcrumbs = False
 
     def set_request_content_type(self):
@@ -90,6 +91,8 @@
         adapter = entity.cw_adapt_to('IDownloadable')
         self.w(adapter.download_data())
 
+    def last_modified(self):
+        return self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0).modification_date
 
 class DownloadLinkView(EntityView):
     """view displaying a link to download the file"""