prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Legal pages don't remember your acceptance of the agreemenet
  This is likely only a problem on the [LibreOffice dev server](https://translations-test.documentfoundation.org/projects/) but tracking it here so we don't forget.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `pootle/apps/staticpages/managers.py`
  Content:
  ```
  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 #
  4 # Copyright (C) Pootle contributors.
  5 #
  6 # This file is a part of the Pootle project. It is distributed under the GPL3
  7 # or later license. See the LICENSE file for a copy of the license and the
  8 # AUTHORS file for copyright and authorship information.
  9 
  10 from django.db.models import F, Manager
  11 
  12 
  13 class PageManager(Manager):
  14 
  15     def live(self, user=None, **kwargs):
  16         """Filters active (live) pages.
  17 
  18         :param user: Current active user. If omitted or the user doesn't
  19             have administration privileges, only active pages will be
  20             returned.
  21         """
  22         if user is not None and user.is_superuser:
  23             return self.get_queryset()
  24 
  25         return self.get_queryset().filter(active=True)
  26 
  27     def pending_user_agreement(self, user, **kwargs):
  28         """Filters active pages where the given `user` has pending
  29         agreements.
  30         """
  31         # FIXME: This should be a method exclusive to a LegalPage manager
  32         return self.live().exclude(
  33             agreement__user=user,
  34             modified_on__lt=F('agreement__agreed_on'),
  35         ).distinct()
  36
  ```
  
  Path: `pootle/apps/pootle_misc/context_processors.py`
  Content:
  ```
  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 #
  4 # Copyright (C) Pootle contributors.
  5 #
  6 # This file is a part of the Pootle project. It is distributed under the GPL3
  7 # or later license. See the LICENSE file for a copy of the license and the
  8 # AUTHORS file for copyright and authorship information.
  9 
  10 from django.conf import settings
  11 from django.utils import translation
  12 
  13 from pootle import __version__
  14 from pootle_language.models import Language
  15 from pootle_project.models import Project
  16 from staticpages.models import LegalPage
  17 
  18 
  19 def _agreement_context(request):
  20     """Returns whether the agreement box should be displayed or not."""
  21     request_path = request.META['PATH_INFO']
  22     nocheck = filter(lambda x: request_path.startswith(x),
  23                      settings.LEGALPAGE_NOCHECK_PREFIXES)
  24 
  25     if (request.user.is_authenticated() and not nocheck and
  26         LegalPage.objects.pending_user_agreement(request.user).exists()):
  27         return True
  28 
  29     return False
  30 
  31 
  32 def pootle_context(request):
  33     """Exposes settings to templates."""
  34     #FIXME: maybe we should expose relevant settings only?
  35     return {
  36         'settings': {
  37             'TITLE': settings.TITLE,
  38             'POOTLE_INSTANCE_ID': settings.POOTLE_INSTANCE_ID,
  39             'CAN_CONTACT': settings.CAN_CONTACT and settings.CONTACT_EMAIL,
  40             'SCRIPT_NAME': settings.SCRIPT_NAME,
  41             'POOTLE_VERSION': __version__,
  42             'CACHE_TIMEOUT': settings.CACHE_MIDDLEWARE_SECONDS,
  43             'POOTLE_CACHE_TIMEOUT': settings.POOTLE_CACHE_TIMEOUT,
  44             'DEBUG': settings.DEBUG,
  45         },
  46         'custom': settings.CUSTOM_TEMPLATE_CONTEXT,
  47         'ALL_LANGUAGES': Language.live.cached_dict(translation.get_language()),
  48         'ALL_PROJECTS': Project.objects.cached_dict(request.user),
  49         'display_agreement': _agreement_context(request),
  50     }
  51
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/pootle/apps/pootle_misc/context_processors.py b/pootle/apps/pootle_misc/context_processors.py
  --- a/pootle/apps/pootle_misc/context_processors.py
  +++ b/pootle/apps/pootle_misc/context_processors.py
  @@ -23,7 +23,7 @@
                        settings.LEGALPAGE_NOCHECK_PREFIXES)
   
       if (request.user.is_authenticated() and not nocheck and
  -        LegalPage.objects.pending_user_agreement(request.user).exists()):
  +        LegalPage.objects.has_pending_agreement(request.user)):
           return True
   
       return False
  diff --git a/pootle/apps/staticpages/managers.py b/pootle/apps/staticpages/managers.py
  --- a/pootle/apps/staticpages/managers.py
  +++ b/pootle/apps/staticpages/managers.py
  @@ -24,12 +24,24 @@
   
           return self.get_queryset().filter(active=True)
   
  +    def has_pending_agreement(self, user):
  +        agreements = self.pending_user_agreement(user)
  +        return len(list(agreements)) > 0
  +
       def pending_user_agreement(self, user, **kwargs):
           """Filters active pages where the given `user` has pending
           agreements.
           """
           # FIXME: This should be a method exclusive to a LegalPage manager
  -        return self.live().exclude(
  -            agreement__user=user,
  -            modified_on__lt=F('agreement__agreed_on'),
  -        ).distinct()
  +        return self.raw('''
  +            SELECT DISTINCT staticpages_legalpage.id
  +            FROM staticpages_legalpage
  +            WHERE (staticpages_legalpage.active = TRUE
  +                   AND NOT (staticpages_legalpage.id IN
  +                            (SELECT A.document_id
  +                             FROM staticpages_legalpage AS LP
  +                             INNER JOIN staticpages_agreement AS A
  +                                        ON LP.id = A.document_id
  +                             WHERE A.user_id = %s AND
  +                             A.agreed_on > LP.modified_on)))
  +        ''', [user.id])
  
verification_info: {"golden_diff": "diff --git a/pootle/apps/pootle_misc/context_processors.py b/pootle/apps/pootle_misc/context_processors.py\n--- a/pootle/apps/pootle_misc/context_processors.py\n+++ b/pootle/apps/pootle_misc/context_processors.py\n@@ -23,7 +23,7 @@\n                      settings.LEGALPAGE_NOCHECK_PREFIXES)\n \n     if (request.user.is_authenticated() and not nocheck and\n-        LegalPage.objects.pending_user_agreement(request.user).exists()):\n+        LegalPage.objects.has_pending_agreement(request.user)):\n         return True\n \n     return False\ndiff --git a/pootle/apps/staticpages/managers.py b/pootle/apps/staticpages/managers.py\n--- a/pootle/apps/staticpages/managers.py\n+++ b/pootle/apps/staticpages/managers.py\n@@ -24,12 +24,24 @@\n \n         return self.get_queryset().filter(active=True)\n \n+    def has_pending_agreement(self, user):\n+        agreements = self.pending_user_agreement(user)\n+        return len(list(agreements)) > 0\n+\n     def pending_user_agreement(self, user, **kwargs):\n         \"\"\"Filters active pages where the given `user` has pending\n         agreements.\n         \"\"\"\n         # FIXME: This should be a method exclusive to a LegalPage manager\n-        return self.live().exclude(\n-            agreement__user=user,\n-            modified_on__lt=F('agreement__agreed_on'),\n-        ).distinct()\n+        return self.raw('''\n+            SELECT DISTINCT staticpages_legalpage.id\n+            FROM staticpages_legalpage\n+            WHERE (staticpages_legalpage.active = TRUE\n+                   AND NOT (staticpages_legalpage.id IN\n+                            (SELECT A.document_id\n+                             FROM staticpages_legalpage AS LP\n+                             INNER JOIN staticpages_agreement AS A\n+                                        ON LP.id = A.document_id\n+                             WHERE A.user_id = %s AND\n+                             A.agreed_on > LP.modified_on)))\n+        ''', [user.id])\n", "issue": "Legal pages don't remember your acceptance of the agreemenet\nThis is likely only a problem on the [LibreOffice dev server](https://translations-test.documentfoundation.org/projects/) but tracking it here so we don't forget.\n\n", "before_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nfrom django.db.models import F, Manager\n\n\nclass PageManager(Manager):\n\n    def live(self, user=None, **kwargs):\n        \"\"\"Filters active (live) pages.\n\n        :param user: Current active user. If omitted or the user doesn't\n            have administration privileges, only active pages will be\n            returned.\n        \"\"\"\n        if user is not None and user.is_superuser:\n            return self.get_queryset()\n\n        return self.get_queryset().filter(active=True)\n\n    def pending_user_agreement(self, user, **kwargs):\n        \"\"\"Filters active pages where the given `user` has pending\n        agreements.\n        \"\"\"\n        # FIXME: This should be a method exclusive to a LegalPage manager\n        return self.live().exclude(\n            agreement__user=user,\n            modified_on__lt=F('agreement__agreed_on'),\n        ).distinct()\n", "path": "pootle/apps/staticpages/managers.py"}, {"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nfrom django.conf import settings\nfrom django.utils import translation\n\nfrom pootle import __version__\nfrom pootle_language.models import Language\nfrom pootle_project.models import Project\nfrom staticpages.models import LegalPage\n\n\ndef _agreement_context(request):\n    \"\"\"Returns whether the agreement box should be displayed or not.\"\"\"\n    request_path = request.META['PATH_INFO']\n    nocheck = filter(lambda x: request_path.startswith(x),\n                     settings.LEGALPAGE_NOCHECK_PREFIXES)\n\n    if (request.user.is_authenticated() and not nocheck and\n        LegalPage.objects.pending_user_agreement(request.user).exists()):\n        return True\n\n    return False\n\n\ndef pootle_context(request):\n    \"\"\"Exposes settings to templates.\"\"\"\n    #FIXME: maybe we should expose relevant settings only?\n    return {\n        'settings': {\n            'TITLE': settings.TITLE,\n            'POOTLE_INSTANCE_ID': settings.POOTLE_INSTANCE_ID,\n            'CAN_CONTACT': settings.CAN_CONTACT and settings.CONTACT_EMAIL,\n            'SCRIPT_NAME': settings.SCRIPT_NAME,\n            'POOTLE_VERSION': __version__,\n            'CACHE_TIMEOUT': settings.CACHE_MIDDLEWARE_SECONDS,\n            'POOTLE_CACHE_TIMEOUT': settings.POOTLE_CACHE_TIMEOUT,\n            'DEBUG': settings.DEBUG,\n        },\n        'custom': settings.CUSTOM_TEMPLATE_CONTEXT,\n        'ALL_LANGUAGES': Language.live.cached_dict(translation.get_language()),\n        'ALL_PROJECTS': Project.objects.cached_dict(request.user),\n        'display_agreement': _agreement_context(request),\n    }\n", "path": "pootle/apps/pootle_misc/context_processors.py"}], "after_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nfrom django.db.models import F, Manager\n\n\nclass PageManager(Manager):\n\n    def live(self, user=None, **kwargs):\n        \"\"\"Filters active (live) pages.\n\n        :param user: Current active user. If omitted or the user doesn't\n            have administration privileges, only active pages will be\n            returned.\n        \"\"\"\n        if user is not None and user.is_superuser:\n            return self.get_queryset()\n\n        return self.get_queryset().filter(active=True)\n\n    def has_pending_agreement(self, user):\n        agreements = self.pending_user_agreement(user)\n        return len(list(agreements)) > 0\n\n    def pending_user_agreement(self, user, **kwargs):\n        \"\"\"Filters active pages where the given `user` has pending\n        agreements.\n        \"\"\"\n        # FIXME: This should be a method exclusive to a LegalPage manager\n        return self.raw('''\n            SELECT DISTINCT staticpages_legalpage.id\n            FROM staticpages_legalpage\n            WHERE (staticpages_legalpage.active = TRUE\n                   AND NOT (staticpages_legalpage.id IN\n                            (SELECT A.document_id\n                             FROM staticpages_legalpage AS LP\n                             INNER JOIN staticpages_agreement AS A\n                                        ON LP.id = A.document_id\n                             WHERE A.user_id = %s AND\n                             A.agreed_on > LP.modified_on)))\n        ''', [user.id])\n", "path": "pootle/apps/staticpages/managers.py"}, {"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nfrom django.conf import settings\nfrom django.utils import translation\n\nfrom pootle import __version__\nfrom pootle_language.models import Language\nfrom pootle_project.models import Project\nfrom staticpages.models import LegalPage\n\n\ndef _agreement_context(request):\n    \"\"\"Returns whether the agreement box should be displayed or not.\"\"\"\n    request_path = request.META['PATH_INFO']\n    nocheck = filter(lambda x: request_path.startswith(x),\n                     settings.LEGALPAGE_NOCHECK_PREFIXES)\n\n    if (request.user.is_authenticated() and not nocheck and\n        LegalPage.objects.has_pending_agreement(request.user)):\n        return True\n\n    return False\n\n\ndef pootle_context(request):\n    \"\"\"Exposes settings to templates.\"\"\"\n    #FIXME: maybe we should expose relevant settings only?\n    return {\n        'settings': {\n            'TITLE': settings.TITLE,\n            'POOTLE_INSTANCE_ID': settings.POOTLE_INSTANCE_ID,\n            'CAN_CONTACT': settings.CAN_CONTACT and settings.CONTACT_EMAIL,\n            'SCRIPT_NAME': settings.SCRIPT_NAME,\n            'POOTLE_VERSION': __version__,\n            'CACHE_TIMEOUT': settings.CACHE_MIDDLEWARE_SECONDS,\n            'POOTLE_CACHE_TIMEOUT': settings.POOTLE_CACHE_TIMEOUT,\n            'DEBUG': settings.DEBUG,\n        },\n        'custom': settings.CUSTOM_TEMPLATE_CONTEXT,\n        'ALL_LANGUAGES': Language.live.cached_dict(translation.get_language()),\n        'ALL_PROJECTS': Project.objects.cached_dict(request.user),\n        'display_agreement': _agreement_context(request),\n    }\n", "path": "pootle/apps/pootle_misc/context_processors.py"}]}