Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
103 changes: 103 additions & 0 deletions fastapi_endpoint_context/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
========================
fastapi_endpoint_context
========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:088fe199709796fff2a016b1442b64d18d60b0aee7af68c52ad5a15f2137da50
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github
:target: https://github.com/OCA/rest-framework/tree/16.0/fastapi_endpoint_context
:alt: OCA/rest-framework
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi_endpoint_context
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module provides an overridable request context for FastAPI
endpoints.

It allows to customize the behavior of requests deep in the odoo call
stack according to the current request context without having to
override routers that may come from external modules.

This context contains by default the current endpoint app and id,
respectively accessible via
``self.env.context.get('fastapi_endpoint_app')`` and
``self.env.context.get('fastapi_endpoint_id')``.

**Table of contents**

.. contents::
:local:

Usage
=====

odoo_env dependency is automatically overridden to include FastAPI
endpoint context values.

You can override the ``fastapi.endpoint``->\ ``_get_app_context()``
method to add custom values to the context for your applications.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/rest-framework/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`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**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Akretion

Contributors
------------

- Florian Mounier florian.mounier@akretion.com

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

.. |maintainer-paradoxxxzero| image:: https://github.com/paradoxxxzero.png?size=40px
:target: https://github.com/paradoxxxzero
:alt: paradoxxxzero

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-paradoxxxzero|

This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/16.0/fastapi_endpoint_context>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions fastapi_endpoint_context/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
17 changes: 17 additions & 0 deletions fastapi_endpoint_context/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2025 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "fastapi_endpoint_context",
"version": "16.0.1.0.0",
"author": "Akretion, Odoo Community Association (OCA)",
"summary": "Provides an overridable request context for FastAPI endpoints",
"depends": ["fastapi"],
"website": "https://github.com/OCA/rest-framework",
"data": [],
"maintainers": ["paradoxxxzero"],
"demo": [],
"installable": True,
"license": "AGPL-3",
}
28 changes: 28 additions & 0 deletions fastapi_endpoint_context/dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2025 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from typing import Annotated

from odoo.api import Environment

from odoo.addons.fastapi import context, dependencies

from fastapi import Depends


def odoo_env_with_context(
company_id: Annotated[int | None, Depends(dependencies.company_id)],
fastapi_endpoint_id: Annotated[int, Depends(dependencies.fastapi_endpoint_id)],
) -> Environment:
# Add a per endpoint customizable odoo environment context
env = context.odoo_env_ctx.get()
endpoint = env["fastapi.endpoint"].browse(fastapi_endpoint_id)
env = env(
context=dict(
env.context,
**endpoint._get_app_context(),
**({"allow_company_ids": [company_id]} if company_id else {}),
)
)
yield env
1 change: 1 addition & 0 deletions fastapi_endpoint_context/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import fastapi_endpoint
25 changes: 25 additions & 0 deletions fastapi_endpoint_context/models/fastapi_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2025 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models

from odoo.addons.fastapi import dependencies

from ..dependencies import odoo_env_with_context


class FastapiEndpoint(models.Model):
_inherit = "fastapi.endpoint"

def _get_app_context(self):
"""Add the fastapi app and endpoint id to the odoo env context"""
return {
"fastapi_app": self.app,
"fastapi_endpoint_id": self.id,
}

def _get_app_dependencies_overrides(self):
res = super()._get_app_dependencies_overrides()
res.update({dependencies.odoo_env: odoo_env_with_context})
return res
1 change: 1 addition & 0 deletions fastapi_endpoint_context/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Florian Mounier <florian.mounier@akretion.com>
9 changes: 9 additions & 0 deletions fastapi_endpoint_context/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This module provides an overridable request context for FastAPI endpoints.

It allows to customize the behavior of requests deep in the odoo call stack
according to the current request context without having to override routers
that may come from external modules.

This context contains by default the current endpoint app and id, respectively
accessible via `self.env.context.get('fastapi_endpoint_app')` and
`self.env.context.get('fastapi_endpoint_id')`.
3 changes: 3 additions & 0 deletions fastapi_endpoint_context/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
odoo_env dependency is automatically overridden to include FastAPI endpoint context values.

You can override the `fastapi.endpoint`->`_get_app_context()` method to add custom values to the context for your applications.
Loading