Repository: translate/pootle

Issue:
Move session data into session
atm there is data like sidebar stuff that is stored in the actual cookie.

apart from slowing down normal users (they send all that data in *every* request), bots are acumulating cookie cruft and needlessly posting that at our sites.

if the cookie cruft gets too long it gets truncated and errors out


Discussion:

Resolving patch:
diff --git a/pootle/core/helpers.py b/pootle/core/helpers.py
--- a/pootle/core/helpers.py
+++ b/pootle/core/helpers.py
@@ -12,7 +12,7 @@
 from django.utils import dateformat
 
 
-SIDEBAR_COOKIE_NAME = 'pootle-browser-sidebar'
+SIDEBAR_COOKIE_NAME = 'pootle-browser-open-sidebar'
 
 
 def get_sidebar_announcements_context(request, objects):
@@ -24,6 +24,7 @@ def get_sidebar_announcements_context(request, objects):
                     missing, but it is recommended for them to be in that exact
                     order.
     """
+    must_show_announcement = False
     announcements = []
     new_cookie_data = {}
     cookie_data = {}
@@ -41,20 +42,20 @@ def get_sidebar_announcements_context(request, objects):
             continue
 
         announcements.append(announcement)
-        # The virtual_path cannot be used as is for JSON.
-        ann_key = announcement.virtual_path.replace('/', '_')
+
         ann_mtime = dateformat.format(announcement.modified_on, 'U')
-        stored_mtime = cookie_data.get(ann_key, None)
+        stored_mtime = request.session.get(announcement.virtual_path, None)
 
         if ann_mtime != stored_mtime:
-            new_cookie_data[ann_key] = ann_mtime
+            # Some announcement has been changed or was never displayed before,
+            # so display sidebar and save the changed mtimes in the session to
+            # not display it next time unless it is necessary.
+            must_show_announcement = True
+            request.session[announcement.virtual_path] = ann_mtime
 
-    if new_cookie_data:
-        # Some announcement has been changed or was never displayed before, so
-        # display sidebar and save the changed mtimes in the cookie to not
-        # display it next time unless it is necessary.
+    if must_show_announcement and not is_sidebar_open:
         is_sidebar_open = True
-        cookie_data.update(new_cookie_data)
+        cookie_data['isOpen'] = is_sidebar_open
         new_cookie_data = quote(json.dumps(cookie_data))
 
     ctx = {


Test patch:
diff --git a/tests/views/language.py b/tests/views/language.py
--- a/tests/views/language.py
+++ b/tests/views/language.py
@@ -34,10 +34,11 @@
 def _test_browse_view(language, request, response, kwargs):
     assert (
         response.cookies["pootle-language"].value == language.code)
-    cookie_data = json.loads(
-        unquote(response.cookies[SIDEBAR_COOKIE_NAME].value))
-    assert cookie_data["foo"] == "bar"
-    assert "announcements_%s" % language.code in cookie_data
+    if SIDEBAR_COOKIE_NAME in response.cookies:
+        cookie_data = json.loads(
+            unquote(response.cookies[SIDEBAR_COOKIE_NAME].value))
+        assert cookie_data["foo"] == "bar"
+    assert "announcements/%s" % language.code in request.session
     ctx = response.context
     user_tps = language.get_children_for_user(request.user)
     stats = language.data_tool.get_stats(user=request.user)
diff --git a/tests/views/project.py b/tests/views/project.py
--- a/tests/views/project.py
+++ b/tests/views/project.py
@@ -93,10 +93,11 @@ def _test_translate_view(project, request, response, kwargs, settings):
 
 
 def _test_browse_view(project, request, response, kwargs):
-    cookie_data = json.loads(
-        unquote(response.cookies[SIDEBAR_COOKIE_NAME].value))
-    assert cookie_data["foo"] == "bar"
-    assert "announcements_projects_%s" % project.code in cookie_data
+    if SIDEBAR_COOKIE_NAME in response.cookies:
+        cookie_data = json.loads(
+            unquote(response.cookies[SIDEBAR_COOKIE_NAME].value))
+        assert cookie_data["foo"] == "bar"
+    assert "announcements/projects/%s" % project.code in request.session
     ctx = response.context
     kwargs["project_code"] = project.code
     resource_path = (
diff --git a/tests/views/tp.py b/tests/views/tp.py
--- a/tests/views/tp.py
+++ b/tests/views/tp.py
@@ -35,16 +35,16 @@
 
 
 def _test_browse_view(tp, request, response, kwargs):
-
-    cookie_data = json.loads(
-        unquote(response.cookies[SIDEBAR_COOKIE_NAME].value))
-    assert cookie_data["foo"] == "bar"
-    assert "announcements_projects_%s" % tp.project.code in cookie_data
-    assert "announcements_%s" % tp.language.code in cookie_data
+    if SIDEBAR_COOKIE_NAME in response.cookies:
+        cookie_data = json.loads(
+            unquote(response.cookies[SIDEBAR_COOKIE_NAME].value))
+        assert cookie_data["foo"] == "bar"
+    assert "announcements/projects/%s" % tp.project.code in request.session
+    assert "announcements/%s" % tp.language.code in request.session
     assert (
-        "announcements_%s_%s"
+        "announcements/%s/%s"
         % (tp.language.code, tp.project.code)
-        in cookie_data)
+        in request.session)
     ctx = response.context
     kwargs["project_code"] = tp.project.code
     kwargs["language_code"] = tp.language.code