Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][ADD] base_future_response #2660

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added base_future_response/README.rst
Empty file.
1 change: 1 addition & 0 deletions base_future_response/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import future_response
13 changes: 13 additions & 0 deletions base_future_response/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

{
"name": "Future Response",
"summary": """
Backport Odoo 16 FutureReponse mechanism.""",
"version": "14.0.1.0.0",
"license": "LGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"maintainers": ["sbidoul"],
"website": "https://github.com/OCA/server-tools",
}
73 changes: 73 additions & 0 deletions base_future_response/future_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import functools

import werkzeug
from werkzeug.wrappers import Response

from odoo import http


class FutureResponse:
"""
werkzeug.Response mock class that only serves as placeholder for
headers to be injected in the final response.
"""

# used by werkzeug.Response.set_cookie
charset = "utf-8"
max_cookie_size = 4093

def __init__(self):
self.headers = werkzeug.datastructures.Headers()

@functools.wraps(werkzeug.Response.set_cookie)
def set_cookie(
self,
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite=None,
):
werkzeug.Response.set_cookie(
self,
key,
value=value,
max_age=max_age,
expires=expires,
path=path,
domain=domain,
secure=secure,
httponly=httponly,
samesite=samesite,
)


_original_WebRequest_init = http.WebRequest.__init__


def WebRequest_init(self, httprequest):
_original_WebRequest_init(self, httprequest)
self.future_response = FutureResponse()


http.WebRequest.__init__ = WebRequest_init


_original_get_response = http.root.__class__.get_response


def get_response(self, httprequest, result, explicit_session):
response = _original_get_response(self, httprequest, result, explicit_session)
if isinstance(response, Response):
response.headers.extend(http.request.future_response.headers)
return response


http.root.__class__.get_response = get_response
2 changes: 2 additions & 0 deletions base_future_response/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a backport of Odoo 16 ``request.future_response`` mechanism, to allow
setting cookies in the response before the response is constructed.
6 changes: 6 additions & 0 deletions setup/base_future_response/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)