[web/request] use a picklable Counter object for tab index counters
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 12 Jun 2014 12:15:18 +0200
changeset 9884 5ad1c3a9c4a1
parent 9883 0a5890491ab3
child 9885 9f546848ba48
[web/request] use a picklable Counter object for tab index counters Related to #1381328.
web/request.py
--- a/web/request.py	Tue Jul 15 18:07:31 2014 +0200
+++ b/web/request.py	Thu Jun 12 12:15:18 2014 +0200
@@ -30,7 +30,6 @@
 from datetime import date, datetime
 from urlparse import urlsplit
 import httplib
-from itertools import count
 from warnings import warn
 
 from rql.utils import rqlvar_maker
@@ -82,6 +81,24 @@
     return [v for v in value if v != INTERNAL_FIELD_VALUE]
 
 
+class Counter(object):
+    """A picklable counter object, usable for e.g. page tab index count"""
+    __slots__ = ('value',)
+
+    def __init__(self, initialvalue=0):
+        self.value = initialvalue
+
+    def __call__(self):
+        value = self.value
+        self.value += 1
+        return value
+
+    def __getstate__(self):
+        return {'value': self.value}
+
+    def __setstate__(self, state):
+        self.value = state['value']
+
 
 class _CubicWebRequestBase(RequestSessionBase):
     """abstract HTTP request, should be extended according to the HTTP backend
@@ -201,7 +218,7 @@
     def next_tabindex(self):
         nextfunc = self.get_page_data('nexttabfunc')
         if nextfunc is None:
-            nextfunc = count(1).next
+            nextfunc = Counter(1)
             self.set_page_data('nexttabfunc', nextfunc)
         return nextfunc()