Skip to content

Commit cbb743f

Browse files
committed
[ADD] fastapi_endpoint_context
1 parent 1390f0d commit cbb743f

File tree

14 files changed

+688
-0
lines changed

14 files changed

+688
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
========================
2+
fastapi_endpoint_context
3+
========================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:088fe199709796fff2a016b1442b64d18d60b0aee7af68c52ad5a15f2137da50
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github
20+
:target: https://github.com/OCA/rest-framework/tree/16.0/fastapi_endpoint_context
21+
:alt: OCA/rest-framework
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi_endpoint_context
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=16.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module provides an overridable request context for FastAPI
32+
endpoints.
33+
34+
It allows to customize the behavior of requests deep in the odoo call
35+
stack according to the current request context without having to
36+
override routers that may come from external modules.
37+
38+
This context contains by default the current endpoint app and id,
39+
respectively accessible via
40+
``self.env.context.get('fastapi_endpoint_app')`` and
41+
``self.env.context.get('fastapi_endpoint_id')``.
42+
43+
**Table of contents**
44+
45+
.. contents::
46+
:local:
47+
48+
Usage
49+
=====
50+
51+
odoo_env dependency is automatically overridden to include FastAPI
52+
endpoint context values.
53+
54+
You can override the ``fastapi.endpoint``->\ ``_get_app_context()``
55+
method to add custom values to the context for your applications.
56+
57+
Bug Tracker
58+
===========
59+
60+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/rest-framework/issues>`_.
61+
In case of trouble, please check there if your issue has already been reported.
62+
If you spotted it first, help us to smash it by providing a detailed and welcomed
63+
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20fastapi_endpoint_context%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
64+
65+
Do not contact contributors directly about support or help with technical issues.
66+
67+
Credits
68+
=======
69+
70+
Authors
71+
-------
72+
73+
* Akretion
74+
75+
Contributors
76+
------------
77+
78+
- Florian Mounier [email protected]
79+
80+
Maintainers
81+
-----------
82+
83+
This module is maintained by the OCA.
84+
85+
.. image:: https://odoo-community.org/logo.png
86+
:alt: Odoo Community Association
87+
:target: https://odoo-community.org
88+
89+
OCA, or the Odoo Community Association, is a nonprofit organization whose
90+
mission is to support the collaborative development of Odoo features and
91+
promote its widespread use.
92+
93+
.. |maintainer-paradoxxxzero| image:: https://github.com/paradoxxxzero.png?size=40px
94+
:target: https://github.com/paradoxxxzero
95+
:alt: paradoxxxzero
96+
97+
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
98+
99+
|maintainer-paradoxxxzero|
100+
101+
This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/16.0/fastapi_endpoint_context>`_ project on GitHub.
102+
103+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2025 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <[email protected]>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
{
6+
"name": "fastapi_endpoint_context",
7+
"version": "16.0.1.0.0",
8+
"author": "Akretion, Odoo Community Association (OCA)",
9+
"summary": "Provides an overridable request context for FastAPI endpoints",
10+
"depends": ["fastapi"],
11+
"website": "https://github.com/OCA/rest-framework",
12+
"data": [],
13+
"maintainers": ["paradoxxxzero"],
14+
"demo": [],
15+
"installable": True,
16+
"license": "AGPL-3",
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2025 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <[email protected]>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
from typing import Annotated
6+
7+
from odoo.api import Environment
8+
9+
from odoo.addons.fastapi import context, dependencies
10+
11+
from fastapi import Depends
12+
13+
14+
def odoo_env_with_context(
15+
company_id: Annotated[int | None, Depends(dependencies.company_id)],
16+
fastapi_endpoint_id: Annotated[int, Depends(dependencies.fastapi_endpoint_id)],
17+
) -> Environment:
18+
# Add a per endpoint customizable odoo environment context
19+
env = context.odoo_env_ctx.get()
20+
endpoint = env["fastapi.endpoint"].browse(fastapi_endpoint_id)
21+
env = env(
22+
context=dict(
23+
env.context,
24+
**endpoint._get_app_context(),
25+
**({"allow_company_ids": [company_id]} if company_id else {}),
26+
)
27+
)
28+
yield env
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import fastapi_endpoint
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2025 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <[email protected]>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
from odoo import models
6+
7+
from odoo.addons.fastapi import dependencies
8+
9+
from ..dependencies import odoo_env_with_context
10+
11+
12+
class FastapiEndpoint(models.Model):
13+
_inherit = "fastapi.endpoint"
14+
15+
def _get_app_context(self):
16+
"""Add the fastapi app and endpoint id to the odoo env context"""
17+
return {
18+
"fastapi_app": self.app,
19+
"fastapi_endpoint_id": self.id,
20+
}
21+
22+
def _get_app_dependencies_overrides(self):
23+
res = super()._get_app_dependencies_overrides()
24+
res.update({dependencies.odoo_env: odoo_env_with_context})
25+
return res
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Florian Mounier <[email protected]>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This module provides an overridable request context for FastAPI endpoints.
2+
3+
It allows to customize the behavior of requests deep in the odoo call stack
4+
according to the current request context without having to override routers
5+
that may come from external modules.
6+
7+
This context contains by default the current endpoint app and id, respectively
8+
accessible via `self.env.context.get('fastapi_endpoint_app')` and
9+
`self.env.context.get('fastapi_endpoint_id')`.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
odoo_env dependency is automatically overridden to include FastAPI endpoint context values.
2+
3+
You can override the `fastapi.endpoint`->`_get_app_context()` method to add custom values to the context for your applications.

0 commit comments

Comments
 (0)